HomeFragment.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. //SK: Temporary Bugfix
  215. //if (HelperUtils.isNetworkAvailable(getActivity())) {
  216. if (HelperUtils.isWifiConnected(getActivity())) {
  217. setStateConnected();
  218. String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "\"\"");
  219. mHomeTextName.setText(ssid.substring(1,ssid.length() - 1));
  220. } else {
  221. setStateNotConnected();
  222. }
  223. boolean hasActiveListeners = false;
  224. int totalAttacks = mDbHelper.numBssidSeen(mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  225. ThreatIndicatorGLRenderer.ThreatLevel threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING;
  226. // decides which threat level to display
  227. if (MainActivity.getInstance().getHostageService() != null) {
  228. if (MainActivity.getInstance().getHostageService().hasRunningListeners()) {
  229. hasActiveListeners = true;
  230. if (MainActivity.getInstance().getHostageService().hasActiveAttacks() && totalAttacks > 0) {
  231. threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.LIVE_THREAT;
  232. } else if (totalAttacks > 0) {
  233. threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.PAST_THREAT;
  234. } else {
  235. threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT;
  236. }
  237. }
  238. }
  239. // if the monitoring is running show the information
  240. if (hasActiveListeners) {
  241. setStateActive(true);
  242. if(!isConnected){
  243. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT);
  244. mHomeTextAttacks.setText("");
  245. mHomeTextSecurity.setText("");
  246. } else {
  247. switch (threatLevel) {
  248. case NO_THREAT:
  249. mHomeTextAttacks.setText(R.string.zero_attacks);
  250. mHomeTextSecurity.setText(R.string.secure);
  251. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_dark_green));
  252. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_dark_green));
  253. break;
  254. case PAST_THREAT:
  255. mHomeTextAttacks.setText(totalAttacks
  256. + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks))
  257. + getResources().getString(R.string.recorded));
  258. mHomeTextSecurity.setText(R.string.insecure);
  259. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_yellow));
  260. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_yellow));
  261. break;
  262. case LIVE_THREAT:
  263. mHomeTextAttacks.setText(totalAttacks
  264. + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks))
  265. + getResources().getString(R.string.recorded));
  266. mHomeTextSecurity.setText(R.string.insecure);
  267. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_red));
  268. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_red));
  269. break;
  270. }
  271. ThreatIndicatorGLRenderer.setThreatLevel(threatLevel);
  272. }
  273. } else {
  274. setStateNotActive();
  275. }
  276. }
  277. /**
  278. * {@inheritDoc}
  279. */
  280. @Override
  281. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  282. super.onCreateView(inflater, container, savedInstanceState);
  283. final Activity activity = getActivity();
  284. if (activity != null) {
  285. activity.setTitle(getResources().getString(R.string.drawer_overview));
  286. }
  287. mDbHelper = new HostageDBOpenHelper(getActivity());
  288. mProfileManager = ProfileManager.getInstance();
  289. mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  290. mRootView = inflater.inflate(R.layout.fragment_home, container, false);
  291. assignViews();
  292. // hook up the connection info button
  293. mHomeConnectionInfoButton.setOnClickListener(new View.OnClickListener() {
  294. @Override
  295. public void onClick(View v) {
  296. final FragmentManager fragmentManager = getFragmentManager();
  297. if (fragmentManager != null) {
  298. ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
  299. connectionInfoDialogFragment.show(fragmentManager.beginTransaction(), connectionInfoDialogFragment.getTag());
  300. }
  301. }
  302. });
  303. mDefaultTextColor = mHomeTextName.getCurrentTextColor();
  304. // sets state and connection initially to off
  305. setStateNotActive(true);
  306. setStateNotConnected();
  307. // register the broadcast receiver
  308. mReceiver = new BroadcastReceiver() {
  309. @SuppressLint("NewApi")
  310. @Override
  311. public void onReceive(Context context, Intent intent) {
  312. if (getUserVisibleHint())
  313. updateUI();
  314. }
  315. };
  316. registerBroadcastReceiver();
  317. updateUI();
  318. // connects the switch listener to the switch button
  319. mHomeSwitchConnection = (Switch) mRootView.findViewById(R.id.home_switch_connection);
  320. mHomeSwitchConnection.setSaveEnabled(false);
  321. if (mSwitchChangeListener == null) {
  322. mSwitchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  323. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  324. // displays a alert dialog if no network is available
  325. //SK: Temporary bugfix to limit to wifi
  326. //if (!HelperUtils.isNetworkAvailable(getActivity())) {
  327. if (!HelperUtils.isWifiConnected(getActivity())) {
  328. //new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.network_not_connected_msg)
  329. new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.wifi_not_connected_msg)
  330. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  331. public void onClick(DialogInterface dialog, int which) {
  332. }
  333. }).setIcon(android.R.drawable.ic_dialog_info).show();
  334. setStateNotActive();
  335. setStateNotConnected();
  336. } else {
  337. if (isChecked) {
  338. boolean protocolActivated = false;
  339. mSharedPreferences = getActivity().getSharedPreferences(getString(R.string.shared_preference_path), Hostage.MODE_PRIVATE);
  340. if(mSharedPreferences.getBoolean( "isFirstEmulation", true)) {
  341. //Notify Porthack is not installed to user
  342. if(!Device.isPorthackInstalled()) {
  343. new AlertDialog.Builder(getActivity())
  344. .setTitle(R.string.information)
  345. .setMessage(R.string.no_portbinder_msg2)
  346. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  347. public void onClick(DialogInterface dialog, int which) {
  348. }
  349. })
  350. .setNegativeButton(R.string.help, new DialogInterface.OnClickListener() {
  351. public void onClick(DialogInterface dialog, int which) {
  352. MainActivity.getInstance().displayView(MainActivity.MainMenuItem.SETTINGS.getValue());
  353. }
  354. })
  355. .setIcon(android.R.drawable.ic_dialog_info).show();
  356. }
  357. // Disable hint for 'first' time emulation notification
  358. SharedPreferences.Editor editor = mSharedPreferences.edit();
  359. editor.putBoolean("isFirstEmulation", false);
  360. editor.commit();
  361. }
  362. if (ProfileManager.getInstance().getCurrentActivatedProfile() == null) {
  363. // starts all services
  364. MainActivity.getInstance().startMonitorServices(Arrays.asList(getResources().getStringArray(R.array.protocols)));
  365. } else {
  366. // starts the services that are actived in the current profile
  367. ProfileManager profileManager = ProfileManager.getInstance();
  368. if (profileManager.isRandomActive()) {
  369. profileManager.randomizeProtocols(profileManager.getRandomProfile());
  370. }
  371. Profile currentProfile = profileManager.getCurrentActivatedProfile();
  372. List<String> protocols = currentProfile.getActiveProtocols();
  373. if(protocols.size() > 0 || currentProfile.mGhostActive){
  374. protocols.add("GHOST");
  375. MainActivity.getInstance().startMonitorServices(protocols);
  376. protocolActivated = true;
  377. }
  378. }
  379. if (protocolActivated) {
  380. setStateActive();
  381. } else {
  382. // no protocol was started, so show alert dialog
  383. new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.profile_no_services_msg)
  384. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  385. public void onClick(DialogInterface dialog, int which) {
  386. }
  387. }).setIcon(android.R.drawable.ic_dialog_info).show();
  388. setStateNotActive();
  389. }
  390. } else {
  391. // stop hostage service and all listeners
  392. if (MainActivity.getInstance().getHostageService() != null) {
  393. MainActivity.getInstance().getHostageService().stopListeners();
  394. MainActivity.getInstance().stopAndUnbind();
  395. }
  396. setStateNotActive();
  397. }
  398. }
  399. }
  400. };
  401. }
  402. mHomeSwitchConnection.setOnCheckedChangeListener(mSwitchChangeListener);
  403. // connects the profile text an click listener
  404. mRootView.findViewById(R.id.home_profile_details).setOnClickListener(new View.OnClickListener() {
  405. @Override
  406. public void onClick(View v) {
  407. Fragment fragment = new ProfileManagerFragment();
  408. MainActivity.getInstance().injectFragment(fragment);
  409. }
  410. });
  411. // connect the attacks text to an click listener
  412. View.OnClickListener attackClickListener = new View.OnClickListener() {
  413. @Override
  414. public void onClick(View v) {
  415. String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "");
  416. if (!ssid.isEmpty()) {
  417. ArrayList<String> ssids = new ArrayList<String>();
  418. ssids.add(ssid);
  419. LogFilter filter = new LogFilter();
  420. filter.setESSIDs(ssids);
  421. RecordOverviewFragment recordOverviewFragment = new RecordOverviewFragment();
  422. recordOverviewFragment.setFilter(filter);
  423. recordOverviewFragment.setGroupKey("ESSID");
  424. MainActivity.getInstance().injectFragment(recordOverviewFragment);
  425. }
  426. }
  427. };
  428. mHomeTextAttacks.setOnClickListener(attackClickListener);
  429. mHomeTextSecurity.setOnClickListener(attackClickListener);
  430. return mRootView;
  431. }
  432. /**
  433. * {@inheritDoc}
  434. */
  435. @Override
  436. public void onStop() {
  437. super.onStop();
  438. unregisterBroadcastReceiver();
  439. }
  440. /**
  441. * {@inheritDoc}
  442. */
  443. @Override
  444. public void onStart() {
  445. super.onStart();
  446. registerBroadcastReceiver();
  447. updateUI();
  448. }
  449. /**
  450. * {@inheritDoc}
  451. */
  452. @Override
  453. public void onDestroy() {
  454. super.onDestroy();
  455. unregisterBroadcastReceiver();
  456. }
  457. }