HomeFragment.java 14 KB

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