HomeFragment.java 13 KB

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