HomeFragment.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import android.annotation.SuppressLint;
  6. import android.app.Activity;
  7. import android.app.AlertDialog;
  8. import android.app.Fragment;
  9. import android.app.FragmentManager;
  10. import android.content.BroadcastReceiver;
  11. import android.content.Context;
  12. import android.content.DialogInterface;
  13. import android.content.Intent;
  14. import android.content.IntentFilter;
  15. import android.content.SharedPreferences;
  16. import android.os.Bundle;
  17. import android.support.v4.content.LocalBroadcastManager;
  18. import android.view.LayoutInflater;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.widget.CompoundButton;
  22. import android.widget.ImageView;
  23. import android.widget.Switch;
  24. import android.widget.TextView;
  25. import de.tudarmstadt.informatik.hostage.R;
  26. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  27. import de.tudarmstadt.informatik.hostage.persistence.ProfileManager;
  28. import de.tudarmstadt.informatik.hostage.model.Profile;
  29. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  30. import de.tudarmstadt.informatik.hostage.ui.LogFilter;
  31. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  32. import de.tudarmstadt.informatik.hostage.ui2.fragment.opengl.ThreatIndicatorGLRenderer;
  33. /**
  34. * This fragments displays the current hostage state and attacks on the device in form of an animation and simple view components
  35. *
  36. * @author Alexander Brakowski
  37. * @created 13.01.14 19:06
  38. */
  39. public class HomeFragment extends Fragment {
  40. /**
  41. * View objects from the layout
  42. */
  43. private Switch mHomeSwitchConnection;
  44. private TextView mHomeTextName;
  45. private TextView mHomeTextSecurity;
  46. private TextView mHomeTextAttacks;
  47. private TextView mHomeTextProfile;
  48. private TextView mHomeTextProfileHeader;
  49. private ImageView mHomeProfileImage;
  50. private ImageView mHomeConnectionInfoButton;
  51. private View mRootView;
  52. /**
  53. * This handles all the broadcasts from the Hostage service
  54. */
  55. private BroadcastReceiver mReceiver;
  56. /**
  57. * A change listener for the monitor switch
  58. */
  59. private CompoundButton.OnCheckedChangeListener mSwitchChangeListener = null;
  60. private int mDefaultTextColor;
  61. /**
  62. * A reference to the profile manager
  63. */
  64. private ProfileManager mProfileManager;
  65. /**
  66. * A shared preference that holds all the connection info of the current network connection
  67. */
  68. private SharedPreferences mConnectionInfo;
  69. /**
  70. * An Helper to access the sqllite database with all the records
  71. */
  72. private HostageDBOpenHelper mDbHelper;
  73. /**
  74. * Holds a state if the broadcast receiver is registered to the hostage service
  75. */
  76. private boolean mReceiverRegistered;
  77. /**
  78. * Holds a state if the hostage service is active
  79. */
  80. private boolean isActive = false;
  81. /**
  82. * Holds a state if the device is currently connected to a network
  83. */
  84. private boolean isConnected = false;
  85. /**
  86. * Looks up all the neccessary views in the layout
  87. */
  88. private void assignViews() {
  89. mHomeSwitchConnection = (Switch) mRootView.findViewById(R.id.home_switch_connection);
  90. mHomeTextName = (TextView) mRootView.findViewById(R.id.home_text_name);
  91. mHomeTextSecurity = (TextView) mRootView.findViewById(R.id.home_text_security);
  92. mHomeTextAttacks = (TextView) mRootView.findViewById(R.id.home_text_attacks);
  93. mHomeTextProfile = (TextView) mRootView.findViewById(R.id.home_text_profile);
  94. mHomeTextProfileHeader = (TextView) mRootView.findViewById(R.id.home_text_profile_header);
  95. mHomeProfileImage = (ImageView) mRootView.findViewById(R.id.home_image_profile);
  96. mHomeConnectionInfoButton = (ImageView) mRootView.findViewById(R.id.home_button_connection_info);
  97. }
  98. /**
  99. * Registers the broadcast receiver with the hostage service
  100. */
  101. private void registerBroadcastReceiver() {
  102. if (!mReceiverRegistered) {
  103. LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
  104. this.mReceiverRegistered = true;
  105. }
  106. }
  107. /**
  108. * Unregisters the broadcast receiver
  109. */
  110. private void unregisterBroadcastReceiver() {
  111. if (mReceiverRegistered) {
  112. LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
  113. this.mReceiverRegistered = false;
  114. }
  115. }
  116. public HomeFragment() {
  117. }
  118. /**
  119. * Sets the view state to not active.
  120. * This means hiding and graying out all the informations in the view, that are not important, if the service is not active.
  121. *
  122. * @param initial indicates that the method was called on creation of view
  123. */
  124. public void setStateNotActive(boolean initial) {
  125. mHomeTextName.setTextColor(getResources().getColor(R.color.light_grey));
  126. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.light_grey));
  127. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.light_grey));
  128. mHomeTextProfile.setTextColor(getResources().getColor(R.color.light_grey));
  129. mHomeTextProfileHeader.setTextColor(getResources().getColor(R.color.light_grey));
  130. if (!initial) {
  131. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING);
  132. }
  133. mHomeSwitchConnection.setChecked(false);
  134. isActive = false;
  135. }
  136. /**
  137. * Alias for calling setStateNotActive with the initial value being false
  138. */
  139. public void setStateNotActive() {
  140. setStateNotActive(false);
  141. }
  142. /**
  143. * Alias for calling setStateActive with the initial value being false
  144. */
  145. public void setStateActive() {
  146. setStateActive(false);
  147. }
  148. /**
  149. * Sets the state of the view to active.
  150. * That means that all the information showing the active state of the hostage service is being displayed.
  151. *
  152. * @param initial indicates that the method was called on creation of view
  153. */
  154. public void setStateActive(boolean initial) {
  155. mHomeTextAttacks.setVisibility(View.VISIBLE);
  156. mHomeTextSecurity.setVisibility(View.VISIBLE);
  157. mHomeTextName.setTextColor(mDefaultTextColor);
  158. mHomeTextProfile.setTextColor(mDefaultTextColor);
  159. mHomeTextProfileHeader.setTextColor(mDefaultTextColor);
  160. if (!initial) {
  161. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT);
  162. }
  163. mHomeSwitchConnection.setChecked(true);
  164. isActive = true;
  165. }
  166. /**
  167. * Sets the state of the view to not connected by hiding information about the attacks.
  168. */
  169. public void setStateNotConnected() {
  170. mHomeTextSecurity.setVisibility(View.INVISIBLE);
  171. mHomeTextAttacks.setVisibility(View.INVISIBLE);
  172. mHomeTextProfile.setVisibility(View.INVISIBLE);
  173. mHomeTextProfileHeader.setVisibility(View.INVISIBLE);
  174. mHomeProfileImage.setVisibility(View.INVISIBLE);
  175. mHomeConnectionInfoButton.setVisibility(View.INVISIBLE);
  176. mHomeTextName.setText(R.string.not_connected);
  177. isConnected = false;
  178. }
  179. /**
  180. * Sets the state of the view to connected by showing informations about attacks
  181. */
  182. public void setStateConnected() {
  183. mHomeTextProfile.setVisibility(View.VISIBLE);
  184. mHomeTextProfileHeader.setVisibility(View.VISIBLE);
  185. mHomeProfileImage.setVisibility(View.VISIBLE);
  186. mHomeConnectionInfoButton.setVisibility(View.VISIBLE);
  187. isConnected = true;
  188. }
  189. /**
  190. * Updates the view.
  191. *
  192. * That means: updating the number of attacks on the view,
  193. * updating the threat level,
  194. * updating the connection state,
  195. * updating the monitoring state
  196. */
  197. public void updateUI() {
  198. Profile profile = mProfileManager.getCurrentActivatedProfile();
  199. if (profile != null) {
  200. mHomeTextProfile.setText(profile.mLabel);
  201. mHomeProfileImage.setImageBitmap(profile.getIconBitmap());
  202. }
  203. // if the device is connected to an network display the network name
  204. if (HelperUtils.isNetworkAvailable(getActivity())) {
  205. setStateConnected();
  206. String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "\"\"");
  207. mHomeTextName.setText(ssid.substring(1,ssid.length() - 1));
  208. } else {
  209. setStateNotConnected();
  210. }
  211. boolean hasActiveListeners = false;
  212. int totalAttacks = mDbHelper.numBssidSeen(mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  213. ThreatIndicatorGLRenderer.ThreatLevel threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING;
  214. // decides which threat level to display
  215. if (MainActivity.getInstance().getHostageService() != null) {
  216. if (MainActivity.getInstance().getHostageService().hasRunningListeners()) {
  217. hasActiveListeners = true;
  218. if (MainActivity.getInstance().getHostageService().hasActiveAttacks() && totalAttacks > 0) {
  219. threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.LIVE_THREAT;
  220. } else if (totalAttacks > 0) {
  221. threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.PAST_THREAT;
  222. } else {
  223. threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT;
  224. }
  225. }
  226. }
  227. // if the monitoring is running show the information
  228. if (hasActiveListeners) {
  229. setStateActive(true);
  230. if(!isConnected){
  231. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT);
  232. mHomeTextAttacks.setText("");
  233. mHomeTextSecurity.setText("");
  234. } else {
  235. switch (threatLevel) {
  236. case NO_THREAT:
  237. mHomeTextAttacks.setText(R.string.zero_attacks);
  238. mHomeTextSecurity.setText(R.string.secure);
  239. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_dark_green));
  240. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_dark_green));
  241. break;
  242. case PAST_THREAT:
  243. mHomeTextAttacks.setText(totalAttacks
  244. + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks))
  245. + getResources().getString(R.string.recorded));
  246. mHomeTextSecurity.setText(R.string.insecure);
  247. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_yellow));
  248. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_yellow));
  249. break;
  250. case LIVE_THREAT:
  251. mHomeTextAttacks.setText(totalAttacks
  252. + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks))
  253. + getResources().getString(R.string.recorded));
  254. mHomeTextSecurity.setText(R.string.insecure);
  255. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_red));
  256. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_red));
  257. break;
  258. }
  259. ThreatIndicatorGLRenderer.setThreatLevel(threatLevel);
  260. }
  261. } else {
  262. setStateNotActive();
  263. }
  264. }
  265. /**
  266. * {@inheritDoc}
  267. */
  268. @Override
  269. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  270. super.onCreateView(inflater, container, savedInstanceState);
  271. final Activity activity = getActivity();
  272. if (activity != null) {
  273. activity.setTitle(getResources().getString(R.string.drawer_overview));
  274. }
  275. mDbHelper = new HostageDBOpenHelper(getActivity());
  276. mProfileManager = ProfileManager.getInstance();
  277. mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  278. mRootView = inflater.inflate(R.layout.fragment_home, container, false);
  279. assignViews();
  280. // hook up the connection info button
  281. mHomeConnectionInfoButton.setOnClickListener(new View.OnClickListener() {
  282. @Override
  283. public void onClick(View v) {
  284. final FragmentManager fragmentManager = getFragmentManager();
  285. if (fragmentManager != null) {
  286. ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
  287. connectionInfoDialogFragment.show(fragmentManager.beginTransaction(), connectionInfoDialogFragment.getTag());
  288. }
  289. }
  290. });
  291. mDefaultTextColor = mHomeTextName.getCurrentTextColor();
  292. // sets state and connection initially to off
  293. setStateNotActive(true);
  294. setStateNotConnected();
  295. // register the broadcast receiver
  296. mReceiver = new BroadcastReceiver() {
  297. @SuppressLint("NewApi")
  298. @Override
  299. public void onReceive(Context context, Intent intent) {
  300. if (getUserVisibleHint())
  301. updateUI();
  302. }
  303. };
  304. registerBroadcastReceiver();
  305. updateUI();
  306. // connects the switch listener to the switch button
  307. mHomeSwitchConnection = (Switch) mRootView.findViewById(R.id.home_switch_connection);
  308. mHomeSwitchConnection.setSaveEnabled(false);
  309. if (mSwitchChangeListener == null) {
  310. mSwitchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  311. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  312. // displays a alert dialog if no network is available
  313. if (!HelperUtils.isNetworkAvailable(getActivity())) {
  314. new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.network_not_connected_msg)
  315. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  316. public void onClick(DialogInterface dialog, int which) {
  317. }
  318. }).setIcon(android.R.drawable.ic_dialog_info).show();
  319. setStateNotActive();
  320. setStateNotConnected();
  321. } else {
  322. if (isChecked) {
  323. boolean protocolActivated = false;
  324. if (ProfileManager.getInstance().getCurrentActivatedProfile() == null) {
  325. // starts all services
  326. MainActivity.getInstance().startMonitorServices(Arrays.asList(getResources().getStringArray(R.array.protocols)));
  327. } else {
  328. // starts the services that are actived in the current profile
  329. ProfileManager profileManager = ProfileManager.getInstance();
  330. if (profileManager.isRandomActive()) {
  331. profileManager.randomizeProtocols(profileManager.getRandomProfile());
  332. }
  333. Profile currentProfile = profileManager.getCurrentActivatedProfile();
  334. List<String> protocols = currentProfile.getActiveProtocols();
  335. if(protocols.size() > 0 || currentProfile.mGhostActive){
  336. protocols.add("GHOST");
  337. MainActivity.getInstance().startMonitorServices(protocols);
  338. protocolActivated = true;
  339. }
  340. }
  341. if (protocolActivated) {
  342. setStateActive();
  343. } else {
  344. // no protocol was started, so show alert dialog
  345. new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.profile_no_services_msg)
  346. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  347. public void onClick(DialogInterface dialog, int which) {
  348. }
  349. }).setIcon(android.R.drawable.ic_dialog_info).show();
  350. setStateNotActive();
  351. }
  352. } else {
  353. // stop hostage service and all listeners
  354. if (MainActivity.getInstance().getHostageService() != null) {
  355. MainActivity.getInstance().getHostageService().stopListeners();
  356. MainActivity.getInstance().stopAndUnbind();
  357. }
  358. setStateNotActive();
  359. }
  360. }
  361. }
  362. };
  363. }
  364. mHomeSwitchConnection.setOnCheckedChangeListener(mSwitchChangeListener);
  365. // connects the profile text an click listener
  366. mRootView.findViewById(R.id.home_profile_details).setOnClickListener(new View.OnClickListener() {
  367. @Override
  368. public void onClick(View v) {
  369. Fragment fragment = new ProfileManagerFragment();
  370. MainActivity.getInstance().injectFragment(fragment, false);
  371. }
  372. });
  373. // connect the attacks text to an click listener
  374. View.OnClickListener attackClickListener = new View.OnClickListener() {
  375. @Override
  376. public void onClick(View v) {
  377. String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "");
  378. if (!ssid.isEmpty()) {
  379. ArrayList<String> ssids = new ArrayList<String>();
  380. ssids.add(ssid);
  381. LogFilter filter = new LogFilter();
  382. filter.setESSIDs(ssids);
  383. RecordOverviewFragment recordOverviewFragment = new RecordOverviewFragment();
  384. recordOverviewFragment.setFilter(filter);
  385. recordOverviewFragment.setGroupKey("ESSID");
  386. MainActivity.getInstance().injectFragment(recordOverviewFragment);
  387. }
  388. }
  389. };
  390. mHomeTextAttacks.setOnClickListener(attackClickListener);
  391. mHomeTextSecurity.setOnClickListener(attackClickListener);
  392. return mRootView;
  393. }
  394. /**
  395. * {@inheritDoc}
  396. */
  397. @Override
  398. public void onStop() {
  399. super.onStop();
  400. unregisterBroadcastReceiver();
  401. }
  402. /**
  403. * {@inheritDoc}
  404. */
  405. @Override
  406. public void onStart() {
  407. super.onStart();
  408. registerBroadcastReceiver();
  409. updateUI();
  410. }
  411. /**
  412. * {@inheritDoc}
  413. */
  414. @Override
  415. public void onDestroy() {
  416. super.onDestroy();
  417. unregisterBroadcastReceiver();
  418. }
  419. }