ServicesFragment.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package de.tudarmstadt.informatik.hostage.ui.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.ui.activity.MainActivity;
  27. import de.tudarmstadt.informatik.hostage.ui.adapter.ServicesListAdapter;
  28. import de.tudarmstadt.informatik.hostage.ui.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 TrackerFragment {
  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. //SK: Temp bugfix
  71. //if (!HelperUtils.isNetworkAvailable(getActivity())) {
  72. if (!HelperUtils.isWifiConnected(getActivity())) {
  73. if(!MainActivity.getInstance().getHostageService().hasRunningListeners()) {
  74. mServicesSwitchService.setOnCheckedChangeListener(null);
  75. setStateNotConnected();
  76. setStateNotActive();
  77. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  78. }
  79. else{
  80. mServicesSwitchService.setOnCheckedChangeListener(null);
  81. setStateNotConnected();
  82. mServicesSwitchService.setChecked(true);
  83. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  84. }
  85. } else {
  86. if (MainActivity.getInstance().getHostageService().hasRunningListeners()) {
  87. setStateActive();
  88. }
  89. String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "");
  90. mServicesTextName.setText(ssid);
  91. }
  92. }
  93. /**
  94. * register a broadcast receiver if not already registered
  95. * and also update the number of attacks per protocol
  96. */
  97. private void registerBroadcastReceiver() {
  98. if (!mReceiverRegistered) {
  99. mReceiver = new BroadcastReceiver() {
  100. @Override
  101. public void onReceive(Context context, Intent intent) {
  102. String sender = intent.getStringExtra("SENDER");
  103. String[] values = intent.getStringArrayExtra("VALUES");
  104. if (sender.equals(Handler.class.getName()) && values[0].equals(getString(R.string.broadcast_started))) {
  105. for (ServicesListItem item : protocolList) {
  106. if (item.protocol.equals(values[1])) {
  107. item.attacks = dbh.numBssidSeen(item.protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  108. }
  109. }
  110. }
  111. if (!MainActivity.getInstance().getHostageService().hasRunningListeners()) {
  112. setStateNotActive();
  113. } else {
  114. setStateActive();
  115. }
  116. updateUI();
  117. adapter.notifyDataSetChanged();
  118. }
  119. };
  120. LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
  121. this.mReceiverRegistered = true;
  122. }
  123. }
  124. /**
  125. * most important method of this class
  126. *
  127. * @param inflater the inflater
  128. * @param container the container
  129. * @param savedInstanceState the saved instance state
  130. * @return rootView
  131. */
  132. @Override
  133. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  134. super.onCreateView(inflater, container, savedInstanceState);
  135. rootView = inflater.inflate(R.layout.fragment_services, container, false);
  136. assignViews();
  137. protocols = getResources().getStringArray(R.array.protocols);
  138. mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  139. updateUI();
  140. ListView list = (ListView) rootView.findViewById(R.id.services_list_view);
  141. protocolList = new ArrayList<ServicesListItem>();
  142. int i = 0;
  143. for (String protocol : protocols) {
  144. protocolList.add(new ServicesListItem(protocol));
  145. protocolList.get(i).attacks = dbh.numBssidSeen(protocolList.get(i).protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  146. i++;
  147. }
  148. mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
  149. if (switchChangeListener == null) {
  150. switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  151. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  152. mProfile = ProfileManager.getInstance().getCurrentActivatedProfile();
  153. //SK: Temp bugfix
  154. //if (!HelperUtils.isNetworkAvailable(getActivity())) {
  155. //replaced with if (!HelperUtils.isWifiConnected(getActivity())) {
  156. if (isChecked) { // switch activated
  157. // we need a network connection
  158. if (!HelperUtils.isWifiConnected(getActivity())) {
  159. new AlertDialog.Builder(getActivity())
  160. .setTitle(R.string.information)
  161. .setMessage(R.string.wifi_not_connected_msg)
  162. .setPositiveButton(android.R.string.ok,
  163. new DialogInterface.OnClickListener() {
  164. public void onClick(DialogInterface dialog, int which) {
  165. }
  166. }
  167. )
  168. .setIcon(android.R.drawable.ic_dialog_info)
  169. .show();
  170. setStateNotActive();
  171. setStateNotConnected();
  172. } else { // we have a connection
  173. // activate all protocols
  174. for (String protocol : protocols) {
  175. if (protocol.equals("GHOST") && mProfile.mGhostActive) {
  176. mGhostPorts = mProfile.getGhostPorts();
  177. if (mGhostPorts.length != 0) {
  178. for (Integer port : mGhostPorts) {
  179. if (MainActivity.getInstance().getHostageService() != null
  180. && !MainActivity.getInstance().getHostageService().isRunning("GHOST", port)) {
  181. MainActivity.getInstance().getHostageService().startListener("GHOST", port);
  182. }
  183. }
  184. }
  185. } else {
  186. if (MainActivity.getInstance().getHostageService() != null
  187. && !MainActivity.getInstance().getHostageService().isRunning(protocol)) {
  188. MainActivity.getInstance().getHostageService().startListener(protocol);
  189. }
  190. }
  191. }
  192. setStateActive();
  193. }
  194. } else { // switch deactivated
  195. if (MainActivity.getInstance().getHostageService() != null) {
  196. // why should the hostage service not be running??
  197. MainActivity.getInstance().getHostageService().stopListeners();
  198. MainActivity.getInstance().stopAndUnbind();
  199. }
  200. setStateNotActive();
  201. }
  202. }
  203. };
  204. }
  205. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  206. adapter = new ServicesListAdapter(getActivity().getBaseContext(), protocolList);
  207. adapter.setActivity(this.getActivity(), this.mServicesSwitchService, this.switchChangeListener);
  208. list.setAdapter(adapter);
  209. registerBroadcastReceiver();
  210. return rootView;
  211. }
  212. /**
  213. * called on start of this fragment.
  214. * registers broadcast receiver and binds change listener to main switch
  215. */
  216. @Override
  217. public void onStart() {
  218. super.onStart();
  219. registerBroadcastReceiver();
  220. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  221. }
  222. /**
  223. * unregister the broadcast receiver if a receiver is already registered
  224. */
  225. private void unregisterBroadcastReceiver() {
  226. if (mReceiverRegistered) {
  227. LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
  228. this.mReceiverRegistered = false;
  229. }
  230. }
  231. /**
  232. * sets main switch to true
  233. */
  234. private void setStateActive() {
  235. mServicesSwitchService.setChecked(true);
  236. }
  237. /**
  238. * sets text of text field to not connected, if the device is not connected to a network
  239. */
  240. private void setStateNotConnected() {
  241. mServicesTextName.setText(R.string.not_connected);
  242. }
  243. /**
  244. * sets main switch to false
  245. */
  246. private void setStateNotActive() {
  247. if (mServicesSwitchService.isChecked()) {
  248. mServicesSwitchService.setChecked(false);
  249. }
  250. }
  251. /**
  252. * overrides onStop
  253. * unloads the ChangeListener
  254. */
  255. @Override
  256. public void onStop() {
  257. super.onStop();
  258. mServicesSwitchService.setOnCheckedChangeListener(null);
  259. }
  260. /**
  261. * overrides onDestroy
  262. * unregisters broadcast receiver, when destroyed
  263. */
  264. @Override
  265. public void onDestroy() {
  266. super.onDestroy();
  267. unregisterBroadcastReceiver();
  268. }
  269. }