@@ -11,7 +11,9 @@ import com.ismartcoding.plain.Constants
1111import com.ismartcoding.plain.R
1212import com.ismartcoding.plain.features.locale.LocaleHelper.getString
1313import com.ismartcoding.plain.ui.MainActivity
14+ import com.ismartcoding.plain.ui.helpers.DialogHelper
1415import java.io.File
16+ import java.io.IOException
1517
1618object ShareHelper {
1719 fun shareUri (
@@ -99,27 +101,94 @@ object ShareHelper {
99101 }
100102 }
101103
104+ /* *
105+ * Check if a file can be accessed by the FileProvider
106+ */
107+ private fun isFileAccessibleByProvider (file : File ): Boolean {
108+ if (! file.exists() || ! file.canRead()) {
109+ return false
110+ }
111+
112+ val path = file.absolutePath
113+ // Files in /apex/ directory are modular system components and cannot be shared via FileProvider
114+ if (path.startsWith(" /apex/" )) {
115+ return false
116+ }
117+
118+ // Files in other system directories may also be inaccessible
119+ if (path.startsWith(" /system/" ) || path.startsWith(" /vendor/" ) || path.startsWith(" /product/" )) {
120+ // Try to access the file to see if it's readable
121+ try {
122+ file.inputStream().use { it.read() }
123+ return true
124+ } catch (e: Exception ) {
125+ return false
126+ }
127+ }
128+
129+ return true
130+ }
131+
102132 fun shareFile (
103133 context : Context ,
104134 file : File
105135 ) {
106- val intent = Intent (Intent .ACTION_SEND )
107- intent.type = file.path.getMimeType()
108- intent.putExtra(Intent .EXTRA_STREAM , FileProvider .getUriForFile(context, Constants .AUTHORITY , file))
109- intent.addFlags(Intent .FLAG_GRANT_READ_URI_PERMISSION )
110- val chooserIntent = Intent .createChooser(intent, getString(R .string.share))
111- chooserIntent.putExtra(Intent .EXTRA_EXCLUDE_COMPONENTS , getExcludeComponentNames(context).toTypedArray())
112- context.startActivity(chooserIntent)
136+ // Check if the file can be accessed by FileProvider
137+ if (! isFileAccessibleByProvider(file)) {
138+ // Show a message that this system file cannot be shared
139+ val errorMessage = if (file.absolutePath.startsWith(" /apex/" )) {
140+ getString(R .string.cannot_share_system_component)
141+ } else {
142+ getString(R .string.cannot_share_system_file)
143+ }
144+ DialogHelper .showErrorMessage(errorMessage)
145+ return
146+ }
147+
148+ try {
149+ val intent = Intent (Intent .ACTION_SEND )
150+ intent.type = file.path.getMimeType()
151+ intent.putExtra(Intent .EXTRA_STREAM , FileProvider .getUriForFile(context, Constants .AUTHORITY , file))
152+ intent.addFlags(Intent .FLAG_GRANT_READ_URI_PERMISSION )
153+ val chooserIntent = Intent .createChooser(intent, getString(R .string.share))
154+ chooserIntent.putExtra(Intent .EXTRA_EXCLUDE_COMPONENTS , getExcludeComponentNames(context).toTypedArray())
155+ context.startActivity(chooserIntent)
156+ } catch (e: IllegalArgumentException ) {
157+ // This handles the "Failed to find configured root" error
158+ val errorMessage = getString(R .string.cannot_share_file_not_accessible)
159+ DialogHelper .showErrorMessage(errorMessage)
160+ } catch (e: Exception ) {
161+ DialogHelper .showErrorMessage(e.message ? : getString(R .string.unknown_error))
162+ }
113163 }
114164
115165 fun shareFiles (
116166 context : Context ,
117167 files : List <File >,
118168 ) {
119169 val fileUris = arrayListOf<Uri >()
170+ val inaccessibleFiles = mutableListOf<String >()
120171
121172 for (file in files) {
122- fileUris.add(FileProvider .getUriForFile(context, Constants .AUTHORITY , file))
173+ if (isFileAccessibleByProvider(file)) {
174+ try {
175+ fileUris.add(FileProvider .getUriForFile(context, Constants .AUTHORITY , file))
176+ } catch (e: IllegalArgumentException ) {
177+ inaccessibleFiles.add(file.name)
178+ }
179+ } else {
180+ inaccessibleFiles.add(file.name)
181+ }
182+ }
183+
184+ if (fileUris.isEmpty()) {
185+ DialogHelper .showErrorMessage(getString(R .string.cannot_share_any_files))
186+ return
187+ }
188+
189+ if (inaccessibleFiles.isNotEmpty()) {
190+ val message = getString(R .string.some_files_cannot_be_shared) + " : ${inaccessibleFiles.joinToString(" , " )} "
191+ DialogHelper .showErrorMessage(message)
123192 }
124193
125194 val intent = Intent (Intent .ACTION_SEND_MULTIPLE )
@@ -135,16 +204,33 @@ object ShareHelper {
135204 context : Context ,
136205 path : String ,
137206 ) {
138- val intent = Intent ()
139- intent.action = Intent .ACTION_VIEW
140- intent.addCategory(Intent .CATEGORY_DEFAULT )
141- val uri = FileProvider .getUriForFile(context, Constants .AUTHORITY , File (path))
142- val mimeType = path.getMimeType()
143- intent.setDataAndType(uri, mimeType)
144- intent.putExtra(" mimeType" , mimeType)
145- intent.addFlags(Intent .FLAG_GRANT_READ_URI_PERMISSION )
146- val chooserIntent = Intent .createChooser(intent, getString(R .string.open_with))
147- chooserIntent.putExtra(Intent .EXTRA_EXCLUDE_COMPONENTS , getExcludeComponentNames(context).toTypedArray())
148- context.startActivity(chooserIntent)
207+ val file = File (path)
208+ if (! isFileAccessibleByProvider(file)) {
209+ val errorMessage = if (file.absolutePath.startsWith(" /apex/" )) {
210+ getString(R .string.cannot_open_system_component)
211+ } else {
212+ getString(R .string.cannot_open_system_file)
213+ }
214+ DialogHelper .showErrorMessage(errorMessage)
215+ return
216+ }
217+
218+ try {
219+ val intent = Intent ()
220+ intent.action = Intent .ACTION_VIEW
221+ intent.addCategory(Intent .CATEGORY_DEFAULT )
222+ val uri = FileProvider .getUriForFile(context, Constants .AUTHORITY , file)
223+ val mimeType = path.getMimeType()
224+ intent.setDataAndType(uri, mimeType)
225+ intent.putExtra(" mimeType" , mimeType)
226+ intent.addFlags(Intent .FLAG_GRANT_READ_URI_PERMISSION )
227+ val chooserIntent = Intent .createChooser(intent, getString(R .string.open_with))
228+ chooserIntent.putExtra(Intent .EXTRA_EXCLUDE_COMPONENTS , getExcludeComponentNames(context).toTypedArray())
229+ context.startActivity(chooserIntent)
230+ } catch (e: IllegalArgumentException ) {
231+ DialogHelper .showErrorMessage(getString(R .string.cannot_open_file_not_accessible))
232+ } catch (e: Exception ) {
233+ DialogHelper .showErrorMessage(e.message ? : getString(R .string.unknown_error))
234+ }
149235 }
150236}
0 commit comments