Skip to content

Commit 97b9036

Browse files
committed
fix(upload-list-adapter): FileUriExposedException
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent e95ecba commit 97b9036

1 file changed

Lines changed: 30 additions & 13 deletions

File tree

‎app/src/main/java/com/owncloud/android/ui/adapter/uploadList/helper/UploadListAdapterHelper.kt‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package com.owncloud.android.ui.adapter.uploadList.helper
1010
import android.content.ActivityNotFoundException
1111
import android.content.Intent
1212
import android.net.Uri
13+
import androidx.core.content.FileProvider
1314
import com.owncloud.android.R
1415
import com.owncloud.android.datamodel.OCFile
1516
import com.owncloud.android.db.OCUpload
@@ -19,22 +20,24 @@ import com.owncloud.android.ui.activity.FileActivity
1920
import com.owncloud.android.ui.activity.FileDisplayActivity
2021
import com.owncloud.android.ui.preview.PreviewImageFragment
2122
import com.owncloud.android.utils.DisplayUtils
23+
import com.owncloud.android.utils.MimeType
2224
import com.owncloud.android.utils.MimeTypeUtil
2325
import java.io.File
2426

2527
class UploadListAdapterHelper(private val activity: FileActivity) {
2628

2729
companion object {
2830
private const val TAG = "UploadListAdapterHelper"
31+
private const val ANY_MIME_TYPE = "*/*"
2932
}
3033

3134
fun openConflictActivity(file: OCFile, upload: OCUpload) {
3235
file.setStoragePath(upload.localPath)
3336
val user = activity.accountManager.getUser(upload.accountName)
34-
if (user.isPresent) {
37+
user.ifPresent {
3538
val intent = ConflictsResolveActivity.createIntent(
3639
file,
37-
user.get(),
40+
it,
3841
upload.uploadId,
3942
Intent.FLAG_ACTIVITY_NEW_TASK,
4043
activity
@@ -44,12 +47,12 @@ class UploadListAdapterHelper(private val activity: FileActivity) {
4447
}
4548

4649
fun onUploadingItemClick(file: OCUpload) {
47-
val f = File(file.localPath)
48-
if (!f.exists()) {
49-
DisplayUtils.showSnackMessage(activity, R.string.local_file_not_found_message)
50-
} else {
50+
if (File(file.localPath).exists()) {
5151
openFileWithDefault(file.localPath)
52+
return
5253
}
54+
55+
DisplayUtils.showSnackMessage(activity, R.string.local_file_not_found_message)
5356
}
5457

5558
fun onUploadedItemClick(upload: OCUpload) {
@@ -77,17 +80,31 @@ class UploadListAdapterHelper(private val activity: FileActivity) {
7780
}
7881

7982
fun openFileWithDefault(localPath: String) {
83+
val uri = exposedUriFor(localPath)
84+
if (uri == null) {
85+
DisplayUtils.showSnackMessage(activity, R.string.error_retrieving_file)
86+
return
87+
}
88+
8089
var mimetype = MimeTypeUtil.getBestMimeTypeByFilename(localPath)
81-
if (mimetype == "application/octet-stream") mimetype = "*/*"
90+
if (mimetype == MimeType.FILE) mimetype = ANY_MIME_TYPE
91+
8292
try {
83-
activity.startActivity(
84-
Intent(Intent.ACTION_VIEW).apply {
85-
setDataAndType(Uri.fromFile(File(localPath)), mimetype)
86-
}
87-
)
93+
val intent = Intent(Intent.ACTION_VIEW).apply {
94+
setDataAndType(uri, mimetype)
95+
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
96+
}
97+
activity.startActivity(intent)
8898
} catch (e: ActivityNotFoundException) {
8999
DisplayUtils.showSnackMessage(activity, R.string.file_list_no_app_for_file_type)
90-
Log_OC.i(TAG, "Could not find app for sending log history: $e")
100+
Log_OC.i(TAG, "Could not find app for opening the local file: $e")
91101
}
92102
}
103+
104+
private fun exposedUriFor(localPath: String): Uri? = try {
105+
FileProvider.getUriForFile(activity, activity.getString(R.string.file_provider_authority), File(localPath))
106+
} catch (e: IllegalArgumentException) {
107+
Log_OC.e(TAG, "Local file is outside the paths supported by the file provider: $e")
108+
null
109+
}
93110
}

0 commit comments

Comments
 (0)