HomeFragment.java 14 KB

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