@@ -18,6 +18,7 @@ object FileManagerHelper {
18
18
)
19
19
for (pkg in launchPackages) {
20
20
pm.getLaunchIntentForPackage(pkg)?.let {
21
+ it.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
21
22
context.startActivity(it)
22
23
return true
23
24
}
@@ -34,7 +35,7 @@ object FileManagerHelper {
34
35
)
35
36
val mime = context.contentResolver.getType(uri) ? : " */*"
36
37
val intent = Intent (Intent .ACTION_VIEW ).setDataAndType(uri, mime)
37
- intent.addFlags(Intent .FLAG_GRANT_READ_URI_PERMISSION )
38
+ intent.addFlags(Intent .FLAG_GRANT_READ_URI_PERMISSION or Intent . FLAG_ACTIVITY_NEW_TASK )
38
39
if (intent.resolveActivity(context.packageManager) != null ) {
39
40
context.startActivity(intent)
40
41
} else {
@@ -57,7 +58,12 @@ object FileManagerHelper {
57
58
}
58
59
}
59
60
60
- fun openFolderOrSettings (context : Context , folder : File ) {
61
+ private fun openFolder (
62
+ context : Context ,
63
+ folder : File ,
64
+ onNotStarted : () -> Unit ,
65
+ onFailure : () -> Unit
66
+ ) {
61
67
val pm = context.packageManager
62
68
runCatching {
63
69
val uri = FileProvider .getUriForFile(
@@ -67,13 +73,15 @@ object FileManagerHelper {
67
73
)
68
74
69
75
val baseIntent = Intent (Intent .ACTION_VIEW ).setDataAndType(uri, " resource/folder" )
70
- baseIntent.addFlags(Intent .FLAG_GRANT_READ_URI_PERMISSION )
76
+ baseIntent.addFlags(
77
+ Intent .FLAG_GRANT_READ_URI_PERMISSION or Intent .FLAG_ACTIVITY_NEW_TASK
78
+ )
71
79
72
80
val explorerPackages = listOf (
73
- " com.google.android.apps.nbu.files" , // Files by Google
74
- " com.android.documentsui" , // AOSP/Pixel
75
- " com.sec.android.app.myfiles" , // Samsung
76
- " com.mi.android.fileexplorer" // Xiaomi
81
+ " com.google.android.apps.nbu.files" ,
82
+ " com.android.documentsui" ,
83
+ " com.sec.android.app.myfiles" ,
84
+ " com.mi.android.fileexplorer"
77
85
)
78
86
79
87
var started = false
@@ -93,85 +101,68 @@ object FileManagerHelper {
93
101
94
102
if (! started) {
95
103
if (! launchFileManager(context, pm)) {
96
- val settingsIntent = Intent (Settings .ACTION_INTERNAL_STORAGE_SETTINGS )
97
- if (settingsIntent.resolveActivity(pm) != null ) {
98
- context.startActivity(settingsIntent)
99
- } else {
100
- Toast .makeText(
101
- context,
102
- context.getString(R .string.no_application_found),
103
- Toast .LENGTH_SHORT
104
- ).show()
105
- }
104
+ onNotStarted()
106
105
}
107
106
}
108
107
}.onFailure {
109
108
if (! launchFileManager(context, pm)) {
109
+ onFailure()
110
+ }
111
+ }
112
+ }
113
+
114
+ fun openFolderOrSettings (context : Context , folder : File ) {
115
+ val pm = context.packageManager
116
+ openFolder(
117
+ context,
118
+ folder,
119
+ onNotStarted = {
110
120
val settingsIntent = Intent (Settings .ACTION_INTERNAL_STORAGE_SETTINGS )
121
+ settingsIntent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
111
122
if (settingsIntent.resolveActivity(pm) != null ) {
112
123
context.startActivity(settingsIntent)
113
124
} else {
114
125
Toast .makeText(
115
126
context,
116
- context.getString(R .string.something_went_wrong ),
127
+ context.getString(R .string.no_application_found ),
117
128
Toast .LENGTH_SHORT
118
129
).show()
119
130
}
120
- }
121
- }
122
- }
123
-
124
- fun openFolderOrToast (context : Context , folder : File ) {
125
- val pm = context.packageManager
126
- runCatching {
127
- val uri = FileProvider .getUriForFile(
128
- context,
129
- context.packageName + " .fileprovider" ,
130
- folder
131
- )
132
-
133
- val baseIntent = Intent (Intent .ACTION_VIEW ).setDataAndType(uri, " resource/folder" )
134
- baseIntent.addFlags(Intent .FLAG_GRANT_READ_URI_PERMISSION )
135
-
136
- val explorerPackages = listOf (
137
- " com.google.android.apps.nbu.files" ,
138
- " com.android.documentsui" ,
139
- " com.sec.android.app.myfiles" ,
140
- " com.mi.android.fileexplorer"
141
- )
142
-
143
- var started = false
144
- for (pkg in explorerPackages) {
145
- val intent = Intent (baseIntent).setPackage(pkg)
146
- if (intent.resolveActivity(pm) != null ) {
147
- context.startActivity(intent)
148
- started = true
149
- break
150
- }
151
- }
152
-
153
- if (! started && baseIntent.resolveActivity(pm) != null ) {
154
- context.startActivity(baseIntent)
155
- started = true
156
- }
157
-
158
- if (! started) {
159
- if (! launchFileManager(context, pm)) {
131
+ },
132
+ onFailure = {
133
+ val settingsIntent = Intent (Settings .ACTION_INTERNAL_STORAGE_SETTINGS )
134
+ settingsIntent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
135
+ if (settingsIntent.resolveActivity(pm) != null ) {
136
+ context.startActivity(settingsIntent)
137
+ } else {
160
138
Toast .makeText(
161
139
context,
162
- context.getString(R .string.no_application_found ),
140
+ context.getString(R .string.something_went_wrong ),
163
141
Toast .LENGTH_SHORT
164
142
).show()
165
143
}
166
144
}
167
- }.onFailure {
168
- if (! launchFileManager(context, pm)) {
145
+ )
146
+ }
147
+
148
+ fun openFolderOrToast (context : Context , folder : File ) {
149
+ openFolder(
150
+ context,
151
+ folder,
152
+ onNotStarted = {
153
+ Toast .makeText(
154
+ context,
155
+ context.getString(R .string.no_application_found),
156
+ Toast .LENGTH_SHORT
157
+ ).show()
158
+ },
159
+ onFailure = {
169
160
Toast .makeText(
170
161
context,
171
162
context.getString(R .string.something_went_wrong),
172
163
Toast .LENGTH_SHORT
173
164
).show()
174
165
}
175
- }
166
+ )
176
167
}
177
168
}
0 commit comments