HomeFragment.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. package de.tudarmstadt.informatik.hostage.ui.fragment;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import android.annotation.SuppressLint;
  7. import android.app.Activity;
  8. import android.app.AlertDialog;
  9. import android.app.Fragment;
  10. import android.app.FragmentManager;
  11. import android.content.BroadcastReceiver;
  12. import android.content.Context;
  13. import android.content.DialogInterface;
  14. import android.content.Intent;
  15. import android.content.IntentFilter;
  16. import android.content.SharedPreferences;
  17. import android.os.AsyncTask;
  18. import android.os.Bundle;
  19. import android.support.v4.content.LocalBroadcastManager;
  20. import android.view.LayoutInflater;
  21. import android.view.MotionEvent;
  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 android.widget.Toast;
  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.persistence.HostageDBOpenHelper;
  34. import de.tudarmstadt.informatik.hostage.services.MultiStageAlarm;
  35. import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
  36. import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
  37. import de.tudarmstadt.informatik.hostage.ui.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 HostageDBOpenHelper mDbHelper;
  58. private boolean mReceiverRegistered;
  59. private boolean mRestoredFromSaved = false;
  60. private boolean isActive = false;
  61. private boolean isConnected = false;
  62. public Context getContext() {
  63. return context;
  64. }
  65. public void setContext(Context context) {
  66. this.context = context;
  67. }
  68. public Context context;
  69. private ThreatIndicatorGLRenderer.ThreatLevel mThreatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING;
  70. private void assignViews() {
  71. mHomeSwitchConnection = (Switch) mRootView.findViewById(R.id.home_switch_connection);
  72. mHomeTextName = (TextView) mRootView.findViewById(R.id.home_text_name);
  73. mHomeTextSecurity = (TextView) mRootView.findViewById(R.id.home_text_security);
  74. mHomeTextAttacks = (TextView) mRootView.findViewById(R.id.home_text_attacks);
  75. mHomeTextProfile = (TextView) mRootView.findViewById(R.id.home_text_profile);
  76. mHomeTextProfileHeader = (TextView) mRootView.findViewById(R.id.home_text_profile_header);
  77. mHomeProfileImage = (ImageView) mRootView.findViewById(R.id.home_image_profile);
  78. mHomeConnectionInfoButton = (ImageView) mRootView.findViewById(R.id.home_button_connection_info);
  79. }
  80. private void registerBroadcastReceiver() {
  81. if (!mReceiverRegistered) {
  82. LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
  83. this.mReceiverRegistered = true;
  84. }
  85. }
  86. private void unregisterBroadcastReceiver() {
  87. if (mReceiverRegistered) {
  88. LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
  89. this.mReceiverRegistered = false;
  90. }
  91. }
  92. public HomeFragment() {
  93. }
  94. public void setStateNotActive(boolean initial) {
  95. mHomeTextName.setTextColor(getResources().getColor(R.color.light_grey));
  96. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.light_grey));
  97. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.light_grey));
  98. mHomeTextProfile.setTextColor(getResources().getColor(R.color.light_grey));
  99. mHomeTextProfileHeader.setTextColor(getResources().getColor(R.color.light_grey));
  100. if (!initial) {
  101. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING);
  102. }
  103. mHomeSwitchConnection.setChecked(false);
  104. isActive = false;
  105. }
  106. public void setStateNotActive() {
  107. setStateNotActive(false);
  108. }
  109. public void setStateActive() {
  110. setStateActive(false);
  111. }
  112. public void setStateActive(boolean initial) {
  113. mHomeTextName.setTextColor(mDefaultTextColor);
  114. mHomeTextProfile.setTextColor(mDefaultTextColor);
  115. mHomeTextProfileHeader.setTextColor(mDefaultTextColor);
  116. mHomeSwitchConnection.setChecked(true);
  117. isActive = true;
  118. }
  119. public void setStateNotConnected() {
  120. mHomeTextSecurity.setVisibility(View.INVISIBLE);
  121. mHomeTextAttacks.setVisibility(View.INVISIBLE);
  122. mHomeTextProfile.setVisibility(View.INVISIBLE);
  123. mHomeTextProfileHeader.setVisibility(View.INVISIBLE);
  124. mHomeProfileImage.setVisibility(View.INVISIBLE);
  125. mHomeConnectionInfoButton.setVisibility(View.INVISIBLE);
  126. mHomeTextName.setText(R.string.not_connected);
  127. isConnected = false;
  128. }
  129. public void setStateConnected() {
  130. mHomeTextAttacks.setVisibility(View.VISIBLE);
  131. mHomeTextSecurity.setVisibility(View.VISIBLE);
  132. mHomeTextProfile.setVisibility(View.VISIBLE);
  133. mHomeTextProfileHeader.setVisibility(View.VISIBLE);
  134. mHomeProfileImage.setVisibility(View.VISIBLE);
  135. mHomeConnectionInfoButton.setVisibility(View.VISIBLE);
  136. isConnected = true;
  137. }
  138. public void updateUI() {
  139. Profile profile = mProfileManager.getCurrentActivatedProfile();
  140. if (profile != null) {
  141. mHomeTextProfile.setText(profile.mLabel);
  142. mHomeProfileImage.setImageBitmap(profile.getIconBitmap());
  143. }
  144. //new FileAlertTask().execute();
  145. if (HelperUtils.isNetworkAvailable(getActivity())) {
  146. setStateConnected();
  147. String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "");
  148. mHomeTextName.setText(ssid);
  149. } else {
  150. setStateNotConnected();
  151. }
  152. boolean hasActiveListeners = false;
  153. int totalAttacks = mDbHelper.getNumAttacksSeenByBSSID(
  154. mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  155. if (MainActivity.getInstance().getHostageService() != null) {
  156. if (MainActivity.getInstance().getHostageService().hasRunningListeners()) {
  157. hasActiveListeners = true;
  158. if (MainActivity.getInstance().getHostageService().hasActiveAttacks() && totalAttacks > 0) {
  159. mThreatLevel = ThreatIndicatorGLRenderer.ThreatLevel.LIVE_THREAT;
  160. } else if (totalAttacks > 0) {
  161. mThreatLevel = ThreatIndicatorGLRenderer.ThreatLevel.PAST_THREAT;
  162. } else {
  163. mThreatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT;
  164. }
  165. }
  166. }
  167. if (isConnected) {
  168. if (totalAttacks == 0) {
  169. mHomeTextAttacks.setText(R.string.zero_attacks);
  170. mHomeTextSecurity.setText(R.string.secure);
  171. } else {
  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. }
  177. } else {
  178. mHomeTextAttacks.setText("");
  179. mHomeTextSecurity.setText("");
  180. }
  181. if (hasActiveListeners) {
  182. setStateActive(true);
  183. // color text according to threat level
  184. switch (mThreatLevel) {
  185. case NO_THREAT:
  186. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_dark_green));
  187. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_dark_green));
  188. break;
  189. case PAST_THREAT:
  190. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_yellow));
  191. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_yellow));
  192. break;
  193. case LIVE_THREAT:
  194. mHomeTextAttacks.setText(totalAttacks
  195. + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks))
  196. + getResources().getString(R.string.recorded));
  197. mHomeTextSecurity.setText(R.string.insecure);
  198. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_red));
  199. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_red));
  200. break;
  201. }
  202. ThreatIndicatorGLRenderer.setThreatLevel(mThreatLevel);
  203. } else {
  204. setStateNotActive();
  205. }
  206. }
  207. @Override
  208. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  209. super.onCreateView(inflater, container, savedInstanceState);
  210. final MultiStageAlarm alarm = new MultiStageAlarm();
  211. final Activity activity = getActivity();
  212. if (activity != null) {
  213. activity.setTitle(getResources().getString(R.string.drawer_overview));
  214. }
  215. mDbHelper = new HostageDBOpenHelper(getActivity());
  216. mProfileManager = ProfileManager.getInstance();
  217. mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  218. mRootView = inflater.inflate(R.layout.fragment_home, container, false);
  219. assignViews();
  220. mRootView.findViewById(R.id.surfaceview).setOnTouchListener(new View.OnTouchListener() {
  221. @Override
  222. public boolean onTouch(View v, MotionEvent event) {
  223. float relx = event.getX() / (float) v.getWidth();
  224. float rely = event.getY() / (float) v.getHeight();
  225. if (relx < 0.25f || relx > 0.75f) return false;
  226. if (rely < 0.25f || rely > 0.9f) return false;
  227. ThreatIndicatorGLRenderer.showSpeechBubble();
  228. return false;
  229. }
  230. });
  231. // hook up the connection info button
  232. mHomeConnectionInfoButton.setOnClickListener(new View.OnClickListener() {
  233. @Override
  234. public void onClick(View v) {
  235. final FragmentManager fragmentManager = getFragmentManager();
  236. if (fragmentManager != null) {
  237. ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
  238. connectionInfoDialogFragment.show(fragmentManager.beginTransaction(), connectionInfoDialogFragment.getTag());
  239. }
  240. }
  241. });
  242. mDefaultTextColor = mHomeTextName.getCurrentTextColor();
  243. setStateNotActive(true);
  244. setStateNotConnected();
  245. mReceiver = new BroadcastReceiver() {
  246. @SuppressLint("NewApi")
  247. @Override
  248. public void onReceive(Context context, Intent intent) {
  249. if (getUserVisibleHint())
  250. updateUI();
  251. }
  252. };
  253. registerBroadcastReceiver();
  254. updateUI();
  255. mHomeSwitchConnection = (Switch) mRootView.findViewById(R.id.home_switch_connection);
  256. mHomeSwitchConnection.setSaveEnabled(false);
  257. if (mSwitchChangeListener == null) {
  258. mSwitchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  259. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  260. if (isChecked) { // switch activated
  261. // we need a network connection
  262. // 2.5.2015 Fabio: for now we only check for wifi connections
  263. if (!HelperUtils.isWifiConnected(getActivity())) {
  264. new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.network_not_connected_msg)
  265. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  266. public void onClick(DialogInterface dialog, int which) {
  267. }
  268. }).setIcon(android.R.drawable.ic_dialog_info).show();
  269. setStateNotActive();
  270. setStateNotConnected();
  271. } else { // network available
  272. boolean protocolActivated = false;
  273. if (ProfileManager.getInstance().getCurrentActivatedProfile() == null) {
  274. MainActivity.getInstance().startMonitorServices(Arrays.asList(
  275. getResources().getStringArray(R.array.protocols)));
  276. } else {
  277. ProfileManager profileManager = ProfileManager.getInstance();
  278. if (profileManager.isRandomActive()) {
  279. profileManager
  280. .randomizeProtocols(profileManager.getRandomProfile());
  281. }
  282. Profile currentProfile = profileManager
  283. .getCurrentActivatedProfile();
  284. List<String> protocols = currentProfile.getActiveProtocols();
  285. startAlarm();
  286. if (protocols.size() > 0 || currentProfile.mGhostActive) {
  287. protocols.add("GHOST");
  288. MainActivity.getInstance().startMonitorServices(protocols);
  289. protocolActivated = true;
  290. }
  291. }
  292. if (protocolActivated) {
  293. setStateActive();
  294. } else {
  295. new AlertDialog.Builder(getActivity())
  296. .setTitle(R.string.information)
  297. .setMessage(R.string.profile_no_services_msg)
  298. .setPositiveButton(android.R.string.ok,
  299. new DialogInterface.OnClickListener() {
  300. public void onClick(DialogInterface dialog,
  301. int which) {
  302. }
  303. }).setIcon(android.R.drawable.ic_dialog_info)
  304. .show();
  305. setStateNotActive();
  306. }
  307. }
  308. } else { // switch deactivated
  309. if (MainActivity.getInstance().getHostageService() != null) {
  310. MainActivity.getInstance().getHostageService().stopListeners();
  311. MainActivity.getInstance().stopAndUnbind();
  312. }
  313. setStateNotActive();
  314. }
  315. }
  316. private void startAlarm() {
  317. Context context = getActivity();
  318. if(alarm!=null){
  319. alarm.SetAlarm(context);
  320. }
  321. else {
  322. Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
  323. }
  324. }
  325. };
  326. }
  327. mHomeSwitchConnection.setOnCheckedChangeListener(mSwitchChangeListener);
  328. mRootView.findViewById(R.id.home_profile_details).setOnClickListener(new View.OnClickListener() {
  329. @Override
  330. public void onClick(View v) {
  331. Fragment fragment = new ProfileManagerFragment();
  332. MainActivity.getInstance().injectFragment(fragment);
  333. }
  334. });
  335. View.OnClickListener attackClickListener = new View.OnClickListener() {
  336. @Override
  337. public void onClick(View v) {
  338. String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "");
  339. if (!ssid.isEmpty()) {
  340. ArrayList<String> ssids = new ArrayList<String>();
  341. ssids.add(ssid);
  342. LogFilter filter = new LogFilter();
  343. filter.setESSIDs(ssids);
  344. RecordOverviewFragment recordOverviewFragment = new RecordOverviewFragment();
  345. recordOverviewFragment.setFilter(filter);
  346. recordOverviewFragment.setGroupKey("ESSID");
  347. MainActivity.getInstance().injectFragment(recordOverviewFragment);
  348. }
  349. }
  350. };
  351. mHomeTextAttacks.setOnClickListener(attackClickListener);
  352. mHomeTextSecurity.setOnClickListener(attackClickListener);
  353. return mRootView;
  354. }
  355. @Override
  356. public void onStop() {
  357. super.onStop();
  358. unregisterBroadcastReceiver();
  359. }
  360. @Override
  361. public void onStart() {
  362. super.onStart();
  363. registerBroadcastReceiver();
  364. updateUI();
  365. }
  366. @Override
  367. public void onDestroy() {
  368. super.onDestroy();
  369. unregisterBroadcastReceiver();
  370. }
  371. public void AlertFile(String fname) {
  372. AlertDialog alert = new AlertDialog.Builder(getActivity()).create();
  373. alert.setTitle("Delete entry");
  374. alert.setMessage("Are you sure you want to delete this entry?:\n" + fname);
  375. alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
  376. new DialogInterface.OnClickListener() {
  377. public void onClick(DialogInterface dialog, int which) {
  378. dialog.dismiss();
  379. }
  380. });
  381. alert.show();
  382. }
  383. }