1
+ /* *
2
+ * SPDX-FileCopyrightText: 2025 microG Project Team
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ package org.microg.gms.settings
7
+
8
+ import android.content.Context
9
+ import android.content.pm.ProviderInfo
10
+ import android.database.Cursor
11
+ import android.database.MatrixCursor
12
+ import android.net.Uri
13
+ import android.os.CancellationSignal
14
+ import android.os.ParcelFileDescriptor
15
+ import android.util.Log
16
+ import androidx.core.content.FileProvider
17
+ import java.io.FileNotFoundException
18
+
19
+ private const val TAG = " GmsFileProvider"
20
+
21
+ class GmsFileProvider : FileProvider () {
22
+ private val emptyProjection = arrayOfNulls<String >(0 )
23
+ private var initializationFailed = false
24
+
25
+ override fun attachInfo (context : Context , info : ProviderInfo ) {
26
+ try {
27
+ super .attachInfo(context, info)
28
+ } catch (e: Exception ) {
29
+ initializationFailed = true
30
+ Log .e(TAG , " attachInfo error:${e.message} " )
31
+ }
32
+ }
33
+
34
+ override fun onCreate (): Boolean {
35
+ return true
36
+ }
37
+
38
+ override fun getType (uri : Uri ): String? {
39
+ if (initializationFailed) {
40
+ return null
41
+ }
42
+ return super .getType(uri)
43
+ }
44
+
45
+ override fun openFile (
46
+ uri : Uri , mode : String , signal : CancellationSignal ?
47
+ ): ParcelFileDescriptor ? {
48
+ if (! initializationFailed) {
49
+ return super .openFile(uri, mode, signal)
50
+ }
51
+ throw FileNotFoundException (" FileProvider creation failed" )
52
+ }
53
+
54
+ override fun delete (uri : Uri , selection : String? , selectionArgs : Array <out String >? ): Int {
55
+ if (initializationFailed) {
56
+ return 0
57
+ }
58
+ return super .delete(uri, selection, selectionArgs)
59
+ }
60
+
61
+ override fun query (
62
+ uri : Uri , projection : Array <out String >? , selection : String? , selectionArgs : Array <out String >? , sortOrder : String?
63
+ ): Cursor {
64
+ if (initializationFailed) {
65
+ return MatrixCursor (emptyProjection)
66
+ }
67
+ return super .query(uri, projection, selection, selectionArgs, sortOrder)
68
+ }
69
+ }
0 commit comments