HomeFragment.java 6.2 KB

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