From a8417496cd3758a17018c78ff626d5e605929b5f Mon Sep 17 00:00:00 2001 From: HearSilent Date: Thu, 11 Jun 2015 17:02:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=A1=E5=8D=80=E6=94=B9=E7=94=A8Headers=20&?= =?UTF-8?q?=20IP=E5=88=A4=E6=96=B7,=20=E4=BF=AE=E6=AD=A3=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E6=AC=84=E7=84=A1=E6=B3=95=E6=B8=85=E9=99=A4=E4=B9=8B=E5=95=8F?= =?UTF-8?q?=E9=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- KUASWifiAutoLogin/.idea/workspace.xml | 204 +++++++----------- .../kuas/wifiautologin/libs/LoginHelper.java | 159 ++++++++++---- README.md | 2 +- 3 files changed, 188 insertions(+), 177 deletions(-) diff --git a/KUASWifiAutoLogin/.idea/workspace.xml b/KUASWifiAutoLogin/.idea/workspace.xml index 84341eb..02c574d 100644 --- a/KUASWifiAutoLogin/.idea/workspace.xml +++ b/KUASWifiAutoLogin/.idea/workspace.xml @@ -36,32 +36,32 @@ - + - - + + - - - - - - - - - - - + + + + + + + + + + + - + - + @@ -1512,8 +1512,8 @@ @@ -1556,7 +1556,6 @@ - @@ -1575,69 +1574,12 @@ + @@ -1865,7 +1807,7 @@ - + @@ -1952,17 +1894,17 @@ - - - - - - - - - - - + + + + + + + + + + + @@ -2039,17 +1981,17 @@ - - - - - - - - - - - + + + + + + + + + + + @@ -2118,17 +2060,17 @@ - - - - - - - - - - - + + + + + + + + + + + @@ -2331,26 +2273,6 @@ - - - - - - - - - - - - - - - - - - - - @@ -2361,11 +2283,31 @@ - + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/libs/LoginHelper.java b/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/libs/LoginHelper.java index 0046577..b9ab0d6 100644 --- a/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/libs/LoginHelper.java +++ b/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/libs/LoginHelper.java @@ -6,6 +6,7 @@ import android.content.Context; import android.content.Intent; import android.graphics.Color; +import android.net.Uri; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.support.v4.app.NotificationCompat; @@ -21,6 +22,7 @@ import org.apache.http.Header; +import tw.edu.kuas.wifiautologin.MainActivity; import tw.edu.kuas.wifiautologin.R; import tw.edu.kuas.wifiautologin.callbacks.Constant; import tw.edu.kuas.wifiautologin.callbacks.GeneralCallback; @@ -91,7 +93,7 @@ public static void login(final Context context, String idType, String user, Stri mBuilder = new NotificationCompat.Builder(context); mBuilder.setContentTitle(context.getString(R.string.app_name)).setContentText( String.format(context.getString(R.string.login_to_ssid), currentSsid)) - .setSmallIcon(R.drawable.ic_stat_login).setProgress(0, 0, true).setOngoing(true); + .setSmallIcon(R.drawable.ic_stat_login).setProgress(0, 0, true).setOngoing(false); mTestClient.get(context, "http://www.example.com", new AsyncHttpResponseHandler() { @Override @@ -109,14 +111,31 @@ public void onSuccess(int statusCode, Header[] headers, byte[] bytes) { if (callback != null) callback.onSuccess(resultString); - Toast.makeText(context, resultString, Toast.LENGTH_LONG).show(); + + loginSuccess(context, loginType, callback, "建工", resultString, true); + Toast.makeText(context, resultString, Toast.LENGTH_SHORT).show(); } else { mNotificationManager.notify(Constant.NOTIFICATION_LOGIN_ID, mBuilder.build()); - if (_IP.split("\\.")[0].equals("172") && _IP.split("\\.")[1].equals("17")) - loginJiangong(context, params, loginType, callback, true); + String loginServer = ""; + if (headers != null) { + for (Header header : headers) { + if (header.getName().toLowerCase().equals("location")) + { + Uri uri = Uri.parse(header.getValue()); + loginServer = uri.getAuthority(); + break; + } + } + } + + if (!loginServer.equals("")) + loginWithHeader(context, params, loginType, callback, false, loginServer); else - loginYanchao(context, params, loginType, callback, true); + if (_IP.split("\\.")[0].equals("172") && _IP.split("\\.")[1].equals("17")) + loginJiangong(context, params, loginType, callback, true); + else + loginYanchao(context, params, loginType, callback, true); } } @@ -125,14 +144,66 @@ public void onFailure(int statusCode, Header[] headers, byte[] bytes, Throwable mNotificationManager.notify(Constant.NOTIFICATION_LOGIN_ID, mBuilder.build()); String _IP = getIPAddress(context); - if (_IP.split("\\.")[0].equals("172") && _IP.split("\\.")[1].equals("17")) - loginJiangong(context, params, loginType, callback, true); + String loginServer = ""; + if (headers != null) { + for (Header header : headers) { + if (header.getName().toLowerCase().equals("location")) + { + Uri uri = Uri.parse(header.getValue()); + loginServer = uri.getAuthority(); + break; + } + } + } + + if (!loginServer.equals("")) + loginWithHeader(context, params, loginType, callback, false, loginServer); else - loginYanchao(context, params, loginType, callback, true); + if (_IP.split("\\.")[0].equals("172") && _IP.split("\\.")[1].equals("17")) + loginJiangong(context, params, loginType, callback, true); + else + loginYanchao(context, params, loginType, callback, true); } }); } + private static void loginWithHeader(final Context context, final RequestParams params, final String loginType, + final GeneralCallback callback, final boolean firstCheck, final String loginServer) { + Log.d(Constant.TAG, "loginWithHeader"); + + mClient.post(context, "http://" + loginServer + "/cgi-bin/ace_web_auth.cgi", params, + new AsyncHttpResponseHandler() { + + @Override + public void onSuccess(int statusCode, Header[] headers, byte[] response) { + + mTestClient.get(context, "http://www.example.com", new AsyncHttpResponseHandler() { + @Override + public void onSuccess(int statusCode, Header[] headers, byte[] bytes) { + if (statusCode == 200) + loginSuccess(context, loginType, callback, loginServer, "", true); + else + retryLogin(context, params, loginType, callback, firstCheck, loginServer, statusCode); + } + + @Override + public void onFailure(int statusCode, Header[] headers, byte[] bytes, Throwable e) { + e.printStackTrace(); + + retryLogin(context, params, loginType, callback, firstCheck, loginServer, statusCode); + } + }); + } + + @Override + public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) { + e.printStackTrace(); + + retryLogin(context, params, loginType, callback, firstCheck, loginServer, statusCode); + } + }); + } + private static void loginJiangong(final Context context, final RequestParams params, final String loginType, final GeneralCallback callback, final boolean firstCheck) { Log.d(Constant.TAG, "loginJiangong"); @@ -147,7 +218,7 @@ public void onSuccess(int statusCode, Header[] headers, byte[] response) { @Override public void onSuccess(int statusCode, Header[] headers, byte[] bytes) { if (statusCode == 200) - loginSuccess(context, loginType, callback, "建工"); + loginSuccess(context, loginType, callback, "建工", "", true); else retryLogin(context, params, loginType, callback, firstCheck, "建工", statusCode); } @@ -184,7 +255,7 @@ public void onSuccess(int statusCode, Header[] headers, final byte[] response) { @Override public void onSuccess(int statusCode, Header[] headers, byte[] bytes) { if (statusCode == 200) - loginSuccess(context, loginType, callback, "燕巢"); + loginSuccess(context, loginType, callback, "燕巢", "", true); else retryLogin(context, params, loginType, callback, firstCheck, "燕巢", statusCode); } @@ -232,7 +303,7 @@ private static void retryLogin(Context context, RequestParams params, String log resultString = context.getString(R.string.failed_to_login); mBuilder.setContentTitle(context.getString(R.string.app_name)) .setContentText(resultString).setSmallIcon(R.drawable.ic_stat_login) - .setContentIntent(getDefaultPendingIntent(context)) + .setContentIntent(getFailPendingIntent(context)) .setAutoCancel(true) .setVibrate(new long[]{300, 200, 300, 200}) .setLights(Color.RED, 800, 800) @@ -251,24 +322,25 @@ private static void retryLogin(Context context, RequestParams params, String log tracker.send(new HitBuilders.EventBuilder() .setCategory("UX") .setAction("onFailure") - .setLabel(loginSpace + statusCode + "/" + _IP + "/" + loginType) + .setLabel(loginSpace + "/" + _IP + "/" + loginType) .build()); } - private static void loginSuccess(Context context, String loginType, GeneralCallback callback, String loginSpace) + private static void loginSuccess(Context context, String loginType, GeneralCallback callback, String loginSpace, String resultString, boolean vibrate) { - String resultString; - - switch (loginType) + if (resultString.equals("")) { - case "Student": - resultString = context.getString(R.string.login_successfully); - break; - case "Cyber": - resultString = context.getString(R.string.login_cyber_successfully); - break; - default: - resultString = context.getString(R.string.login_guest_successfully); + switch (loginType) + { + case "Student": + resultString = context.getString(R.string.login_successfully); + break; + case "Cyber": + resultString = context.getString(R.string.login_cyber_successfully); + break; + default: + resultString = context.getString(R.string.login_guest_successfully); + } } if (callback != null) @@ -278,18 +350,23 @@ private static void loginSuccess(Context context, String loginType, GeneralCallb .setContentText(resultString).setSmallIcon(R.drawable.ic_stat_login) .setContentIntent(getDefaultPendingIntent(context)) .setAutoCancel(true) - .setVibrate(new long[]{300, 200, 300, 200}) - .setLights(Color.GREEN, 800, 800) - .setDefaults(Notification.DEFAULT_SOUND) .setProgress(0, 0, false); + mNotificationManager .notify(Constant.NOTIFICATION_LOGIN_ID, mBuilder.build()); - tracker.send(new HitBuilders.EventBuilder() - .setCategory("UX") - .setAction("onSuccess") - .setLabel( loginSpace + "/" + loginType) - .build()); + if (vibrate) + { + mBuilder.setVibrate(new long[]{300, 200, 300, 200}) + .setLights(Color.GREEN, 800, 800) + .setDefaults(Notification.DEFAULT_SOUND); + + tracker.send(new HitBuilders.EventBuilder() + .setCategory("UX") + .setAction("onSuccess") + .setLabel(loginSpace + "/" + loginType) + .build()); + } } public static String getIPAddress(Context context) { @@ -302,19 +379,6 @@ public static String getIPAddress(Context context) { (ip >> 8 & 0xff), (ip >> 16 & 0xff), (ip >> 24 & 0xff)); - /*{ - for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { - NetworkInterface intf = en.nextElement(); - for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { - InetAddress inetAddress = enumIpAddr.nextElement(); - if (!inetAddress.isLoopbackAddress()) { - return inetAddress.getHostAddress().toString(); - } - } - } - } catch (Exception e) { - e.printStackTrace(); - }*/ return "0.0.0.0"; } @@ -322,4 +386,9 @@ public static String getIPAddress(Context context) { private static PendingIntent getDefaultPendingIntent(Context context) { return PendingIntent.getActivity(context, 0, new Intent(), 0); } + + private static PendingIntent getFailPendingIntent(Context context) { + Intent notificationIntent = new Intent(context, MainActivity.class); + return PendingIntent.getActivity(context, 0, notificationIntent, 0); + } } \ No newline at end of file diff --git a/README.md b/README.md index 8dc0b2c..9103bd4 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ KUAS Wifi Auto Login ## 已完成功能: - Wifi Auto Login - Wifi Auto Login (Backgroud) -- GPS定位校區 (目前改用IP網段判斷) +- GPS定位校區 (改用Headers & IP判斷) ## TODO-List: - TANetRoaming支援