ServicesFragment.java 12 KB

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