HomeFragment.java 14 KB

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