Эх сурвалжийг харах

Add foreground feedback notification for sync process

Signed-off-by: Jonas Mayer <jonas.a.mayer@gmx.net>
Jonas Mayer 1 жил өмнө
parent
commit
07ffe3f46d

+ 29 - 4
app/src/main/java/com/nextcloud/client/jobs/FilesSyncWork.kt

@@ -26,8 +26,10 @@ import android.content.ContentResolver
 import android.content.Context
 import android.content.res.Resources
 import android.text.TextUtils
+import androidx.core.app.NotificationCompat
 import androidx.exifinterface.media.ExifInterface
-import androidx.work.Worker
+import androidx.work.CoroutineWorker
+import androidx.work.ForegroundInfo
 import androidx.work.WorkerParameters
 import com.nextcloud.client.account.UserAccountManager
 import com.nextcloud.client.device.PowerManagementService
@@ -45,6 +47,7 @@ import com.owncloud.android.files.services.FileUploader
 import com.owncloud.android.lib.common.utils.Log_OC
 import com.owncloud.android.operations.UploadFileOperation
 import com.owncloud.android.ui.activity.SettingsActivity
+import com.owncloud.android.ui.notifications.NotificationUtils
 import com.owncloud.android.utils.FileStorageUtils
 import com.owncloud.android.utils.FilesSyncHelper
 import com.owncloud.android.utils.MimeType
@@ -66,16 +69,33 @@ class FilesSyncWork(
     private val powerManagementService: PowerManagementService,
     private val syncedFolderProvider: SyncedFolderProvider,
     private val backgroundJobManager: BackgroundJobManager
-) : Worker(context, params) {
+) : CoroutineWorker(context, params) {
 
     companion object {
         const val TAG = "FilesSyncJob"
         const val SKIP_CUSTOM = "skipCustom"
         const val OVERRIDE_POWER_SAVING = "overridePowerSaving"
+        const val FOREGROUND_SERVICE_ID = 414
     }
 
-    override fun doWork(): Result {
+    private fun createForegroundInfo(progressPercent: Int): ForegroundInfo {
+        //update throughout worker execution to give use feedback how far worker is
+
+        val notification = NotificationCompat.Builder(context, NotificationUtils.NOTIFICATION_CHANNEL_FILE_SYNC)
+            .setTicker(context.getString(R.string.autoupload_worker_foreground_info))
+            .setContentText(context.getString(R.string.autoupload_worker_foreground_info))
+            .setSmallIcon(R.drawable.notification_icon)
+            .setContentTitle(context.getString(R.string.autoupload_worker_foreground_info))
+            .setOngoing(true)
+            .setProgress(100, progressPercent, false)
+            .build()
+
+        return ForegroundInfo(FOREGROUND_SERVICE_ID, notification)
+    }
+
+    override suspend fun doWork(): Result {
         backgroundJobManager.logStartOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class))
+        setForeground(createForegroundInfo(0))
 
         val overridePowerSaving = inputData.getBoolean(OVERRIDE_POWER_SAVING, false)
         // If we are in power save mode, better to postpone upload
@@ -93,13 +113,18 @@ class FilesSyncWork(
             connectivityService,
             powerManagementService
         )
+        setForeground(createForegroundInfo(5))
         FilesSyncHelper.insertAllDBEntries(skipCustom, syncedFolderProvider)
+        setForeground(createForegroundInfo(50))
         // Create all the providers we'll need
         val filesystemDataProvider = FilesystemDataProvider(contentResolver)
         val currentLocale = resources.configuration.locale
         val dateFormat = SimpleDateFormat("yyyy:MM:dd HH:mm:ss", currentLocale)
         dateFormat.timeZone = TimeZone.getTimeZone(TimeZone.getDefault().id)
-        for (syncedFolder in syncedFolderProvider.syncedFolders) {
+
+        val syncedFolders = syncedFolderProvider.syncedFolders
+        for ((index,syncedFolder) in syncedFolders.withIndex()) {
+            setForeground(createForegroundInfo((50+(index.toDouble()/syncedFolders.size.toDouble())*50).toInt()))
             if (syncedFolder.isEnabled && (!skipCustom || MediaFolderType.CUSTOM != syncedFolder.type)) {
                 syncFolder(
                     context,

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -628,6 +628,7 @@
     <string name="autoupload_hide_folder">Hide folder</string>
     <string name="autoupload_configure">Configure</string>
     <string name="synced_folders_configure_folders">Configure folders</string>
+    <string name="autoupload_worker_foreground_info">Preparing auto upload</string>
 
     <string name="empty" translatable="false" />
     <string name="test_server_button">Test server connection</string>