Skip to content

Commit 1fbb9d1

Browse files
refactor folder opening helper and add new task flag
1 parent 331d88b commit 1fbb9d1

File tree

1 file changed

+53
-62
lines changed

1 file changed

+53
-62
lines changed

app/src/main/kotlin/com/d4rk/cleaner/core/utils/helpers/FileManagerHelper.kt

Lines changed: 53 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ object FileManagerHelper {
1818
)
1919
for (pkg in launchPackages) {
2020
pm.getLaunchIntentForPackage(pkg)?.let {
21+
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
2122
context.startActivity(it)
2223
return true
2324
}
@@ -34,7 +35,7 @@ object FileManagerHelper {
3435
)
3536
val mime = context.contentResolver.getType(uri) ?: "*/*"
3637
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)
3839
if (intent.resolveActivity(context.packageManager) != null) {
3940
context.startActivity(intent)
4041
} else {
@@ -57,7 +58,12 @@ object FileManagerHelper {
5758
}
5859
}
5960

60-
fun openFolderOrSettings(context: Context, folder: File) {
61+
private fun openFolder(
62+
context: Context,
63+
folder: File,
64+
onNotStarted: () -> Unit,
65+
onFailure: () -> Unit
66+
) {
6167
val pm = context.packageManager
6268
runCatching {
6369
val uri = FileProvider.getUriForFile(
@@ -67,13 +73,15 @@ object FileManagerHelper {
6773
)
6874

6975
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+
)
7179

7280
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"
7785
)
7886

7987
var started = false
@@ -93,85 +101,68 @@ object FileManagerHelper {
93101

94102
if (!started) {
95103
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()
106105
}
107106
}
108107
}.onFailure {
109108
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 = {
110120
val settingsIntent = Intent(Settings.ACTION_INTERNAL_STORAGE_SETTINGS)
121+
settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
111122
if (settingsIntent.resolveActivity(pm) != null) {
112123
context.startActivity(settingsIntent)
113124
} else {
114125
Toast.makeText(
115126
context,
116-
context.getString(R.string.something_went_wrong),
127+
context.getString(R.string.no_application_found),
117128
Toast.LENGTH_SHORT
118129
).show()
119130
}
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 {
160138
Toast.makeText(
161139
context,
162-
context.getString(R.string.no_application_found),
140+
context.getString(R.string.something_went_wrong),
163141
Toast.LENGTH_SHORT
164142
).show()
165143
}
166144
}
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 = {
169160
Toast.makeText(
170161
context,
171162
context.getString(R.string.something_went_wrong),
172163
Toast.LENGTH_SHORT
173164
).show()
174165
}
175-
}
166+
)
176167
}
177168
}

0 commit comments

Comments
 (0)