HomeFragment.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. package de.tudarmstadt.informatik.hostage.ui.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.MotionEvent;
  20. import android.view.View;
  21. import android.view.ViewGroup;
  22. import android.widget.CompoundButton;
  23. import android.widget.ImageView;
  24. import android.widget.Switch;
  25. import android.widget.TextView;
  26. import de.tudarmstadt.informatik.hostage.R;
  27. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  28. import de.tudarmstadt.informatik.hostage.persistence.ProfileManager;
  29. import de.tudarmstadt.informatik.hostage.model.Profile;
  30. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  31. import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
  32. import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
  33. import de.tudarmstadt.informatik.hostage.ui.fragment.opengl.ThreatIndicatorGLRenderer;
  34. /**
  35. * @author Alexander Brakowski
  36. * @created 13.01.14 19:06
  37. */
  38. public class HomeFragment extends Fragment {
  39. private Switch mHomeSwitchConnection;
  40. private TextView mHomeTextName;
  41. private TextView mHomeTextSecurity;
  42. private TextView mHomeTextAttacks;
  43. private TextView mHomeTextProfile;
  44. private TextView mHomeTextProfileHeader;
  45. private ImageView mHomeProfileImage;
  46. private ImageView mHomeConnectionInfoButton;
  47. private View mRootView;
  48. private BroadcastReceiver mReceiver;
  49. private CompoundButton.OnCheckedChangeListener mSwitchChangeListener = null;
  50. private int mDefaultTextColor;
  51. private ProfileManager mProfileManager;
  52. private SharedPreferences mConnectionInfo;
  53. private HostageDBOpenHelper mDbHelper;
  54. private boolean mReceiverRegistered;
  55. private boolean mRestoredFromSaved = false;
  56. private boolean isActive = false;
  57. private boolean isConnected = false;
  58. private ThreatIndicatorGLRenderer.ThreatLevel mThreatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING;
  59. private void assignViews() {
  60. mHomeSwitchConnection = (Switch) mRootView.findViewById(R.id.home_switch_connection);
  61. mHomeTextName = (TextView) mRootView.findViewById(R.id.home_text_name);
  62. mHomeTextSecurity = (TextView) mRootView.findViewById(R.id.home_text_security);
  63. mHomeTextAttacks = (TextView) mRootView.findViewById(R.id.home_text_attacks);
  64. mHomeTextProfile = (TextView) mRootView.findViewById(R.id.home_text_profile);
  65. mHomeTextProfileHeader = (TextView) mRootView.findViewById(R.id.home_text_profile_header);
  66. mHomeProfileImage = (ImageView) mRootView.findViewById(R.id.home_image_profile);
  67. mHomeConnectionInfoButton = (ImageView) mRootView.findViewById(R.id.home_button_connection_info);
  68. }
  69. private void registerBroadcastReceiver() {
  70. if (!mReceiverRegistered) {
  71. LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
  72. this.mReceiverRegistered = true;
  73. }
  74. }
  75. private void unregisterBroadcastReceiver() {
  76. if (mReceiverRegistered) {
  77. LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
  78. this.mReceiverRegistered = false;
  79. }
  80. }
  81. public HomeFragment() {
  82. }
  83. public void setStateNotActive(boolean initial) {
  84. mHomeTextName.setTextColor(getResources().getColor(R.color.light_grey));
  85. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.light_grey));
  86. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.light_grey));
  87. mHomeTextProfile.setTextColor(getResources().getColor(R.color.light_grey));
  88. mHomeTextProfileHeader.setTextColor(getResources().getColor(R.color.light_grey));
  89. if (!initial) {
  90. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING);
  91. }
  92. mHomeSwitchConnection.setChecked(false);
  93. isActive = false;
  94. }
  95. public void setStateNotActive() {
  96. setStateNotActive(false);
  97. }
  98. public void setStateActive() {
  99. setStateActive(false);
  100. }
  101. public void setStateActive(boolean initial) {
  102. mHomeTextName.setTextColor(mDefaultTextColor);
  103. mHomeTextProfile.setTextColor(mDefaultTextColor);
  104. mHomeTextProfileHeader.setTextColor(mDefaultTextColor);
  105. mHomeSwitchConnection.setChecked(true);
  106. isActive = true;
  107. }
  108. public void setStateNotConnected() {
  109. mHomeTextSecurity.setVisibility(View.INVISIBLE);
  110. mHomeTextAttacks.setVisibility(View.INVISIBLE);
  111. mHomeTextProfile.setVisibility(View.INVISIBLE);
  112. mHomeTextProfileHeader.setVisibility(View.INVISIBLE);
  113. mHomeProfileImage.setVisibility(View.INVISIBLE);
  114. mHomeConnectionInfoButton.setVisibility(View.INVISIBLE);
  115. mHomeTextName.setText(R.string.not_connected);
  116. isConnected = false;
  117. }
  118. public void setStateConnected() {
  119. mHomeTextAttacks.setVisibility(View.VISIBLE);
  120. mHomeTextSecurity.setVisibility(View.VISIBLE);
  121. mHomeTextProfile.setVisibility(View.VISIBLE);
  122. mHomeTextProfileHeader.setVisibility(View.VISIBLE);
  123. mHomeProfileImage.setVisibility(View.VISIBLE);
  124. mHomeConnectionInfoButton.setVisibility(View.VISIBLE);
  125. isConnected = true;
  126. }
  127. public void updateUI() {
  128. Profile profile = mProfileManager.getCurrentActivatedProfile();
  129. if (profile != null) {
  130. mHomeTextProfile.setText(profile.mLabel);
  131. mHomeProfileImage.setImageBitmap(profile.getIconBitmap());
  132. }
  133. if (HelperUtils.isNetworkAvailable(getActivity())) {
  134. setStateConnected();
  135. String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "");
  136. mHomeTextName.setText(ssid);
  137. } else {
  138. setStateNotConnected();
  139. }
  140. boolean hasActiveListeners = false;
  141. int totalAttacks = mDbHelper.getNumAttacksSeenByBSSID(
  142. mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  143. if (MainActivity.getInstance().getHostageService() != null) {
  144. if (MainActivity.getInstance().getHostageService().hasRunningListeners()) {
  145. hasActiveListeners = true;
  146. if (MainActivity.getInstance().getHostageService().hasActiveAttacks() && totalAttacks > 0) {
  147. mThreatLevel = ThreatIndicatorGLRenderer.ThreatLevel.LIVE_THREAT;
  148. } else if (totalAttacks > 0) {
  149. mThreatLevel = ThreatIndicatorGLRenderer.ThreatLevel.PAST_THREAT;
  150. } else {
  151. mThreatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT;
  152. }
  153. }
  154. }
  155. if (isConnected) {
  156. if (totalAttacks == 0) {
  157. mHomeTextAttacks.setText(R.string.zero_attacks);
  158. mHomeTextSecurity.setText(R.string.secure);
  159. } else {
  160. mHomeTextAttacks.setText(totalAttacks
  161. + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks))
  162. + getResources().getString(R.string.recorded));
  163. mHomeTextSecurity.setText(R.string.insecure);
  164. }
  165. } else {
  166. mHomeTextAttacks.setText("");
  167. mHomeTextSecurity.setText("");
  168. }
  169. if (hasActiveListeners) {
  170. setStateActive(true);
  171. // color text according to threat level
  172. switch (mThreatLevel) {
  173. case NO_THREAT:
  174. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_dark_green));
  175. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_dark_green));
  176. break;
  177. case PAST_THREAT:
  178. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_yellow));
  179. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_yellow));
  180. break;
  181. case LIVE_THREAT:
  182. mHomeTextAttacks.setText(totalAttacks
  183. + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks))
  184. + getResources().getString(R.string.recorded));
  185. mHomeTextSecurity.setText(R.string.insecure);
  186. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_red));
  187. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_red));
  188. break;
  189. }
  190. ThreatIndicatorGLRenderer.setThreatLevel(mThreatLevel);
  191. } else {
  192. setStateNotActive();
  193. }
  194. }
  195. @Override
  196. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  197. super.onCreateView(inflater, container, savedInstanceState);
  198. final Activity activity = getActivity();
  199. if (activity != null) {
  200. activity.setTitle(getResources().getString(R.string.drawer_overview));
  201. }
  202. mDbHelper = new HostageDBOpenHelper(getActivity());
  203. mProfileManager = ProfileManager.getInstance();
  204. mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  205. mRootView = inflater.inflate(R.layout.fragment_home, container, false);
  206. assignViews();
  207. mRootView.findViewById(R.id.surfaceview).setOnTouchListener(new View.OnTouchListener() {
  208. @Override
  209. public boolean onTouch(View v, MotionEvent event) {
  210. float relx = event.getX() / (float)v.getWidth();
  211. float rely = event.getY() / (float)v.getHeight();
  212. if (relx < 0.25f || relx > 0.75f) return false;
  213. if (rely < 0.25f || rely > 0.9f) return false;
  214. ThreatIndicatorGLRenderer.showSpeechBubble();
  215. return false;
  216. }
  217. });
  218. // hook up the connection info button
  219. mHomeConnectionInfoButton.setOnClickListener(new View.OnClickListener() {
  220. @Override
  221. public void onClick(View v) {
  222. final FragmentManager fragmentManager = getFragmentManager();
  223. if (fragmentManager != null) {
  224. ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
  225. connectionInfoDialogFragment.show(fragmentManager.beginTransaction(), connectionInfoDialogFragment.getTag());
  226. }
  227. }
  228. });
  229. mDefaultTextColor = mHomeTextName.getCurrentTextColor();
  230. setStateNotActive(true);
  231. setStateNotConnected();
  232. mReceiver = new BroadcastReceiver() {
  233. @SuppressLint("NewApi")
  234. @Override
  235. public void onReceive(Context context, Intent intent) {
  236. if (getUserVisibleHint())
  237. updateUI();
  238. }
  239. };
  240. registerBroadcastReceiver();
  241. updateUI();
  242. mHomeSwitchConnection = (Switch) mRootView.findViewById(R.id.home_switch_connection);
  243. mHomeSwitchConnection.setSaveEnabled(false);
  244. if (mSwitchChangeListener == null) {
  245. mSwitchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  246. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  247. if (isChecked) { // switch activated
  248. // we need a network connection
  249. // 2.5.2015 Fabio: for now we only check for wifi connections
  250. if (!HelperUtils.isWifiConnected(getActivity())) {
  251. new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.network_not_connected_msg)
  252. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  253. public void onClick(DialogInterface dialog, int which) {
  254. }
  255. }).setIcon(android.R.drawable.ic_dialog_info).show();
  256. setStateNotActive();
  257. setStateNotConnected();
  258. } else { // network available
  259. boolean protocolActivated = false;
  260. if (ProfileManager.getInstance().getCurrentActivatedProfile() == null) {
  261. MainActivity.getInstance().startMonitorServices(Arrays.asList(
  262. getResources().getStringArray(R.array.protocols)));
  263. } else {
  264. ProfileManager profileManager = ProfileManager.getInstance();
  265. if (profileManager.isRandomActive()) {
  266. profileManager
  267. .randomizeProtocols(profileManager.getRandomProfile());
  268. }
  269. Profile currentProfile = profileManager
  270. .getCurrentActivatedProfile();
  271. List<String> protocols = currentProfile.getActiveProtocols();
  272. if (protocols.size() > 0 || currentProfile.mGhostActive) {
  273. protocols.add("GHOST");
  274. MainActivity.getInstance().startMonitorServices(protocols);
  275. protocolActivated = true;
  276. }
  277. }
  278. if (protocolActivated) {
  279. setStateActive();
  280. } else {
  281. new AlertDialog.Builder(getActivity())
  282. .setTitle(R.string.information)
  283. .setMessage(R.string.profile_no_services_msg)
  284. .setPositiveButton(android.R.string.ok,
  285. new DialogInterface.OnClickListener() {
  286. public void onClick(DialogInterface dialog,
  287. int which) {
  288. }
  289. }).setIcon(android.R.drawable.ic_dialog_info)
  290. .show();
  291. setStateNotActive();
  292. }
  293. }
  294. } else { // switch deactivated
  295. if (MainActivity.getInstance().getHostageService() != null) {
  296. MainActivity.getInstance().getHostageService().stopListeners();
  297. MainActivity.getInstance().stopAndUnbind();
  298. }
  299. setStateNotActive();
  300. }
  301. }
  302. };
  303. }
  304. mHomeSwitchConnection.setOnCheckedChangeListener(mSwitchChangeListener);
  305. mRootView.findViewById(R.id.home_profile_details).setOnClickListener(new View.OnClickListener() {
  306. @Override
  307. public void onClick(View v) {
  308. Fragment fragment = new ProfileManagerFragment();
  309. MainActivity.getInstance().injectFragment(fragment);
  310. }
  311. });
  312. View.OnClickListener attackClickListener = new View.OnClickListener() {
  313. @Override
  314. public void onClick(View v) {
  315. String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "");
  316. if (!ssid.isEmpty()) {
  317. ArrayList<String> ssids = new ArrayList<String>();
  318. ssids.add(ssid);
  319. LogFilter filter = new LogFilter();
  320. filter.setESSIDs(ssids);
  321. RecordOverviewFragment recordOverviewFragment = new RecordOverviewFragment();
  322. recordOverviewFragment.setFilter(filter);
  323. recordOverviewFragment.setGroupKey("ESSID");
  324. MainActivity.getInstance().injectFragment(recordOverviewFragment);
  325. }
  326. }
  327. };
  328. mHomeTextAttacks.setOnClickListener(attackClickListener);
  329. mHomeTextSecurity.setOnClickListener(attackClickListener);
  330. return mRootView;
  331. }
  332. @Override
  333. public void onStop() {
  334. super.onStop();
  335. unregisterBroadcastReceiver();
  336. }
  337. @Override
  338. public void onStart() {
  339. super.onStart();
  340. registerBroadcastReceiver();
  341. updateUI();
  342. }
  343. @Override
  344. public void onDestroy() {
  345. super.onDestroy();
  346. unregisterBroadcastReceiver();
  347. }
  348. }