diff --git a/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/OfflineOperationsNotificationManager.kt b/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/OfflineOperationsNotificationManager.kt index a27fd1cb20..8befa75b06 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/OfflineOperationsNotificationManager.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/offlineOperations/OfflineOperationsNotificationManager.kt @@ -10,6 +10,7 @@ package com.nextcloud.client.jobs.offlineOperations import android.app.PendingIntent import android.content.Context import androidx.core.app.NotificationCompat +import com.nextcloud.client.database.entity.OfflineOperationEntity import com.nextcloud.client.jobs.notification.WorkerNotificationManager import com.owncloud.android.R import com.owncloud.android.datamodel.OCFile @@ -76,13 +77,19 @@ class OfflineOperationsNotificationManager(private val context: Context, viewThe } } - fun showConflictResolveNotification(file: OCFile, path: String) { - val notificationId = file.fileId.toInt() + fun showConflictResolveNotification(file: OCFile, entity: OfflineOperationEntity?) { + val path = entity?.path + val id = entity?.id + + if (path == null || id == null) { + return + } + val intent = ConflictsResolveActivity.createIntent(file, path, context) val pendingIntent = PendingIntent.getActivity( context, - notificationId, + id, intent, PendingIntent.FLAG_IMMUTABLE ) @@ -104,9 +111,11 @@ class OfflineOperationsNotificationManager(private val context: Context, viewThe .setContentIntent(pendingIntent) .addAction(action) - notificationManager.notify( - notificationId, - notificationBuilder.build() - ) + notificationManager.notify(id, notificationBuilder.build()) + } + + fun dismissNotification(id: Int?) { + if (id == null) return + notificationManager.cancel(id) } } diff --git a/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 299929f5a1..a67ceb98e8 100644 --- a/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -68,6 +68,7 @@ import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -233,6 +234,29 @@ public class FileDataStorageManager { addCreateFolderOfflineOperation(newPath, newFolderName, file.getParentId()); } + @SuppressLint("SimpleDateFormat") + public void keepOfflineOperationAndServerFile(OfflineOperationEntity entity) { + Long parentOCFileId = entity.getParentOCFileId(); + if (parentOCFileId == null) return; + + OCFile parentFolder = getFileById(parentOCFileId); + if (parentFolder == null) return; + + SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy - HH:mm"); + String currentDateTime = sdf.format(new Date()); + + String newFolderName = entity.getFilename() + " - " + currentDateTime; + String newPath = parentFolder.getDecryptedRemotePath() + newFolderName + OCFile.PATH_SEPARATOR; + entity.setPath(newPath); + entity.setFilename(newFolderName); + + offlineOperationDao.update(entity); + + OCFile file = new OCFile(entity.getPath()); + file.setMimeType(MimeType.DIRECTORY); + saveFileWithParent(file, MainApp.getAppContext()); + } + private @Nullable OCFile getFileByPath(String type, String path) { final boolean shouldUseEncryptedPath = ProviderTableMeta.FILE_PATH.equals(type); diff --git a/app/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.kt b/app/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.kt index accee1702b..e0dfdd6b2d 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.kt +++ b/app/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.kt @@ -17,6 +17,7 @@ import androidx.fragment.app.FragmentTransaction import androidx.lifecycle.lifecycleScope import com.nextcloud.client.account.User import com.nextcloud.client.jobs.download.FileDownloadHelper +import com.nextcloud.client.jobs.offlineOperations.OfflineOperationsNotificationManager import com.nextcloud.client.jobs.upload.FileUploadHelper import com.nextcloud.client.jobs.upload.FileUploadWorker import com.nextcloud.client.jobs.upload.UploadNotificationManager @@ -24,7 +25,6 @@ import com.nextcloud.model.HTTPStatusCodes import com.nextcloud.utils.extensions.getParcelableArgument import com.nextcloud.utils.extensions.logFileSize import com.owncloud.android.R -import com.owncloud.android.datamodel.FileDataStorageManager import com.owncloud.android.datamodel.OCFile import com.owncloud.android.datamodel.UploadsStorageManager import com.owncloud.android.db.OCUpload @@ -47,14 +47,12 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener @Inject lateinit var uploadsStorageManager: UploadsStorageManager - @Inject - lateinit var fileStorageManager: FileDataStorageManager - private var conflictUploadId: Long = 0 private var offlineOperationPath: String? = null private var existingFile: OCFile? = null private var newFile: OCFile? = null private var localBehaviour = FileUploadWorker.LOCAL_BEHAVIOUR_FORGET + private lateinit var offlineOperationNotificationManager: OfflineOperationsNotificationManager @JvmField var listener: OnConflictDecisionMadeListener? = null @@ -72,6 +70,7 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener // new file was modified locally in file system newFile = file setupOnConflictDecisionMadeListener(upload) + offlineOperationNotificationManager = OfflineOperationsNotificationManager(this, viewThemeUtils) } private fun getArguments(savedInstanceState: Bundle?) { @@ -130,7 +129,9 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener } Decision.KEEP_BOTH_FOLDER -> { - + fileDataStorageManager.keepOfflineOperationAndServerFile(entity) + backgroundJobManager.startOfflineOperations() + offlineOperationNotificationManager.dismissNotification(offlineOperation.id) } else -> Unit @@ -140,7 +141,6 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener } } - private fun keepLocal(file: OCFile?, upload: OCUpload?, user: User) { upload?.let { FileUploadHelper.instance().removeFileUpload(it.remotePath, it.accountName) @@ -233,7 +233,7 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener } if (existingFile == null) { - val remotePath = fileStorageManager.retrieveRemotePathConsideringEncryption(newFile) ?: return + val remotePath = fileDataStorageManager.retrieveRemotePathConsideringEncryption(newFile) ?: return val operation = ReadFileRemoteOperation(remotePath) @Suppress("TooGenericExceptionCaught") @@ -254,7 +254,7 @@ class ConflictsResolveActivity : FileActivity(), OnConflictDecisionMadeListener } } } else { - val remotePath = fileStorageManager.retrieveRemotePathConsideringEncryption(existingFile) ?: return + val remotePath = fileDataStorageManager.retrieveRemotePathConsideringEncryption(existingFile) ?: return startDialog(remotePath) } } diff --git a/app/src/main/java/com/owncloud/android/ui/activity/fileDisplayActivity/OfflineFolderConflictManager.kt b/app/src/main/java/com/owncloud/android/ui/activity/fileDisplayActivity/OfflineFolderConflictManager.kt index d9ffffa62e..3f1bf23d3d 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/fileDisplayActivity/OfflineFolderConflictManager.kt +++ b/app/src/main/java/com/owncloud/android/ui/activity/fileDisplayActivity/OfflineFolderConflictManager.kt @@ -45,7 +45,8 @@ class OfflineFolderConflictManager(private val activity: FileDisplayActivity) { remoteIdsToOperationPaths.forEach { (remoteId, path) -> val file = activity.storageManager.getFileByRemoteId(remoteId) file?.let { - notificationManager.showConflictResolveNotification(file, path) + val entity = activity.storageManager.offlineOperationDao.getByPath(path) + notificationManager.showConflictResolveNotification(file, entity) } } }