From 9810020dec0af580ec9188705dea3df462092ace Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 27 Nov 2024 11:30:44 +0100 Subject: [PATCH] Check BOOT_COMPLETED BroadcastReceiver Usage Signed-off-by: alperozturk --- .../java/com/owncloud/android/MainApp.java | 46 +++++++++++++------ .../files/BootupBroadcastReceiver.java | 10 ++-- .../android/utils/ReceiversHelper.java | 17 +++---- 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/MainApp.java b/app/src/main/java/com/owncloud/android/MainApp.java index 4afaf9fcbf..ab28515cb6 100644 --- a/app/src/main/java/com/owncloud/android/MainApp.java +++ b/app/src/main/java/com/owncloud/android/MainApp.java @@ -352,7 +352,8 @@ public class MainApp extends Application implements HasAndroidInjector, NetworkC } catch (Exception e) { Log_OC.d("Debug", "Failed to disable uri exposure"); } - initSyncOperations(this, + initSyncOperations(true, + this, preferences, uploadsStorageManager, accountManager, @@ -363,7 +364,7 @@ public class MainApp extends Application implements HasAndroidInjector, NetworkC viewThemeUtils, walledCheckCache, syncedFolderProvider); - initContactsBackup(accountManager, backgroundJobManager); + initContactsBackup(accountManager, backgroundJobManager, true); notificationChannels(); if (backgroundJobManager != null) { @@ -505,7 +506,11 @@ public class MainApp extends Application implements HasAndroidInjector, NetworkC }); } - public static void initContactsBackup(UserAccountManager accountManager, BackgroundJobManager backgroundJobManager) { + public static void initContactsBackup(UserAccountManager accountManager, BackgroundJobManager backgroundJobManager, boolean shouldSchedulePeriodicContactsBackup) { + if (!shouldSchedulePeriodicContactsBackup) { + return; + } + ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProviderImpl(appContext.get()); if (accountManager == null) { return; @@ -606,7 +611,14 @@ public class MainApp extends Application implements HasAndroidInjector, NetworkC } } + /** + * @param shouldSync Android 15 restricts BOOT_COMPLETED broadcast receivers from starting certain foreground + * services (dataSync, mediaPlayback, mediaProjection ...), throwing a + * ForegroundServiceStartNotAllowedException if violated. Under the hood, WorkManager manages and + * runs a foreground service. The shouldSync flag ensures proper WorkManager usage. + */ public static void initSyncOperations( + final boolean shouldSync, final Context context, final AppPreferences preferences, final UploadsStorageManager uploadsStorageManager, @@ -618,6 +630,7 @@ public class MainApp extends Application implements HasAndroidInjector, NetworkC final ViewThemeUtils viewThemeUtils, final WalledCheckCache walledCheckCache, final SyncedFolderProvider syncedFolderProvider) { + updateToAutoUpload(context); cleanOldEntries(clock); updateAutoUploadEntries(clock); @@ -630,35 +643,40 @@ public class MainApp extends Application implements HasAndroidInjector, NetworkC } } - if (!preferences.isAutoUploadInitialized()) { + if (!preferences.isAutoUploadInitialized() && shouldSync) { FilesSyncHelper.startFilesSyncForAllFolders(syncedFolderProvider, backgroundJobManager,false, new String[]{}); preferences.setAutoUploadInit(true); } - FilesSyncHelper.scheduleFilesSyncForAllFoldersIfNeeded(appContext.get(), syncedFolderProvider, backgroundJobManager); - FilesSyncHelper.restartUploadsIfNeeded( - uploadsStorageManager, - accountManager, - connectivityService, - powerManagementService); + if (shouldSync) { + FilesSyncHelper.scheduleFilesSyncForAllFoldersIfNeeded(appContext.get(), syncedFolderProvider, backgroundJobManager); + FilesSyncHelper.restartUploadsIfNeeded( + uploadsStorageManager, + accountManager, + connectivityService, + powerManagementService); - backgroundJobManager.scheduleOfflineSync(); + backgroundJobManager.scheduleOfflineSync(); + } ReceiversHelper.registerNetworkChangeReceiver(uploadsStorageManager, accountManager, connectivityService, powerManagementService, - walledCheckCache); + walledCheckCache, + shouldSync); ReceiversHelper.registerPowerChangeReceiver(uploadsStorageManager, accountManager, connectivityService, - powerManagementService); + powerManagementService, + shouldSync); ReceiversHelper.registerPowerSaveReceiver(uploadsStorageManager, accountManager, connectivityService, - powerManagementService); + powerManagementService, + shouldSync); } public static void notificationChannels() { diff --git a/app/src/main/java/com/owncloud/android/files/BootupBroadcastReceiver.java b/app/src/main/java/com/owncloud/android/files/BootupBroadcastReceiver.java index 0e367ca766..b2e918e1cd 100644 --- a/app/src/main/java/com/owncloud/android/files/BootupBroadcastReceiver.java +++ b/app/src/main/java/com/owncloud/android/files/BootupBroadcastReceiver.java @@ -14,6 +14,7 @@ package com.owncloud.android.files; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.os.Build; import com.nextcloud.client.account.UserAccountManager; import com.nextcloud.client.core.Clock; @@ -62,7 +63,10 @@ public class BootupBroadcastReceiver extends BroadcastReceiver { AndroidInjection.inject(this, context); if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { - MainApp.initSyncOperations(context, + boolean isApiLevel35OrHigher = (Build.VERSION.SDK_INT >= 35); + + MainApp.initSyncOperations(!isApiLevel35OrHigher, + context, preferences, uploadsStorageManager, accountManager, @@ -73,8 +77,8 @@ public class BootupBroadcastReceiver extends BroadcastReceiver { viewThemeUtils, walledCheckCache, syncedFolderProvider - ); - MainApp.initContactsBackup(accountManager, backgroundJobManager); + ); + MainApp.initContactsBackup(accountManager, backgroundJobManager, !isApiLevel35OrHigher); } else { Log_OC.d(TAG, "Getting wrong intent: " + intent.getAction()); } diff --git a/app/src/main/java/com/owncloud/android/utils/ReceiversHelper.java b/app/src/main/java/com/owncloud/android/utils/ReceiversHelper.java index 898200cfc8..4f8a611cf2 100644 --- a/app/src/main/java/com/owncloud/android/utils/ReceiversHelper.java +++ b/app/src/main/java/com/owncloud/android/utils/ReceiversHelper.java @@ -36,7 +36,8 @@ public final class ReceiversHelper { final UserAccountManager accountManager, final ConnectivityService connectivityService, final PowerManagementService powerManagementService, - final WalledCheckCache walledCheckCache) { + final WalledCheckCache walledCheckCache, + final boolean shouldRegister) { Context context = MainApp.getAppContext(); IntentFilter intentFilter = new IntentFilter(); @@ -48,7 +49,7 @@ public final class ReceiversHelper { public void onReceive(Context context, Intent intent) { DNSCache.clear(); walledCheckCache.clear(); - if (connectivityService.getConnectivity().isConnected()) { + if (connectivityService.getConnectivity().isConnected() && shouldRegister) { FilesSyncHelper.restartUploadsIfNeeded(uploadsStorageManager, accountManager, connectivityService, @@ -64,8 +65,8 @@ public final class ReceiversHelper { final UploadsStorageManager uploadsStorageManager, final UserAccountManager accountManager, final ConnectivityService connectivityService, - final PowerManagementService powerManagementService - ) { + final PowerManagementService powerManagementService, + final boolean shouldRegister) { Context context = MainApp.getAppContext(); IntentFilter intentFilter = new IntentFilter(); @@ -75,7 +76,7 @@ public final class ReceiversHelper { BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())) { + if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction()) && shouldRegister) { FilesSyncHelper.restartUploadsIfNeeded(uploadsStorageManager, accountManager, connectivityService, @@ -91,8 +92,8 @@ public final class ReceiversHelper { final UploadsStorageManager uploadsStorageManager, final UserAccountManager accountManager, final ConnectivityService connectivityService, - final PowerManagementService powerManagementService - ) { + final PowerManagementService powerManagementService, + final boolean shouldRegister) { Context context = MainApp.getAppContext(); IntentFilter intentFilter = new IntentFilter(); @@ -101,7 +102,7 @@ public final class ReceiversHelper { BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - if (!powerManagementService.isPowerSavingEnabled()) { + if (!powerManagementService.isPowerSavingEnabled() && shouldRegister) { FilesSyncHelper.restartUploadsIfNeeded(uploadsStorageManager, accountManager, connectivityService,