HomeFragment.java 14 KB

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