|
@@ -3,6 +3,14 @@ package de.tudarmstadt.informatik.hostage.ui;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
import android.app.Activity;
|
|
|
import android.app.ActivityManager;
|
|
|
import android.app.ActivityManager.RunningServiceInfo;
|
|
@@ -15,6 +23,7 @@ import android.content.ServiceConnection;
|
|
|
import android.content.SharedPreferences;
|
|
|
import android.content.SharedPreferences.Editor;
|
|
|
import android.net.ConnectivityManager;
|
|
|
+import android.os.AsyncTask;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.IBinder;
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
|
@@ -93,8 +102,8 @@ public class MainActivity extends Activity {
|
|
|
|
|
|
private HoneyService mService;
|
|
|
private boolean serviceBound;
|
|
|
- private SharedPreferences pref;
|
|
|
- private Editor editor;
|
|
|
+ private SharedPreferences sessionPref;
|
|
|
+ private Editor sessionEditor;
|
|
|
private Logger logger;
|
|
|
|
|
|
// variables for the swipe animation
|
|
@@ -121,9 +130,9 @@ public class MainActivity extends Activity {
|
|
|
initViewAnimator();
|
|
|
initListView();
|
|
|
// Initialize Class variables
|
|
|
- pref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
|
|
|
+ sessionPref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
|
|
|
logger = new SQLLogger(this);
|
|
|
- editor = pref.edit();
|
|
|
+ sessionEditor = sessionPref.edit();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -157,7 +166,7 @@ public class MainActivity extends Activity {
|
|
|
if (isServiceRunning()) {
|
|
|
bindService(getServiceIntent(), mConnection, BIND_AUTO_CREATE);
|
|
|
} else {
|
|
|
- String bssid_old = pref.getString(MainActivity.BSSID, "");
|
|
|
+ String bssid_old = sessionPref.getString(MainActivity.BSSID, "");
|
|
|
String bssid_new = HelperUtils.getBSSID(this);
|
|
|
if(bssid_new == null || !bssid_new.equals(bssid_old)){
|
|
|
deleteSessionData();
|
|
@@ -165,7 +174,7 @@ public class MainActivity extends Activity {
|
|
|
}
|
|
|
// Update UI
|
|
|
updateUI();
|
|
|
- updateConnectionInfo();
|
|
|
+ updateConnectionInfText();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -366,8 +375,8 @@ public class MainActivity extends Activity {
|
|
|
* Deletes all session related Data.
|
|
|
*/
|
|
|
private void deleteSessionData(){
|
|
|
- editor.clear();
|
|
|
- editor.commit();
|
|
|
+ sessionEditor.clear();
|
|
|
+ sessionEditor.commit();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -422,13 +431,13 @@ public class MainActivity extends Activity {
|
|
|
private BroadcastReceiver netReceiver = new BroadcastReceiver() {
|
|
|
@Override
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
- String bssid_old = pref.getString(BSSID, "");
|
|
|
+ String bssid_old = sessionPref.getString(BSSID, "");
|
|
|
String bssid_new = HelperUtils.getBSSID(context);
|
|
|
if ((bssid_new == null || !bssid_new.equals(bssid_old)) && serviceBound) {
|
|
|
Toast.makeText(getApplicationContext(),"Connection changed! Services stopped!", Toast.LENGTH_LONG).show();
|
|
|
unbindService();
|
|
|
}
|
|
|
- updateConnectionInfo();
|
|
|
+ updateConnectionInfText();
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -444,9 +453,9 @@ public class MainActivity extends Activity {
|
|
|
//Update protocol lights and connection information.
|
|
|
for(String protocol : getResources().getStringArray(R.array.protocols)){
|
|
|
//Check if protocol is active
|
|
|
- if(pref.getBoolean(protocol + LISTENER, false)){
|
|
|
+ if(sessionPref.getBoolean(protocol + LISTENER, false)){
|
|
|
activeListeners = true;
|
|
|
- int handlerCount = pref.getInt(protocol + HANDLER_COUNT, 0);
|
|
|
+ int handlerCount = sessionPref.getInt(protocol + HANDLER_COUNT, 0);
|
|
|
//Check if attacks have been recorded in this session.
|
|
|
if(handlerCount > 0){
|
|
|
activeHandlers = true;
|
|
@@ -567,33 +576,24 @@ public class MainActivity extends Activity {
|
|
|
/**
|
|
|
* Gets Information about connection state and updates the GUI.
|
|
|
*/
|
|
|
- private void updateConnectionInfo() {
|
|
|
- /*
|
|
|
- final Context context = this;
|
|
|
- new Thread(new Runnable() {
|
|
|
- public void run() {
|
|
|
- */
|
|
|
- //Get text fields
|
|
|
+ private void updateConnectionInfText() {
|
|
|
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);
|
|
|
- /*
|
|
|
- ssidView.setText("Loading...");
|
|
|
- bssidView.setText("Loading...");
|
|
|
- internalIPView.setText("Loading...");
|
|
|
+
|
|
|
externalIPView.setText("Loading...");
|
|
|
- */
|
|
|
|
|
|
//Update the connection information
|
|
|
- HelperUtils.updateConnectionInfo(context);
|
|
|
+ updateConnectionInfo();
|
|
|
+ SetExternalIPTask async = new SetExternalIPTask();
|
|
|
+ async.execute(new String[]{"http://ip2country.sourceforge.net/ip2c.php?format=JSON"});
|
|
|
|
|
|
//Get connection information
|
|
|
- String ssid = pref.getString(SSID, "-");
|
|
|
- String bssid = pref.getString(BSSID, "-");
|
|
|
- String internalIP = pref.getString(INTERNAL_IP, "-");
|
|
|
- String externalIP = pref.getString(EXTERNAL_IP, "-");
|
|
|
-
|
|
|
+ String ssid = sessionPref.getString(SSID, null);
|
|
|
+ String bssid = sessionPref.getString(BSSID, null);
|
|
|
+ String internalIP = sessionPref.getString(INTERNAL_IP, null);
|
|
|
+
|
|
|
//Set text fields
|
|
|
if (ssid != null)
|
|
|
ssidView.setText(ssid);
|
|
@@ -609,15 +609,55 @@ public class MainActivity extends Activity {
|
|
|
internalIPView.setText(internalIP);
|
|
|
else
|
|
|
internalIPView.setText("-");
|
|
|
-
|
|
|
- if (externalIP != null)
|
|
|
- externalIPView.setText(externalIP);
|
|
|
- else
|
|
|
- externalIPView.setText("-");
|
|
|
- /* }
|
|
|
- }).start();
|
|
|
- */
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Updates the connection info and saves them in the the SharedPreferences for session data.
|
|
|
+ * @param context Needs a context to get system recourses.
|
|
|
+ * @see MainActivity#SESSION_DATA
|
|
|
+ */
|
|
|
+ private void updateConnectionInfo() {
|
|
|
+ SharedPreferences pref = context.getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
|
|
|
+ Editor editor = pref.edit();
|
|
|
+ editor.putString(MainActivity.SSID, HelperUtils.getSSID(context));
|
|
|
+ editor.putString(MainActivity.BSSID, HelperUtils.getBSSID(context));
|
|
|
+ editor.putString(MainActivity.INTERNAL_IP, HelperUtils.getInternalIP(context));
|
|
|
+ editor.commit();
|
|
|
+ }
|
|
|
+
|
|
|
+ private class SetExternalIPTask extends AsyncTask<String, Void, String>{
|
|
|
+ @Override
|
|
|
+ protected String doInBackground(String... url) {
|
|
|
+ String ipAddress = null;
|
|
|
+ try {
|
|
|
+ HttpClient httpclient = new DefaultHttpClient();
|
|
|
+ HttpGet httpget = new HttpGet(url[0]);
|
|
|
+ HttpResponse response;
|
|
|
+
|
|
|
+ response = httpclient.execute(httpget);
|
|
|
+
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ entity.getContentLength();
|
|
|
+ String str = EntityUtils.toString(entity);
|
|
|
+ JSONObject json_data = new JSONObject(str);
|
|
|
+ ipAddress = json_data.getString("ip");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return ipAddress;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onPostExecute(String result){
|
|
|
+ sessionEditor.putString(MainActivity.EXTERNAL_IP, result);
|
|
|
+ sessionEditor.commit();
|
|
|
+ TextView externalIPView = (TextView) findViewById(R.id.textViewExternalIPValue);
|
|
|
+ if (result != null)
|
|
|
+ externalIPView.setText(result);
|
|
|
+ else
|
|
|
+ externalIPView.setText("-");
|
|
|
+ }
|
|
|
+ };
|
|
|
|
|
|
|
|
|
/*############# Help functions for animation ##################*/
|