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

Add long running task support

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 жил өмнө
parent
commit
ef2ebb9176

+ 6 - 0
app/src/main/AndroidManifest.xml

@@ -239,6 +239,12 @@
             android:exported="false"
             android:exported="false"
             android:configChanges="orientation|screenLayout|screenSize|keyboardHidden"
             android:configChanges="orientation|screenLayout|screenSize|keyboardHidden"
             android:theme="@style/Theme.ownCloud.Media" />
             android:theme="@style/Theme.ownCloud.Media" />
+        <service
+            android:name="androidx.work.impl.foreground.SystemForegroundService"
+            android:directBootAware="false"
+            android:enabled="@bool/enable_system_foreground_service_default"
+            android:exported="false"
+            android:foregroundServiceType="dataSync" />
         <service
         <service
             android:name=".authentication.AccountAuthenticatorService"
             android:name=".authentication.AccountAuthenticatorService"
             android:exported="false">
             android:exported="false">

+ 9 - 1
app/src/main/java/com/nextcloud/client/files/downloader/DownloadNotificationManager.kt

@@ -111,7 +111,7 @@ class DownloadNotificationManager(
     @Suppress("MagicNumber")
     @Suppress("MagicNumber")
     fun showCompleteNotification(text: String) {
     fun showCompleteNotification(text: String) {
         Handler(Looper.getMainLooper()).postDelayed({
         Handler(Looper.getMainLooper()).postDelayed({
-            updateNotificationText(null,text, true)
+            updateNotificationText(null, text, true)
             dismissNotification()
             dismissNotification()
         }, 3000)
         }, 3000)
     }
     }
@@ -156,4 +156,12 @@ class DownloadNotificationManager(
             )
             )
         )
         )
     }
     }
+
+    fun getId(): Int {
+        return id
+    }
+
+    fun getNotification(): Notification {
+        return notificationBuilder.build()
+    }
 }
 }

+ 10 - 0
app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt

@@ -36,8 +36,10 @@ import com.nextcloud.client.account.UserAccountManager
 import com.nextcloud.java.util.Optional
 import com.nextcloud.java.util.Optional
 import com.nextcloud.model.WorkerState
 import com.nextcloud.model.WorkerState
 import com.nextcloud.model.WorkerStateLiveData
 import com.nextcloud.model.WorkerStateLiveData
+import com.nextcloud.utils.ForegroundServiceHelper
 import com.owncloud.android.R
 import com.owncloud.android.R
 import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.datamodel.FileDataStorageManager
+import com.owncloud.android.datamodel.ForegroundServiceType
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.files.services.IndexedForest
 import com.owncloud.android.files.services.IndexedForest
 import com.owncloud.android.lib.common.OwnCloudAccount
 import com.owncloud.android.lib.common.OwnCloudAccount
@@ -123,6 +125,14 @@ class FileDownloadWorker(
 
 
             addAccountUpdateListener()
             addAccountUpdateListener()
 
 
+            setForegroundAsync(
+                ForegroundServiceHelper.createWorkerForegroundInfo(
+                    notificationManager.getId(),
+                    notificationManager.getNotification(),
+                    ForegroundServiceType.DataSync
+                )
+            )
+
             requestDownloads.forEach {
             requestDownloads.forEach {
                 downloadFile(it)
                 downloadFile(it)
             }
             }

+ 13 - 0
app/src/main/java/com/nextcloud/utils/ForegroundServiceHelper.kt

@@ -25,6 +25,7 @@ import android.app.Notification
 import android.app.Service
 import android.app.Service
 import android.os.Build
 import android.os.Build
 import androidx.core.app.ServiceCompat
 import androidx.core.app.ServiceCompat
+import androidx.work.ForegroundInfo
 import com.owncloud.android.datamodel.ForegroundServiceType
 import com.owncloud.android.datamodel.ForegroundServiceType
 
 
 object ForegroundServiceHelper {
 object ForegroundServiceHelper {
@@ -45,4 +46,16 @@ object ForegroundServiceHelper {
             service.startForeground(id, notification)
             service.startForeground(id, notification)
         }
         }
     }
     }
+
+    fun createWorkerForegroundInfo(
+        id: Int,
+        notification: Notification,
+        foregroundServiceType: ForegroundServiceType
+    ): ForegroundInfo {
+        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
+            ForegroundInfo(id, notification, foregroundServiceType.getId())
+        } else {
+            ForegroundInfo(id, notification)
+        }
+    }
 }
 }