HomeFragment.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.app.AlertDialog;
  3. import android.app.Fragment;
  4. import android.content.BroadcastReceiver;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.content.IntentFilter;
  9. import android.content.SharedPreferences;
  10. import android.net.wifi.WifiManager;
  11. import android.os.Bundle;
  12. import android.support.v4.content.LocalBroadcastManager;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.CompoundButton;
  17. import android.widget.ImageView;
  18. import android.widget.Switch;
  19. import android.widget.TextView;
  20. import de.tudarmstadt.informatik.hostage.R;
  21. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  22. import de.tudarmstadt.informatik.hostage.dao.ProfileManager;
  23. import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
  24. import de.tudarmstadt.informatik.hostage.model.Profile;
  25. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  26. import de.tudarmstadt.informatik.hostage.ui2.fragment.opengl.ThreatIndicatorGLRenderer;
  27. /**
  28. * @author Alexander Brakowski
  29. * @created 13.01.14 19:06
  30. */
  31. public class HomeFragment extends Fragment {
  32. private Switch mHomeSwitchConnection;
  33. private TextView mHomeTextName;
  34. private TextView mHomeTextSecurity;
  35. private TextView mHomeTextAttacks;
  36. private TextView mHomeTextProfile;
  37. private TextView mHomeTextProfileHeader;
  38. private ImageView mHomeProfileImage;
  39. private View rootView;
  40. private BroadcastReceiver mReceiver;
  41. private CompoundButton.OnCheckedChangeListener switchChangeListener = null;
  42. private int defaultTextColor;
  43. private ProfileManager mProfileManager;
  44. private SharedPreferences mConnectionInfo;
  45. private UglyDbHelper dbh;
  46. private static int DANGER_LEVEL= 0;
  47. private void assignViews() {
  48. mHomeSwitchConnection = (Switch) rootView.findViewById(R.id.home_switch_connection);
  49. mHomeTextName = (TextView) rootView.findViewById(R.id.home_text_name);
  50. mHomeTextSecurity = (TextView) rootView.findViewById(R.id.home_text_security);
  51. mHomeTextAttacks = (TextView) rootView.findViewById(R.id.home_text_attacks);
  52. mHomeTextProfile = (TextView) rootView.findViewById(R.id.home_text_profile);
  53. mHomeTextProfileHeader = (TextView) rootView.findViewById(R.id.home_text_profile_header);
  54. mHomeProfileImage = (ImageView) rootView.findViewById(R.id.home_image_profile);
  55. }
  56. private void registerBroadcastReceiver(){
  57. mReceiver = new BroadcastReceiver() {
  58. @Override
  59. public void onReceive(Context context, Intent intent) {
  60. //final String action = intent.getAction();
  61. //if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION) || action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
  62. updateUI();
  63. /*if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){
  64. } else {
  65. // wifi connection was lost
  66. }*/
  67. //}
  68. }
  69. };
  70. LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
  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.kThreatLevelNotMonitoring);
  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.kThreatLevelNoThreat);
  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. mHomeTextName.setText("Not connected");
  107. }
  108. public void setStateConnected(){
  109. mHomeTextProfile.setVisibility(View.VISIBLE);
  110. mHomeTextProfileHeader.setVisibility(View.VISIBLE);
  111. }
  112. public void updateUI(){
  113. Profile profile = mProfileManager.getCurrentActivatedProfile();
  114. if(profile != null){
  115. mHomeTextProfile.setText(profile.mLabel);
  116. mHomeProfileImage.setImageBitmap(profile.getIconBitmap());
  117. }
  118. if(HelperUtils.isWifiConnected(getActivity())){
  119. setStateConnected();
  120. mHomeTextName.setText(mConnectionInfo.getString(getString(R.string.connection_info_ssid), ""));
  121. }
  122. int dangerLevel = 0;
  123. boolean hasActiveListeners = false;
  124. int totalAttacks = 0;
  125. int totalLogged = 0;
  126. if(MainActivity.getInstance().isServiceBound()){
  127. for(String protocol: getResources().getStringArray(R.array.protocols)){
  128. if(MainActivity.getInstance().getHoneyService().isRunning(protocol)){
  129. hasActiveListeners = true;
  130. int attacks = MainActivity.getInstance().getHoneyService().getNumberOfActiveConnections(protocol);
  131. if(attacks == 0){
  132. int log_attacks = dbh.numBssidSeen(protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  133. totalLogged += log_attacks;
  134. if(log_attacks > 0){
  135. DANGER_LEVEL = Math.max(1, DANGER_LEVEL);
  136. } else {
  137. DANGER_LEVEL = Math.max(0, DANGER_LEVEL);
  138. }
  139. } else {
  140. totalAttacks += attacks;
  141. DANGER_LEVEL = Math.max(2, DANGER_LEVEL);
  142. }
  143. }
  144. }
  145. }
  146. if(hasActiveListeners){
  147. setStateActive(true);
  148. if(DANGER_LEVEL == 0){
  149. mHomeTextAttacks.setText("0 attacks recorded");
  150. mHomeTextSecurity.setText("Secure");
  151. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_dark_green));
  152. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_dark_green));
  153. } else if(DANGER_LEVEL == 1){
  154. mHomeTextAttacks.setText(totalLogged + (totalLogged == 1 ? " attack" : " attacks") + " logged");
  155. mHomeTextSecurity.setText("Insecure");
  156. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_yellow));
  157. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_yellow));
  158. } else if(DANGER_LEVEL == 2){
  159. mHomeTextAttacks.setText(totalAttacks + (totalAttacks == 1 ? " attack" : " attacks") + " recorded");
  160. mHomeTextSecurity.setText("Insecure");
  161. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_red));
  162. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_red));
  163. }
  164. ThreatIndicatorGLRenderer.setThreatLevel(DANGER_LEVEL + 1);
  165. } else {
  166. mHomeSwitchConnection.setOnCheckedChangeListener(null);
  167. setStateNotActive();
  168. DANGER_LEVEL = 0;
  169. if(!HelperUtils.isWifiConnected(getActivity())){
  170. setStateNotConnected();
  171. }
  172. mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener);
  173. }
  174. }
  175. @Override
  176. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  177. Bundle savedInstanceState) {
  178. super.onCreateView(inflater, container, savedInstanceState);
  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. defaultTextColor = mHomeTextName.getCurrentTextColor();
  185. setStateNotActive(true);
  186. setStateNotConnected();
  187. registerBroadcastReceiver();
  188. updateUI();
  189. final String[] protocols = getResources().getStringArray(R.array.protocols);
  190. mHomeSwitchConnection = (Switch) rootView.findViewById(R.id.home_switch_connection);
  191. if(switchChangeListener == null){
  192. switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  193. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  194. if(!HelperUtils.isWifiConnected(getActivity())){
  195. new AlertDialog.Builder(getActivity())
  196. .setTitle("Information")
  197. .setMessage("You are not connected to a WiFi network. \n\nPlease connect to one, before trying to activate HosTaGe.")
  198. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  199. public void onClick(DialogInterface dialog, int which) {
  200. }
  201. })
  202. .setIcon(android.R.drawable.ic_dialog_info)
  203. .show();
  204. setStateNotActive();
  205. setStateNotConnected();
  206. } else {
  207. if(isChecked){
  208. for(String protocol: protocols){
  209. MainActivity.getInstance().getHoneyService().startListener(protocol);
  210. }
  211. setStateActive();
  212. } else {
  213. MainActivity.getInstance().getHoneyService().stopListeners();
  214. MainActivity.getInstance().stopAndUnbind();
  215. setStateNotActive();
  216. }
  217. }
  218. }
  219. };
  220. }
  221. mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener);
  222. return rootView;
  223. }
  224. @Override
  225. public void onDestroy(){
  226. super.onDestroy();
  227. LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
  228. }
  229. }