diff --git a/KUASWifiAutoLogin/.idea/workspace.xml b/KUASWifiAutoLogin/.idea/workspace.xml index 4ad2117..dfb68f3 100644 --- a/KUASWifiAutoLogin/.idea/workspace.xml +++ b/KUASWifiAutoLogin/.idea/workspace.xml @@ -6,7 +6,8 @@ - @@ -36,43 +37,71 @@ - + - - + + - - + + - + - - + + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + - + - + @@ -83,7 +112,49 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -98,6 +169,11 @@ + + @@ -1596,6 +1726,9 @@ + + + @@ -1618,64 +1751,6 @@ - - - @@ -1773,8 +1827,12 @@ - + + + + + @@ -1985,7 +2043,7 @@ - + @@ -2044,7 +2102,31 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2056,14 +2138,14 @@ + + + - - - @@ -2080,14 +2162,14 @@ + + + - - - @@ -2107,7 +2189,31 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2119,14 +2225,14 @@ + + + - - - @@ -2143,14 +2249,14 @@ + + + - - - @@ -2167,14 +2273,14 @@ + + + - - - @@ -2191,25 +2297,19 @@ + + + - - - - - - - - - @@ -2236,30 +2336,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - @@ -2274,17 +2350,6 @@ - - - - - - - - - - - @@ -2304,11 +2369,6 @@ - - - - - @@ -2346,38 +2406,81 @@ - + - + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + - + - + - + - + @@ -2385,26 +2488,55 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - + + - - + + + + + + + - + - - + + diff --git a/KUASWifiAutoLogin/app/build.gradle b/KUASWifiAutoLogin/app/build.gradle index e9942ec..e2d39d2 100644 --- a/KUASWifiAutoLogin/app/build.gradle +++ b/KUASWifiAutoLogin/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "tw.edu.kuas.wifiautologin" minSdkVersion 9 targetSdkVersion 22 - versionCode 212 - versionName "2.1.2" + versionCode 213 + versionName "2.1.3" } buildTypes { release { diff --git a/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/MainActivity.java b/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/MainActivity.java index 287410c..a2f5b22 100644 --- a/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/MainActivity.java +++ b/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/MainActivity.java @@ -1,7 +1,6 @@ package tw.edu.kuas.wifiautologin; import android.app.Activity; -import android.graphics.Color; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; @@ -132,20 +131,20 @@ private void saveAndLogin() { if (ssid.equals(Constant.EXPECTED_SSIDS[2])) loginType = "Dorm"; - LoginHelper.login(this, userData.split(",")[1], userData.split(",")[0], + LoginHelper.login(this, userData.split(",")[0], password, loginType, new GeneralCallback() { @Override public void onSuccess(String message) { - mDebugTextView.setTextColor(Color.DKGRAY); + mDebugTextView.setTextColor(getResources().getColor(R.color.md_grey_900)); showMessage(message, false); finish(); } @Override public void onFail(String reason) { - mDebugTextView.setTextColor(Color.RED); - showMessage(reason , true); + mDebugTextView.setTextColor(getResources().getColor(R.color.md_red_a700)); + showMessage(reason, true); } }); } diff --git a/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/WifiReceiver.java b/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/WifiReceiver.java index 4760345..4b2ebcd 100644 --- a/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/WifiReceiver.java +++ b/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/WifiReceiver.java @@ -32,10 +32,10 @@ public void onReceive(final Context context, Intent intent) { if (user != null && password != null) { String userData = tranUser(user); if (ssid.equals(Constant.EXPECTED_SSIDS[2])) - LoginHelper.login(context, userData.split(",")[1], userData.split(",")[0], + LoginHelper.login(context, userData.split(",")[0], password, "Dorm", null); else - LoginHelper.login(context, userData.split(",")[1], userData.split(",")[0], + LoginHelper.login(context, userData.split(",")[0], password, userData.split(",")[2], null); } } diff --git a/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/callbacks/Constant.java b/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/callbacks/Constant.java index ecf7d8c..022f3b1 100644 --- a/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/callbacks/Constant.java +++ b/KUASWifiAutoLogin/app/src/main/java/tw/edu/kuas/wifiautologin/callbacks/Constant.java @@ -6,7 +6,7 @@ public class Constant { public static final String TAG = "HearSilent"; public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0"; - public static final int TIMEOUT = 5000; + public static final int TIMEOUT = 8000; public static final String JIANGONG_WIFI_SERVER = "172.16.61.253"; public static final String YANCHAO_WIFI_SERVER = "172.16.109.253"; @@ -16,5 +16,6 @@ public class Constant { // Memory (Shared Preferences) Keys public static final String MEMORY_KEY_USER = "MEMORY_KEY_USER"; public static final String MEMORY_KEY_PASSWORD = "MEMORY_KEY_PASSWORD"; + public static final String MEMORY_KEY_ERRORTIMES = "MEMORY_KEY_ERRORTIMES"; } 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 ac805ac..4bf7c7d 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 @@ -22,17 +22,18 @@ import com.loopj.android.http.AsyncHttpResponseHandler; import org.apache.http.Header; +import org.apache.http.HttpStatus; import org.jsoup.Connection; import org.jsoup.Jsoup; import java.io.IOException; import java.util.LinkedHashMap; -import java.util.Map; 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; +import tw.edu.kuas.wifiautologin.callbacks.Memory; public class LoginHelper { private static AsyncHttpClient mClient = init(); @@ -51,7 +52,7 @@ private static AsyncHttpClient init() { return client; } - public static void login(final Context context, String idType, String user, String password, + public static void login(final Context context, String user, String password, final String loginType, final GeneralCallback callback) { // init GA analytics = GoogleAnalytics.getInstance(context); @@ -77,7 +78,7 @@ public static void login(final Context context, String idType, String user, Stri return; } - final Map paramsMap = new LinkedHashMap<>(); + final LinkedHashMap paramsMap = new LinkedHashMap<>(); paramsMap.put("username", user); paramsMap.put("userpwd", password); paramsMap.put("login", ""); @@ -86,7 +87,7 @@ public static void login(final Context context, String idType, String user, Stri mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); mBuilder = new NotificationCompat.Builder(context); - mBuilder.setContentTitle(context.getString(R.string.app_name)).setContentText( + mBuilder.setContentTitle(context.getString(R.string.kuas_wifi_auto_login)).setContentText( String.format(context.getString(R.string.login_to_ssid), currentSsid)) .setSmallIcon(R.drawable.ic_stat_login).setProgress(0, 0, true).setOngoing(false); @@ -98,20 +99,25 @@ public static void login(final Context context, String idType, String user, Stri public void onSuccess(int statusCode, Header[] headers, byte[] bytes) { String resultString = context.getString(R.string.login_ready); String _IP = getIPAddress(context); - if (statusCode == 200) { + if (statusCode == HttpStatus.SC_OK) { Log.d(Constant.TAG, "Already Login."); - tracker.send(new HitBuilders.EventBuilder() - .setCategory("alreadyLogin") - .setAction("onSuccess") - .setLabel(_IP + "/" + loginType) - .build()); - if (callback != null) + { + loginSuccess(context, loginType, callback, + (_IP.split("\\.")[0].equals("172") && _IP.split("\\.")[1].equals("17")) ? Constant.JIANGONG_WIFI_SERVER : Constant.YANCHAO_WIFI_SERVER + , resultString, false); + Toast.makeText(context, resultString, Toast.LENGTH_SHORT).show(); + + tracker.send(new HitBuilders.EventBuilder() + .setCategory("onSuccess") + .setAction("alreadyLogin") + .setLabel(_IP + "/" + loginType) + .build()); + callback.onSuccess(resultString); + } - loginSuccess(context, loginType, callback, "建工", resultString, false); - Toast.makeText(context, resultString, Toast.LENGTH_SHORT).show(); } else { mNotificationManager.notify(Constant.NOTIFICATION_LOGIN_ID, mBuilder.build()); @@ -158,7 +164,7 @@ else if (_IP.split("\\.")[0].equals("172") && _IP.split("\\.")[1].equals("17")) }); } - private static void loginWithHeader(final Context context, final Map paramsMap, final String loginType, + private static void loginWithHeader(final Context context, final LinkedHashMap paramsMap, final String loginType, final GeneralCallback callback, final boolean retry, final String loginServer) { final Handler refresh = new Handler(Looper.getMainLooper()); new Thread(new Runnable() { @@ -175,34 +181,56 @@ public void run() { .method(Connection.Method.POST) .execute(); - final int statusCode = response.statusCode(); + final String _response = response.body(); + final int _statusCode = response.statusCode(); - if(statusCode == 200) { + if(_statusCode == HttpStatus.SC_OK) { refresh.post(new Runnable() { public void run() { mClient.get(context, "http://www.example.com/", new AsyncHttpResponseHandler() { @Override public void onSuccess(final int statusCode, Header[] headers, byte[] bytes) { - if (statusCode == 200) + if (statusCode == HttpStatus.SC_OK) loginSuccess(context, loginType, callback, loginServer, "", true); else - retryLogin(context, paramsMap, loginType, callback, retry, loginServer, ""); + retryLogin(context, paramsMap, loginType, callback, retry, loginServer, "", + retry ? "" : _response); } @Override public void onFailure(final int statusCode, Header[] headers, byte[] bytes, Throwable e) { e.printStackTrace(); - retryLogin(context, paramsMap, loginType, callback, retry, loginServer, ""); + retryLogin(context, paramsMap, loginType, callback, retry, loginServer, "", + retry ? "" : _response); } }); } }); } else { + if (_statusCode == HttpStatus.SC_MOVED_TEMPORARILY) + Log.d(Constant.TAG, Integer.toString(_statusCode)); refresh.post(new Runnable() { public void run() { - retryLogin(context, paramsMap, loginType, callback, retry, loginServer, ""); + mClient.get(context, "http://www.example.com/", new AsyncHttpResponseHandler() { + @Override + public void onSuccess(final int statusCode, Header[] headers, byte[] bytes) { + if (statusCode == HttpStatus.SC_OK) + loginSuccess(context, loginType, callback, loginServer, "", true); + else + retryLogin(context, paramsMap, loginType, callback, retry, loginServer, "", + retry ? "" : _response); + } + + @Override + public void onFailure(final int statusCode, Header[] headers, byte[] bytes, Throwable e) { + e.printStackTrace(); + + retryLogin(context, paramsMap, loginType, callback, retry, loginServer, "", + retry ? "" : _response); + } + }); } }); } @@ -211,7 +239,7 @@ public void run() { refresh.post(new Runnable() { public void run() { retryLogin(context, paramsMap, loginType, callback, retry, loginServer, - context.getString(R.string.login_timeout)); + context.getString(R.string.login_timeout), "Time Out"); } }); } @@ -219,8 +247,8 @@ public void run() { }).start(); } - private static void retryLogin(Context context, final Map paramsMap, String loginType, - GeneralCallback callback, boolean retry, String loginServer, String resultString) + private static void retryLogin(Context context, final LinkedHashMap paramsMap, String loginType, + GeneralCallback callback, boolean retry, String loginServer, String resultString, String response) { if (retry) { tracker.send(new HitBuilders.EventBuilder() @@ -238,7 +266,8 @@ private static void retryLogin(Context context, final Map params if (resultString.equals("")) resultString = context.getString(R.string.failed_to_login); - mBuilder.setContentTitle(context.getString(R.string.app_name)) + + mBuilder.setContentTitle(context.getString(R.string.kuas_wifi_auto_login)) .setContentText(resultString).setSmallIcon(R.drawable.ic_stat_login) .setContentIntent(getFailPendingIntent(context)) .setAutoCancel(true) @@ -246,20 +275,25 @@ private static void retryLogin(Context context, final Map params .setLights(Color.RED, 800, 800) .setProgress(0, 0, false); - if (callback != null) { + if (callback != null) callback.onFail(resultString); - } - // Show error details in the expanded notification + + int errorTimes = Memory.getInt(context, Constant.MEMORY_KEY_ERRORTIMES, 0); + mBuilder.setStyle(new NotificationCompat.BigTextStyle() .bigText(resultString)); - mNotificationManager - .notify(Constant.NOTIFICATION_LOGIN_ID, mBuilder.build()); + if (!(callback == null && errorTimes >= 3)) + mNotificationManager + .notify(Constant.NOTIFICATION_LOGIN_ID, mBuilder.build()); + + errorTimes++; + Memory.setInt(context, Constant.MEMORY_KEY_ERRORTIMES, errorTimes); tracker.send(new HitBuilders.EventBuilder() - .setCategory("UX") - .setAction("onFailure") - .setLabel((loginServer.equals(Constant.JIANGONG_WIFI_SERVER) ? "建工" : "燕巢") + "/" + loginType) + .setCategory("onFailure") + .setAction((loginServer.equals(Constant.JIANGONG_WIFI_SERVER) ? "建工" : "燕巢") + "/" + loginType) + .setLabel(response) .build()); } @@ -293,14 +327,21 @@ private static void loginSuccess(Context context, String loginType, GeneralCallb } if (callback != null) - callback.onSuccess(resultString); + { + if (resultString.equals(context.getString(R.string.login_ready))) + callback.onSuccess(resultString); + else + callback.onSuccess(resultString.split(",")[0] + ",\n" + resultString.split(",")[1]); + } - mBuilder.setContentTitle(context.getString(R.string.app_name)) + mBuilder.setContentTitle(context.getString(R.string.kuas_wifi_auto_login)) .setContentText(resultString).setSmallIcon(R.drawable.ic_stat_login) .setContentIntent(getDefaultPendingIntent(context)) .setAutoCancel(true) .setProgress(0, 0, false); + Memory.setInt(context, Constant.MEMORY_KEY_ERRORTIMES, 0); + if (notify) { mBuilder.setVibrate(new long[]{300, 200, 300, 200}) @@ -308,14 +349,18 @@ private static void loginSuccess(Context context, String loginType, GeneralCallb .setDefaults(Notification.DEFAULT_SOUND); tracker.send(new HitBuilders.EventBuilder() - .setCategory("UX") - .setAction("onSuccess") - .setLabel((loginServer.equals(Constant.JIANGONG_WIFI_SERVER) ? "建工" : "燕巢") + "/" + loginType) + .setCategory("onSuccess") + .setAction((loginServer.equals(Constant.JIANGONG_WIFI_SERVER) ? "建工" : "燕巢")) + .setLabel(loginType) .build()); } - mBuilder.setStyle(new NotificationCompat.BigTextStyle() - .bigText(resultString)); + if (resultString.equals(context.getString(R.string.login_ready))) + mBuilder.setStyle(new NotificationCompat.BigTextStyle() + .bigText(resultString)); + else + mBuilder.setStyle(new NotificationCompat.BigTextStyle() + .bigText(resultString.split(",")[0] + ",\n" + resultString.split(",")[1])); mNotificationManager .notify(Constant.NOTIFICATION_LOGIN_ID, mBuilder.build()); diff --git a/KUASWifiAutoLogin/app/src/main/res/layout-land/activity_main.xml b/KUASWifiAutoLogin/app/src/main/res/layout-land/activity_main.xml index 8c4f299..b4a3082 100644 --- a/KUASWifiAutoLogin/app/src/main/res/layout-land/activity_main.xml +++ b/KUASWifiAutoLogin/app/src/main/res/layout-land/activity_main.xml @@ -25,7 +25,7 @@ @@ -98,9 +98,8 @@ app:pv_circular="false" app:pv_progressStyle="@style/ColorsLinearProgressDrawableStyle" app:pv_progressMode="indeterminate" - android:layout_marginTop="8sp" - android:layout_marginRight="20sp" - android:layout_marginLeft="20sp" + android:layout_marginRight="21sp" + android:layout_marginLeft="21sp" android:visibility="gone"/> diff --git a/KUASWifiAutoLogin/app/src/main/res/layout/activity_main.xml b/KUASWifiAutoLogin/app/src/main/res/layout/activity_main.xml index a8ee597..5eac9fe 100644 --- a/KUASWifiAutoLogin/app/src/main/res/layout/activity_main.xml +++ b/KUASWifiAutoLogin/app/src/main/res/layout/activity_main.xml @@ -29,7 +29,7 @@ @@ -88,9 +88,8 @@ app:pv_circular="false" app:pv_progressStyle="@style/ColorsLinearProgressDrawableStyle" app:pv_progressMode="indeterminate" - android:layout_marginTop="8sp" - android:layout_marginRight="40sp" - android:layout_marginLeft="40sp" + android:layout_marginRight="41sp" + android:layout_marginLeft="41sp" android:visibility="gone"/> diff --git a/KUASWifiAutoLogin/app/src/main/res/values-zh-rTW/strings.xml b/KUASWifiAutoLogin/app/src/main/res/values-zh-rTW/strings.xml index 8023938..e13a36a 100644 --- a/KUASWifiAutoLogin/app/src/main/res/values-zh-rTW/strings.xml +++ b/KUASWifiAutoLogin/app/src/main/res/values-zh-rTW/strings.xml @@ -7,14 +7,14 @@ 密碼 儲存並登入 登出 - 高應無線通 + 高應無線通 登入成功。 建工 燕巢 - Wi-Fi 登入成功, 歡迎來到高應大%s校區! - 賓客登入成功, 歡迎來到高應大%s校區! - 漫遊登入成功, 歡迎來到高應大%s校區! - 宿舍登入成功, 歡迎回到高應大%s宿舍! + Wi-Fi登入成功,歡迎來到高應大%s校區! + 賓客登入成功,歡迎來到高應大%s校區! + 漫遊登入成功,歡迎來到高應大%s校區! + 宿舍登入成功,歡迎回到高應大%s宿舍! 您已經登入或是有可用網路了。 登出成功。 已連接到 %s。 diff --git a/KUASWifiAutoLogin/app/src/main/res/values/strings.xml b/KUASWifiAutoLogin/app/src/main/res/values/strings.xml index 2257814..6e0af5f 100644 --- a/KUASWifiAutoLogin/app/src/main/res/values/strings.xml +++ b/KUASWifiAutoLogin/app/src/main/res/values/strings.xml @@ -7,14 +7,14 @@ Password Save & Login Logout - KUAS Wi-Fi Login + KUAS WiFi Auto Login Login successful. JianGong YanChao - Wi-Fi login successful, Welcome to KUAS %s! - Guest login successful, Welcome to KUAS %s! - Cyber login successful, Welcome to KUAS %s! - Dorm login successful, Welcome back to KUAS %s Dorm! + Wi-Fi login successful,Welcome to KUAS (%s)! + Guest login successful,Welcome to KUAS (%s)! + Cyber login successful,Welcome to KUAS (%s)! + Dorm login successful,Welcome back to KUAS Dorm (%s)! Already logged in. Logout successful. Connected to %s.