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.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. mHomeTextName.setText(mConnectionInfo.getString(getString(R.string.connection_info_ssid), ""));
  143. } else {
  144. setStateNotConnected();
  145. if(isActive) ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT);
  146. return;
  147. }
  148. boolean hasActiveListeners = false;
  149. int totalAttacks = mDbHelper.numBssidSeen(mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  150. ThreatIndicatorGLRenderer.ThreatLevel threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING;
  151. if (MainActivity.getInstance().getHostageService() != null) {
  152. if (MainActivity.getInstance().getHostageService().hasRunningListeners()) {
  153. hasActiveListeners = true;
  154. if (MainActivity.getInstance().getHostageService().hasActiveAttacks() && totalAttacks > 0) {
  155. threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.LIVE_THREAT;
  156. } else if (totalAttacks > 0) {
  157. threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.PAST_THREAT;
  158. } else {
  159. threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT;
  160. }
  161. }
  162. }
  163. if (hasActiveListeners) {
  164. setStateActive(true);
  165. switch (threatLevel) {
  166. case NO_THREAT:
  167. mHomeTextAttacks.setText(R.string.zero_attacks);
  168. mHomeTextSecurity.setText(R.string.secure);
  169. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_dark_green));
  170. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_dark_green));
  171. break;
  172. case PAST_THREAT:
  173. mHomeTextAttacks.setText(totalAttacks
  174. + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks))
  175. + getResources().getString(R.string.recorded));
  176. mHomeTextSecurity.setText(R.string.insecure);
  177. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_yellow));
  178. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_yellow));
  179. break;
  180. case LIVE_THREAT:
  181. mHomeTextAttacks.setText(totalAttacks
  182. + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks))
  183. + getResources().getString(R.string.recorded));
  184. mHomeTextSecurity.setText(R.string.insecure);
  185. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_red));
  186. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_red));
  187. break;
  188. }
  189. ThreatIndicatorGLRenderer.setThreatLevel(threatLevel);
  190. } else {
  191. setStateNotActive();
  192. if (!HelperUtils.isNetworkAvailable(getActivity())) {
  193. setStateNotConnected();
  194. }
  195. }
  196. }
  197. @Override
  198. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  199. super.onCreateView(inflater, container, savedInstanceState);
  200. final Activity activity = getActivity();
  201. if (activity != null) {
  202. activity.setTitle(getResources().getString(R.string.drawer_overview));
  203. }
  204. mDbHelper = new HostageDBOpenHelper(getActivity());
  205. mProfileManager = ProfileManager.getInstance();
  206. mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  207. mRootView = inflater.inflate(R.layout.fragment_home, container, false);
  208. assignViews();
  209. // hook up the connection info button
  210. mHomeConnectionInfoButton.setOnClickListener(new View.OnClickListener() {
  211. @Override
  212. public void onClick(View v) {
  213. final FragmentManager fragmentManager = getFragmentManager();
  214. if (fragmentManager != null) {
  215. ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
  216. connectionInfoDialogFragment.show(fragmentManager.beginTransaction(), connectionInfoDialogFragment.getTag());
  217. }
  218. }
  219. });
  220. mDefaultTextColor = mHomeTextName.getCurrentTextColor();
  221. setStateNotActive(true);
  222. setStateNotConnected();
  223. mReceiver = new BroadcastReceiver() {
  224. @SuppressLint("NewApi")
  225. @Override
  226. public void onReceive(Context context, Intent intent) {
  227. if (getUserVisibleHint())
  228. updateUI();
  229. }
  230. };
  231. registerBroadcastReceiver();
  232. updateUI();
  233. mHomeSwitchConnection = (Switch) mRootView.findViewById(R.id.home_switch_connection);
  234. mHomeSwitchConnection.setSaveEnabled(false);
  235. if (mSwitchChangeListener == null) {
  236. mSwitchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  237. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  238. if (!HelperUtils.isNetworkAvailable(getActivity())) {
  239. new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.network_not_connected_msg)
  240. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  241. public void onClick(DialogInterface dialog, int which) {
  242. }
  243. }).setIcon(android.R.drawable.ic_dialog_info).show();
  244. setStateNotActive();
  245. setStateNotConnected();
  246. } else {
  247. if (isChecked) {
  248. boolean protocolActivated = false;
  249. if (ProfileManager.getInstance().getCurrentActivatedProfile() == null) {
  250. MainActivity.getInstance().startMonitorServices(Arrays.asList(getResources().getStringArray(R.array.protocols)));
  251. } else {
  252. ProfileManager profileManager = ProfileManager.getInstance();
  253. if (profileManager.isRandomActive()) {
  254. profileManager.randomizeProtocols(profileManager.getRandomProfile());
  255. }
  256. Profile currentProfile = profileManager.getCurrentActivatedProfile();
  257. List<String> protocols = currentProfile.getActiveProtocols();
  258. if(protocols.size() > 0 || currentProfile.mGhostActive){
  259. protocols.add("GHOST");
  260. MainActivity.getInstance().startMonitorServices(protocols);
  261. protocolActivated = true;
  262. }
  263. }
  264. if (protocolActivated) {
  265. setStateActive();
  266. } else {
  267. new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.profile_no_services_msg)
  268. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  269. public void onClick(DialogInterface dialog, int which) {
  270. }
  271. }).setIcon(android.R.drawable.ic_dialog_info).show();
  272. setStateNotActive();
  273. }
  274. } else {
  275. if (MainActivity.getInstance().getHostageService() != null) {
  276. MainActivity.getInstance().getHostageService().stopListeners();
  277. MainActivity.getInstance().stopAndUnbind();
  278. }
  279. setStateNotActive();
  280. }
  281. }
  282. }
  283. };
  284. }
  285. mHomeSwitchConnection.setOnCheckedChangeListener(mSwitchChangeListener);
  286. mRootView.findViewById(R.id.home_profile_details).setOnClickListener(new View.OnClickListener() {
  287. @Override
  288. public void onClick(View v) {
  289. Fragment fragment = new ProfileManagerFragment();
  290. MainActivity.getInstance().injectFragment(fragment, false);
  291. }
  292. });
  293. View.OnClickListener attackClickListener = new View.OnClickListener() {
  294. @Override
  295. public void onClick(View v) {
  296. String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "");
  297. if (!ssid.isEmpty()) {
  298. ArrayList<String> ssids = new ArrayList<String>();
  299. ssids.add(ssid);
  300. LogFilter filter = new LogFilter();
  301. filter.setESSIDs(ssids);
  302. RecordOverviewFragment recordOverviewFragment = new RecordOverviewFragment();
  303. recordOverviewFragment.setFilter(filter);
  304. recordOverviewFragment.setGroupKey("ESSID");
  305. MainActivity.getInstance().injectFragment(recordOverviewFragment);
  306. }
  307. }
  308. };
  309. mHomeTextAttacks.setOnClickListener(attackClickListener);
  310. mHomeTextSecurity.setOnClickListener(attackClickListener);
  311. return mRootView;
  312. }
  313. @Override
  314. public void onStop() {
  315. super.onStop();
  316. unregisterBroadcastReceiver();
  317. }
  318. @Override
  319. public void onStart() {
  320. super.onStart();
  321. registerBroadcastReceiver();
  322. updateUI();
  323. }
  324. @Override
  325. public void onDestroy() {
  326. super.onDestroy();
  327. unregisterBroadcastReceiver();
  328. }
  329. }