ServicesFragment.java 11 KB

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