forked from nextcloud/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMediaFoldersDetectionWork.kt
More file actions
286 lines (265 loc) · 12 KB
/
MediaFoldersDetectionWork.kt
File metadata and controls
286 lines (265 loc) · 12 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Nextcloud Android client application
*
* @author Mario Danic
* @author Andy Scherzinger
* @author Chris Narkiewicz
* Copyright (C) 2018 Mario Danic
* Copyright (C) 2018 Andy Scherzinger
* Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com>
*
* SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
*/
package com.nextcloud.client.jobs
import android.app.Activity
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.ContentResolver
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.graphics.BitmapFactory
import android.text.TextUtils
import androidx.core.app.NotificationCompat
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.google.gson.Gson
import com.nextcloud.client.account.User
import com.nextcloud.client.account.UserAccountManager
import com.nextcloud.client.core.Clock
import com.nextcloud.client.preferences.AppPreferences
import com.nextcloud.client.preferences.AppPreferencesImpl
import com.owncloud.android.MainApp
import com.owncloud.android.R
import com.owncloud.android.datamodel.ArbitraryDataProvider
import com.owncloud.android.datamodel.ArbitraryDataProviderImpl
import com.owncloud.android.datamodel.MediaFolderType
import com.owncloud.android.datamodel.MediaFoldersModel
import com.owncloud.android.datamodel.MediaProvider
import com.owncloud.android.datamodel.SyncedFolderProvider
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.ui.activity.ManageAccountsActivity
import com.owncloud.android.ui.activity.SyncedFoldersActivity
import com.owncloud.android.ui.notifications.NotificationUtils
import com.owncloud.android.utils.SyncedFolderUtils
import com.owncloud.android.utils.theme.ViewThemeUtils
import java.util.Random
@Suppress("LongParameterList") // dependencies injection
class MediaFoldersDetectionWork(
private val context: Context,
params: WorkerParameters,
private val resources: Resources,
private val contentResolver: ContentResolver,
private val userAccountManager: UserAccountManager,
private val preferences: AppPreferences,
private val clock: Clock,
private val viewThemeUtils: ViewThemeUtils,
private val syncedFolderProvider: SyncedFolderProvider
) : Worker(context, params) {
companion object {
const val TAG = "MediaFoldersDetectionJob"
const val KEY_MEDIA_FOLDER_PATH = "KEY_MEDIA_FOLDER_PATH"
const val KEY_MEDIA_FOLDER_TYPE = "KEY_MEDIA_FOLDER_TYPE"
private const val ACCOUNT_NAME_GLOBAL = "global"
private const val KEY_MEDIA_FOLDERS = "media_folders"
const val NOTIFICATION_ID = "NOTIFICATION_ID"
private val DISABLE_DETECTION_CLICK = MainApp.getAuthority() + "_DISABLE_DETECTION_CLICK"
}
private val randomIdGenerator = Random(clock.currentTime)
@Suppress("LongMethod", "ComplexMethod", "NestedBlockDepth", "ReturnCount") // legacy code
override fun doWork(): Result {
val arbitraryDataProvider: ArbitraryDataProvider = ArbitraryDataProviderImpl(context)
val gson = Gson()
val mediaFoldersModel: MediaFoldersModel
val imageMediaFolders = MediaProvider.getImageFolders(
contentResolver,
1,
null,
true
)
val videoMediaFolders = MediaProvider.getVideoFolders(
contentResolver,
1,
null,
true
)
val imageMediaFolderPaths: MutableList<String> = ArrayList()
val videoMediaFolderPaths: MutableList<String> = ArrayList()
for (imageMediaFolder in imageMediaFolders) {
imageMediaFolder.absolutePath?.let {
imageMediaFolderPaths.add(it)
}
}
for (videoMediaFolder in videoMediaFolders) {
videoMediaFolder.absolutePath?.let {
imageMediaFolderPaths.add(it)
}
}
val arbitraryDataString = arbitraryDataProvider.getValue(ACCOUNT_NAME_GLOBAL, KEY_MEDIA_FOLDERS)
if (!TextUtils.isEmpty(arbitraryDataString)) {
mediaFoldersModel = gson.fromJson(arbitraryDataString, MediaFoldersModel::class.java)
// merge new detected paths with already notified ones
for (existingImageFolderPath in mediaFoldersModel.imageMediaFolders) {
if (!imageMediaFolderPaths.contains(existingImageFolderPath)) {
imageMediaFolderPaths.add(existingImageFolderPath)
}
}
for (existingVideoFolderPath in mediaFoldersModel.videoMediaFolders) {
if (!videoMediaFolderPaths.contains(existingVideoFolderPath)) {
videoMediaFolderPaths.add(existingVideoFolderPath)
}
}
// Store updated values
arbitraryDataProvider.storeOrUpdateKeyValue(
ACCOUNT_NAME_GLOBAL,
KEY_MEDIA_FOLDERS,
gson.toJson(MediaFoldersModel(imageMediaFolderPaths, videoMediaFolderPaths))
)
if (preferences.isShowMediaScanNotifications) {
imageMediaFolderPaths.removeAll(mediaFoldersModel.imageMediaFolders)
videoMediaFolderPaths.removeAll(mediaFoldersModel.videoMediaFolders)
if (imageMediaFolderPaths.isNotEmpty() || videoMediaFolderPaths.isNotEmpty()) {
val allUsers = userAccountManager.allUsers
val activeUsers: MutableList<User> = ArrayList()
for (user in allUsers) {
if (!arbitraryDataProvider.getBooleanValue(user, ManageAccountsActivity.PENDING_FOR_REMOVAL)) {
activeUsers.add(user)
}
}
for (user in activeUsers) {
for (imageMediaFolder in imageMediaFolderPaths) {
val folder = syncedFolderProvider.findByLocalPathAndAccount(
imageMediaFolder,
user
)
if (folder == null &&
SyncedFolderUtils.isQualifyingMediaFolder(imageMediaFolder, MediaFolderType.IMAGE)
) {
val contentTitle = String.format(
resources.getString(R.string.new_media_folder_detected),
resources.getString(R.string.new_media_folder_photos)
)
sendNotification(
contentTitle,
imageMediaFolder.substring(imageMediaFolder.lastIndexOf('/') + 1),
user,
imageMediaFolder,
MediaFolderType.IMAGE.id
)
}
}
for (videoMediaFolder in videoMediaFolderPaths) {
val folder = syncedFolderProvider.findByLocalPathAndAccount(
videoMediaFolder,
user
)
if (folder == null) {
val contentTitle = String.format(
context.getString(R.string.new_media_folder_detected),
context.getString(R.string.new_media_folder_videos)
)
sendNotification(
contentTitle,
videoMediaFolder.substring(videoMediaFolder.lastIndexOf('/') + 1),
user,
videoMediaFolder,
MediaFolderType.VIDEO.id
)
}
}
}
}
}
} else {
mediaFoldersModel = MediaFoldersModel(imageMediaFolderPaths, videoMediaFolderPaths)
arbitraryDataProvider.storeOrUpdateKeyValue(
ACCOUNT_NAME_GLOBAL,
KEY_MEDIA_FOLDERS,
gson.toJson(mediaFoldersModel)
)
}
return Result.success()
}
@Suppress("LongMethod")
private fun sendNotification(contentTitle: String, subtitle: String, user: User, path: String, type: Int) {
val notificationId = randomIdGenerator.nextInt()
val intent = Intent(context, SyncedFoldersActivity::class.java).apply {
putExtra(NOTIFICATION_ID, notificationId)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
putExtra(NotificationWork.KEY_NOTIFICATION_ACCOUNT, user.accountName)
putExtra(KEY_MEDIA_FOLDER_PATH, path)
putExtra(KEY_MEDIA_FOLDER_TYPE, type)
}
val pendingIntent = PendingIntent.getActivity(
context,
0,
intent,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)
val notificationBuilder = NotificationCompat.Builder(
context,
NotificationUtils.NOTIFICATION_CHANNEL_GENERAL
)
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.notification_icon))
.setSubText(user.accountName)
.setContentTitle(contentTitle)
.setContentText(subtitle)
.setAutoCancel(true)
.setSound(null)
.setVibrate(null)
.setSilent(true)
.setContentIntent(pendingIntent)
// NMC Customization
notificationBuilder.color = context.resources.getColor(R.color.primary, null)
val disableDetection = Intent(context, NotificationReceiver::class.java).apply {
putExtra(NOTIFICATION_ID, notificationId)
action = DISABLE_DETECTION_CLICK
}
val disableIntent = PendingIntent.getBroadcast(
context,
notificationId,
disableDetection,
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
notificationBuilder.addAction(
NotificationCompat.Action(
R.drawable.ic_close,
context.getString(R.string.disable_new_media_folder_detection_notifications),
disableIntent
)
)
val configureIntent = PendingIntent.getActivity(
context,
notificationId,
intent,
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
notificationBuilder.addAction(
NotificationCompat.Action(
R.drawable.ic_settings,
context.getString(R.string.configure_new_media_folder_detection_notifications),
configureIntent
)
)
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(notificationId, notificationBuilder.build())
}
class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
val notificationId = intent.getIntExtra(NOTIFICATION_ID, 0)
val preferences = AppPreferencesImpl.fromContext(context)
if (DISABLE_DETECTION_CLICK == action) {
Log_OC.d(this, "Disable media scan notifications")
preferences.isShowMediaScanNotifications = false
cancel(context, notificationId)
}
}
private fun cancel(context: Context, notificationId: Int) {
val notificationManager = context.getSystemService(Activity.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(notificationId)
}
}
}