ServicesFragment.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.deprecated.UglyDbHelper;
  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. * @author Daniel Lazar
  29. * @created 05.02.14
  30. * Fragment that displays a switch for every protocol.
  31. * Also it can de-/activate every protocol by using this switch.
  32. */
  33. public class ServicesFragment extends Fragment {
  34. private Switch mServicesSwitchService;
  35. private TextView mServicesTextName;
  36. private View rootView;
  37. private CompoundButton.OnCheckedChangeListener switchChangeListener = null;
  38. private BroadcastReceiver mReceiver;
  39. private ServicesListAdapter adapter;
  40. private ArrayList<ServicesListItem> protocolList;
  41. private UglyDbHelper dbh = new UglyDbHelper(MainActivity.getContext());
  42. private String[] protocols;
  43. private SharedPreferences mConnectionInfo;
  44. private boolean mReceiverRegistered = false;
  45. public ServicesFragment() {
  46. }
  47. /**
  48. * assign views which are not asynchronously loaded
  49. */
  50. private void assignViews() {
  51. mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
  52. mServicesTextName = (TextView) rootView.findViewById(R.id.services_text_name);
  53. rootView.findViewById(R.id.services_button_connection_info).setOnClickListener(new View.OnClickListener() {
  54. @Override
  55. public void onClick(View v) {
  56. ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
  57. connectionInfoDialogFragment.show(getFragmentManager().beginTransaction(), connectionInfoDialogFragment.getTag());
  58. }
  59. });
  60. }
  61. /**
  62. * updates the user interface
  63. * in detail: the main switch and the textField mServicesTextName
  64. */
  65. public void updateUI() {
  66. if (!HelperUtils.isWifiConnected(getActivity())) {
  67. mServicesSwitchService.setOnCheckedChangeListener(null);
  68. setStateNotConnected();
  69. setStateNotActive();
  70. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  71. } else {
  72. if (MainActivity.getInstance().getHostageService().hasRunningListeners()) {
  73. setStateActive();
  74. }
  75. mServicesTextName.setText(HelperUtils.getSSID(getActivity()));
  76. }
  77. }
  78. /**
  79. * register a broadcast receiver if not already registered
  80. * and also update the number of attacks per protocol
  81. */
  82. private void registerBroadcastReceiver() {
  83. if (!mReceiverRegistered) {
  84. mReceiver = new BroadcastReceiver() {
  85. @Override
  86. public void onReceive(Context context, Intent intent) {
  87. String sender = intent.getStringExtra("SENDER");
  88. String[] values = intent.getStringArrayExtra("VALUES");
  89. if (sender.equals(Handler.class.getName()) && values[0].equals(getString(R.string.broadcast_started))) {
  90. for (ServicesListItem item : protocolList) {
  91. if (item.protocol.equals(values[1])) {
  92. item.attacks = dbh.numBssidSeen(item.protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  93. }
  94. }
  95. }
  96. if (!MainActivity.getInstance().getHostageService().hasRunningListeners()) {
  97. setStateNotActive();
  98. } else {
  99. setStateActive();
  100. }
  101. adapter.notifyDataSetChanged();
  102. }
  103. };
  104. LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
  105. this.mReceiverRegistered = true;
  106. }
  107. }
  108. /**
  109. * most important method of this class
  110. *
  111. * @param inflater
  112. * @param container
  113. * @param savedInstanceState
  114. * @return rootView
  115. */
  116. @Override
  117. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  118. super.onCreateView(inflater, container, savedInstanceState);
  119. rootView = inflater.inflate(R.layout.fragment_services, container, false);
  120. assignViews();
  121. protocols = getResources().getStringArray(R.array.protocols);
  122. mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  123. updateUI();
  124. ListView list = (ListView) rootView.findViewById(R.id.services_list_view);
  125. protocolList = new ArrayList<ServicesListItem>();
  126. int i = 0;
  127. for (String protocol : protocols) {
  128. protocolList.add(new ServicesListItem(protocol));
  129. protocolList.get(i).attacks = dbh.numBssidSeen(protocolList.get(i).protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  130. i++;
  131. }
  132. mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
  133. if (switchChangeListener == null) {
  134. switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  135. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  136. if (!HelperUtils.isWifiConnected(getActivity())) {
  137. new AlertDialog.Builder(getActivity())
  138. .setTitle(R.string.information)
  139. .setMessage(R.string.wifi_not_connected_msg)
  140. .setPositiveButton(android.R.string.ok,
  141. new DialogInterface.OnClickListener() {
  142. public void onClick(DialogInterface dialog, int which) {
  143. }
  144. }
  145. )
  146. .setIcon(android.R.drawable.ic_dialog_info)
  147. .show();
  148. setStateNotActive();
  149. setStateNotConnected();
  150. } else {
  151. if (MainActivity.getInstance().isServiceBound()) {
  152. if (isChecked) {
  153. for (String protocol : protocols) {
  154. if (!MainActivity.getInstance().getHostageService().isRunning(protocol)) {
  155. MainActivity.getInstance().getHostageService().startListener(protocol);
  156. }
  157. }
  158. setStateActive();
  159. } else {
  160. MainActivity.getInstance().getHostageService().stopListeners();
  161. MainActivity.getInstance().stopAndUnbind();
  162. setStateNotActive();
  163. }
  164. }
  165. }
  166. }
  167. };
  168. }
  169. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  170. adapter = new ServicesListAdapter(getActivity().getBaseContext(), protocolList);
  171. adapter.setActivity(this.getActivity(), this.mServicesSwitchService, this.switchChangeListener);
  172. list.setAdapter(adapter);
  173. registerBroadcastReceiver();
  174. return rootView;
  175. }
  176. ;
  177. /**
  178. * called on start of this fragment.
  179. * registers broadcast receiver and binds change listener to main switch
  180. */
  181. @Override
  182. public void onStart() {
  183. super.onStart();
  184. registerBroadcastReceiver();
  185. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  186. }
  187. /**
  188. * unregister the broadcast receiver if a receiver is already registered
  189. */
  190. private void unregisterBroadcastReceiver() {
  191. if (mReceiverRegistered) {
  192. LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
  193. this.mReceiverRegistered = false;
  194. }
  195. }
  196. /**
  197. * sets main switch to true
  198. */
  199. private void setStateActive() {
  200. mServicesSwitchService.setChecked(true);
  201. }
  202. /**
  203. * sets text of text field to not connected, if the device is not connected to a network
  204. */
  205. private void setStateNotConnected() {
  206. mServicesTextName.setText(R.string.not_connected);
  207. }
  208. /**
  209. * sets main switch to false
  210. */
  211. private void setStateNotActive() {
  212. mServicesSwitchService.setChecked(false);
  213. }
  214. /**
  215. * overrides onStop
  216. * unloads the ChangeListener
  217. */
  218. @Override
  219. public void onStop() {
  220. super.onStop();
  221. mServicesSwitchService.setOnCheckedChangeListener(null);
  222. }
  223. /**
  224. * overrides onDestroy
  225. * unregisters broadcast receiver, when destroyed
  226. */
  227. @Override
  228. public void onDestroy() {
  229. super.onDestroy();
  230. unregisterBroadcastReceiver();
  231. }
  232. }