HomeFragment.java 21 KB

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