-
Notifications
You must be signed in to change notification settings - Fork 20
/
FileUtil.kt
454 lines (403 loc) · 17.7 KB
/
FileUtil.kt
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* Wire
* Copyright (C) 2023 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*
*/
@file:Suppress("TooManyFunctions")
package com.wire.android.util
import android.app.DownloadManager
import android.content.ActivityNotFoundException
import android.content.ContentResolver
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.database.Cursor
import android.graphics.drawable.Drawable
import android.media.MediaMetadataRetriever
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.os.Environment.DIRECTORY_DOWNLOADS
import android.os.Parcelable
import android.provider.MediaStore
import android.provider.MediaStore.MediaColumns.DATA
import android.provider.MediaStore.MediaColumns.DISPLAY_NAME
import android.provider.MediaStore.MediaColumns.MIME_TYPE
import android.provider.MediaStore.MediaColumns.SIZE
import android.provider.OpenableColumns
import android.provider.Settings
import android.webkit.MimeTypeMap
import androidx.annotation.AnyRes
import androidx.annotation.NonNull
import androidx.annotation.VisibleForTesting
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import com.wire.android.R
import com.wire.android.appLogger
import com.wire.android.util.ImageUtil.ImageSizeClass
import com.wire.android.util.ImageUtil.ImageSizeClass.Medium
import com.wire.android.util.dispatchers.DefaultDispatcherProvider
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.kalium.logic.data.asset.isAudioMimeType
import com.wire.kalium.logic.util.buildFileName
import com.wire.kalium.logic.util.splitFileExtensionAndCopyCounter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okio.Path
import java.io.File
import java.io.FileNotFoundException
import java.io.InputStream
import java.util.Locale
import kotlin.time.Duration.Companion.milliseconds
/**
* Gets the uri of any drawable or given resource
* @param context - context
* @param drawableId - drawable res id
* @return - uri
*/
fun getUriFromDrawable(
@NonNull context: Context,
@AnyRes drawableId: Int
): Uri {
return Uri.parse(
ContentResolver.SCHEME_ANDROID_RESOURCE +
"://" + context.resources.getResourcePackageName(drawableId) +
'/' + context.resources.getResourceTypeName(drawableId) +
'/' + context.resources.getResourceEntryName(drawableId)
)
}
@Suppress("MagicNumber")
suspend fun Uri.toByteArray(context: Context, dispatcher: DispatcherProvider = DefaultDispatcherProvider()): ByteArray {
return withContext(dispatcher.io()) {
context.contentResolver.openInputStream(this@toByteArray)?.use { it.readBytes() } ?: ByteArray(16)
}
}
suspend fun Uri.toDrawable(context: Context, dispatcher: DispatcherProvider = DefaultDispatcherProvider()): Drawable? {
val dataUri = this
return withContext(dispatcher.io()) {
try {
context.contentResolver.openInputStream(dataUri).use { inputStream ->
Drawable.createFromStream(inputStream, dataUri.toString())
}
} catch (e: FileNotFoundException) {
defaultGalleryIcon(context)
}
}
}
private fun defaultGalleryIcon(context: Context) = ContextCompat.getDrawable(context, R.drawable.ic_gallery)
fun getTempWritableAttachmentUri(context: Context, attachmentPath: Path): Uri {
val file = attachmentPath.toFile()
file.setWritable(true)
return FileProvider.getUriForFile(context, context.getProviderAuthority(), file)
}
suspend fun createPemFile(
pathname: String,
content: String
): File {
return withContext(Dispatchers.IO) {
return@withContext File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS
),
pathname
).apply {
writeText(content)
}
}
}
private fun Context.saveFileDataToDownloadsFolder(assetName: String, downloadedDataPath: Path, fileSize: Long): Uri? {
val resolver = contentResolver
val mimeType = Uri.parse(downloadedDataPath.toString()).getMimeType(this@saveFileDataToDownloadsFolder)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val contentValues = ContentValues().apply {
// ContentResolver modifies the name if another file with the given name already exists, so we don't have to worry about it
put(DISPLAY_NAME, assetName.ifEmpty { ATTACHMENT_FILENAME })
put(MIME_TYPE, mimeType)
put(SIZE, fileSize)
}
resolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
} else {
val authority = getProviderAuthority()
val downloadsDir = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)
// we need to find the next available name with copy counter by ourselves before copying
val availableAssetName = findFirstUniqueName(downloadsDir, assetName.ifEmpty { ATTACHMENT_FILENAME })
val destinationFile = File(downloadsDir, availableAssetName)
val uri = FileProvider.getUriForFile(this, authority, destinationFile)
if (mimeType?.isNotEmpty() == true) {
val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
downloadManager.addCompletedDownload(
/* title = */ availableAssetName,
/* description = */ availableAssetName,
/* isMediaScannerScannable = */ true,
/* mimeType = */ mimeType,
/* path = */ destinationFile.absolutePath,
/* length = */ fileSize,
/* showNotification = */ false
)
}
uri
}?.also { downloadedUri -> resolver.copyFile(downloadedUri, downloadedDataPath) }
}
fun ContentResolver.copyFile(destinationUri: Uri, sourcePath: Path) {
openOutputStream(destinationUri).use { outputStream ->
val brr = ByteArray(DATA_COPY_BUFFER_SIZE)
var len: Int
val bufferedInputStream: InputStream = File(sourcePath.toString()).inputStream()
while ((bufferedInputStream.read(brr, 0, brr.size).also { len = it }) != -1) {
outputStream?.write(brr, 0, len)
}
outputStream?.flush()
bufferedInputStream.close()
}
}
private fun Context.saveFileDataToMediaFolder(assetName: String, downloadedDataPath: Path, fileSize: Long, mimeType: String): Uri? {
val normalizedFileName = assetName.normalizeFileName()
val resolver = contentResolver
val directory = Environment.getExternalStoragePublicDirectory(
when {
isVideoFile(mimeType) -> Environment.DIRECTORY_MOVIES
isImageFile(mimeType) -> Environment.DIRECTORY_PICTURES
isAudioFile(mimeType) -> Environment.DIRECTORY_MUSIC
else -> Environment.DIRECTORY_DOCUMENTS
}
)
directory.mkdirs()
val contentValues = ContentValues().apply {
val availableAssetName = findFirstUniqueName(directory, normalizedFileName.ifEmpty { ATTACHMENT_FILENAME })
put(DISPLAY_NAME, availableAssetName)
put(MIME_TYPE, mimeType)
put(SIZE, fileSize)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
put(DATA, "$directory/$availableAssetName")
}
}
val volume = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) MediaStore.VOLUME_EXTERNAL else MediaStore.VOLUME_EXTERNAL_PRIMARY
val externalContentUri = when {
isVideoFile(mimeType) -> MediaStore.Video.Media.getContentUri(volume)
isImageFile(mimeType) -> MediaStore.Images.Media.getContentUri(volume)
isAudioFile(mimeType) -> MediaStore.Audio.Media.getContentUri(volume)
else -> MediaStore.Files.getContentUri(volume)
}
val insertedUri = resolver.insert(externalContentUri, contentValues) ?: run {
val authority = getProviderAuthority()
// we need to find the next available name with copy counter by ourselves before copying
val availableAssetName = findFirstUniqueName(directory, normalizedFileName.ifEmpty { ATTACHMENT_FILENAME })
val destinationFile = File(directory, availableAssetName)
FileProvider.getUriForFile(this, authority, destinationFile)
}
resolver.copyFile(insertedUri, downloadedDataPath)
return insertedUri
}
fun Context.pathToUri(assetDataPath: Path, assetName: String?): Uri =
FileProvider.getUriForFile(this, getProviderAuthority(), assetDataPath.toFile(), assetName ?: assetDataPath.name)
fun Uri.getMimeType(context: Context): String? {
val mimeType: String? = if (this.scheme == ContentResolver.SCHEME_CONTENT) {
context.contentResolver.getType(this)
} else {
// If scheme is a File
// This will replace white spaces with %20 and also other special characters.
// This will avoid returning null values on file name with spaces and special characters.
val fileExtension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(this.path?.let { File(it) }).toString())
MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension.lowercase(Locale.getDefault()))
}
return mimeType
}
suspend fun Uri.resampleImageAndCopyToTempPath(
context: Context,
tempCachePath: Path,
sizeClass: ImageSizeClass = Medium,
dispatcher: DispatcherProvider = DefaultDispatcherProvider()
): Long {
return withContext(dispatcher.io()) {
var size: Long
val originalImage = toByteArray(context, dispatcher)
if (originalImage.isEmpty()) return@withContext 0L // if the image is empty, resampling it would cause an exception
ImageUtil.resample(originalImage, sizeClass).let { processedImage ->
val file = tempCachePath.toFile()
size = processedImage.size.toLong()
file.setWritable(true)
file.outputStream().use { it.write(processedImage) }
}
size
}
}
fun Context.getFileName(uri: Uri): String? = when (uri.scheme) {
ContentResolver.SCHEME_CONTENT -> getContentFileName(uri)
else -> uri.path?.let(::File)?.name
}
private fun Context.getContentFileName(uri: Uri): String? = runCatching {
contentResolver.query(uri, null, null, null, null)?.use { cursor ->
cursor.moveToFirst()
return@use cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME).let(cursor::getString)
}
}.getOrNull()
fun Context.startFileShareIntent(path: Path, assetName: String?) {
val assetDisplayName = assetName ?: path.name
val fileURI = FileProvider.getUriForFile(
this, getProviderAuthority(),
path.toFile(), assetDisplayName
)
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
shareIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.export_media_subject_title))
shareIntent.putExtra(Intent.EXTRA_STREAM, fileURI)
assetName?.let { shareIntent.putExtra(Intent.EXTRA_SUBJECT, it) }
shareIntent.type = fileURI.getMimeType(context = this)
startActivity(shareIntent)
}
fun saveFileToDownloadsFolder(assetName: String, assetDataPath: Path, assetDataSize: Long, context: Context): Uri? =
context.saveFileDataToDownloadsFolder(assetName, assetDataPath, assetDataSize)
fun saveFileDataToMediaFolder(assetName: String, assetDataPath: Path, assetDataSize: Long, assetMimeType: String, context: Context): Uri? =
context.saveFileDataToMediaFolder(assetName, assetDataPath, assetDataSize, assetMimeType)
fun Context.multipleFileSharingIntent(uris: ArrayList<Uri>): Intent {
val intent = Intent(Intent.ACTION_SEND_MULTIPLE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
return intent
}
fun Context.getUrisOfFilesInDirectory(dir: File): ArrayList<Uri> {
val files = ArrayList<Uri>()
dir.listFiles()?.map {
val uri = FileProvider.getUriForFile(
this, getProviderAuthority(),
it
)
files.add(uri)
}
return files
}
fun openAssetFileWithExternalApp(assetDataPath: Path, context: Context, assetName: String?, onError: () -> Unit) {
try {
val assetUri = context.pathToUri(assetDataPath, assetName)
val mimeType = assetUri.getMimeType(context)
// Set intent and launch
val intent = Intent()
intent.apply {
action = Intent.ACTION_VIEW
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
setDataAndType(assetUri, mimeType)
}
context.startActivity(intent)
} catch (e: java.lang.IllegalArgumentException) {
appLogger.e("The file couldn't be found on the internal storage \n$e")
onError()
} catch (noActivityFoundException: ActivityNotFoundException) {
appLogger.e("Couldn't find a proper app to process the asset")
onError()
}
}
fun shareAssetFileWithExternalApp(assetDataPath: Path, context: Context, assetName: String?, onError: () -> Unit) {
try {
val assetUri = context.pathToUri(assetDataPath, assetName)
val mimeType = assetUri.getMimeType(context)
// Set intent and launch
val intent = Intent()
intent.apply {
action = Intent.ACTION_SEND
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
setDataAndType(assetUri, mimeType)
putExtra(Intent.EXTRA_STREAM, assetUri)
}
context.startActivity(intent)
} catch (e: java.lang.IllegalArgumentException) {
appLogger.e("The file couldn't be found on the internal storage \n$e")
onError()
} catch (noActivityFoundException: ActivityNotFoundException) {
appLogger.e("Couldn't find a proper app to process the asset")
onError()
}
}
inline fun <reified T : Parcelable> Intent.parcelable(key: String): T? = when {
Build.VERSION.SDK_INT >= SDK_VERSION -> getParcelableExtra(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelableExtra(key) as? T
}
inline fun <reified T : Parcelable> Intent.parcelableArrayList(key: String): ArrayList<T>? = when {
Build.VERSION.SDK_INT >= SDK_VERSION -> getParcelableArrayListExtra(key, T::class.java)
else -> @Suppress("DEPRECATION") getParcelableArrayListExtra(key)
}
fun Uri.getMetadataFromUri(context: Context): FileMetaData {
return context.contentResolver.query(this, null, null, null, null)?.use { cursor ->
if (cursor.moveToFirst()) {
val displayName = cursor.getValidDisplayName()
val mimeType = cursor.getValidMimeType()
val size = cursor.getValidSize()
FileMetaData(displayName, size, mimeType)
} else {
FileMetaData()
}
} ?: FileMetaData()
}
private fun Cursor.getValidDisplayName(): String =
getColumnIndex(DISPLAY_NAME).run { takeIf { it > -1 }?.let { getString(it) } ?: "" }
private fun Cursor.getValidMimeType(): String =
getColumnIndex(MIME_TYPE).run { takeIf { it > -1 }?.let { getString(it) } ?: "" }
private fun Cursor.getValidSize(): Long =
getColumnIndex(SIZE).run { takeIf { it > -1 }?.let { getLong(it) } ?: 0L }
data class FileMetaData(val name: String = "", val sizeInBytes: Long = 0L, val mimeType: String = "")
fun isImageFile(mimeType: String?): Boolean {
return mimeType != null && mimeType.startsWith("image/")
}
fun isVideoFile(mimeType: String?): Boolean {
return mimeType != null && mimeType.startsWith("video/")
}
fun isAudioFile(mimeType: String?): Boolean {
return mimeType != null && mimeType.startsWith("audio/")
}
fun isText(mimeType: String?): Boolean {
return mimeType != null && mimeType.startsWith("text/")
}
@Suppress("MagicNumber")
fun Context.getDeviceIdString(): String? {
if (Build.VERSION.SDK_INT >= 26) {
return Settings.Secure.getString(this.contentResolver, Settings.Secure.ANDROID_ID)
}
return null
}
fun Context.getGitBuildId(): String = runCatching {
assets.open("version.txt").use { inputStream ->
inputStream.bufferedReader().use { it.readText() }
}
}.getOrDefault("")
fun Context.getProviderAuthority() = "$packageName.provider"
@VisibleForTesting
fun findFirstUniqueName(dir: File, desiredName: String): String {
var currentName: String = desiredName
while (File(dir, currentName).exists()) {
val (nameWithoutCopyCounter, copyCounter, extension) = currentName.splitFileExtensionAndCopyCounter()
currentName = buildFileName(nameWithoutCopyCounter, extension, copyCounter + 1)
}
return currentName
}
fun getAudioLengthInMs(dataPath: Path, mimeType: String): Long =
if (isAudioMimeType(mimeType)) {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(dataPath.toFile().absolutePath)
val rawDuration = retriever
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
?.toLong() ?: 0L
rawDuration.milliseconds.inWholeMilliseconds
} else {
0L
}
private const val ATTACHMENT_FILENAME = "attachment"
private const val DATA_COPY_BUFFER_SIZE = 2048
const val SDK_VERSION = 33
const val AUDIO_MIME_TYPE = "audio/mp4"