From 18f443138b4e25f9c83e183a096037099110daea Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 23 Mar 2022 11:07:18 +0100 Subject: [PATCH 1/4] remove deprecated code in CommonUtils. haveNetworkConnexion(Context context) --- .../foundation/e/drive/utils/CommonUtils.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java index 21c2099b..92abc6aa 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -17,6 +17,7 @@ import android.content.ContentResolver; import android.content.Context; import android.media.MediaScannerConnection; import android.net.ConnectivityManager; +import android.net.Network; import android.net.NetworkInfo; import android.net.Uri; import android.util.Log; @@ -195,14 +196,13 @@ public abstract class CommonUtils { boolean haveConnectedMobile = false; ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo[] netInfo = cm.getAllNetworkInfo(); - for (NetworkInfo ni : netInfo) { - if (ni.getType()== ConnectivityManager.TYPE_WIFI) // Replaced the ni.getTypeName by ni.getType to make the test from ObserverServiceTest to work. But looks a better solution in all case - if (ni.isConnected()) - haveConnectedWifi = true; - if (ni.getType()== ConnectivityManager.TYPE_MOBILE) - if (ni.isConnected()) - haveConnectedMobile = true; + Network[] networks = cm.getAllNetworks(); + for (Network network : networks) { + NetworkInfo networkInfo = cm.getNetworkInfo(network); + if (networkInfo.getType()== ConnectivityManager.TYPE_WIFI) + haveConnectedWifi |= networkInfo.isConnected(); + if (networkInfo.getType()== ConnectivityManager.TYPE_MOBILE) + haveConnectedMobile |= networkInfo.isConnected(); } return haveConnectedWifi || haveConnectedMobile; } -- GitLab From b798c44df9f906378d0adad2e2b17741f075e0af Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 23 Mar 2022 11:08:37 +0100 Subject: [PATCH 2/4] FullScanWorker is only trigger with unmetered network --- app/src/main/java/foundation/e/drive/utils/CommonUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java index 92abc6aa..9179a152 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -289,7 +289,7 @@ public abstract class CommonUtils { */ public static void registerPeriodicFullScanWorker(WorkManager workManager){ final Constraints constraints = new Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) + .setRequiredNetworkType(NetworkType.UNMETERED) .setRequiresBatteryNotLow(true) .build(); -- GitLab From 3a407d47a4f954f85bc86704ae65b4aa4819e515 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 23 Mar 2022 14:25:22 +0100 Subject: [PATCH 3/4] rewrite commonUtils.haveNetworkConnexion() --- .../foundation/e/drive/utils/CommonUtils.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java index 9179a152..74d80836 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -18,6 +18,7 @@ import android.content.Context; import android.media.MediaScannerConnection; import android.net.ConnectivityManager; import android.net.Network; +import android.net.NetworkCapabilities; import android.net.NetworkInfo; import android.net.Uri; import android.util.Log; @@ -192,21 +193,21 @@ public abstract class CommonUtils { */ public static boolean haveNetworkConnexion(Context context) { Log.i(TAG, "haveNetworkConnexion()"); - boolean haveConnectedWifi = false; - boolean haveConnectedMobile = false; - - ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - Network[] networks = cm.getAllNetworks(); - for (Network network : networks) { - NetworkInfo networkInfo = cm.getNetworkInfo(network); - if (networkInfo.getType()== ConnectivityManager.TYPE_WIFI) - haveConnectedWifi |= networkInfo.isConnected(); - if (networkInfo.getType()== ConnectivityManager.TYPE_MOBILE) - haveConnectedMobile |= networkInfo.isConnected(); + + ConnectivityManager cm = context.getSystemService(ConnectivityManager.class); + NetworkCapabilities capabilities = cm.getNetworkCapabilities(cm.getActiveNetwork()); + + if (capabilities != null + && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) + && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)) { + return true; } - return haveConnectedWifi || haveConnectedMobile; + return false; } + + /** * Get mimetype of file from the file itself * -- GitLab From d7f6d3be3b2c26cd5ca9a4d540a5eef79b1096a7 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 24 Mar 2022 09:13:58 +0100 Subject: [PATCH 4/4] fix typo in method name --- .../java/foundation/e/drive/services/ObserverService.java | 5 +---- .../foundation/e/drive/services/SynchronizationService.java | 2 +- app/src/main/java/foundation/e/drive/utils/CommonUtils.java | 4 ++-- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/services/ObserverService.java b/app/src/main/java/foundation/e/drive/services/ObserverService.java index a84cb696..943a15d7 100644 --- a/app/src/main/java/foundation/e/drive/services/ObserverService.java +++ b/app/src/main/java/foundation/e/drive/services/ObserverService.java @@ -11,10 +11,8 @@ package foundation.e.drive.services; import android.accounts.Account; import android.accounts.AccountManager; import android.app.Service; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.ServiceConnection; import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.os.Handler; @@ -44,7 +42,6 @@ import foundation.e.drive.models.SyncRequest; import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.models.SyncedFileState; import foundation.e.drive.operations.ListFileRemoteOperation; -import foundation.e.drive.operations.RemoveFileOperation; import foundation.e.drive.receivers.ForceSyncReceiver; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; @@ -145,7 +142,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene } //check for the case where intent has been launched by initializerService - if (!CommonUtils.haveNetworkConnexion(this)) { + if (!CommonUtils.haveNetworkConnection(this)) { Log.w(TAG, "There is no Internet connexion."); return super.onStartCommand( intent, flags, startId ); } diff --git a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java index cd4de9fe..1a11ca76 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -118,7 +118,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation private void startWorker(int threadIndex){ if (syncedRequestQueue.isEmpty()) return; - if (!threadWorkingState[threadIndex] && CommonUtils.haveNetworkConnexion(getApplicationContext())) { //check if the thread corresponding to threadIndex isn't already working + if (!threadWorkingState[threadIndex] && CommonUtils.haveNetworkConnection(getApplicationContext())) { //check if the thread corresponding to threadIndex isn't already working final SyncRequest request = this.syncedRequestQueue.poll(); //return null if deque is empty diff --git a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java index 74d80836..a7fcdf0c 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -191,8 +191,8 @@ public abstract class CommonUtils { * @param context Activity or service which are calling this method * @return True if there is connexion, false either */ - public static boolean haveNetworkConnexion(Context context) { - Log.i(TAG, "haveNetworkConnexion()"); + public static boolean haveNetworkConnection(Context context) { + Log.i(TAG, "haveNetworkConnection()"); ConnectivityManager cm = context.getSystemService(ConnectivityManager.class); NetworkCapabilities capabilities = cm.getNetworkCapabilities(cm.getActiveNetwork()); -- GitLab