ServicesFragment.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import java.util.ArrayList;
  3. import android.app.AlertDialog;
  4. import android.app.Fragment;
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.content.IntentFilter;
  10. import android.content.SharedPreferences;
  11. import android.os.Bundle;
  12. import android.support.v4.content.LocalBroadcastManager;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.CompoundButton;
  17. import android.widget.ListView;
  18. import android.widget.Switch;
  19. import android.widget.TextView;
  20. import de.tudarmstadt.informatik.hostage.Handler;
  21. import de.tudarmstadt.informatik.hostage.R;
  22. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  23. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  24. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  25. import de.tudarmstadt.informatik.hostage.ui2.adapter.ServicesListAdapter;
  26. import de.tudarmstadt.informatik.hostage.ui2.model.ServicesListItem;
  27. /**
  28. * Created by Daniel Lazar on 05.02.14.
  29. */
  30. public class ServicesFragment extends Fragment {
  31. private Switch mServicesSwitchService;
  32. private TextView mServicesTextName;
  33. private View rootView;
  34. private CompoundButton.OnCheckedChangeListener switchChangeListener = null;
  35. private BroadcastReceiver mReceiver;
  36. private ServicesListAdapter adapter;
  37. private ArrayList<ServicesListItem> protocolList;
  38. private HostageDBOpenHelper dbh = new HostageDBOpenHelper(MainActivity.getContext());
  39. private String[] protocols;
  40. private SharedPreferences mConnectionInfo;
  41. private boolean mReceiverRegistered = false;
  42. private void assignViews() {
  43. mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
  44. mServicesTextName = (TextView) rootView.findViewById(R.id.services_text_name);
  45. rootView.findViewById(R.id.services_button_connection_info).setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
  49. connectionInfoDialogFragment.show(getFragmentManager().beginTransaction(), connectionInfoDialogFragment.getTag());
  50. }
  51. });
  52. }
  53. public void updateUI() {
  54. if (!HelperUtils.isWifiConnected(getActivity())) {
  55. mServicesSwitchService.setOnCheckedChangeListener(null);
  56. setStateNotConnected();
  57. setStateNotActive();
  58. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  59. } else {
  60. if (MainActivity.getInstance().getHostageService().hasRunningListeners()) {
  61. setStateActive();
  62. }
  63. mServicesTextName.setText(HelperUtils.getSSID(getActivity()));
  64. }
  65. }
  66. public ServicesFragment() {
  67. }
  68. private void registerBroadcastReceiver() {
  69. if (!mReceiverRegistered) {
  70. mReceiver = new BroadcastReceiver() {
  71. @Override
  72. public void onReceive(Context context, Intent intent) {
  73. String sender = intent.getStringExtra("SENDER");
  74. String[] values = intent.getStringArrayExtra("VALUES");
  75. if (sender.equals(Handler.class.getName()) && values[0].equals(getString(R.string.broadcast_started))) {
  76. for (ServicesListItem item : protocolList) {
  77. if (item.protocol.equals(values[1])) {
  78. item.attacks = dbh.numBssidSeen(item.protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  79. }
  80. }
  81. }
  82. if (!MainActivity.getInstance().getHostageService().hasRunningListeners()) {
  83. setStateNotActive();
  84. } else {
  85. setStateActive();
  86. }
  87. adapter.notifyDataSetChanged();
  88. }
  89. };
  90. LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
  91. this.mReceiverRegistered = true;
  92. }
  93. }
  94. @Override
  95. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  96. super.onCreateView(inflater, container, savedInstanceState);
  97. rootView = inflater.inflate(R.layout.fragment_services, container, false);
  98. assignViews();
  99. protocols = getResources().getStringArray(R.array.protocols);
  100. mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  101. updateUI();
  102. ListView list = (ListView) rootView.findViewById(R.id.services_list_view);
  103. protocolList = new ArrayList<ServicesListItem>();
  104. int i = 0;
  105. for (String protocol : protocols) {
  106. protocolList.add(new ServicesListItem(protocol));
  107. protocolList.get(i).attacks = dbh.numBssidSeen(protocolList.get(i).protocol,
  108. mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  109. i++;
  110. }
  111. mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
  112. if (switchChangeListener == null) {
  113. switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  114. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  115. if (!HelperUtils.isWifiConnected(getActivity())) {
  116. new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.wifi_not_connected_msg)
  117. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  118. public void onClick(DialogInterface dialog, int which) {
  119. }
  120. }).setIcon(android.R.drawable.ic_dialog_info).show();
  121. setStateNotActive();
  122. setStateNotConnected();
  123. } else {
  124. if (MainActivity.getInstance().isServiceBound()) {
  125. if (isChecked) {
  126. for (String protocol : protocols) {
  127. if (!MainActivity.getInstance().getHostageService().isRunning(protocol)) {
  128. MainActivity.getInstance().getHostageService().startListener(protocol);
  129. }
  130. }
  131. setStateActive();
  132. } else {
  133. MainActivity.getInstance().getHostageService().stopListeners();
  134. MainActivity.getInstance().stopAndUnbind();
  135. setStateNotActive();
  136. }
  137. }
  138. }
  139. }
  140. };
  141. }
  142. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  143. adapter = new ServicesListAdapter(getActivity().getBaseContext(), protocolList);
  144. adapter.setActivity(this.getActivity(), this.mServicesSwitchService, this.switchChangeListener);
  145. list.setAdapter(adapter);
  146. registerBroadcastReceiver();
  147. return rootView;
  148. };
  149. @Override
  150. public void onStart() {
  151. super.onStart();
  152. registerBroadcastReceiver();
  153. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  154. }
  155. private void unregisterBroadcastReceiver() {
  156. if (mReceiverRegistered) {
  157. LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
  158. this.mReceiverRegistered = false;
  159. }
  160. }
  161. private void setStateActive() {
  162. mServicesSwitchService.setChecked(true);
  163. }
  164. private void setStateNotConnected() {
  165. mServicesTextName.setText(R.string.not_connected);
  166. }
  167. private void setStateNotActive() {
  168. mServicesSwitchService.setChecked(false);
  169. }
  170. @Override
  171. public void onStop() {
  172. super.onStop();
  173. mServicesSwitchService.setOnCheckedChangeListener(null);
  174. }
  175. @Override
  176. public void onDestroy() {
  177. super.onDestroy();
  178. unregisterBroadcastReceiver();
  179. }
  180. }