From 7546ca6e52cc034735973df21929ec8d53beeb11 Mon Sep 17 00:00:00 2001 From: HearSilent Date: Thu, 18 Jun 2015 20:07:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=99=BB=E5=87=BA=E5=8A=9F?= =?UTF-8?q?=E8=83=BD,=20=E6=B8=9B=E5=B0=91=E5=86=97=E8=B4=85=E7=A8=8B?= =?UTF-8?q?=E5=BC=8F=E7=A2=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- KUASWifiAutoLogin/.idea/workspace.xml | 583 ++++++++---------- .../edu/kuas/wifiautologin/MainActivity.java | 89 ++- .../edu/kuas/wifiautologin/WifiReceiver.java | 23 +- .../wifiautologin/callbacks/Constant.java | 3 +- .../kuas/wifiautologin/libs/LoginHelper.java | 154 ++++- .../tw/edu/kuas/wifiautologin/libs/Utils.java | 22 + .../app/src/main/res/drawable/button_red.xml | 24 + .../main/res/layout-land/activity_main.xml | 42 +- .../app/src/main/res/layout/activity_main.xml | 44 +- .../src/main/res/values-zh-rTW/strings.xml | 4 +- .../app/src/main/res/values/strings.xml | 4 +- .../app/src/main/res/values/styles.xml | 10 +- README.md | 4 +- 13 files changed, 583 insertions(+), 423 deletions(-) create mode 100644 KUASWifiAutoLogin/app/src/main/res/drawable/button_red.xml diff --git a/KUASWifiAutoLogin/.idea/workspace.xml b/KUASWifiAutoLogin/.idea/workspace.xml index dfb68f3..9e9ee3f 100644 --- a/KUASWifiAutoLogin/.idea/workspace.xml +++ b/KUASWifiAutoLogin/.idea/workspace.xml @@ -37,101 +37,40 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - + + - + - - + + - + - + - + @@ -140,21 +79,31 @@ - - + + - - + + - - + + - - + + + + + + + + + + + + @@ -1664,26 +1613,29 @@ @@ -1726,10 +1678,10 @@ - - + + @@ -1751,6 +1703,108 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1765,6 +1819,10 @@ + + @@ -1784,6 +1842,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1829,13 +1937,16 @@ - + + + + @@ -2083,53 +2194,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -2137,16 +2201,6 @@ - - - - - - - - - - @@ -2161,16 +2215,6 @@ - - - - - - - - - - @@ -2189,31 +2233,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - @@ -2224,16 +2243,6 @@ - - - - - - - - - - @@ -2248,16 +2257,6 @@ - - - - - - - - - - @@ -2272,16 +2271,6 @@ - - - - - - - - - - @@ -2296,16 +2285,6 @@ - - - - - - - - - - @@ -2313,14 +2292,6 @@ - - - - - - - - @@ -2328,14 +2299,6 @@ - - - - - - - - @@ -2343,13 +2306,6 @@ - - - - - - - @@ -2389,98 +2345,121 @@ - + - + - + + + + + + + + + + + - + - + - - + - - - - + - - + + - - + + - - + + - - + + + + + + + + - - - + + + + + + - - + + - - + + - + - + + + + + + + + + - + - + - + - + - + @@ -2488,58 +2467,44 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + - + - + - + - + - - + + + + + + + + + + + + \ No newline at end of file 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 a2f5b22..29846ed 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 @@ -31,6 +31,9 @@ public class MainActivity extends Activity { @InjectView(R.id.button_login) Button mLoginButton; + @InjectView(R.id.button_logout) + Button mLogoutButton; + @InjectView(R.id.editText_user) EditText mUsernameEditText; @@ -70,7 +73,10 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { } }); - // init GA + initGA(); + } + + private void initGA() { analytics = GoogleAnalytics.getInstance(this); analytics.setLocalDispatchPeriod(30); @@ -89,40 +95,81 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { } @OnClick (R.id.button_login) - public void submit() { + public void login() { tracker.send(new HitBuilders.EventBuilder() .setCategory("UX") .setAction("Click") .setLabel("Save & Login") .build()); + disableViews(); + saveAndLogin(); + } + + @OnClick(R.id.button_logout) + public void logout() { + tracker.send(new HitBuilders.EventBuilder() + .setCategory("UX") + .setAction("Click") + .setLabel("Logout") + .build()); + + disableViews(); + LoginHelper.logout(this, new GeneralCallback() { + + @Override + public void onSuccess(String message) { + mDebugTextView.setTextColor(getResources().getColor(R.color.md_grey_900)); + showMessage(message, false); + } + + @Override + public void onFail(String reason) { + mDebugTextView.setTextColor(getResources().getColor(R.color.md_red_a700)); + showMessage(reason, true); + } + }); + } + + private void disableViews() + { mDebugTextView.setVisibility(View.GONE); mLoginButton.setEnabled(false); - mTableLayout.setEnabled(false); + mLogoutButton.setEnabled(false); mLoginButton.setBackgroundResource(R.drawable.button_bluegrey); + mLogoutButton.setBackgroundResource(R.drawable.button_bluegrey); + mTableLayout.setEnabled(false); mProgressView.setVisibility(View.VISIBLE); InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mUsernameEditText.getWindowToken(), 0); imm.hideSoftInputFromWindow(mPasswordEditText.getWindowToken(), 0); mUsernameEditText.clearFocus(); mPasswordEditText.clearFocus(); - saveAndLogin(); + } + + private void enableViews() + { + mLoginButton.setEnabled(true); + mLogoutButton.setEnabled(true); + mTableLayout.setEnabled(true); + mProgressView.setVisibility(View.GONE); + mLoginButton.setBackgroundResource(R.drawable.button_blue); + mLogoutButton.setBackgroundResource(R.drawable.button_red); } private void saveAndLogin() { Memory.setString(this, Constant.MEMORY_KEY_USER, mUsernameEditText.getText().toString()); - Memory.setString(this, Constant.MEMORY_KEY_PASSWORD, - mPasswordEditText.getText().toString()); + Memory.setString(this, Constant.MEMORY_KEY_PASSWORD, mPasswordEditText.getText().toString()); String userData; String password = mPasswordEditText.getText().toString(); if (mUsernameEditText.getText().toString().equals("") || mPasswordEditText.getText().toString().equals("")) { - userData = tranUser("0937808285@guest"); + userData = Utils.tranUser("0937808285@guest"); password = "1306"; } else - userData = tranUser(mUsernameEditText.getText().toString()); + userData = Utils.tranUser(mUsernameEditText.getText().toString()); String loginType = userData.split(",")[2]; String ssid = Utils.getCurrentSsid(this); @@ -149,36 +196,12 @@ public void onFail(String reason) { }); } - private String tranUser(String user) - { - if (user.contains("@kuas.edu.tw") || user.contains("@gm.kuas.edu.tw")) - if (user.contains("@kuas.edu.tw")) - return user + ",1,Student"; - else - return user + ",@gm.kuas.edu.tw,Student"; - else if (user.length() == 10 && !user.substring(0,2).equals("09")) - if (Integer.parseInt(user.substring(1,4)) <= 102) - return user + "@kuas.edu.tw" + ",1,Student"; - else - return user + "@gm.kuas.edu.tw" + ",@gm.kuas.edu.tw,Student"; - else if (user.contains("@") && !user.contains("@guest")) - return user + ",,Cyber"; - else - if (user.contains("@guest")) - return user + ",,Guest"; - else - return user + "@guest,,Guest"; - } - private void showMessage(CharSequence message, boolean shake) { mDebugTextView.setVisibility(View.VISIBLE); mDebugTextView.setText(message); if (shake) YoYo.with(Techniques.Shake).duration(700).playOn(mDebugTextView); - mLoginButton.setEnabled(true); - mTableLayout.setEnabled(true); - mProgressView.setVisibility(View.GONE); - mLoginButton.setBackgroundResource(R.drawable.button_blue); + enableViews(); tracker.send(new HitBuilders.EventBuilder() .setCategory("UX") 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 4b2ebcd..dd63ee1 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 @@ -30,7 +30,7 @@ public void onReceive(final Context context, Intent intent) { String user = Memory.getString(context, Constant.MEMORY_KEY_USER, null); String password = Memory.getString(context, Constant.MEMORY_KEY_PASSWORD, null); if (user != null && password != null) { - String userData = tranUser(user); + String userData = Utils.tranUser(user); if (ssid.equals(Constant.EXPECTED_SSIDS[2])) LoginHelper.login(context, userData.split(",")[0], password, "Dorm", null); @@ -53,25 +53,4 @@ public void onReceive(final Context context, Intent intent) { } } } - - private String tranUser(String user) - { - if (user.contains("@kuas.edu.tw") || user.contains("@gm.kuas.edu.tw")) - if (user.contains("@kuas.edu.tw")) - return user + ",1,Student"; - else - return user + ",@gm.kuas.edu.tw,Student"; - else if (user.length() == 10 && !user.substring(0,2).equals("09")) - if (Integer.parseInt(user.substring(1,4)) <= 102) - return user + "@kuas.edu.tw" + ",1,Student"; - else - return user + "@gm.kuas.edu.tw" + ",@gm.kuas.edu.tw,Student"; - else if (user.contains("@") && !user.contains("@guest")) - return user + ",,Cyber"; - else - if (user.contains("@guest")) - return user + ",,Guest"; - else - return user + "@guest,,Guest"; - } } \ No newline at end of file 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 022f3b1..483dadc 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,8 @@ 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 = 8000; + public static final int TIMEOUT_LOGOUT = 8000; + public static final int TIMEOUT_LOGIN = 6000; public static final String JIANGONG_WIFI_SERVER = "172.16.61.253"; public static final String YANCHAO_WIFI_SERVER = "172.16.109.253"; 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 4bf7c7d..4329046 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 @@ -46,15 +46,13 @@ public class LoginHelper { private static AsyncHttpClient init() { AsyncHttpClient client = new AsyncHttpClient(); - client.setTimeout(Constant.TIMEOUT); client.setUserAgent(Constant.USER_AGENT); client.setEnableRedirects(false); return client; } - public static void login(final Context context, String user, String password, - final String loginType, final GeneralCallback callback) { - // init GA + private static void initGA(Context context) + { analytics = GoogleAnalytics.getInstance(context); analytics.setLocalDispatchPeriod(30); @@ -64,19 +62,15 @@ public static void login(final Context context, String user, String password, tracker.enableAutoActivityTracking(true); tracker.setScreenName("LoginHelper"); + } + + public static void login(final Context context, String user, String password, + final String loginType, final GeneralCallback callback) { + if (!checkSSID(context, callback)) return; + + initGA(context); String currentSsid = Utils.getCurrentSsid(context); - if (currentSsid == null || !Utils.isExpectedSsid(currentSsid)) { - if (currentSsid == null) { - currentSsid = context.getString(R.string.no_wifi_connection); - if (callback != null) - callback.onFail(currentSsid); - return; - } - if (callback != null) - callback.onFail(String.format(context.getString(R.string.ssid_no_support), currentSsid)); - return; - } final LinkedHashMap paramsMap = new LinkedHashMap<>(); paramsMap.put("username", user); @@ -91,6 +85,7 @@ public static void login(final Context context, String user, String password, String.format(context.getString(R.string.login_to_ssid), currentSsid)) .setSmallIcon(R.drawable.ic_stat_login).setProgress(0, 0, true).setOngoing(false); + mClient.setTimeout(Constant.TIMEOUT_LOGIN); mClient.get(context, "http://www.example.com", new AsyncHttpResponseHandler() { String _IP = getIPAddress(context); String loginServer = ""; @@ -132,11 +127,11 @@ public void onSuccess(int statusCode, Header[] headers, byte[] bytes) { } if (!loginServer.equals("")) - loginWithHeader(context, paramsMap, loginType, callback, false, loginServer); + login(context, paramsMap, loginType, callback, false, loginServer); else if (_IP.split("\\.")[0].equals("172") && _IP.split("\\.")[1].equals("17")) - loginWithHeader(context, paramsMap, loginType, callback, true, Constant.JIANGONG_WIFI_SERVER); + login(context, paramsMap, loginType, callback, true, Constant.JIANGONG_WIFI_SERVER); else - loginWithHeader(context, paramsMap, loginType, callback, true, Constant.YANCHAO_WIFI_SERVER); + login(context, paramsMap, loginType, callback, true, Constant.YANCHAO_WIFI_SERVER); } } @@ -155,16 +150,16 @@ public void onFailure(int statusCode, Header[] headers, byte[] bytes, Throwable } if (!loginServer.equals("")) - loginWithHeader(context, paramsMap, loginType, callback, false, loginServer); + login(context, paramsMap, loginType, callback, false, loginServer); else if (_IP.split("\\.")[0].equals("172") && _IP.split("\\.")[1].equals("17")) - loginWithHeader(context, paramsMap, loginType, callback, true, Constant.JIANGONG_WIFI_SERVER); + login(context, paramsMap, loginType, callback, true, Constant.JIANGONG_WIFI_SERVER); else - loginWithHeader(context, paramsMap, loginType, callback, true, Constant.YANCHAO_WIFI_SERVER); + login(context, paramsMap, loginType, callback, true, Constant.YANCHAO_WIFI_SERVER); } }); } - private static void loginWithHeader(final Context context, final LinkedHashMap paramsMap, final String loginType, + private static void login(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() { @@ -174,7 +169,7 @@ public void run() { Connection.Response response = Jsoup.connect(String.format("http://%s/cgi-bin/ace_web_auth.cgi", loginServer)) .data(paramsMap) .userAgent(Constant.USER_AGENT) - .timeout(Constant.TIMEOUT) + .timeout(Constant.TIMEOUT_LOGIN) .followRedirects(false) .ignoreContentType(true) .ignoreHttpErrors(true) @@ -247,9 +242,96 @@ public void run() { }).start(); } + public static void logout(final Context context, final GeneralCallback callback) { + if (!checkSSID(context, callback)) return; + initGA(context); + + mClient.setTimeout(Constant.TIMEOUT_LOGOUT); + mClient.get(String.format("http://%s/", Constant.JIANGONG_WIFI_SERVER), + new AsyncHttpResponseHandler() { + String loginServer = ""; + + @Override + public void onSuccess(int statusCode, Header[] headers, byte[] response) { + if (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) { + 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("")) { + if (loginServer.contains("login_online")) + logout(context, callback, Constant.JIANGONG_WIFI_SERVER); + else if (loginServer.contains("auth_entry")) { + tracker.send(new HitBuilders.EventBuilder() + .setCategory("onFailure") + .setAction("Logout") + .setLabel("Already logged out") + .build()); + callback.onFail(context.getText(R.string.already_logged_out).toString()); + } else + logout(context, callback, Constant.YANCHAO_WIFI_SERVER); + } else { + callback.onFail(context.getText(R.string.failed_to_logout).toString()); + tracker.send(new HitBuilders.EventBuilder() + .setCategory("onFailure") + .setAction("Logout") + .setLabel("Null headers") + .build()); + } + } else { + callback.onFail(context.getText(R.string.failed_to_logout).toString()); + tracker.send(new HitBuilders.EventBuilder() + .setCategory("onFailure") + .setAction("Logout") + .setLabel(Integer.toString(statusCode)) + .build()); + } + } + + @Override + public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, + Throwable e) { + + callback.onFail(context.getText(R.string.login_timeout).toString()); + tracker.send(new HitBuilders.EventBuilder() + .setCategory("onFailure") + .setAction("Logout") + .setLabel("Time Out") + .build()); + } + }); + } + + private static void logout(final Context context, final GeneralCallback callback, final String logoutServer) { + mClient.get(String.format("http://%s/cgi-bin/ace_web_auth.cgi?logout", logoutServer), + new AsyncHttpResponseHandler() { + + @Override + public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { + callback.onSuccess(context.getText(R.string.logout_successful).toString()); + mNotificationManager.cancel(Constant.NOTIFICATION_LOGIN_ID); + + tracker.send(new HitBuilders.EventBuilder() + .setCategory("onSuccess") + .setAction("Logout") + .setLabel("logout successful") + .build()); + } + + @Override + public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { + callback.onFail(context.getText(R.string.failed_to_logout).toString()); + } + }); + } + private static void retryLogin(Context context, final LinkedHashMap paramsMap, String loginType, - GeneralCallback callback, boolean retry, String loginServer, String resultString, String response) - { + GeneralCallback callback, boolean retry, String loginServer, String resultString, String response) { if (retry) { tracker.send(new HitBuilders.EventBuilder() .setCategory("retryLogin") @@ -258,9 +340,9 @@ private static void retryLogin(Context context, final LinkedHashMap + + + + + + + + + + \ No newline at end of file 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 b4a3082..43b0cb8 100644 --- a/KUASWifiAutoLogin/app/src/main/res/layout-land/activity_main.xml +++ b/KUASWifiAutoLogin/app/src/main/res/layout-land/activity_main.xml @@ -77,17 +77,37 @@ - + + + + + + + - + + + + + + + + + 漫遊登入成功,歡迎來到高應大%s校區! 宿舍登入成功,歡迎回到高應大%s宿舍! 您已經登入或是有可用網路了。 - 登出成功。 + 登出成功。 已連接到 %s。 不支援的 SSID:%s。 - 正在登入 %s…. + 正在登入 %s… 已經登入了。 登入失敗,請再嘗試一次。 請求 Wi-Fi 伺服器的連線逾時。 diff --git a/KUASWifiAutoLogin/app/src/main/res/values/strings.xml b/KUASWifiAutoLogin/app/src/main/res/values/strings.xml index 6e0af5f..a2d8217 100644 --- a/KUASWifiAutoLogin/app/src/main/res/values/strings.xml +++ b/KUASWifiAutoLogin/app/src/main/res/values/strings.xml @@ -16,10 +16,10 @@ Cyber login successful,Welcome to KUAS (%s)! Dorm login successful,Welcome back to KUAS Dorm (%s)! Already logged in. - Logout successful. + Logout successful. Connected to %s. SSID not supported:%s. - Login to %s…. + Login to %s… Already logged in. Failed to login. Connection Timeout. diff --git a/KUASWifiAutoLogin/app/src/main/res/values/styles.xml b/KUASWifiAutoLogin/app/src/main/res/values/styles.xml index 365364a..0371692 100644 --- a/KUASWifiAutoLogin/app/src/main/res/values/styles.xml +++ b/KUASWifiAutoLogin/app/src/main/res/values/styles.xml @@ -15,11 +15,19 @@ #00000000 - + + \ No newline at end of file diff --git a/README.md b/README.md index 9103bd4..91ee5dd 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,13 @@ KUAS Wifi Auto Login =========== ## 已完成功能: -- Wifi Auto Login +- Login +- Logout - Wifi Auto Login (Backgroud) - GPS定位校區 (改用Headers & IP判斷) ## TODO-List: - TANetRoaming支援 -- Logout - 流量統計 ## 許願池: