HomeFragment.java 16 KB

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