HomeFragment.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 Fragment {
  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. // tracking stuff
  278. Tracker t = ((HostageApplication)activity.getApplication()).getTracker();
  279. t.setScreenName(HomeFragment.class.getName());
  280. t.send(new HitBuilders.AppViewBuilder().build());
  281. }
  282. mDbHelper = new HostageDBOpenHelper(getActivity());
  283. mProfileManager = ProfileManager.getInstance();
  284. mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  285. mRootView = inflater.inflate(R.layout.fragment_home, container, false);
  286. assignViews();
  287. // hook up the connection info button
  288. mHomeConnectionInfoButton.setOnClickListener(new View.OnClickListener() {
  289. @Override
  290. public void onClick(View v) {
  291. final FragmentManager fragmentManager = getFragmentManager();
  292. if (fragmentManager != null) {
  293. ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
  294. connectionInfoDialogFragment.show(fragmentManager.beginTransaction(), connectionInfoDialogFragment.getTag());
  295. }
  296. }
  297. });
  298. mDefaultTextColor = mHomeTextName.getCurrentTextColor();
  299. // sets state and connection initially to off
  300. setStateNotActive(true);
  301. setStateNotConnected();
  302. // register the broadcast receiver
  303. mReceiver = new BroadcastReceiver() {
  304. @SuppressLint("NewApi")
  305. @Override
  306. public void onReceive(Context context, Intent intent) {
  307. if (getUserVisibleHint())
  308. updateUI();
  309. }
  310. };
  311. registerBroadcastReceiver();
  312. updateUI();
  313. // connects the switch listener to the switch button
  314. mHomeSwitchConnection = (Switch) mRootView.findViewById(R.id.home_switch_connection);
  315. mHomeSwitchConnection.setSaveEnabled(false);
  316. if (mSwitchChangeListener == null) {
  317. mSwitchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  318. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  319. // displays a alert dialog if no network is available
  320. if (!HelperUtils.isNetworkAvailable(getActivity())) {
  321. new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.network_not_connected_msg)
  322. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  323. public void onClick(DialogInterface dialog, int which) {
  324. }
  325. }).setIcon(android.R.drawable.ic_dialog_info).show();
  326. setStateNotActive();
  327. setStateNotConnected();
  328. } else {
  329. if (isChecked) {
  330. boolean protocolActivated = false;
  331. if (ProfileManager.getInstance().getCurrentActivatedProfile() == null) {
  332. // starts all services
  333. MainActivity.getInstance().startMonitorServices(Arrays.asList(getResources().getStringArray(R.array.protocols)));
  334. } else {
  335. // starts the services that are actived in the current profile
  336. ProfileManager profileManager = ProfileManager.getInstance();
  337. if (profileManager.isRandomActive()) {
  338. profileManager.randomizeProtocols(profileManager.getRandomProfile());
  339. }
  340. Profile currentProfile = profileManager.getCurrentActivatedProfile();
  341. List<String> protocols = currentProfile.getActiveProtocols();
  342. if(protocols.size() > 0 || currentProfile.mGhostActive){
  343. protocols.add("GHOST");
  344. MainActivity.getInstance().startMonitorServices(protocols);
  345. protocolActivated = true;
  346. }
  347. }
  348. if (protocolActivated) {
  349. setStateActive();
  350. } else {
  351. // no protocol was started, so show alert dialog
  352. new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.profile_no_services_msg)
  353. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  354. public void onClick(DialogInterface dialog, int which) {
  355. }
  356. }).setIcon(android.R.drawable.ic_dialog_info).show();
  357. setStateNotActive();
  358. }
  359. } else {
  360. // stop hostage service and all listeners
  361. if (MainActivity.getInstance().getHostageService() != null) {
  362. MainActivity.getInstance().getHostageService().stopListeners();
  363. MainActivity.getInstance().stopAndUnbind();
  364. }
  365. setStateNotActive();
  366. }
  367. }
  368. }
  369. };
  370. }
  371. mHomeSwitchConnection.setOnCheckedChangeListener(mSwitchChangeListener);
  372. // connects the profile text an click listener
  373. mRootView.findViewById(R.id.home_profile_details).setOnClickListener(new View.OnClickListener() {
  374. @Override
  375. public void onClick(View v) {
  376. Fragment fragment = new ProfileManagerFragment();
  377. MainActivity.getInstance().injectFragment(fragment);
  378. }
  379. });
  380. // connect the attacks text to an click listener
  381. View.OnClickListener attackClickListener = new View.OnClickListener() {
  382. @Override
  383. public void onClick(View v) {
  384. String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "");
  385. if (!ssid.isEmpty()) {
  386. ArrayList<String> ssids = new ArrayList<String>();
  387. ssids.add(ssid);
  388. LogFilter filter = new LogFilter();
  389. filter.setESSIDs(ssids);
  390. RecordOverviewFragment recordOverviewFragment = new RecordOverviewFragment();
  391. recordOverviewFragment.setFilter(filter);
  392. recordOverviewFragment.setGroupKey("ESSID");
  393. MainActivity.getInstance().injectFragment(recordOverviewFragment);
  394. }
  395. }
  396. };
  397. mHomeTextAttacks.setOnClickListener(attackClickListener);
  398. mHomeTextSecurity.setOnClickListener(attackClickListener);
  399. return mRootView;
  400. }
  401. /**
  402. * {@inheritDoc}
  403. */
  404. @Override
  405. public void onStop() {
  406. super.onStop();
  407. unregisterBroadcastReceiver();
  408. }
  409. /**
  410. * {@inheritDoc}
  411. */
  412. @Override
  413. public void onStart() {
  414. super.onStart();
  415. registerBroadcastReceiver();
  416. updateUI();
  417. }
  418. /**
  419. * {@inheritDoc}
  420. */
  421. @Override
  422. public void onDestroy() {
  423. super.onDestroy();
  424. unregisterBroadcastReceiver();
  425. }
  426. }