@@ -10,6 +10,7 @@ package com.owncloud.android.ui.adapter.uploadList.helper
1010import android.content.ActivityNotFoundException
1111import android.content.Intent
1212import android.net.Uri
13+ import androidx.core.content.FileProvider
1314import com.owncloud.android.R
1415import com.owncloud.android.datamodel.OCFile
1516import com.owncloud.android.db.OCUpload
@@ -19,22 +20,24 @@ import com.owncloud.android.ui.activity.FileActivity
1920import com.owncloud.android.ui.activity.FileDisplayActivity
2021import com.owncloud.android.ui.preview.PreviewImageFragment
2122import com.owncloud.android.utils.DisplayUtils
23+ import com.owncloud.android.utils.MimeType
2224import com.owncloud.android.utils.MimeTypeUtil
2325import java.io.File
2426
2527class 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