HomeFragment.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. mHomeTextAttacks.setVisibility(View.VISIBLE);
  103. mHomeTextSecurity.setVisibility(View.VISIBLE);
  104. mHomeTextName.setTextColor(mDefaultTextColor);
  105. mHomeTextProfile.setTextColor(mDefaultTextColor);
  106. mHomeTextProfileHeader.setTextColor(mDefaultTextColor);
  107. mHomeSwitchConnection.setChecked(true);
  108. isActive = true;
  109. }
  110. public void setStateNotConnected() {
  111. mHomeTextSecurity.setVisibility(View.INVISIBLE);
  112. mHomeTextAttacks.setVisibility(View.INVISIBLE);
  113. mHomeTextProfile.setVisibility(View.INVISIBLE);
  114. mHomeTextProfileHeader.setVisibility(View.INVISIBLE);
  115. mHomeProfileImage.setVisibility(View.INVISIBLE);
  116. mHomeConnectionInfoButton.setVisibility(View.INVISIBLE);
  117. mHomeTextName.setText(R.string.not_connected);
  118. isConnected = false;
  119. }
  120. public void setStateConnected() {
  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 (hasActiveListeners) {
  156. setStateActive(true);
  157. if(!isConnected){
  158. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT);
  159. mHomeTextAttacks.setText("");
  160. mHomeTextSecurity.setText("");
  161. } else {
  162. switch (mThreatLevel) {
  163. case NO_THREAT:
  164. mHomeTextAttacks.setText(R.string.zero_attacks);
  165. mHomeTextSecurity.setText(R.string.secure);
  166. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_dark_green));
  167. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_dark_green));
  168. break;
  169. case PAST_THREAT:
  170. mHomeTextAttacks.setText(totalAttacks
  171. + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks))
  172. + getResources().getString(R.string.recorded));
  173. mHomeTextSecurity.setText(R.string.insecure);
  174. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_yellow));
  175. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_yellow));
  176. break;
  177. case LIVE_THREAT:
  178. mHomeTextAttacks.setText(totalAttacks
  179. + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks))
  180. + getResources().getString(R.string.recorded));
  181. mHomeTextSecurity.setText(R.string.insecure);
  182. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_red));
  183. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_red));
  184. break;
  185. }
  186. ThreatIndicatorGLRenderer.setThreatLevel(mThreatLevel);
  187. }
  188. } else {
  189. setStateNotActive();
  190. }
  191. }
  192. @Override
  193. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  194. super.onCreateView(inflater, container, savedInstanceState);
  195. final Activity activity = getActivity();
  196. if (activity != null) {
  197. activity.setTitle(getResources().getString(R.string.drawer_overview));
  198. }
  199. mDbHelper = new HostageDBOpenHelper(getActivity());
  200. mProfileManager = ProfileManager.getInstance();
  201. mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  202. mRootView = inflater.inflate(R.layout.fragment_home, container, false);
  203. assignViews();
  204. mRootView.findViewById(R.id.surfaceview).setOnTouchListener(new View.OnTouchListener() {
  205. @Override
  206. public boolean onTouch(View v, MotionEvent event) {
  207. float relx = event.getX() / (float)v.getWidth();
  208. float rely = event.getY() / (float)v.getHeight();
  209. if (relx < 0.25f || relx > 0.75f) return false;
  210. if (rely < 0.25f || rely > 0.9f) return false;
  211. ThreatIndicatorGLRenderer.showSpeechBubble();
  212. return false;
  213. }
  214. });
  215. // hook up the connection info button
  216. mHomeConnectionInfoButton.setOnClickListener(new View.OnClickListener() {
  217. @Override
  218. public void onClick(View v) {
  219. final FragmentManager fragmentManager = getFragmentManager();
  220. if (fragmentManager != null) {
  221. ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
  222. connectionInfoDialogFragment.show(fragmentManager.beginTransaction(), connectionInfoDialogFragment.getTag());
  223. }
  224. }
  225. });
  226. mDefaultTextColor = mHomeTextName.getCurrentTextColor();
  227. setStateNotActive(true);
  228. setStateNotConnected();
  229. mReceiver = new BroadcastReceiver() {
  230. @SuppressLint("NewApi")
  231. @Override
  232. public void onReceive(Context context, Intent intent) {
  233. if (getUserVisibleHint())
  234. updateUI();
  235. }
  236. };
  237. registerBroadcastReceiver();
  238. updateUI();
  239. mHomeSwitchConnection = (Switch) mRootView.findViewById(R.id.home_switch_connection);
  240. mHomeSwitchConnection.setSaveEnabled(false);
  241. if (mSwitchChangeListener == null) {
  242. mSwitchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  243. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  244. if (isChecked) { // switch activated
  245. // we need a network connection
  246. // 2.5.2015 Fabio: for now we only check for wifi connections
  247. if (!HelperUtils.isWifiConnected(getActivity())) {
  248. new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.network_not_connected_msg)
  249. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  250. public void onClick(DialogInterface dialog, int which) {
  251. }
  252. }).setIcon(android.R.drawable.ic_dialog_info).show();
  253. setStateNotActive();
  254. setStateNotConnected();
  255. } else { // network available
  256. boolean protocolActivated = false;
  257. if (ProfileManager.getInstance().getCurrentActivatedProfile() == null) {
  258. MainActivity.getInstance().startMonitorServices(Arrays.asList(
  259. getResources().getStringArray(R.array.protocols)));
  260. } else {
  261. ProfileManager profileManager = ProfileManager.getInstance();
  262. if (profileManager.isRandomActive()) {
  263. profileManager
  264. .randomizeProtocols(profileManager.getRandomProfile());
  265. }
  266. Profile currentProfile = profileManager
  267. .getCurrentActivatedProfile();
  268. List<String> protocols = currentProfile.getActiveProtocols();
  269. if (protocols.size() > 0 || currentProfile.mGhostActive) {
  270. protocols.add("GHOST");
  271. MainActivity.getInstance().startMonitorServices(protocols);
  272. protocolActivated = true;
  273. }
  274. }
  275. if (protocolActivated) {
  276. setStateActive();
  277. } else {
  278. new AlertDialog.Builder(getActivity())
  279. .setTitle(R.string.information)
  280. .setMessage(R.string.profile_no_services_msg)
  281. .setPositiveButton(android.R.string.ok,
  282. new DialogInterface.OnClickListener() {
  283. public void onClick(DialogInterface dialog,
  284. int which) {
  285. }
  286. }).setIcon(android.R.drawable.ic_dialog_info)
  287. .show();
  288. setStateNotActive();
  289. }
  290. }
  291. } else { // switch deactivated
  292. if (MainActivity.getInstance().getHostageService() != null) {
  293. MainActivity.getInstance().getHostageService().stopListeners();
  294. MainActivity.getInstance().stopAndUnbind();
  295. }
  296. setStateNotActive();
  297. }
  298. }
  299. };
  300. }
  301. mHomeSwitchConnection.setOnCheckedChangeListener(mSwitchChangeListener);
  302. mRootView.findViewById(R.id.home_profile_details).setOnClickListener(new View.OnClickListener() {
  303. @Override
  304. public void onClick(View v) {
  305. Fragment fragment = new ProfileManagerFragment();
  306. MainActivity.getInstance().injectFragment(fragment);
  307. }
  308. });
  309. View.OnClickListener attackClickListener = new View.OnClickListener() {
  310. @Override
  311. public void onClick(View v) {
  312. String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "");
  313. if (!ssid.isEmpty()) {
  314. ArrayList<String> ssids = new ArrayList<String>();
  315. ssids.add(ssid);
  316. LogFilter filter = new LogFilter();
  317. filter.setESSIDs(ssids);
  318. RecordOverviewFragment recordOverviewFragment = new RecordOverviewFragment();
  319. recordOverviewFragment.setFilter(filter);
  320. recordOverviewFragment.setGroupKey("ESSID");
  321. MainActivity.getInstance().injectFragment(recordOverviewFragment);
  322. }
  323. }
  324. };
  325. mHomeTextAttacks.setOnClickListener(attackClickListener);
  326. mHomeTextSecurity.setOnClickListener(attackClickListener);
  327. return mRootView;
  328. }
  329. @Override
  330. public void onStop() {
  331. super.onStop();
  332. unregisterBroadcastReceiver();
  333. }
  334. @Override
  335. public void onStart() {
  336. super.onStart();
  337. registerBroadcastReceiver();
  338. updateUI();
  339. }
  340. @Override
  341. public void onDestroy() {
  342. super.onDestroy();
  343. unregisterBroadcastReceiver();
  344. }
  345. }