ServicesFragment.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.app.AlertDialog;
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.content.SharedPreferences;
  9. import android.os.Bundle;
  10. import android.app.Fragment;
  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 java.util.ArrayList;
  20. import de.tudarmstadt.informatik.hostage.R;
  21. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  22. import de.tudarmstadt.informatik.hostage.logging.LogResultReceiver;
  23. import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
  24. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  25. import de.tudarmstadt.informatik.hostage.ui2.adapter.ServicesListAdapter;
  26. import de.tudarmstadt.informatik.hostage.ui2.model.ServicesListItem;
  27. /**
  28. * Created by Daniel Lazar on 05.02.14.
  29. */
  30. public class ServicesFragment extends Fragment{
  31. private Switch mServicesSwitchService;
  32. private TextView mServicesTextName;
  33. private View rootView;
  34. private CompoundButton.OnCheckedChangeListener switchChangeListener = null;
  35. private BroadcastReceiver mReceiver;
  36. private ServicesListAdapter adapter;
  37. private ArrayList<ServicesListItem> protocolList;
  38. private UglyDbHelper dbh = new UglyDbHelper(MainActivity.getContext());
  39. private String[] protocols;
  40. private SharedPreferences mConnectionInfo;
  41. private void assignViews(){
  42. mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
  43. mServicesTextName = (TextView) rootView.findViewById(R.id.services_text_name);
  44. }
  45. public void updateUI(){
  46. if(!HelperUtils.isWifiConnected(getActivity())){
  47. mServicesSwitchService.setOnCheckedChangeListener(null);
  48. setStateNotConnected();
  49. setStateNotActive();
  50. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  51. }
  52. else {
  53. mServicesTextName.setText(HelperUtils.getSSID(getActivity()));
  54. }
  55. }
  56. public ServicesFragment(){}
  57. private void registerBroadcastReceiver(){
  58. mReceiver = new BroadcastReceiver() {
  59. @Override
  60. public void onReceive(Context context, Intent intent) {
  61. for(int i = 0; i < protocols.length; i++ ){
  62. protocolList.get(i).attacks = String.valueOf(dbh.numBssidSeen(protocolList.get(i).protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null)));
  63. }
  64. adapter.notifyDataSetChanged();
  65. }
  66. };
  67. LocalBroadcastManager
  68. .getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
  69. }
  70. @Override
  71. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
  72. super.onCreateView(inflater, container, savedInstanceState);
  73. rootView = inflater.inflate(R.layout.fragment_services, container, false);
  74. assignViews();
  75. protocols = getResources().getStringArray(R.array.protocols);
  76. mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  77. updateUI();
  78. ListView list = (ListView) rootView.findViewById(R.id.services_list_view);
  79. protocolList= new ArrayList<ServicesListItem>();
  80. for(String protocol: protocols){
  81. protocolList.add(new ServicesListItem(protocol));
  82. if(MainActivity.getInstance().getHoneyService().isRunning(protocol)){
  83. setStateActive();
  84. }
  85. }
  86. for(int i=0; i < protocols.length; i++){
  87. protocolList.get(i).attacks = String.valueOf(dbh.numBssidSeen(protocolList.get(i).protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null)));
  88. protocolList.get(i).initialValue = Integer.valueOf(protocolList.get(i).attacks);
  89. }
  90. adapter = new ServicesListAdapter(getActivity().getBaseContext(), protocolList);
  91. adapter.setActivity(this.getActivity(), this.mServicesSwitchService, this.switchChangeListener);
  92. list.setAdapter(adapter);
  93. registerBroadcastReceiver();
  94. mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
  95. if(switchChangeListener == null){
  96. switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  97. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  98. if(!HelperUtils.isWifiConnected(getActivity())){
  99. new AlertDialog.Builder(getActivity())
  100. .setTitle("Information")
  101. .setMessage("You are not connected to a WiFi network. \n\nPlease connect to one, before trying to activate HosTaGe.")
  102. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  103. public void onClick(DialogInterface dialog, int which) {
  104. }
  105. })
  106. .setIcon(android.R.drawable.ic_dialog_info)
  107. .show();
  108. setStateNotActive();
  109. setStateNotConnected();
  110. } else {
  111. if(isChecked){
  112. for(String protocol: protocols){
  113. if(!MainActivity.getInstance().getHoneyService().isRunning(protocol)) {
  114. MainActivity.getInstance().getHoneyService().startListener(protocol);
  115. }
  116. }
  117. setStateActive();
  118. } else {
  119. MainActivity.getInstance().getHoneyService().stopListeners();
  120. MainActivity.getInstance().stopAndUnbind();
  121. setStateNotActive();
  122. }
  123. }
  124. }
  125. };
  126. }
  127. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  128. return rootView;
  129. };
  130. private void setStateActive() {
  131. mServicesSwitchService.setChecked(true);
  132. }
  133. private void setStateNotConnected() {
  134. mServicesTextName.setText("Not connected");
  135. }
  136. private void setStateNotActive() {
  137. mServicesSwitchService.setChecked(false);
  138. }
  139. @Override
  140. public void onDestroy(){
  141. super.onDestroy();
  142. LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
  143. }
  144. }