Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method "resetBindAll" improvements #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions src/android/wifiwizard2/WifiWizard2.java
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,12 @@ private void registerBindALL(int netID){
if( API_VERSION > 21 ){
Log.d(TAG, "registerBindALL: registering net changed receiver");
desired = new AP(netID,null,null);

try {
// Unregister net changed receiver -- should only be registered in API versions > 21
cordova.getActivity().getApplicationContext().unregisterReceiver(networkChangedReceiver);
} catch (Exception e) {}

cordova.getActivity().getApplicationContext().registerReceiver(networkChangedReceiver, NETWORK_STATE_CHANGED_FILTER);
} else {
Log.d(TAG, "registerBindALL: API older than 21, bindall ignored.");
Expand All @@ -1695,11 +1701,15 @@ private void registerBindALL(int netID){
* being routed through Wifi.
*/
private void maybeResetBindALL(){
maybeResetBindALL(false);
}

private void maybeResetBindALL(boolean forceReset){

Log.d(TAG, "maybeResetBindALL");

// desired should have a value if receiver is registered
if( desired != null ){
if( desired!=null || forceReset ){

if( API_VERSION > 21 ){

Expand All @@ -1710,13 +1720,6 @@ private void maybeResetBindALL(){

}

// Lollipop OS or newer
if ( API_VERSION >= 23 ) {
connectivityManager.bindProcessToNetwork(null);
} else if( API_VERSION >= 21 && API_VERSION < 23 ){
connectivityManager.setProcessDefaultNetwork(null);
}

if ( API_VERSION > 21 && networkCallback != null) {

try {
Expand All @@ -1725,6 +1728,13 @@ private void maybeResetBindALL(){
} catch (Exception e) {}
}

// Lollipop OS or newer
if ( API_VERSION >= 23 ) {
connectivityManager.bindProcessToNetwork(null);
} else if( API_VERSION >= 21 && API_VERSION < 23 ){
connectivityManager.setProcessDefaultNetwork(null);
}

networkCallback = null;
previous = null;
desired = null;
Expand All @@ -1742,7 +1752,7 @@ private void resetBindAll(CallbackContext callbackContext) {
Log.d(TAG, "WifiWizard2: resetBindALL");

try {
maybeResetBindALL();
maybeResetBindALL(true);
callbackContext.success("Successfully reset BindALL");
} catch (Exception e) {
Log.e(TAG, "InterruptedException error.", e);
Expand Down Expand Up @@ -1804,6 +1814,11 @@ public void onAvailable(Network network) {
}
};

try {
// Same behavior as releaseNetworkRequest
connectivityManager.unregisterNetworkCallback(networkCallback); // Added in API 21
} catch (Exception e) {}

connectivityManager.requestNetwork(request, networkCallback);

// Only lollipop (API 21 && 22) use setProcessDefaultNetwork, API < 21 already does this by default
Expand All @@ -1824,6 +1839,11 @@ public void onAvailable(Network network) {
}
};

try {
// Same behavior as releaseNetworkRequest
connectivityManager.unregisterNetworkCallback(networkCallback); // Added in API 21
} catch (Exception e) {}

connectivityManager.requestNetwork(request, networkCallback);

} else {
Expand Down