From 25212bc85e669c5f6bd948f4aeb4c0a4096a1cd0 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 23 Mar 2022 14:25:22 +0100 Subject: [PATCH 1/2] rewrite commonUtils.haveNetworkConnexion() --- .../foundation/e/drive/utils/CommonUtils.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 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 e0f762fc..d424b434 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; @@ -202,22 +203,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); - 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; + + 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 6086ab5b08be7e77117c0a0386f4339d7528c382 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 30 Mar 2022 10:41:24 +0200 Subject: [PATCH 2/2] fix typo in haveNetworkConnection() method name --- .../foundation/e/drive/receivers/ScreenOffReceiver.java | 2 +- .../java/foundation/e/drive/services/ObserverService.java | 2 +- .../e/drive/services/OperationManagerService.java | 3 +-- .../main/java/foundation/e/drive/utils/CommonUtils.java | 8 ++++---- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/receivers/ScreenOffReceiver.java b/app/src/main/java/foundation/e/drive/receivers/ScreenOffReceiver.java index 27d48959..766f9477 100644 --- a/app/src/main/java/foundation/e/drive/receivers/ScreenOffReceiver.java +++ b/app/src/main/java/foundation/e/drive/receivers/ScreenOffReceiver.java @@ -42,7 +42,7 @@ public class ScreenOffReceiver extends BroadcastReceiver { if(intentAction == null){ Log.e(TAG, "intent Action is null"); } else if ( intent.getAction().equals(Intent.ACTION_SCREEN_OFF) - && CommonUtils.haveNetworkConnexion( context ) ) { + && CommonUtils.haveNetworkConnection( context ) ) { Log.d(TAG, "onReceive: ACTION_SCREEN_OFF"); Intent cloudIntent = new Intent(context, ObserverService.class); context.startService(cloudIntent); 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 c90b4167..eda6c911 100644 --- a/app/src/main/java/foundation/e/drive/services/ObserverService.java +++ b/app/src/main/java/foundation/e/drive/services/ObserverService.java @@ -140,7 +140,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/OperationManagerService.java b/app/src/main/java/foundation/e/drive/services/OperationManagerService.java index c523f54c..2c0bb07b 100644 --- a/app/src/main/java/foundation/e/drive/services/OperationManagerService.java +++ b/app/src/main/java/foundation/e/drive/services/OperationManagerService.java @@ -41,7 +41,6 @@ import foundation.e.drive.operations.UploadFileOperation; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.DavClientProvider; -import foundation.e.drive.utils.ServiceExceptionHandler; /** * @author Vincent Bourgmayer @@ -110,7 +109,7 @@ public class OperationManagerService extends Service implements OnRemoteOperatio } } //start the new Job - if( !mThreadWorkingState[threadIndex] && CommonUtils.haveNetworkConnexion( getApplicationContext() ) ) { //check if the thread corresponding to threadIndex isn't already working + if( !mThreadWorkingState[threadIndex] && CommonUtils.haveNetworkConnection( getApplicationContext() ) ) { //check if the thread corresponding to threadIndex isn't already working ComparableOperation operation = this.mOperationsQueue.poll(); //return null if deque is empty if (operation != null) { 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 d424b434..3bc17bb3 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -196,13 +196,13 @@ public abstract class CommonUtils { } /** - * Tell if there is internet connexion + * Tell if there is internet connection * * @param context Activity or service which are calling this method - * @return True if there is connexion, false either + * @return True if there is connection, 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