Skip to content

Commit

Permalink
Update version to 2.2.1, Hotfix connectivityManager doesn't reset net…
Browse files Browse the repository at this point in the history
…work
  • Loading branch information
hearsilent committed Apr 27, 2016
1 parent 115818c commit 35a3225
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 47 deletions.
4 changes: 2 additions & 2 deletions Android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
PorterDuff.Mode.SRC_IN);

initGA();
Utils.forceUseWifi(this);
}

private void initGA() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
Expand All @@ -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;
}
Expand All @@ -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);

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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));
}
}
});
Expand All @@ -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(
Expand All @@ -207,21 +206,21 @@ 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);
NotificationHelper.cancelNotification(context, Constant.NOTIFICATION_FAIL_ID);
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));
}
}
});
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
79 changes: 65 additions & 14 deletions Android/app/src/main/java/tw/edu/kuas/wifiautologin/libs/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 35a3225

Please sign in to comment.