forked from nextcloud/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratePdfFromImagesWork.kt
More file actions
133 lines (116 loc) · 5.23 KB
/
GeneratePdfFromImagesWork.kt
File metadata and controls
133 lines (116 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2022 Tobias Kaminsky <tobias@kaminsky.me>
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH
* SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
*/
package com.nextcloud.client.documentscan
import android.app.NotificationManager
import android.content.Context
import android.graphics.BitmapFactory
import androidx.annotation.StringRes
import androidx.core.app.NotificationCompat
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.nextcloud.client.account.AnonymousUser
import com.nextcloud.client.account.User
import com.nextcloud.client.account.UserAccountManager
import com.nextcloud.client.jobs.upload.FileUploadHelper
import com.nextcloud.client.jobs.upload.FileUploadWorker
import com.nextcloud.client.logger.Logger
import com.owncloud.android.R
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.files.services.NameCollisionPolicy
import com.owncloud.android.operations.UploadFileOperation
import com.owncloud.android.ui.notifications.NotificationUtils
import com.owncloud.android.utils.theme.ViewThemeUtils
import java.io.File
import java.security.SecureRandom
@Suppress("Detekt.LongParameterList") // constructed only from factory method and tests
class GeneratePdfFromImagesWork(
private val appContext: Context,
private val generatePdfUseCase: GeneratePDFUseCase,
private val viewThemeUtils: ViewThemeUtils,
private val notificationManager: NotificationManager,
private val userAccountManager: UserAccountManager,
private val logger: Logger,
params: WorkerParameters
) : Worker(appContext, params) {
override fun doWork(): Result {
val inputPaths = inputData.getStringArray(INPUT_IMAGE_FILE_PATHS)?.toList()
val outputFilePath = inputData.getString(INPUT_OUTPUT_FILE_PATH)
val uploadFolder = inputData.getString(INPUT_UPLOAD_FOLDER)
val accountName = inputData.getString(INPUT_UPLOAD_ACCOUNT)
@Suppress("Detekt.ComplexCondition") // not that complex
require(!inputPaths.isNullOrEmpty() && outputFilePath != null && uploadFolder != null && accountName != null) {
"PDF generation work started with missing parameters:" +
" inputPaths: $inputPaths, outputFilePath: $outputFilePath," +
" uploadFolder: $uploadFolder, accountName: $accountName"
}
val user = userAccountManager.getUser(accountName)
require(user.isPresent && user.get() !is AnonymousUser) { "Invalid or not found user" }
logger.d(
TAG,
"PDF generation work started with parameters: inputPaths=$inputPaths," +
"outputFilePath=$outputFilePath, uploadFolder=$uploadFolder, accountName=$accountName"
)
val notificationId = showNotification(R.string.document_scan_pdf_generation_in_progress)
val result = generatePdfUseCase.execute(inputPaths, outputFilePath)
notificationManager.cancel(notificationId)
if (result) {
uploadFile(user.get(), uploadFolder, outputFilePath)
cleanupImages(inputPaths)
} else {
logger.w(TAG, "PDF generation failed")
showNotification(R.string.document_scan_pdf_generation_failed)
return Result.failure()
}
logger.d(TAG, "PDF generation work finished")
return Result.success()
}
private fun cleanupImages(inputPaths: List<String>) {
inputPaths.forEach {
val deleted = File(it).delete()
logger.d(TAG, "Deleted $it: success = $deleted")
}
}
private fun showNotification(@StringRes messageRes: Int): Int {
val notificationId = SecureRandom().nextInt()
val message = appContext.getString(messageRes)
val notificationBuilder = NotificationCompat.Builder(
appContext,
NotificationUtils.NOTIFICATION_CHANNEL_GENERAL
)
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(BitmapFactory.decodeResource(appContext.resources, R.drawable.notification_icon))
.setContentText(message)
.setAutoCancel(true)
// NMC Customization
notificationBuilder.color = appContext.resources.getColor(R.color.primary, null)
notificationManager.notify(notificationId, notificationBuilder.build())
return notificationId
}
private fun uploadFile(user: User, uploadFolder: String, pdfPath: String) {
val uploadPath = uploadFolder + OCFile.PATH_SEPARATOR + File(pdfPath).name
FileUploadHelper().uploadNewFiles(
user,
arrayOf(pdfPath),
arrayOf(uploadPath),
// MIME type will be detected from file name
FileUploadWorker.LOCAL_BEHAVIOUR_DELETE,
true,
UploadFileOperation.CREATED_BY_USER,
false,
false,
NameCollisionPolicy.ASK_USER
)
}
companion object {
const val INPUT_IMAGE_FILE_PATHS = "input_image_file_paths"
const val INPUT_OUTPUT_FILE_PATH = "input_output_file_path"
const val INPUT_UPLOAD_FOLDER = "input_upload_folder"
const val INPUT_UPLOAD_ACCOUNT = "input_upload_account"
private const val TAG = "GeneratePdfFromImagesWo"
}
}