HomeFragment.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.net.wifi.WifiManager;
  10. import android.os.Bundle;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.CompoundButton;
  15. import android.widget.Switch;
  16. import android.widget.TextView;
  17. import de.tudarmstadt.informatik.hostage.R;
  18. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  19. import de.tudarmstadt.informatik.hostage.ui2.fragment.opengl.ThreatIndicatorGLRenderer;
  20. /**
  21. * @author Alexander Brakowski
  22. * @created 13.01.14 19:06
  23. */
  24. public class HomeFragment extends Fragment {
  25. private Switch mHomeSwitchConnection;
  26. private TextView mHomeTextName;
  27. private TextView mHomeTextSecurity;
  28. private TextView mHomeTextAttacks;
  29. private TextView mHomeTextProfile;
  30. private TextView mHomeTextProfileHeader;
  31. private View rootView;
  32. private BroadcastReceiver wifiReceiver;
  33. private CompoundButton.OnCheckedChangeListener switchChangeListener = null;
  34. private int defaultTextColor;
  35. private void assignViews() {
  36. mHomeSwitchConnection = (Switch) rootView.findViewById(R.id.home_switch_connection);
  37. mHomeTextName = (TextView) rootView.findViewById(R.id.home_text_name);
  38. mHomeTextSecurity = (TextView) rootView.findViewById(R.id.home_text_security);
  39. mHomeTextAttacks = (TextView) rootView.findViewById(R.id.home_text_attacks);
  40. mHomeTextProfile = (TextView) rootView.findViewById(R.id.home_text_profile);
  41. mHomeTextProfileHeader = (TextView) rootView.findViewById(R.id.home_text_profile_header);
  42. }
  43. private void registerBroadcastReceiver(){
  44. wifiReceiver = new BroadcastReceiver() {
  45. @Override
  46. public void onReceive(Context context, Intent intent) {
  47. final String action = intent.getAction();
  48. //if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION) || action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
  49. updateUI();
  50. /*if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){
  51. } else {
  52. // wifi connection was lost
  53. }*/
  54. //}
  55. }
  56. };
  57. IntentFilter intentFilter = new IntentFilter();
  58. intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
  59. intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
  60. intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
  61. intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
  62. getActivity().registerReceiver(wifiReceiver, intentFilter);
  63. }
  64. public HomeFragment(){}
  65. public void setStateNotActive(){
  66. mHomeTextName.setTextColor(getResources().getColor(R.color.light_grey));
  67. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.light_grey));
  68. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.light_grey));
  69. mHomeTextProfile.setTextColor(getResources().getColor(R.color.light_grey));
  70. mHomeTextProfileHeader.setTextColor(getResources().getColor(R.color.light_grey));
  71. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.kThreatLevelNotMonitoring);
  72. mHomeSwitchConnection.setChecked(false);
  73. }
  74. public void setStateActive(){
  75. mHomeTextAttacks.setText("0 attacks recorded");
  76. mHomeTextSecurity.setText("Secure");
  77. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_dark_green));
  78. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_dark_green));
  79. mHomeTextAttacks.setVisibility(View.VISIBLE);
  80. mHomeTextSecurity.setVisibility(View.VISIBLE);
  81. mHomeTextName.setTextColor(defaultTextColor);
  82. mHomeTextProfile.setTextColor(defaultTextColor);
  83. mHomeTextProfileHeader.setTextColor(defaultTextColor);
  84. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.kThreatLevelNoThreat);
  85. mHomeSwitchConnection.setChecked(true);
  86. }
  87. public void setStateNotConnected(){
  88. mHomeTextSecurity.setVisibility(View.INVISIBLE);
  89. mHomeTextAttacks.setVisibility(View.INVISIBLE);
  90. mHomeTextProfile.setVisibility(View.INVISIBLE);
  91. mHomeTextProfileHeader.setVisibility(View.INVISIBLE);
  92. mHomeTextName.setText("Not connected");
  93. }
  94. public void setStateConnected(){
  95. mHomeTextProfile.setVisibility(View.VISIBLE);
  96. mHomeTextProfileHeader.setVisibility(View.VISIBLE);
  97. }
  98. public void updateUI(){
  99. if(!HelperUtils.isWifiConnected(getActivity())){
  100. mHomeSwitchConnection.setOnCheckedChangeListener(null);
  101. setStateNotConnected();
  102. setStateNotActive();
  103. mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener);
  104. } else {
  105. setStateConnected();
  106. mHomeTextName.setText(HelperUtils.getSSID(getActivity()));
  107. }
  108. }
  109. @Override
  110. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  111. Bundle savedInstanceState) {
  112. super.onCreateView(inflater, container, savedInstanceState);
  113. rootView = inflater.inflate(R.layout.fragment_home, container, false);
  114. assignViews();
  115. defaultTextColor = mHomeTextName.getCurrentTextColor();
  116. setStateNotActive();
  117. setStateNotConnected();
  118. registerBroadcastReceiver();
  119. updateUI();
  120. mHomeSwitchConnection = (Switch) rootView.findViewById(R.id.home_switch_connection);
  121. if(switchChangeListener == null){
  122. switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  123. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  124. if(!HelperUtils.isWifiConnected(getActivity())){
  125. new AlertDialog.Builder(getActivity())
  126. .setTitle("Information")
  127. .setMessage("You are not connected to a WiFi network. \n\nPlease connect to one, before trying to activate HosTaGe.")
  128. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  129. public void onClick(DialogInterface dialog, int which) {
  130. }
  131. })
  132. .setIcon(android.R.drawable.ic_dialog_info)
  133. .show();
  134. setStateNotActive();
  135. setStateNotConnected();
  136. } else {
  137. if(isChecked){
  138. setStateActive();
  139. } else {
  140. setStateNotActive();
  141. }
  142. }
  143. }
  144. };
  145. }
  146. mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener);
  147. return rootView;
  148. }
  149. @Override
  150. public void onDestroy(){
  151. super.onDestroy();
  152. getActivity().unregisterReceiver(wifiReceiver);
  153. }
  154. }