HomeFragment.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.app.Fragment;
  5. import android.app.FragmentManager;
  6. import android.content.BroadcastReceiver;
  7. import android.content.Context;
  8. import android.content.DialogInterface;
  9. import android.content.Intent;
  10. import android.content.IntentFilter;
  11. import android.content.SharedPreferences;
  12. import android.os.Bundle;
  13. import android.support.v4.content.LocalBroadcastManager;
  14. import android.view.LayoutInflater;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.widget.CompoundButton;
  18. import android.widget.ImageView;
  19. import android.widget.Switch;
  20. import android.widget.TextView;
  21. import de.tudarmstadt.informatik.hostage.R;
  22. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  23. import de.tudarmstadt.informatik.hostage.dao.ProfileManager;
  24. import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
  25. import de.tudarmstadt.informatik.hostage.model.Profile;
  26. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  27. import de.tudarmstadt.informatik.hostage.ui2.fragment.opengl.ThreatIndicatorGLRenderer;
  28. /**
  29. * @author Alexander Brakowski
  30. * @created 13.01.14 19:06
  31. */
  32. public class HomeFragment extends Fragment {
  33. private Switch mHomeSwitchConnection;
  34. private TextView mHomeTextName;
  35. private TextView mHomeTextSecurity;
  36. private TextView mHomeTextAttacks;
  37. private TextView mHomeTextProfile;
  38. private TextView mHomeTextProfileHeader;
  39. private ImageView mHomeProfileImage;
  40. private ImageView mHomeConnectionInfoButton;
  41. private View rootView;
  42. private BroadcastReceiver mReceiver;
  43. private CompoundButton.OnCheckedChangeListener switchChangeListener = null;
  44. private int defaultTextColor;
  45. private ProfileManager mProfileManager;
  46. private SharedPreferences mConnectionInfo;
  47. private UglyDbHelper dbh;
  48. private static ThreatIndicatorGLRenderer.ThreatLevel THREAT_LEVEL = ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT;
  49. private boolean mReceiverRegistered;
  50. private void assignViews() {
  51. mHomeSwitchConnection = (Switch) rootView.findViewById(R.id.home_switch_connection);
  52. mHomeTextName = (TextView) rootView.findViewById(R.id.home_text_name);
  53. mHomeTextSecurity = (TextView) rootView.findViewById(R.id.home_text_security);
  54. mHomeTextAttacks = (TextView) rootView.findViewById(R.id.home_text_attacks);
  55. mHomeTextProfile = (TextView) rootView.findViewById(R.id.home_text_profile);
  56. mHomeTextProfileHeader = (TextView) rootView.findViewById(R.id.home_text_profile_header);
  57. mHomeProfileImage = (ImageView) rootView.findViewById(R.id.home_image_profile);
  58. mHomeConnectionInfoButton = (ImageView) rootView.findViewById(R.id.home_button_connection_info);
  59. }
  60. private void registerBroadcastReceiver(){
  61. if(!mReceiverRegistered) {
  62. LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
  63. this.mReceiverRegistered = true;
  64. }
  65. }
  66. private void unregisterBroadcastReceiver(){
  67. if(mReceiverRegistered){
  68. LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
  69. this.mReceiverRegistered = false;
  70. }
  71. }
  72. public HomeFragment(){}
  73. public void setStateNotActive(boolean initial){
  74. mHomeTextName.setTextColor(getResources().getColor(R.color.light_grey));
  75. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.light_grey));
  76. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.light_grey));
  77. mHomeTextProfile.setTextColor(getResources().getColor(R.color.light_grey));
  78. mHomeTextProfileHeader.setTextColor(getResources().getColor(R.color.light_grey));
  79. if(!initial){
  80. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING);
  81. }
  82. mHomeSwitchConnection.setChecked(false);
  83. }
  84. public void setStateNotActive(){
  85. setStateNotActive(false);
  86. }
  87. public void setStateActive(){
  88. setStateActive(false);
  89. }
  90. public void setStateActive(boolean initial){
  91. mHomeTextAttacks.setVisibility(View.VISIBLE);
  92. mHomeTextSecurity.setVisibility(View.VISIBLE);
  93. mHomeTextName.setTextColor(defaultTextColor);
  94. mHomeTextProfile.setTextColor(defaultTextColor);
  95. mHomeTextProfileHeader.setTextColor(defaultTextColor);
  96. if(!initial){
  97. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT);
  98. }
  99. mHomeSwitchConnection.setChecked(true);
  100. }
  101. public void setStateNotConnected(){
  102. mHomeTextSecurity.setVisibility(View.INVISIBLE);
  103. mHomeTextAttacks.setVisibility(View.INVISIBLE);
  104. mHomeTextProfile.setVisibility(View.INVISIBLE);
  105. mHomeTextProfileHeader.setVisibility(View.INVISIBLE);
  106. mHomeProfileImage.setVisibility(View.INVISIBLE);
  107. mHomeConnectionInfoButton.setVisibility(View.INVISIBLE);
  108. mHomeTextName.setText(R.string.not_connected);
  109. }
  110. public void setStateConnected(){
  111. mHomeTextProfile.setVisibility(View.VISIBLE);
  112. mHomeTextProfileHeader.setVisibility(View.VISIBLE);
  113. mHomeProfileImage.setVisibility(View.VISIBLE);
  114. mHomeConnectionInfoButton.setVisibility(View.VISIBLE);
  115. }
  116. public void updateUI(){
  117. Profile profile = mProfileManager.getCurrentActivatedProfile();
  118. if(profile != null){
  119. mHomeTextProfile.setText(profile.mLabel);
  120. mHomeProfileImage.setImageBitmap(profile.getIconBitmap());
  121. }
  122. if(HelperUtils.isWifiConnected(getActivity())){
  123. setStateConnected();
  124. mHomeTextName.setText(mConnectionInfo.getString(getString(R.string.connection_info_ssid), ""));
  125. }
  126. boolean hasActiveListeners = false;
  127. int totalAttacks = dbh.numBssidSeen(mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  128. ThreatIndicatorGLRenderer.ThreatLevel threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING;
  129. if(MainActivity.getInstance().getHoneyService() != null){
  130. if(MainActivity.getInstance().getHoneyService().hasRunningListeners()){
  131. hasActiveListeners = true;
  132. if(MainActivity.getInstance().getHoneyService().hasActiveAttacks() && totalAttacks > 0){
  133. threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.LIVE_THREAT;
  134. } else if(totalAttacks > 0){
  135. threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.PAST_THREAT;
  136. } else {
  137. threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT;
  138. }
  139. }
  140. }
  141. if(hasActiveListeners){
  142. setStateActive(true);
  143. switch(threatLevel){
  144. case NO_THREAT:
  145. mHomeTextAttacks.setText(R.string.zero_attacks);
  146. mHomeTextSecurity.setText(R.string.secure);
  147. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_dark_green));
  148. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_dark_green));
  149. break;
  150. case PAST_THREAT:
  151. mHomeTextAttacks.setText(totalAttacks + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks)) + getResources().getString(R.string.logged));
  152. mHomeTextSecurity.setText(R.string.insecure);
  153. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_yellow));
  154. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_yellow));
  155. break;
  156. case LIVE_THREAT:
  157. mHomeTextAttacks.setText(totalAttacks + (totalAttacks == 1 ? getResources().getString(R.string.attack) : getResources().getString(R.string.attacks)) + getResources().getString(R.string.recorded));
  158. mHomeTextSecurity.setText(R.string.insecure);
  159. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_red));
  160. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_red));
  161. break;
  162. }
  163. ThreatIndicatorGLRenderer.setThreatLevel(threatLevel);
  164. } else {
  165. setStateNotActive();
  166. if(!HelperUtils.isWifiConnected(getActivity())){
  167. setStateNotConnected();
  168. }
  169. }
  170. }
  171. @Override
  172. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  173. Bundle savedInstanceState) {
  174. super.onCreateView(inflater, container, savedInstanceState);
  175. final Activity activity = getActivity();
  176. if (activity != null) {
  177. activity.setTitle(getResources().getString(R.string.drawer_overview));
  178. }
  179. dbh = new UglyDbHelper(getActivity());
  180. mProfileManager = ProfileManager.getInstance();
  181. mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  182. rootView = inflater.inflate(R.layout.fragment_home, container, false);
  183. assignViews();
  184. // hook up the connection info button
  185. mHomeConnectionInfoButton.setOnClickListener(new View.OnClickListener() {
  186. @Override
  187. public void onClick(View v) {
  188. final FragmentManager fragmentManager = getFragmentManager();
  189. if (fragmentManager != null) {
  190. ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
  191. connectionInfoDialogFragment.show(fragmentManager.beginTransaction(), connectionInfoDialogFragment.getTag());
  192. }
  193. }
  194. });
  195. defaultTextColor = mHomeTextName.getCurrentTextColor();
  196. setStateNotActive(true);
  197. setStateNotConnected();
  198. mReceiver = new BroadcastReceiver() {
  199. @Override
  200. public void onReceive(Context context, Intent intent) {
  201. updateUI();
  202. }
  203. };
  204. registerBroadcastReceiver();
  205. updateUI();
  206. mHomeSwitchConnection = (Switch) rootView.findViewById(R.id.home_switch_connection);
  207. if(switchChangeListener == null){
  208. switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  209. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  210. if(!HelperUtils.isWifiConnected(getActivity())){
  211. new AlertDialog.Builder(getActivity())
  212. .setTitle(R.string.information)
  213. .setMessage(R.string.wifi_not_connected_msg)
  214. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  215. public void onClick(DialogInterface dialog, int which) {
  216. }
  217. })
  218. .setIcon(android.R.drawable.ic_dialog_info)
  219. .show();
  220. setStateNotActive();
  221. setStateNotConnected();
  222. } else {
  223. if(isChecked){
  224. boolean protocolActivated = false;
  225. if(ProfileManager.getInstance().getCurrentActivatedProfile() == null){
  226. for(String protocol: getResources().getStringArray(R.array.protocols)){
  227. if(!MainActivity.getInstance().getHoneyService().isRunning(protocol)){
  228. MainActivity.getInstance().getHoneyService().startListener(protocol);
  229. protocolActivated = true;
  230. }
  231. }
  232. } else {
  233. for(String protocol: ProfileManager.getInstance().getCurrentActivatedProfile().getActiveProtocols()){
  234. if(!MainActivity.getInstance().getHoneyService().isRunning(protocol)) {
  235. MainActivity.getInstance().getHoneyService().startListener(protocol);
  236. protocolActivated = true;
  237. }
  238. }
  239. }
  240. if(protocolActivated){
  241. setStateActive();
  242. } else {
  243. new AlertDialog.Builder(getActivity())
  244. .setTitle(R.string.information)
  245. .setMessage(R.string.profile_no_services_msg)
  246. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  247. public void onClick(DialogInterface dialog, int which) {
  248. }
  249. })
  250. .setIcon(android.R.drawable.ic_dialog_info)
  251. .show();
  252. setStateNotActive();
  253. }
  254. } else {
  255. if(MainActivity.getInstance().getHoneyService() != null && MainActivity.getInstance().isServiceRunning()){
  256. MainActivity.getInstance().getHoneyService().stopListeners();
  257. MainActivity.getInstance().stopAndUnbind();
  258. }
  259. setStateNotActive();
  260. }
  261. }
  262. }
  263. };
  264. }
  265. mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener);
  266. rootView.findViewById(R.id.home_profile_details).setOnClickListener(
  267. new View.OnClickListener() {
  268. @Override
  269. public void onClick(View v) {
  270. Fragment fragment = new ProfileManagerFragment();
  271. MainActivity.getInstance().injectFragment(fragment, false);
  272. }
  273. }
  274. );
  275. return rootView;
  276. }
  277. @Override
  278. public void onStop(){
  279. super.onStop();
  280. unregisterBroadcastReceiver();
  281. //mHomeSwitchConnection.setOnCheckedChangeListener(null);
  282. }
  283. @Override
  284. public void onStart(){
  285. super.onStart();
  286. registerBroadcastReceiver();
  287. updateUI();
  288. //mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener);
  289. }
  290. @Override
  291. public void onDestroy(){
  292. super.onDestroy();
  293. unregisterBroadcastReceiver();
  294. }
  295. }