-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(auto-upload): calls #16393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
fix(auto-upload): calls #16393
Changes from all commits
80100ab
1af8f6b
7ad3200
5388fb1
979dc15
7d13475
c5c8216
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,32 +7,25 @@ | |
| */ | ||
| package com.nextcloud.client.jobs | ||
|
|
||
| import android.app.Notification | ||
| import android.content.Context | ||
| import androidx.core.app.NotificationCompat | ||
| import androidx.work.CoroutineWorker | ||
| import androidx.work.WorkerParameters | ||
| import com.nextcloud.client.device.PowerManagementService | ||
| import com.nextcloud.utils.ForegroundServiceHelper | ||
| import com.owncloud.android.R | ||
| import com.owncloud.android.datamodel.ForegroundServiceType | ||
| import com.owncloud.android.datamodel.SyncedFolderProvider | ||
| import com.owncloud.android.lib.common.utils.Log_OC | ||
| import com.owncloud.android.ui.notifications.NotificationUtils | ||
| import com.owncloud.android.utils.FilesSyncHelper | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
|
|
||
| /** | ||
| * This work is triggered when OS detects change in media folders. | ||
| * | ||
| * It fires media detection job and sync job and finishes immediately. | ||
| * It fires media detection worker and auto upload worker and finishes immediately. | ||
| * | ||
| * This job must not be started on API < 24. | ||
| */ | ||
| @Suppress("TooGenericExceptionCaught") | ||
| class ContentObserverWork( | ||
| private val context: Context, | ||
| context: Context, | ||
| private val params: WorkerParameters, | ||
| private val syncedFolderProvider: SyncedFolderProvider, | ||
| private val powerManagementService: PowerManagementService, | ||
|
|
@@ -41,8 +34,6 @@ class ContentObserverWork( | |
|
|
||
| companion object { | ||
| private const val TAG = "🔍" + "ContentObserverWork" | ||
| private const val CHANNEL_ID = NotificationUtils.NOTIFICATION_CHANNEL_CONTENT_OBSERVER | ||
| private const val NOTIFICATION_ID = 774 | ||
| } | ||
|
|
||
| override suspend fun doWork(): Result = withContext(Dispatchers.IO) { | ||
|
|
@@ -53,10 +44,6 @@ class ContentObserverWork( | |
| try { | ||
| if (params.triggeredContentUris.isNotEmpty()) { | ||
| Log_OC.d(TAG, "📸 content observer detected file changes.") | ||
|
|
||
| val notificationTitle = context.getString(R.string.content_observer_work_notification_title) | ||
| val notification = createNotification(notificationTitle) | ||
| updateForegroundInfo(notification) | ||
| checkAndTriggerAutoUpload() | ||
|
|
||
| // prevent worker fail because of another worker | ||
|
|
@@ -69,46 +56,19 @@ class ContentObserverWork( | |
| Log_OC.d(TAG, "⚠️ triggeredContentUris is empty — nothing to sync.") | ||
| } | ||
|
|
||
| rescheduleSelf() | ||
|
|
||
| val result = Result.success() | ||
| backgroundJobManager.logEndOfWorker(workerName, result) | ||
| Log_OC.d(TAG, "finished") | ||
| result | ||
| } catch (e: Exception) { | ||
| Log_OC.e(TAG, "❌ Exception in ContentObserverWork: ${e.message}", e) | ||
| Result.retry() | ||
| } finally { | ||
| Log_OC.d(TAG, "🔄" + "re-scheduling job") | ||
| backgroundJobManager.scheduleContentObserverJob() | ||
| } | ||
| } | ||
|
|
||
| private suspend fun updateForegroundInfo(notification: Notification) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| val foregroundInfo = ForegroundServiceHelper.createWorkerForegroundInfo( | ||
| NOTIFICATION_ID, | ||
| notification, | ||
| ForegroundServiceType.DataSync | ||
| ) | ||
| setForeground(foregroundInfo) | ||
| } | ||
|
|
||
| private fun createNotification(title: String): Notification = NotificationCompat.Builder(context, CHANNEL_ID) | ||
| .setContentTitle(title) | ||
| .setSmallIcon(R.drawable.ic_find_in_page) | ||
| .setOngoing(true) | ||
| .setSound(null) | ||
| .setVibrate(null) | ||
| .setOnlyAlertOnce(true) | ||
| .setPriority(NotificationCompat.PRIORITY_LOW) | ||
| .setSilent(true) | ||
| .build() | ||
|
|
||
| /** | ||
| * Re-schedules this observer to ensure continuous monitoring of media changes. | ||
| */ | ||
| private fun rescheduleSelf() { | ||
| Log_OC.d(TAG, "🔁 Rescheduling ContentObserverWork for continued observation.") | ||
| backgroundJobManager.scheduleContentObserverJob() | ||
| } | ||
|
|
||
| private suspend fun checkAndTriggerAutoUpload() = withContext(Dispatchers.IO) { | ||
| if (powerManagementService.isPowerSavingEnabled) { | ||
| Log_OC.w(TAG, "⚡ Power saving mode active — skipping file sync.") | ||
|
|
@@ -124,15 +84,15 @@ class ContentObserverWork( | |
| val contentUris = params.triggeredContentUris.map { uri -> | ||
| // adds uri strings e.g. content://media/external/images/media/2281 | ||
| uri.toString() | ||
| }.toTypedArray() | ||
| }.toTypedArray<String?>() | ||
| Log_OC.d(TAG, "📄 Content uris detected") | ||
|
|
||
| try { | ||
| FilesSyncHelper.startAutoUploadImmediatelyWithContentUris( | ||
| FilesSyncHelper.startAutoUploadForEnabledSyncedFolders( | ||
| syncedFolderProvider, | ||
| backgroundJobManager, | ||
| false, | ||
| contentUris | ||
| contentUris, | ||
| false | ||
| ) | ||
| Log_OC.d(TAG, "✅ auto upload triggered successfully for ${contentUris.size} file(s).") | ||
| } catch (e: Exception) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,16 +31,6 @@ class AutoUploadHelper { | |
| } | ||
|
|
||
| fun insertEntries(folder: SyncedFolder, repository: FileSystemRepository) { | ||
| val enabledTimestampMs = folder.enabledTimestampMs | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (!folder.isEnabled || (!folder.isExisting && enabledTimestampMs < 0)) { | ||
| Log_OC.w( | ||
| TAG, | ||
| "Skipping insertDBEntries: enabled=${folder.isEnabled}, " + | ||
| "exists=${folder.isExisting}, enabledTs=$enabledTimestampMs" | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| when (folder.type) { | ||
| MediaFolderType.IMAGE -> { | ||
| repository.insertFromUri(MediaStore.Images.Media.INTERNAL_CONTENT_URI, folder) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating 5, 10 as a const value to fix
MagicNumberis not good for this use case.Duration.ofSeconds(5)clearly indicates what it is.