ServicesFragment.java 11 KB

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