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 a84cb696c85e132e5a8c07d53103e84ccca9c626..943a15d77ec52b72454e4aac6c1b55c68bbac6e5 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 cd4de9feda26533c6e24d479c04b6a3009767b91..1a11ca760963496a9bd4e662a10884021a6ca18d 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 21c2099bd416c54e9c352299b650d0da08335a43..a7fcdf0c181ff6c4d77bc35dc1706c9b8d780873 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,8 @@ import android.content.ContentResolver; 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; @@ -189,24 +191,23 @@ 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()"); - 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; + public static boolean haveNetworkConnection(Context context) { + Log.i(TAG, "haveNetworkConnection()"); + + 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 * @@ -289,7 +290,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();