Skip to content

Commit

Permalink
Attempt to solve: Option to disable auto sync with metered connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedarus committed May 28, 2022
1 parent 502ee7a commit 654eb9d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/async/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,35 @@ public static boolean isOnline() {
}
}

@SuppressWarnings("deprecation")
public static boolean isWifiConnected(){
if (!isOnline()) return false;
ConnectivityManager cm = (ConnectivityManager) AnkiDroidApp.getInstance().getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
return false;
}
/* NetworkInfo is deprecated in API 29 so we have to check separately for higher API Levels */
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Network network = cm.getActiveNetwork();
if (network == null) {
return false;
}
NetworkCapabilities networkCapabilities = cm.getNetworkCapabilities(network);
if (networkCapabilities == null) {
return false;
}
return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
} else {
android.net.NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo.State state = info.getState();
if (android.net.NetworkInfo.State.CONNECTED==state) {
return true;
}
return false;
}
}


public interface TaskListener {
void onPreExecute();
Expand Down

0 comments on commit 654eb9d

Please sign in to comment.