HomeFragment.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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,
  65. new IntentFilter(getString(R.string.broadcast)));
  66. //getActivity().registerReceiver(mReceiver, intentFilter);
  67. }
  68. public HomeFragment(){}
  69. public void setStateNotActive(){
  70. mHomeTextName.setTextColor(getResources().getColor(R.color.light_grey));
  71. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.light_grey));
  72. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.light_grey));
  73. mHomeTextProfile.setTextColor(getResources().getColor(R.color.light_grey));
  74. mHomeTextProfileHeader.setTextColor(getResources().getColor(R.color.light_grey));
  75. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.kThreatLevelNotMonitoring);
  76. mHomeSwitchConnection.setChecked(false);
  77. }
  78. public void setStateActive(){
  79. mHomeTextAttacks.setText("0 attacks recorded");
  80. mHomeTextSecurity.setText("Secure");
  81. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_dark_green));
  82. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_dark_green));
  83. mHomeTextAttacks.setVisibility(View.VISIBLE);
  84. mHomeTextSecurity.setVisibility(View.VISIBLE);
  85. mHomeTextName.setTextColor(defaultTextColor);
  86. mHomeTextProfile.setTextColor(defaultTextColor);
  87. mHomeTextProfileHeader.setTextColor(defaultTextColor);
  88. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.kThreatLevelNoThreat);
  89. mHomeSwitchConnection.setChecked(true);
  90. }
  91. public void setStateNotConnected(){
  92. mHomeTextSecurity.setVisibility(View.INVISIBLE);
  93. mHomeTextAttacks.setVisibility(View.INVISIBLE);
  94. mHomeTextProfile.setVisibility(View.INVISIBLE);
  95. mHomeTextProfileHeader.setVisibility(View.INVISIBLE);
  96. mHomeTextName.setText("Not connected");
  97. }
  98. public void setStateConnected(){
  99. mHomeTextProfile.setVisibility(View.VISIBLE);
  100. mHomeTextProfileHeader.setVisibility(View.VISIBLE);
  101. }
  102. public void updateUI(){
  103. if(!HelperUtils.isWifiConnected(getActivity())){
  104. mHomeSwitchConnection.setOnCheckedChangeListener(null);
  105. setStateNotConnected();
  106. setStateNotActive();
  107. mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener);
  108. } else {
  109. setStateConnected();
  110. mHomeTextName.setText(HelperUtils.getSSID(getActivity()));
  111. }
  112. }
  113. @Override
  114. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  115. Bundle savedInstanceState) {
  116. super.onCreateView(inflater, container, savedInstanceState);
  117. rootView = inflater.inflate(R.layout.fragment_home, container, false);
  118. assignViews();
  119. defaultTextColor = mHomeTextName.getCurrentTextColor();
  120. setStateNotActive();
  121. setStateNotConnected();
  122. registerBroadcastReceiver();
  123. updateUI();
  124. final String[] protocols = getResources().getStringArray(R.array.protocols);
  125. if(MainActivity.getInstance().getHoneyService() == null){
  126. // do nothing
  127. }
  128. else {
  129. for(String protocol: protocols){
  130. if (MainActivity.getInstance().getHoneyService().isRunning(protocol)) {
  131. setStateActive();
  132. }
  133. }
  134. }
  135. mHomeSwitchConnection = (Switch) rootView.findViewById(R.id.home_switch_connection);
  136. if(switchChangeListener == null){
  137. switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  138. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  139. if(!HelperUtils.isWifiConnected(getActivity())){
  140. new AlertDialog.Builder(getActivity())
  141. .setTitle("Information")
  142. .setMessage("You are not connected to a WiFi network. \n\nPlease connect to one, before trying to activate HosTaGe.")
  143. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  144. public void onClick(DialogInterface dialog, int which) {
  145. }
  146. })
  147. .setIcon(android.R.drawable.ic_dialog_info)
  148. .show();
  149. setStateNotActive();
  150. setStateNotConnected();
  151. } else {
  152. if(isChecked){
  153. for(String protocol: protocols){
  154. MainActivity.getInstance().getHoneyService().startListener(protocol);
  155. }
  156. setStateActive();
  157. } else {
  158. MainActivity.getInstance().getHoneyService().stopListeners();
  159. MainActivity.getInstance().stopAndUnbind();
  160. setStateNotActive();
  161. }
  162. }
  163. }
  164. };
  165. }
  166. mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener);
  167. return rootView;
  168. }
  169. @Override
  170. public void onDestroy(){
  171. super.onDestroy();
  172. LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
  173. }
  174. }