HomeFragment.java 7.2 KB

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