package de.tudarmstadt.informatik.hostage.ui; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityManager.RunningServiceInfo; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.net.ConnectivityManager; import android.os.Bundle; import android.os.IBinder; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.ToggleButton; import android.widget.ViewAnimator; import de.tudarmstadt.informatik.hostage.HoneyService; import de.tudarmstadt.informatik.hostage.HoneyService.LocalBinder; import de.tudarmstadt.informatik.hostage.R; import de.tudarmstadt.informatik.hostage.commons.HelperUtils; import de.tudarmstadt.informatik.hostage.logging.DatabaseHandler; public class MainActivity extends Activity { public static final String BROADCAST = "de.tudarmstadt.informatik.hostage.BROADCAST"; public static final String PREF_NAME = "de.tudarmstadt.informatik.hostage.SESSION_DATA"; public static final String LISTENER = "_LISTENER"; public static final String HANDLER_COUNT = "_HANDLER_COUNT"; public static final int LIGHT_GREY = 0x01; public static final int LIGHT_GREEN = 0x02; public static final int LIGHT_RED = 0x03; public static final int LIGHT_YELLOW = 0x04; private HoneyService mService; private SharedPreferences pref; private Editor editor; private DatabaseHandler dbh; private ViewAnimator viewAnimator; private GestureDetector gestureDetector; private Animation animFlipInLR; private Animation animFlipOutLR; private Animation animFlipInRL; private Animation animFlipOutRL; private ListView listView; private ListViewAdapter adapter; private String protocolClicked; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViewAnimator(); initListView(); pref = getSharedPreferences(MainActivity.PREF_NAME, Context.MODE_PRIVATE); dbh = new DatabaseHandler(getApplicationContext()); editor = pref.edit(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.action_settings: startActivity(new Intent(this, SettingsActivity.class)); default: return super.onOptionsItemSelected(item); } } @Override protected void onStart() { super.onStart(); registerReceiver(); registerNetReceiver(); if (isServiceRunning()) { bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE); } } @Override protected void onStop() { if (isServiceRunning()) { unbindService(mConnection); } unregisterNetReceiver(); unregisterReceiver(); super.onStop(); } @Override protected void onDestroy(){ super.onDestroy(); if(!isServiceRunning()){ deleteSessionData(); } } @Override protected void onResume() { super.onResume(); updateConnectionInfo(); } public void buttonOnOffClick(View view) { if (((ToggleButton) view).isChecked()) { if (isParanoid()) { protocolClicked = "PANIC"; } else { protocolClicked = "SMB"; } startAndBind(); } else { mService.stopListeners(); stopAndUnbind(); } } private void startAndBind() { startService(getServiceIntent()); bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE); } private void stopAndUnbind() { unbindService(mConnection); stopService(getServiceIntent()); } private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mService = ((LocalBinder) service).getService(); if(protocolClicked != null && protocolClicked.equals("PANIC")){ mService.startListeners(); }else{ mService.toggleListener(protocolClicked); } protocolClicked = null; updateUI(); } @Override public void onServiceDisconnected(ComponentName name) { mService = null; } }; private Intent getServiceIntent() { return new Intent(this, HoneyService.class); } private boolean isParanoid() { return ((CheckBox) findViewById(R.id.checkBoxParanoid)).isChecked(); } private void initListView() { ArrayList> data = new ArrayList>(); for (String protocol : getResources().getStringArray(R.array.protocols)) { HashMap d = new HashMap(); d.put("light", String.valueOf(R.drawable.light_grey)); d.put("protocol", protocol); d.put("connections", "-"); data.add(d); } listView = (ListView) findViewById(R.id.listViewProtocols); adapter = new ListViewAdapter(getLayoutInflater(), data); listView.setAdapter(adapter); listView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } }); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { String protocolName = (String) ((HashMap) adapter .getItem(position)).get("protocol"); if (isServiceRunning()) { mService.toggleListener(protocolName); if(!mService.hasRunningListeners()) stopAndUnbind(); }else{ protocolClicked = protocolName; startAndBind(); } } }); } private boolean isServiceRunning() { ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if (service.service.getClassName().equals(HoneyService.class.getName())) { return true; } } return false; } // Delete session data private void deleteSessionData(){ editor.clear(); editor.commit(); } private void registerReceiver() { LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, new IntentFilter(BROADCAST)); } private void unregisterReceiver() { LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); } private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { updateUI(); } }; private void registerNetReceiver() { // register BroadcastReceiver on network state changes final IntentFilter intent = new IntentFilter(); intent.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION); //"android.net.conn.CONNECTIVITY_CHANGE" registerReceiver(netReceiver, intent); } private void unregisterNetReceiver() { LocalBroadcastManager.getInstance(this).unregisterReceiver(netReceiver); } private BroadcastReceiver netReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (isServiceRunning()) { Toast.makeText(getApplicationContext(),"Connection changed! Services stopped!", Toast.LENGTH_LONG).show(); mService.stopListeners(); stopAndUnbind(); } deleteSessionData(); updateConnectionInfo(); updateUI(); } }; private void updateUI() { boolean activeListeners = false; boolean activeHandlers = false; boolean yellowLight = false; for(String protocol : getResources().getStringArray(R.array.protocols)){ if(pref.getBoolean(protocol + LISTENER, false)){ activeListeners = true; int handlerCount = pref.getInt(protocol + HANDLER_COUNT, 0); if(handlerCount > 0){ activeHandlers = true; updateProtocolLight(LIGHT_RED, protocol); updateProtocolConnections(handlerCount, protocol); } else{ if(dbh.bssidSeen(protocol, HelperUtils.getBSSID(getApplicationContext()))){ updateProtocolLight(LIGHT_YELLOW, protocol); yellowLight = true; } else{ updateProtocolLight(LIGHT_GREEN, protocol); } updateProtocolConnections(0, protocol); } }else{ updateProtocolLight(LIGHT_GREY, protocol); } } if (activeListeners) { if (activeHandlers) { updateStatusLight(LIGHT_RED); } else { if(yellowLight){ updateStatusLight(LIGHT_YELLOW); } else { updateStatusLight(LIGHT_GREEN); } } ((ToggleButton) findViewById(R.id.toggleButtonOnOff)) .setChecked(true); findViewById(R.id.checkBoxParanoid).setEnabled(false); } else { updateStatusLight(LIGHT_GREY); ((ToggleButton) findViewById(R.id.toggleButtonOnOff)) .setChecked(false); findViewById(R.id.checkBoxParanoid).setEnabled(true); } } private void updateStatusLight(int light) { switch (light) { case LIGHT_GREY: ((ImageView) findViewById(R.id.imageViewLight)) .setImageResource(R.drawable.light_grey_large); break; case LIGHT_GREEN: ((ImageView) findViewById(R.id.imageViewLight)) .setImageResource(R.drawable.light_green_large); break; case LIGHT_RED: ((ImageView) findViewById(R.id.imageViewLight)) .setImageResource(R.drawable.light_red_large); break; case LIGHT_YELLOW: ((ImageView) findViewById(R.id.imageViewLight)) .setImageResource(R.drawable.light_yellow_large); break; } } private void updateProtocolLight(int light, String protocolName) { for (int i = 0; i < adapter.getCount(); ++i) { HashMap d = (HashMap) adapter .getItem(i); if (d.get("protocol").equals(protocolName)) { switch (light) { case LIGHT_GREY: d.put("light", String.valueOf(R.drawable.light_grey)); d.put("connections", "-"); break; case LIGHT_GREEN: d.put("light", String.valueOf(R.drawable.light_green)); break; case LIGHT_RED: d.put("light", String.valueOf(R.drawable.light_red)); break; case LIGHT_YELLOW: d.put("light", String.valueOf(R.drawable.light_yellow)); break; } } } adapter.notifyDataSetChanged(); } private void updateProtocolConnections(int connections, String protocolName) { for (int i = 0; i < adapter.getCount(); ++i) { HashMap d = ((HashMap) adapter .getItem(i)); if (d.get("protocol").equals(protocolName)) { d.put("connections", String.valueOf(connections)); } } adapter.notifyDataSetChanged(); } private void updateConnectionInfo() { TextView ssidView = (TextView) findViewById(R.id.textViewSSIDValue); TextView bssidView = (TextView) findViewById(R.id.textViewBSSIDValue); TextView internalIPView = (TextView) findViewById(R.id.textViewInternalIPValue); TextView externalIPView = (TextView) findViewById(R.id.textViewExternalIPValue); String ssid = HelperUtils.getSSID(this); String bssid = HelperUtils.getBSSID(this); String internalIP = HelperUtils.getInternalIP(this); String externalIP = HelperUtils.getExternalIP(this); if (ssid != null) ssidView.setText(ssid); else ssidView.setText("-"); if (bssid != null) bssidView.setText(bssid); else bssidView.setText("-"); if (internalIP != null) internalIPView.setText(internalIP); else internalIPView.setText("-"); if (externalIP != null) externalIPView.setText(externalIP); else externalIPView.setText("-"); ssidView.invalidate(); bssidView.invalidate(); internalIPView.invalidate(); } /*############# AB HIER KOMMEN HILFSFUNKTIONEN FÜR GESTEN ##################*/ @Override public boolean onTouchEvent(MotionEvent event) { return gestureDetector.onTouchEvent(event); } private void initViewAnimator() { viewAnimator = (ViewAnimator) findViewById(R.id.viewAnimator); gestureDetector = new GestureDetector(this, simpleOnGestureListener); animFlipInLR = AnimationUtils.loadAnimation(this, R.anim.in_left_to_right); animFlipOutLR = AnimationUtils.loadAnimation(this, R.anim.out_left_to_right); animFlipInRL = AnimationUtils.loadAnimation(this, R.anim.in_right_to_left); animFlipOutRL = AnimationUtils.loadAnimation(this, R.anim.out_right_to_left); } private void swipeRightToLeft() { if (viewAnimator.getDisplayedChild() == 0) { viewAnimator.setInAnimation(animFlipInRL); viewAnimator.setOutAnimation(animFlipOutRL); viewAnimator.setDisplayedChild(1); } } private void swipeLeftToRight() { if (viewAnimator.getDisplayedChild() == 1) { viewAnimator.setInAnimation(animFlipInLR); viewAnimator.setOutAnimation(animFlipOutLR); viewAnimator.setDisplayedChild(0); } } SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { float sensitvity = 50; if ((e1.getX() - e2.getX()) > sensitvity) { swipeRightToLeft(); } else if ((e2.getX() - e1.getX()) > sensitvity) { swipeLeftToRight(); } return true; } }; public void showLog(View view){ startActivity(new Intent(this, ViewLog.class)); } }