|
@@ -1,17 +1,29 @@
|
|
|
package de.tudarmstadt.informatik.hostage.commons;
|
|
|
|
|
|
-import java.io.File;
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.UnknownHostException;
|
|
|
+import java.security.KeyStore;
|
|
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.HttpVersion;
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.conn.ClientConnectionManager;
|
|
|
+import org.apache.http.conn.scheme.PlainSocketFactory;
|
|
|
+import org.apache.http.conn.scheme.Scheme;
|
|
|
+import org.apache.http.conn.scheme.SchemeRegistry;
|
|
|
+import org.apache.http.conn.ssl.SSLSocketFactory;
|
|
|
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
|
|
|
+import org.apache.http.params.BasicHttpParams;
|
|
|
+import org.apache.http.params.HttpParams;
|
|
|
+import org.apache.http.params.HttpProtocolParams;
|
|
|
+import org.apache.http.protocol.HTTP;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
+import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
|
|
|
import android.content.Context;
|
|
|
import android.net.ConnectivityManager;
|
|
|
import android.net.NetworkInfo;
|
|
@@ -19,88 +31,126 @@ import android.net.wifi.WifiInfo;
|
|
|
import android.net.wifi.WifiManager;
|
|
|
import android.os.Environment;
|
|
|
import android.text.TextUtils;
|
|
|
-import android.util.Log;
|
|
|
|
|
|
public final class HelperUtils {
|
|
|
|
|
|
-
|
|
|
public static String getSSID(Context context) {
|
|
|
- String ssid = null;
|
|
|
- ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
- NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
|
|
- if (networkInfo != null && networkInfo.isConnected()) {
|
|
|
- final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
|
|
- final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
|
|
|
- if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
|
|
|
- ssid = connectionInfo.getSSID();
|
|
|
- }
|
|
|
- }
|
|
|
- return ssid;
|
|
|
+ String ssid = null;
|
|
|
+ ConnectivityManager connManager = (ConnectivityManager) context
|
|
|
+ .getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+ NetworkInfo networkInfo = connManager
|
|
|
+ .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
|
|
+ if (networkInfo != null && networkInfo.isConnected()) {
|
|
|
+ final WifiManager wifiManager = (WifiManager) context
|
|
|
+ .getSystemService(Context.WIFI_SERVICE);
|
|
|
+ final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
|
|
|
+ if (connectionInfo != null
|
|
|
+ && !TextUtils.isEmpty(connectionInfo.getSSID())) {
|
|
|
+ ssid = connectionInfo.getSSID();
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
+ return ssid;
|
|
|
+ }
|
|
|
+
|
|
|
public static String getBSSID(Context context) {
|
|
|
- String bssid = null;
|
|
|
- ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
- NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
|
|
- if (networkInfo != null && networkInfo.isConnected()) {
|
|
|
- final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
|
|
- final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
|
|
|
- if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
|
|
|
- bssid = connectionInfo.getBSSID();
|
|
|
- }
|
|
|
- }
|
|
|
- return bssid;
|
|
|
+ String bssid = null;
|
|
|
+ ConnectivityManager connManager = (ConnectivityManager) context
|
|
|
+ .getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+ NetworkInfo networkInfo = connManager
|
|
|
+ .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
|
|
+ if (networkInfo != null && networkInfo.isConnected()) {
|
|
|
+ final WifiManager wifiManager = (WifiManager) context
|
|
|
+ .getSystemService(Context.WIFI_SERVICE);
|
|
|
+ final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
|
|
|
+ if (connectionInfo != null
|
|
|
+ && !TextUtils.isEmpty(connectionInfo.getSSID())) {
|
|
|
+ bssid = connectionInfo.getBSSID();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return bssid;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getInternalIP(Context context) {
|
|
|
+ String ipAddress = null;
|
|
|
+ ConnectivityManager connManager = (ConnectivityManager) context
|
|
|
+ .getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+ NetworkInfo networkInfo = connManager
|
|
|
+ .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
|
|
+ if (networkInfo != null && networkInfo.isConnected()) {
|
|
|
+ final WifiManager wifiManager = (WifiManager) context
|
|
|
+ .getSystemService(Context.WIFI_SERVICE);
|
|
|
+ final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
|
|
|
+ if (connectionInfo != null) {
|
|
|
+ try {
|
|
|
+ ipAddress = InetAddress.getByAddress(
|
|
|
+ unpackInetAddress(connectionInfo.getIpAddress()))
|
|
|
+ .getHostAddress();
|
|
|
+ } catch (UnknownHostException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- public static String getInternalIP(Context context){
|
|
|
- String ipAddress = null;
|
|
|
- ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
- NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
|
|
- if (networkInfo != null && networkInfo.isConnected()) {
|
|
|
- final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
|
|
- final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
|
|
|
- if(connectionInfo != null){
|
|
|
- try {
|
|
|
- ipAddress = InetAddress.getByAddress(unpackInetAddress(connectionInfo.getIpAddress())).getHostAddress();
|
|
|
- } catch (UnknownHostException e) {
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return ipAddress;
|
|
|
+ return ipAddress;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
private static byte[] unpackInetAddress(int bytes) {
|
|
|
return new byte[] { (byte) ((bytes) & 0xff),
|
|
|
(byte) ((bytes >>> 8) & 0xff), (byte) ((bytes >>> 16) & 0xff),
|
|
|
(byte) ((bytes >>> 24) & 0xff) };
|
|
|
}
|
|
|
-
|
|
|
- public static String getExternalIP (Context context) {
|
|
|
+
|
|
|
+ public static String getExternalIP(Context context) {
|
|
|
String ipAddress = null;
|
|
|
- try {
|
|
|
- HttpClient httpclient = new DefaultHttpClient();
|
|
|
- HttpGet httpget = new HttpGet("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
|
|
|
- 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){}
|
|
|
-
|
|
|
- return ipAddress;
|
|
|
+ try {
|
|
|
+ HttpClient httpclient = new DefaultHttpClient();
|
|
|
+ HttpGet httpget = new HttpGet(
|
|
|
+ "http://ip2country.sourceforge.net/ip2c.php?format=JSON");
|
|
|
+ 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) {
|
|
|
+ }
|
|
|
+ return ipAddress;
|
|
|
}
|
|
|
|
|
|
/* Checks if external storage is available for read and write */
|
|
|
public boolean isExternalStorageWritable() {
|
|
|
- String state = Environment.getExternalStorageState();
|
|
|
- if (Environment.MEDIA_MOUNTED.equals(state)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
+ String state = Environment.getExternalStorageState();
|
|
|
+ if (Environment.MEDIA_MOUNTED.equals(state)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static HttpClient createHttpClient() {
|
|
|
+ try {
|
|
|
+ KeyStore trustStore = KeyStore.getInstance(KeyStore
|
|
|
+ .getDefaultType());
|
|
|
+ trustStore.load(null, null);
|
|
|
+
|
|
|
+ SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
|
|
|
+ sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
|
|
+
|
|
|
+ HttpParams params = new BasicHttpParams();
|
|
|
+ HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
|
|
|
+ HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
|
|
|
+
|
|
|
+ SchemeRegistry registry = new SchemeRegistry();
|
|
|
+ registry.register(new Scheme("http", PlainSocketFactory
|
|
|
+ .getSocketFactory(), 80));
|
|
|
+ registry.register(new Scheme("https", sf, 443));
|
|
|
+
|
|
|
+ ClientConnectionManager ccm = new ThreadSafeClientConnManager(
|
|
|
+ params, registry);
|
|
|
+
|
|
|
+ return new DefaultHttpClient(ccm, params);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new DefaultHttpClient();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|