From 654eb9def93de4a6e129439a36d9011d277c2810 Mon Sep 17 00:00:00 2001 From: Cedarus <11912202@mail.sustech.edu.cn> Date: Sat, 28 May 2022 22:56:21 +0800 Subject: [PATCH] Attempt to solve: Option to disable auto sync with metered connections #10706 --- .../main/java/com/ichi2/async/Connection.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/AnkiDroid/src/main/java/com/ichi2/async/Connection.java b/AnkiDroid/src/main/java/com/ichi2/async/Connection.java index 4c0b841dddc9..862cdbb376b5 100644 --- a/AnkiDroid/src/main/java/com/ichi2/async/Connection.java +++ b/AnkiDroid/src/main/java/com/ichi2/async/Connection.java @@ -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();