123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- package de.tudarmstadt.informatik.hostage.ui2.fragment;
- import android.app.AlertDialog;
- 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.app.Fragment;
- 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 java.util.ArrayList;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
- import de.tudarmstadt.informatik.hostage.logging.LogResultReceiver;
- import de.tudarmstadt.informatik.hostage.logging.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 void assignViews(){
- mServicesSwitchService = (Switch) rootView.findViewById(R.id.service_switch_connection);
- mServicesTextName = (TextView) rootView.findViewById(R.id.services_text_name);
- }
- public void updateUI(){
- if(!HelperUtils.isWifiConnected(getActivity())){
- mServicesSwitchService.setOnCheckedChangeListener(null);
- setStateNotConnected();
- setStateNotActive();
- mServicesSwitchService.setOnCheckedChangeListener(switchChangeListener);
- }
- else {
- mServicesTextName.setText(HelperUtils.getSSID(getActivity()));
- }
- }
- public ServicesFragment(){}
- private void registerBroadcastReceiver(){
- mReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- for(int i = 0; i < protocols.length; i++ ){
- protocolList.get(i).attacks = String.valueOf(dbh.numBssidSeen(protocolList.get(i).protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null)));
- }
- adapter.notifyDataSetChanged();
- }
- };
- LocalBroadcastManager
- .getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
- }
- @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>();
- for(String protocol: protocols){
- protocolList.add(new ServicesListItem(protocol));
- if(MainActivity.getInstance().getHoneyService().isRunning(protocol)){
- setStateActive();
- }
- }
- for(int i=0; i < protocols.length; i++){
- protocolList.get(i).attacks = String.valueOf(dbh.numBssidSeen(protocolList.get(i).protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null)));
- protocolList.get(i).initialValue = Integer.valueOf(protocolList.get(i).attacks);
- }
- adapter = new ServicesListAdapter(getActivity().getBaseContext(), protocolList);
- adapter.setActivity(this.getActivity(), this.mServicesSwitchService, this.switchChangeListener);
- list.setAdapter(adapter);
- registerBroadcastReceiver();
- 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("Information")
- .setMessage("You are not connected to a WiFi network. \n\nPlease connect to one, before trying to activate HosTaGe.")
- .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(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);
- return rootView;
- };
- private void setStateActive() {
- mServicesSwitchService.setChecked(true);
- }
- private void setStateNotConnected() {
- mServicesTextName.setText("Not connected");
- }
- private void setStateNotActive() {
- mServicesSwitchService.setChecked(false);
- }
- @Override
- public void onDestroy(){
- super.onDestroy();
- LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
- }
- }
|