Add notifications

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-08-16 16:25:56 +02:00
parent 738188942a
commit 22732c99f4
No known key found for this signature in database
GPG key ID: 4E577DC593B59BDF
5 changed files with 80 additions and 11 deletions

View file

@ -35,7 +35,7 @@ open class WorkerNotificationManager(
setSmallIcon(R.drawable.notification_icon)
setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.notification_icon))
setStyle(NotificationCompat.BigTextStyle())
setPriority(NotificationCompat.PRIORITY_LOW)
priority = NotificationCompat.PRIORITY_LOW
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD)
@ -47,16 +47,21 @@ open class WorkerNotificationManager(
}
@Suppress("MagicNumber")
fun setProgress(percent: Int, progressTextId: Int, indeterminate: Boolean) {
val progressText = String.format(
context.getString(progressTextId),
percent
)
fun setProgress(percent: Int, progressTextId: Int?, indeterminate: Boolean) {
notificationBuilder.run {
setProgress(100, percent, indeterminate)
setContentTitle(currentOperationTitle)
setContentText(progressText)
}
progressTextId?.let {
val progressText = String.format(
context.getString(progressTextId),
percent
)
notificationBuilder.run {
setContentText(progressText)
}
}
}

View file

@ -0,0 +1,53 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.nextcloud.client.jobs.offlineOperations
import android.content.Context
import com.nextcloud.client.jobs.notification.WorkerNotificationManager
import com.owncloud.android.R
import com.owncloud.android.utils.theme.ViewThemeUtils
class OfflineOperationsNotificationManager(private val context: Context, viewThemeUtils: ViewThemeUtils) :
WorkerNotificationManager(
ID,
context,
viewThemeUtils,
R.string.offline_operations_worker_notification_manager_ticker
) {
companion object {
private const val ID = 121
}
fun prepareForStart(totalOperationSize: Int, currentOperationIndex: Int, filename: String) {
val title = if (totalOperationSize > 1) {
String.format(
context.getString(R.string.offline_operations_worker_progress_text),
currentOperationIndex,
totalOperationSize,
filename
)
} else {
filename
}
notificationBuilder.run {
setContentTitle(title)
setTicker(title)
setProgress(100, 0, false)
}
showNotification()
}
fun updateNotification(totalOperationSize: Int, currentOperationIndex: Int) {
val percent = (currentOperationIndex * 100) / totalOperationSize
setProgress(percent, null, false)
showNotification()
}
}

View file

@ -24,6 +24,7 @@ import com.owncloud.android.lib.common.OwnCloudClient
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.operations.CreateFolderOperation
import com.owncloud.android.utils.theme.ViewThemeUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
@ -32,6 +33,7 @@ import kotlinx.coroutines.withContext
class OfflineOperationsWorker(
private val user: User,
private val context: Context,
viewThemeUtils: ViewThemeUtils,
params: WorkerParameters
) : CoroutineWorker(context, params) {
@ -42,6 +44,7 @@ class OfflineOperationsWorker(
private val fileDataStorageManager = FileDataStorageManager(user, context.contentResolver)
private val clientFactory = ClientFactoryImpl(context)
private val notificationManager = OfflineOperationsNotificationManager(context, viewThemeUtils)
override suspend fun doWork(): Result = coroutineScope {
val jobName = inputData.getString(JOB_NAME)
@ -65,7 +68,9 @@ class OfflineOperationsWorker(
val client = clientFactory.create(user)
val offlineOperations = fileDataStorageManager.offlineOperationDao.getAll()
offlineOperations.forEach { operation ->
offlineOperations.forEachIndexed { index, operation ->
notificationManager.prepareForStart(offlineOperations.size, index, "filename")
when (operation.type) {
OfflineOperationType.CreateFolder -> {
val createFolderOperation = async(Dispatchers.IO) { createFolder(operation, client) }
@ -80,6 +85,8 @@ class OfflineOperationsWorker(
Log_OC.d(TAG, "Operation terminated, $operationLog")
fileDataStorageManager.offlineOperationDao.delete(operation)
}
notificationManager.updateNotification(offlineOperations.size, index)
}
else -> {
@ -89,6 +96,7 @@ class OfflineOperationsWorker(
}
Log_OC.d(TAG, "OfflineOperationsWorker successfully completed")
notificationManager.dismissNotification()
WorkerStateLiveData.instance().setWorkState(WorkerState.OfflineOperationsCompleted)
return@coroutineScope Result.success()
}
@ -109,4 +117,6 @@ class OfflineOperationsWorker(
}
}
}
}

View file

@ -1606,7 +1606,7 @@ public class FileDisplayActivity extends FileActivity
public void refreshFolderWithDelay() {
OCFileListFragment fileListFragment = getListOfFilesFragment();
if (fileListFragment != null) {
new Handler(Looper.getMainLooper()).postDelayed(fileListFragment::onRefresh, 3000);
new Handler(Looper.getMainLooper()).postDelayed(fileListFragment::onRefresh, 1500);
}
}

View file

@ -30,7 +30,8 @@
<string name="app_config_base_url_title">Base Url</string>
<string name="app_config_proxy_host_title">Proxy Host Name</string>
<string name="app_config_proxy_port_title">Proxy Port</string>
<string name="offline_operations_worker_notification_manager_ticker">Offline Operations</string>
<string name="offline_operations_worker_progress_text" translatable="false">%1$d / %2$d - %3$s</string>
<string name="anonymous_account_type" translatable="false">AnonymousAccountType</string>
<string name="assistant_screen_task_types_error_state_message">Unable to fetch task types, please check your internet connection.</string>