ServicesFragment.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.app.AlertDialog;
  3. import android.content.DialogInterface;
  4. import android.os.Bundle;
  5. import android.app.Fragment;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.CompoundButton;
  10. import android.widget.ListView;
  11. import android.widget.Switch;
  12. import android.widget.TextView;
  13. import java.util.ArrayList;
  14. import de.tudarmstadt.informatik.hostage.R;
  15. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  16. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  17. import de.tudarmstadt.informatik.hostage.ui2.adapter.ServicesListAdapter;
  18. import de.tudarmstadt.informatik.hostage.ui2.model.ServicesListItem;
  19. /**
  20. * Created by Daniel Lazar on 05.02.14.
  21. */
  22. public class ServicesFragment extends Fragment{
  23. private Switch mServicesSwitchService;
  24. private TextView mServicesTextName;
  25. private View rootView;
  26. private CompoundButton.OnCheckedChangeListener switchChangeListener = null;
  27. private void assignViews(){
  28. mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
  29. mServicesTextName = (TextView) rootView.findViewById(R.id.services_text_name);
  30. }
  31. public void updateUI(){
  32. if(!HelperUtils.isWifiConnected(getActivity())){
  33. mServicesSwitchService.setOnCheckedChangeListener(null);
  34. setStateNotConnected();
  35. setStateNotActive();
  36. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  37. }
  38. else {
  39. mServicesTextName.setText(HelperUtils.getSSID(getActivity()));
  40. }
  41. /* //check if ftp monitoring is running
  42. if(!MainActivity.getInstance().getHoneyService().isRunning(protocols[1])){
  43. mServicesSwitchFTP.setOnCheckedChangeListener(null);
  44. }
  45. else {
  46. mServicesSwitchFTP.setChecked(true);
  47. }*/
  48. }
  49. public ServicesFragment(){}
  50. @Override
  51. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
  52. super.onCreateView(inflater, container, savedInstanceState);
  53. rootView = inflater.inflate(R.layout.fragment_services, container, false);
  54. assignViews();
  55. updateUI();
  56. ListView list = (ListView) rootView.findViewById(R.id.services_list_view);
  57. final String[] protocols = getResources().getStringArray(R.array.protocols);
  58. ArrayList<ServicesListItem> protocolList= new ArrayList<ServicesListItem>();
  59. for(String protocol: protocols){
  60. protocolList.add(new ServicesListItem(protocol));
  61. if(MainActivity.getInstance().getHoneyService().isRunning(protocol)){
  62. setStateActive();
  63. }
  64. }
  65. final ServicesListAdapter adapter = new ServicesListAdapter(getActivity().getBaseContext(), protocolList);
  66. list.setAdapter(adapter);
  67. mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
  68. if(switchChangeListener == null){
  69. switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  70. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  71. if(!HelperUtils.isWifiConnected(getActivity())){
  72. new AlertDialog.Builder(getActivity())
  73. .setTitle("Information")
  74. .setMessage("You are not connected to a WiFi network. \n\nPlease connect to one, before trying to activate HosTaGe.")
  75. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  76. public void onClick(DialogInterface dialog, int which) {
  77. }
  78. })
  79. .setIcon(android.R.drawable.ic_dialog_info)
  80. .show();
  81. setStateNotActive();
  82. setStateNotConnected();
  83. } else {
  84. if(isChecked){
  85. for(String protocol: protocols){
  86. if(MainActivity.getInstance().getHoneyService().isRunning(protocol)){
  87. adapter.notifyDataSetChanged();
  88. }
  89. else{
  90. MainActivity.getInstance().getHoneyService().startListener(protocol);
  91. adapter.notifyDataSetChanged();
  92. }
  93. }
  94. setStateActive();
  95. } else {
  96. for(String protocol: protocols){
  97. if(MainActivity.getInstance().getHoneyService().isRunning(protocol)){
  98. MainActivity.getInstance().getHoneyService().stopListener(protocol);
  99. /*MainActivity.getInstance().stopAndUnbind();*/
  100. adapter.notifyDataSetChanged();
  101. }
  102. }
  103. // adapter.notifyDataSetChanged();
  104. setStateNotActive();
  105. }
  106. }
  107. }
  108. };
  109. }
  110. mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
  111. return rootView;
  112. };
  113. private void setStateActive() {
  114. mServicesSwitchService.setChecked(true);
  115. }
  116. private void setStateNotConnected() {
  117. mServicesTextName.setText("Not connected");
  118. }
  119. private void setStateNotActive() {
  120. mServicesSwitchService.setChecked(false);
  121. }
  122. }