HomeFragment.java 6.1 KB

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