From 8ff33e82b52b1058604c6c1db0feb6e3df29ca46 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 14 Apr 2022 10:10:57 +0200 Subject: [PATCH] Update network checking method in commonUtils - change method signature: replace 'Account account' parameter by 'boolean isMeteredNetworkAllowed' - Extract code to check meteredNetwork from ContentResolver into a separate method: isMeteredNetworkAllowed(Account account) - Update call to haveNetworkConnection() to fit with new signature. Use "true" for meteredNetwork in Activity, and load settings for ObserverService, SynchronizationService & CreateRemoteFolderWorker. --- .../e/drive/activity/AccountsActivity.java | 2 +- .../e/drive/services/ObserverService.java | 3 ++- .../e/drive/services/SynchronizationService.java | 3 ++- .../java/foundation/e/drive/utils/CommonUtils.java | 14 +++++++++++--- .../e/drive/work/CreateRemoteFolderWorker.java | 3 ++- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java b/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java index d5deafc3..1b936222 100644 --- a/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java +++ b/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java @@ -101,7 +101,7 @@ public class AccountsActivity extends AppCompatActivity { } }; - if (!CommonUtils.haveNetworkConnection(this, account)) { + if (!CommonUtils.haveNetworkConnection(this, true)) { handleNoInternetConnection(); return; } 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 af672a3c..567dd17b 100644 --- a/app/src/main/java/foundation/e/drive/services/ObserverService.java +++ b/app/src/main/java/foundation/e/drive/services/ObserverService.java @@ -135,8 +135,9 @@ public class ObserverService extends Service implements OnRemoteOperationListene return super.onStartCommand( intent, flags, startId ); } + final boolean meteredNetworkAllowed = CommonUtils.isMeteredNetworkAllowed(mAccount); //check for the case where intent has been launched by initializerService - if (!CommonUtils.haveNetworkConnection(this, mAccount)) { + if (!CommonUtils.haveNetworkConnection(this, meteredNetworkAllowed)) { 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 19073ef5..b05476ac 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,8 @@ public class SynchronizationService extends Service implements OnRemoteOperation private void startWorker(int threadIndex){ if (syncedRequestQueue.isEmpty()) return; - if (!threadWorkingState[threadIndex] && CommonUtils.haveNetworkConnection(getApplicationContext(), account)) { //check if the thread corresponding to threadIndex isn't already working + final boolean meteredNetworkAllowed = CommonUtils.isMeteredNetworkAllowed(account); + if (!threadWorkingState[threadIndex] && CommonUtils.haveNetworkConnection(getApplicationContext(), meteredNetworkAllowed)) { //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 0c1bf7f1..72484925 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -167,6 +167,15 @@ public abstract class CommonUtils { return ContentResolver.getSyncAutomatically(account, SETTINGSYNC_PROVIDER_AUTHORITY); } + /** + * Read accountManager settings + * @param account + * @return true if usage of metered connection is allowed + */ + public static boolean isMeteredNetworkAllowed(Account account){ + return ContentResolver.getSyncAutomatically(account, METERED_NETWORK_ALLOWED_AUTHORITY); + } + /** * @param context app context * @return Owncloud client instance or null @@ -216,16 +225,15 @@ public abstract class CommonUtils { * Tell if there is internet connection * * @param context Activity or service which are calling this method + * @param meteredNetworkAllowed true if service can use metered network / false either * @return True if there is connection, false either */ - public static boolean haveNetworkConnection(Context context, Account account) { + public static boolean haveNetworkConnection(Context context, boolean meteredNetworkAllowed) { Log.i(TAG, "haveNetworkConnection()"); final ConnectivityManager cm = context.getSystemService(ConnectivityManager.class); final NetworkCapabilities capabilities = cm.getNetworkCapabilities(cm.getActiveNetwork()); - final boolean meteredNetworkAllowed = ContentResolver.getSyncAutomatically(account, METERED_NETWORK_ALLOWED_AUTHORITY); - if (capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) diff --git a/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java b/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java index 3459551a..38ec32f0 100644 --- a/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java +++ b/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java @@ -61,7 +61,8 @@ public class CreateRemoteFolderWorker extends Worker { return Result.failure(); } - if (!CommonUtils.haveNetworkConnection(context, account)) { + final boolean meteredNetworkAllowed = CommonUtils.isMeteredNetworkAllowed(account); + if (!CommonUtils.haveNetworkConnection(context, meteredNetworkAllowed)) { Log.e(TAG, "Can't create remote folder because there is no usable connection"); return Result.retry(); } -- GitLab