diff --git a/Android/app/build.gradle b/Android/app/build.gradle index 91c4a06..5be388a 100644 --- a/Android/app/build.gradle +++ b/Android/app/build.gradle @@ -22,8 +22,8 @@ android { applicationId "tw.edu.kuas.wifiautologin" minSdkVersion 9 targetSdkVersion 23 - versionCode 220 - versionName "2.2.0" + versionCode 221 + versionName "2.2.1" } buildTypes { debug { diff --git a/Android/app/src/main/java/tw/edu/kuas/wifiautologin/MainActivity.java b/Android/app/src/main/java/tw/edu/kuas/wifiautologin/MainActivity.java index 6cbf726..be820a2 100644 --- a/Android/app/src/main/java/tw/edu/kuas/wifiautologin/MainActivity.java +++ b/Android/app/src/main/java/tw/edu/kuas/wifiautologin/MainActivity.java @@ -81,7 +81,6 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { PorterDuff.Mode.SRC_IN); initGA(); - Utils.forceUseWifi(this); } private void initGA() { diff --git a/Android/app/src/main/java/tw/edu/kuas/wifiautologin/WifiReceiver.java b/Android/app/src/main/java/tw/edu/kuas/wifiautologin/WifiReceiver.java index 67c13f7..e15c254 100644 --- a/Android/app/src/main/java/tw/edu/kuas/wifiautologin/WifiReceiver.java +++ b/Android/app/src/main/java/tw/edu/kuas/wifiautologin/WifiReceiver.java @@ -33,7 +33,6 @@ public void onReceive(final Context context, Intent intent) { String action = intent.getAction(); if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { - Utils.forceUseWifi(context); initGA(context); NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); @@ -60,14 +59,14 @@ public void onReceive(final Context context, Intent intent) { private void login(Context context, UserModel model) { LoginHelper.login(context, model, new GeneralCallback() { @Override - public void onSuccess(final String message) { + public void onSuccess(String message) { mTracker.send( new HitBuilders.EventBuilder().setCategory("logout").setAction("success") .setLabel(message).build()); } @Override - public void onFail(final String reason) { + public void onFail(String reason) { mTracker.send(new HitBuilders.EventBuilder().setCategory("logout").setAction("fail") .setLabel(reason).build()); } diff --git a/Android/app/src/main/java/tw/edu/kuas/wifiautologin/callbacks/GeneralCallback.java b/Android/app/src/main/java/tw/edu/kuas/wifiautologin/callbacks/GeneralCallback.java index fe96d9f..19d4d70 100644 --- a/Android/app/src/main/java/tw/edu/kuas/wifiautologin/callbacks/GeneralCallback.java +++ b/Android/app/src/main/java/tw/edu/kuas/wifiautologin/callbacks/GeneralCallback.java @@ -1,6 +1,25 @@ package tw.edu.kuas.wifiautologin.callbacks; +import android.content.Context; + +import tw.edu.kuas.wifiautologin.libs.Utils; + public abstract class GeneralCallback { + public void onSuccess(Context context, String message) { + Utils.resetDefaultNetwork(context); + onSuccess(message); + } + + public void onFail(Context context, String reason) { + Utils.resetDefaultNetwork(context); + onFail(reason); + } + + public void onAlready(Context context) { + Utils.resetDefaultNetwork(context); + onAlready(); + } + public abstract void onSuccess(String message); public abstract void onFail(String reason); diff --git a/Android/app/src/main/java/tw/edu/kuas/wifiautologin/libs/LoginHelper.java b/Android/app/src/main/java/tw/edu/kuas/wifiautologin/libs/LoginHelper.java index 27ec1f3..290c80a 100644 --- a/Android/app/src/main/java/tw/edu/kuas/wifiautologin/libs/LoginHelper.java +++ b/Android/app/src/main/java/tw/edu/kuas/wifiautologin/libs/LoginHelper.java @@ -44,11 +44,13 @@ private static void initGA(Context context) { } public static void login(final Context context, final UserModel model, - final GeneralCallback callback) { + @NonNull final GeneralCallback callback) { if (!checkSSID(context, callback)) { return; } + Utils.requestNetwork(context); + final RequestBody requestBody = new FormBody.Builder().add("username", model.username).add("userpwd", model.userpwd) .add("login", "").add("orig_referer", "").build(); @@ -65,9 +67,7 @@ public void onFailure(Call call, IOException e) { public void onResponse(Call call, Response response) { call.cancel(); if (response.code() == 200) { - if (callback != null) { - callback.onAlready(); - } + callback.onAlready(context); if (!(context instanceof MainActivity)) { NotificationHelper.createNotification(context, context.getString(R.string.already_logged_in), false, false, @@ -85,16 +85,14 @@ public void onResponse(Call call, Response response) { }); } - private static boolean checkSSID(Context context, GeneralCallback callback) { + private static boolean checkSSID(Context context, @NonNull GeneralCallback callback) { String ssid = Utils.getCurrentSSID(context); if (TextUtils.isEmpty(ssid) || !Utils.isExpectedSSID(ssid)) { - if (callback != null) { - if (TextUtils.isEmpty(ssid)) { - callback.onFail(context.getString(R.string.no_wifi_connection)); - } else { - callback.onFail( - String.format(context.getString(R.string.ssid_no_support), ssid)); - } + if (TextUtils.isEmpty(ssid)) { + callback.onFail(context, context.getString(R.string.no_wifi_connection)); + } else { + callback.onFail(context, + String.format(context.getString(R.string.ssid_no_support), ssid)); } return false; } @@ -103,7 +101,7 @@ private static boolean checkSSID(Context context, GeneralCallback callback) { private static void login(final Context context, final String location, final UserModel.LoginType loginType, final RequestBody requestBody, - final GeneralCallback callback) { + @NonNull final GeneralCallback callback) { String url = String.format(Locale.getDefault(), LOGIN_URL, location); @@ -152,6 +150,7 @@ public static void logout(final Context context, final boolean recheck, return; } + Utils.requestNetwork(context); initGA(context); String url = String.format(Locale.getDefault(), TEST_LOGOUT_URL, @@ -162,7 +161,7 @@ public static void logout(final Context context, final boolean recheck, mClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { - callback.onFail(context.getString(R.string.failed_to_logout)); + callback.onFail(context, context.getString(R.string.failed_to_logout)); } @Override @@ -171,7 +170,7 @@ public void onResponse(Call call, Response response) { if (response.code() == 302) { checkLogoutLocation(context, response.header("location"), recheck, callback); } else { - callback.onFail(context.getString(R.string.failed_to_logout)); + callback.onFail(context, context.getString(R.string.failed_to_logout)); } } }); @@ -186,7 +185,7 @@ private static void checkLogoutLocation(Context context, String location, boolea if (recheck) { logout(context, false, callback); } else { - callback.onAlready(); + callback.onAlready(context); } } else { mTracker.send( @@ -207,13 +206,13 @@ private static void logout(final Context context, String location, mClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { - callback.onFail(context.getString(R.string.failed_to_logout)); + callback.onFail(context, context.getString(R.string.failed_to_logout)); } @Override public void onResponse(Call call, Response response) { if (response.code() == 200) { - callback.onSuccess(context.getString(R.string.logout_successful)); + callback.onSuccess(context, context.getString(R.string.logout_successful)); NotificationHelper.cancelNotification(context, Constant.NOTIFICATION_LOGIN_ID); NotificationHelper .cancelNotification(context, Constant.NOTIFICATION_SUCCESS_ID); @@ -221,7 +220,7 @@ public void onResponse(Call call, Response response) { NotificationHelper .cancelNotification(context, Constant.NOTIFICATION_ALREADY_ID); } else { - callback.onFail(context.getString(R.string.failed_to_logout)); + callback.onFail(context, context.getString(R.string.failed_to_logout)); } } }); @@ -237,7 +236,8 @@ private static void showLoginNotification(Context context, UserModel.LoginType l } private static void loginSuccess(Context context, String location, - UserModel.LoginType loginType, GeneralCallback callback) { + UserModel.LoginType loginType, + @NonNull GeneralCallback callback) { String campus = location.equals(Constant.JIANGONG_WIFI_SERVER) ? context.getString(R.string.jiangong) : context.getString(R.string.yanchao); String result; @@ -265,16 +265,13 @@ private static void loginSuccess(Context context, String location, NotificationHelper.cancelNotification(context, Constant.NOTIFICATION_FAIL_ID); NotificationHelper.cancelNotification(context, Constant.NOTIFICATION_ALREADY_ID); - if (callback != null) { - callback.onSuccess(result); - } + callback.onSuccess(context, result); } - private static void loginFail(Context context, String reason, GeneralCallback callback) { + private static void loginFail(Context context, String reason, + @NonNull GeneralCallback callback) { NotificationHelper.cancelNotification(context, Constant.NOTIFICATION_LOGIN_ID); - if (callback != null) { - callback.onFail(reason); - } + callback.onFail(context, reason); if (!(context instanceof MainActivity)) { NotificationHelper.createNotification(context, reason, false, true, Constant.NOTIFICATION_FAIL_ID); diff --git a/Android/app/src/main/java/tw/edu/kuas/wifiautologin/libs/Utils.java b/Android/app/src/main/java/tw/edu/kuas/wifiautologin/libs/Utils.java index c501fae..83da980 100644 --- a/Android/app/src/main/java/tw/edu/kuas/wifiautologin/libs/Utils.java +++ b/Android/app/src/main/java/tw/edu/kuas/wifiautologin/libs/Utils.java @@ -10,11 +10,15 @@ import android.net.wifi.WifiManager; import android.os.Build; import android.text.TextUtils; +import android.util.Log; import tw.edu.kuas.wifiautologin.models.UserModel; public class Utils { + private static ConnectivityManager.NetworkCallback mCallback; + private static ConnectivityManager mConnectivityManager; + public static String getCurrentSSID(Context context) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); @@ -72,26 +76,73 @@ public static UserModel tranUser(String user, String pwd) { return model; } - @TargetApi(21) - public static void forceUseWifi(Context context) { - if (!Utils.postVersion(21)) { + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + public static void requestNetwork(Context context) { + if (!Utils.postVersion(Build.VERSION_CODES.LOLLIPOP)) { return; } - final ConnectivityManager cm = - (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkRequest.Builder req = new NetworkRequest.Builder(); - req.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); - req.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); - cm.requestNetwork(req.build(), new ConnectivityManager.NetworkCallback() { + + setUpConnectivityManager(context); + NetworkRequest.Builder builder = new NetworkRequest.Builder(); + builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); + builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); + + if (mCallback != null) { + try { + mConnectivityManager.unregisterNetworkCallback(mCallback); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + } + mCallback = new ConnectivityManager.NetworkCallback() { @Override public void onAvailable(Network network) { - if (Utils.postVersion(23)) { - cm.bindProcessToNetwork(network); - } else { - ConnectivityManager.setProcessDefaultNetwork(network); + if (mCallback != null) { + Log.d(Constant.TAG, network.toString()); + if (Utils.postVersion(Build.VERSION_CODES.M)) { + mConnectivityManager.bindProcessToNetwork(network); + } else { + ConnectivityManager.setProcessDefaultNetwork(network); + } } } - }); + }; + + mConnectivityManager.requestNetwork(builder.build(), mCallback); + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + public static void resetDefaultNetwork(Context context) { + if (!Utils.postVersion(Build.VERSION_CODES.LOLLIPOP)) { + return; + } + + setUpConnectivityManager(context); + if (Utils.postVersion(Build.VERSION_CODES.M)) { + if (mConnectivityManager.getBoundNetworkForProcess() != null) { + mConnectivityManager.bindProcessToNetwork(null); + } + } else { + if (ConnectivityManager.getProcessDefaultNetwork() != null) { + ConnectivityManager.setProcessDefaultNetwork(null); + } + } + + if (mCallback != null) { + try { + mConnectivityManager.unregisterNetworkCallback(mCallback); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + } + mCallback = null; + } + + private static void setUpConnectivityManager(Context context) { + if (mConnectivityManager == null) { + mConnectivityManager = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + } } public static boolean postVersion(int sdkInt) {