ServicesFragment.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. //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.substring(1,ssid.length()-1));
  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. if (!HelperUtils.isWifiConnected(getActivity())) {
  156. if(!MainActivity.getInstance().getHostageService().hasRunningListeners()) {
  157. new AlertDialog.Builder(getActivity())
  158. .setTitle(R.string.information)
  159. .setMessage(R.string.wifi_not_connected_msg)
  160. .setPositiveButton(android.R.string.ok,
  161. new DialogInterface.OnClickListener() {
  162. public void onClick(DialogInterface dialog, int which) {
  163. }
  164. }
  165. )
  166. .setIcon(android.R.drawable.ic_dialog_info)
  167. .show();
  168. if(mServicesSwitchService.isChecked()) {
  169. setStateNotActive();
  170. }
  171. setStateNotConnected();
  172. }
  173. else{
  174. setStateActive();
  175. }
  176. } else {
  177. if (MainActivity.getInstance().isServiceBound()) {
  178. if (isChecked) {
  179. for (String protocol : protocols) {
  180. if (!protocol.equals("GHOST")) {
  181. if (!MainActivity.getInstance().getHostageService().isRunning(protocol)) {
  182. MainActivity.getInstance().getHostageService().startListener(protocol);
  183. }
  184. } else {
  185. if (mProfile.mGhostActive) {
  186. mGhostPorts = mProfile.getGhostPorts();
  187. if (mGhostPorts.length != 0) {
  188. for (Integer port : mGhostPorts) {
  189. if (!MainActivity.getInstance().getHostageService().isRunning("GHOST", port)) {
  190. MainActivity.getInstance().getHostageService().startListener("GHOST", port);
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. setStateActive();
  198. } else{
  199. MainActivity.getInstance().getHostageService().stopListeners();
  200. MainActivity.getInstance().stopAndUnbind();
  201. setStateNotActive();
  202. }
  203. }
  204. }
  205. }
  206. };
  207. }
  208. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  209. adapter = new ServicesListAdapter(getActivity().getBaseContext(), protocolList);
  210. adapter.setActivity(this.getActivity(), this.mServicesSwitchService, this.switchChangeListener);
  211. list.setAdapter(adapter);
  212. registerBroadcastReceiver();
  213. return rootView;
  214. }
  215. /**
  216. * called on start of this fragment.
  217. * registers broadcast receiver and binds change listener to main switch
  218. */
  219. @Override
  220. public void onStart() {
  221. super.onStart();
  222. registerBroadcastReceiver();
  223. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  224. }
  225. /**
  226. * unregister the broadcast receiver if a receiver is already registered
  227. */
  228. private void unregisterBroadcastReceiver() {
  229. if (mReceiverRegistered) {
  230. LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
  231. this.mReceiverRegistered = false;
  232. }
  233. }
  234. /**
  235. * sets main switch to true
  236. */
  237. private void setStateActive() {
  238. mServicesSwitchService.setChecked(true);
  239. }
  240. /**
  241. * sets text of text field to not connected, if the device is not connected to a network
  242. */
  243. private void setStateNotConnected() {
  244. mServicesTextName.setText(R.string.not_connected);
  245. }
  246. /**
  247. * sets main switch to false
  248. */
  249. private void setStateNotActive() {
  250. if (mServicesSwitchService.isChecked()) {
  251. mServicesSwitchService.setChecked(false);
  252. }
  253. }
  254. /**
  255. * overrides onStop
  256. * unloads the ChangeListener
  257. */
  258. @Override
  259. public void onStop() {
  260. super.onStop();
  261. mServicesSwitchService.setOnCheckedChangeListener(null);
  262. }
  263. /**
  264. * overrides onDestroy
  265. * unregisters broadcast receiver, when destroyed
  266. */
  267. @Override
  268. public void onDestroy() {
  269. super.onDestroy();
  270. unregisterBroadcastReceiver();
  271. }
  272. }