123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- package de.tudarmstadt.informatik.hostage.ui2.fragment;
- import java.util.ArrayList;
- import android.app.AlertDialog;
- import android.app.Fragment;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.support.v4.content.LocalBroadcastManager;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.CompoundButton;
- import android.widget.ListView;
- import android.widget.Switch;
- import android.widget.TextView;
- import de.tudarmstadt.informatik.hostage.Handler;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
- import de.tudarmstadt.informatik.hostage.deprecated.UglyDbHelper;
- import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
- import de.tudarmstadt.informatik.hostage.ui2.adapter.ServicesListAdapter;
- import de.tudarmstadt.informatik.hostage.ui2.model.ServicesListItem;
- /**
- * Created by Daniel Lazar on 05.02.14.
- */
- 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 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().getHostageService().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(Handler.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().getHostageService().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().getHostageService().isRunning(protocol)) {
- MainActivity.getInstance().getHostageService().startListener(protocol);
- }
- }
- setStateActive();
- } else {
- MainActivity.getInstance().getHostageService().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();
- }
- }
|