|
@@ -31,206 +31,256 @@ import de.tudarmstadt.informatik.hostage.ui2.model.ServicesListItem;
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * Created by Daniel Lazar on 05.02.14.
|
|
|
+ * @author Daniel Lazar
|
|
|
+ * @created 05.02.14
|
|
|
+ * Fragment that displays a switch for every protocol.
|
|
|
+ * Also it can de-/activate every protocol by using this switch.
|
|
|
*/
|
|
|
-public class ServicesFragment extends Fragment{
|
|
|
+public class ServicesFragment extends Fragment {
|
|
|
+
|
|
|
+ private Switch mServicesSwitchService;
|
|
|
+ private TextView mServicesTextName;
|
|
|
+
|
|
|
+ private View rootView;
|
|
|
+
|
|
|
+ private CompoundButton.OnCheckedChangeListener switchChangeListener = null;
|
|
|
+
|
|
|
+ private BroadcastReceiver mReceiver;
|
|
|
+
|
|
|
+ private ServicesListAdapter adapter;
|
|
|
+
|
|
|
+ private ArrayList<ServicesListItem> protocolList;
|
|
|
+
|
|
|
+ private UglyDbHelper dbh = new UglyDbHelper(MainActivity.getContext());
|
|
|
+
|
|
|
+ private String[] protocols;
|
|
|
+
|
|
|
+ private SharedPreferences mConnectionInfo;
|
|
|
+
|
|
|
+ private boolean mReceiverRegistered = false;
|
|
|
|
|
|
- private Switch mServicesSwitchService;
|
|
|
- private TextView mServicesTextName;
|
|
|
-
|
|
|
- private View rootView;
|
|
|
-
|
|
|
- private CompoundButton.OnCheckedChangeListener switchChangeListener = null;
|
|
|
-
|
|
|
- private BroadcastReceiver mReceiver;
|
|
|
-
|
|
|
- private ServicesListAdapter adapter;
|
|
|
-
|
|
|
- private ArrayList<ServicesListItem> protocolList;
|
|
|
-
|
|
|
- private UglyDbHelper dbh = new UglyDbHelper(MainActivity.getContext());
|
|
|
-
|
|
|
- private String[] protocols;
|
|
|
-
|
|
|
- private SharedPreferences mConnectionInfo;
|
|
|
-
|
|
|
- private boolean mReceiverRegistered = false;
|
|
|
-
|
|
|
-
|
|
|
- private void assignViews(){
|
|
|
- mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
|
|
|
- mServicesTextName = (TextView) rootView.findViewById(R.id.services_text_name);
|
|
|
-
|
|
|
- rootView.findViewById(R.id.services_button_connection_info).setOnClickListener(new View.OnClickListener() {
|
|
|
- @Override
|
|
|
- public void onClick(View v) {
|
|
|
- ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
|
|
|
- connectionInfoDialogFragment.show(getFragmentManager().beginTransaction(), connectionInfoDialogFragment.getTag());
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public void updateUI(){
|
|
|
- if(!HelperUtils.isWifiConnected(getActivity())){
|
|
|
- mServicesSwitchService.setOnCheckedChangeListener(null);
|
|
|
- setStateNotConnected();
|
|
|
- setStateNotActive();
|
|
|
- mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
|
|
|
- }
|
|
|
- else {
|
|
|
- if(MainActivity.getInstance().getHoneyService().hasRunningListeners()){
|
|
|
- setStateActive();
|
|
|
- }
|
|
|
-
|
|
|
- mServicesTextName.setText(HelperUtils.getSSID(getActivity()));
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- public ServicesFragment(){}
|
|
|
-
|
|
|
- private void registerBroadcastReceiver(){
|
|
|
- if(!mReceiverRegistered){
|
|
|
- mReceiver = new BroadcastReceiver() {
|
|
|
- @Override
|
|
|
- public void onReceive(Context context, Intent intent) {
|
|
|
- String sender = intent.getStringExtra("SENDER");
|
|
|
- String[] values = intent.getStringArrayExtra("VALUES");
|
|
|
-
|
|
|
- if(sender.equals(HoneyHandler.class.getName()) && values[0].equals(getString(R.string.broadcast_started))){
|
|
|
- for(ServicesListItem item: protocolList){
|
|
|
- if(item.protocol.equals(values[1])){
|
|
|
- item.attacks = dbh.numBssidSeen(item.protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (!MainActivity.getInstance().getHoneyService().hasRunningListeners()) {
|
|
|
- setStateNotActive();
|
|
|
- } else {
|
|
|
- setStateActive();
|
|
|
- }
|
|
|
-
|
|
|
- adapter.notifyDataSetChanged();
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
|
|
|
- this.mReceiverRegistered = true;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- @Override
|
|
|
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
|
|
|
-
|
|
|
- super.onCreateView(inflater, container, savedInstanceState);
|
|
|
-
|
|
|
- rootView = inflater.inflate(R.layout.fragment_services, container, false);
|
|
|
- assignViews();
|
|
|
-
|
|
|
- protocols = getResources().getStringArray(R.array.protocols);
|
|
|
- mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
|
|
|
-
|
|
|
- updateUI();
|
|
|
-
|
|
|
- ListView list = (ListView) rootView.findViewById(R.id.services_list_view);
|
|
|
-
|
|
|
- protocolList= new ArrayList<ServicesListItem>();
|
|
|
- int i=0;
|
|
|
- for(String protocol: protocols){
|
|
|
- protocolList.add(new ServicesListItem(protocol));
|
|
|
- protocolList.get(i).attacks = dbh.numBssidSeen(protocolList.get(i).protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
|
|
|
- i++;
|
|
|
- }
|
|
|
-
|
|
|
- mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
|
|
|
-
|
|
|
- if (switchChangeListener == null) {
|
|
|
- switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
|
|
|
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
|
|
- if (!HelperUtils.isWifiConnected(getActivity())) {
|
|
|
- new AlertDialog.Builder(getActivity())
|
|
|
- .setTitle(R.string.information)
|
|
|
- .setMessage(R.string.wifi_not_connected_msg)
|
|
|
- .setPositiveButton(android.R.string.ok,
|
|
|
- new DialogInterface.OnClickListener() {
|
|
|
- public void onClick(DialogInterface dialog, int which) {
|
|
|
-
|
|
|
- }
|
|
|
- })
|
|
|
- .setIcon(android.R.drawable.ic_dialog_info)
|
|
|
- .show();
|
|
|
-
|
|
|
- setStateNotActive();
|
|
|
- setStateNotConnected();
|
|
|
- } else {
|
|
|
- if (MainActivity.getInstance().isServiceBound()) {
|
|
|
- if (isChecked) {
|
|
|
- for (String protocol : protocols) {
|
|
|
- if (!MainActivity.getInstance().getHoneyService().isRunning(protocol)) {
|
|
|
- MainActivity.getInstance().getHoneyService().startListener(protocol);
|
|
|
- }
|
|
|
- }
|
|
|
- setStateActive();
|
|
|
- } else {
|
|
|
- MainActivity.getInstance().getHoneyService().stopListeners();
|
|
|
- MainActivity.getInstance().stopAndUnbind();
|
|
|
- setStateNotActive();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- };
|
|
|
- }
|
|
|
- mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
|
|
|
-
|
|
|
- adapter = new ServicesListAdapter(getActivity().getBaseContext(), protocolList);
|
|
|
- adapter.setActivity(this.getActivity(), this.mServicesSwitchService, this.switchChangeListener);
|
|
|
- list.setAdapter(adapter);
|
|
|
-
|
|
|
- registerBroadcastReceiver();
|
|
|
-
|
|
|
- return rootView;
|
|
|
-
|
|
|
- };
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onStart(){
|
|
|
- super.onStart();
|
|
|
- registerBroadcastReceiver();
|
|
|
- mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
|
|
|
- }
|
|
|
-
|
|
|
- private void unregisterBroadcastReceiver(){
|
|
|
- if(mReceiverRegistered){
|
|
|
- LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
|
|
|
- this.mReceiverRegistered = false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void setStateActive() {
|
|
|
- mServicesSwitchService.setChecked(true);
|
|
|
- }
|
|
|
-
|
|
|
- private void setStateNotConnected() {
|
|
|
- mServicesTextName.setText(R.string.not_connected);
|
|
|
- }
|
|
|
-
|
|
|
- private void setStateNotActive() {
|
|
|
- mServicesSwitchService.setChecked(false);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onStop(){
|
|
|
- super.onStop();
|
|
|
- mServicesSwitchService.setOnCheckedChangeListener(null);
|
|
|
- }
|
|
|
- @Override
|
|
|
- public void onDestroy(){
|
|
|
- super.onDestroy();
|
|
|
- unregisterBroadcastReceiver();
|
|
|
- }
|
|
|
+ public ServicesFragment() {
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * assign views which are not asynchronously loaded
|
|
|
+ */
|
|
|
+ private void assignViews() {
|
|
|
+ mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
|
|
|
+ mServicesTextName = (TextView) rootView.findViewById(R.id.services_text_name);
|
|
|
+
|
|
|
+ rootView.findViewById(R.id.services_button_connection_info).setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
|
|
|
+ connectionInfoDialogFragment.show(getFragmentManager().beginTransaction(), connectionInfoDialogFragment.getTag());
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * updates the user interface
|
|
|
+ * in detail: the main switch and the textField mServicesTextName
|
|
|
+ */
|
|
|
+ public void updateUI() {
|
|
|
+ if (!HelperUtils.isWifiConnected(getActivity())) {
|
|
|
+ mServicesSwitchService.setOnCheckedChangeListener(null);
|
|
|
+ setStateNotConnected();
|
|
|
+ setStateNotActive();
|
|
|
+ mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
|
|
|
+ } else {
|
|
|
+ if (MainActivity.getInstance().getHoneyService().hasRunningListeners()) {
|
|
|
+ setStateActive();
|
|
|
+ }
|
|
|
+
|
|
|
+ mServicesTextName.setText(HelperUtils.getSSID(getActivity()));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * register a broadcast receiver if not already registered
|
|
|
+ * and also update the number of attacks per protocol
|
|
|
+ */
|
|
|
+ private void registerBroadcastReceiver() {
|
|
|
+ if (!mReceiverRegistered) {
|
|
|
+ mReceiver = new BroadcastReceiver() {
|
|
|
+ @Override
|
|
|
+ public void onReceive(Context context, Intent intent) {
|
|
|
+ String sender = intent.getStringExtra("SENDER");
|
|
|
+ String[] values = intent.getStringArrayExtra("VALUES");
|
|
|
+
|
|
|
+ if (sender.equals(HoneyHandler.class.getName()) && values[0].equals(getString(R.string.broadcast_started))) {
|
|
|
+ for (ServicesListItem item : protocolList) {
|
|
|
+ if (item.protocol.equals(values[1])) {
|
|
|
+ item.attacks = dbh.numBssidSeen(item.protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!MainActivity.getInstance().getHoneyService().hasRunningListeners()) {
|
|
|
+ setStateNotActive();
|
|
|
+ } else {
|
|
|
+ setStateActive();
|
|
|
+ }
|
|
|
+
|
|
|
+ adapter.notifyDataSetChanged();
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
|
|
|
+ this.mReceiverRegistered = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * most important method of this class
|
|
|
+ *
|
|
|
+ * @param inflater
|
|
|
+ * @param container
|
|
|
+ * @param savedInstanceState
|
|
|
+ * @return rootView
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
+
|
|
|
+ super.onCreateView(inflater, container, savedInstanceState);
|
|
|
+
|
|
|
+ rootView = inflater.inflate(R.layout.fragment_services, container, false);
|
|
|
+ assignViews();
|
|
|
+
|
|
|
+ protocols = getResources().getStringArray(R.array.protocols);
|
|
|
+ mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
|
|
|
+
|
|
|
+ updateUI();
|
|
|
+
|
|
|
+ ListView list = (ListView) rootView.findViewById(R.id.services_list_view);
|
|
|
+
|
|
|
+ protocolList = new ArrayList<ServicesListItem>();
|
|
|
+ int i = 0;
|
|
|
+ for (String protocol : protocols) {
|
|
|
+ protocolList.add(new ServicesListItem(protocol));
|
|
|
+ protocolList.get(i).attacks = dbh.numBssidSeen(protocolList.get(i).protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
|
|
|
+
|
|
|
+ if (switchChangeListener == null) {
|
|
|
+ switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
|
|
|
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
|
|
+ if (!HelperUtils.isWifiConnected(getActivity())) {
|
|
|
+ new AlertDialog.Builder(getActivity())
|
|
|
+ .setTitle(R.string.information)
|
|
|
+ .setMessage(R.string.wifi_not_connected_msg)
|
|
|
+ .setPositiveButton(android.R.string.ok,
|
|
|
+ new DialogInterface.OnClickListener() {
|
|
|
+ public void onClick(DialogInterface dialog, int which) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ )
|
|
|
+ .setIcon(android.R.drawable.ic_dialog_info)
|
|
|
+ .show();
|
|
|
+
|
|
|
+ setStateNotActive();
|
|
|
+ setStateNotConnected();
|
|
|
+ } else {
|
|
|
+ if (MainActivity.getInstance().isServiceBound()) {
|
|
|
+ if (isChecked) {
|
|
|
+ for (String protocol : protocols) {
|
|
|
+ if (!MainActivity.getInstance().getHoneyService().isRunning(protocol)) {
|
|
|
+ MainActivity.getInstance().getHoneyService().startListener(protocol);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ setStateActive();
|
|
|
+ } else {
|
|
|
+ MainActivity.getInstance().getHoneyService().stopListeners();
|
|
|
+ MainActivity.getInstance().stopAndUnbind();
|
|
|
+ setStateNotActive();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+ }
|
|
|
+ mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
|
|
|
+
|
|
|
+ adapter = new ServicesListAdapter(getActivity().getBaseContext(), protocolList);
|
|
|
+ adapter.setActivity(this.getActivity(), this.mServicesSwitchService, this.switchChangeListener);
|
|
|
+ list.setAdapter(adapter);
|
|
|
+
|
|
|
+ registerBroadcastReceiver();
|
|
|
+
|
|
|
+ return rootView;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ ;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * called on start of this fragment.
|
|
|
+ * registers broadcast receiver and binds change listener to main switch
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void onStart() {
|
|
|
+ super.onStart();
|
|
|
+ registerBroadcastReceiver();
|
|
|
+ mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * unregister the broadcast receiver if a receiver is already registered
|
|
|
+ */
|
|
|
+ private void unregisterBroadcastReceiver() {
|
|
|
+ if (mReceiverRegistered) {
|
|
|
+ LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
|
|
|
+ this.mReceiverRegistered = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * sets main switch to true
|
|
|
+ */
|
|
|
+ private void setStateActive() {
|
|
|
+ mServicesSwitchService.setChecked(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * sets text of text field to not connected, if the device is not connected to a network
|
|
|
+ */
|
|
|
+ private void setStateNotConnected() {
|
|
|
+ mServicesTextName.setText(R.string.not_connected);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * sets main switch to false
|
|
|
+ */
|
|
|
+ private void setStateNotActive() {
|
|
|
+ mServicesSwitchService.setChecked(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * overrides onStop
|
|
|
+ * unloads the ChangeListener
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void onStop() {
|
|
|
+ super.onStop();
|
|
|
+ mServicesSwitchService.setOnCheckedChangeListener(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * overrides onDestroy
|
|
|
+ * unregisters broadcast receiver, when destroyed
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ unregisterBroadcastReceiver();
|
|
|
+ }
|
|
|
|
|
|
}
|