Skip to content

Commit 223e644

Browse files
authored
fix: request permission in SaveAsActivity on first-run (#271)
* Fix "Replace characters on save as function, FileNotFoundException" #250 * Update CHANGELOG.md update changelog * Update app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt Co-authored-by: Naveen Singh <[email protected]> * Update CHANGELOG.md Co-authored-by: Naveen Singh <[email protected]> * Update CHANGELOG.md Co-authored-by: Naveen Singh <[email protected]> * Merge branch 'FossifyOrg:main' into main Signed-off-by: Jan Guegel <[email protected]> * Update CHANGELOG.md * fix PR Feedback Signed-off-by: Jan Guegel <[email protected]> * fix duplicated code by moving it to SimpleActivity.kt Signed-off-by: Jan Guegel <[email protected]> * moved handleStoragePermissions() to SimpleActivity.kt from MainActivity.kt introduced tryInitFileManager() to SaveAsActivity.kt removed const MANAGE_STORAGE_RC from MainActivity.kt Signed-off-by: Jan Guegel <[email protected]> * adjusted toast in try catch in storage permission action Signed-off-by: Jan Guegel <[email protected]> * catch ActivityNotFoundException with fallback of SecurityException Signed-off-by: Jan Guegel <[email protected]> * Apply suggestions from code review thanks for the feedback! Co-authored-by: Naveen Singh <[email protected]> * remove indent Signed-off-by: Jan Guegel <[email protected]> * adjust changelog Co-authored-by: Naveen Singh <[email protected]> --------- Signed-off-by: Jan Guegel <[email protected]> Co-authored-by: Jan Guegel <[email protected]> Co-authored-by: Naveen Singh <[email protected]> Refs: #85
1 parent 055bfbd commit 223e644

File tree

4 files changed

+70
-49
lines changed

4 files changed

+70
-49
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Fixed
99
- Fixed folders showing up incorrectly as files in copy/move dialog ([#267])
1010
- Fixed error when saving files with unsupported characters ([#250])
11+
- Fixed missing permission prompt on initial "Save as" launch ([#85])
1112

1213
## [1.2.3] - 2025-09-15
1314
### Fixed
@@ -76,6 +77,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7677
[#251]: https://github.com/FossifyOrg/File-Manager/issues/251
7778
[#267]: https://github.com/FossifyOrg/File-Manager/issues/267
7879
[#250]: https://github.com/FossifyOrg/File-Manager/issues/250
80+
[#85]: https://github.com/FossifyOrg/File-Manager/issues/85
7981

8082
[Unreleased]: https://github.com/FossifyOrg/File-Manager/compare/1.2.3...HEAD
8183
[1.2.3]: https://github.com/FossifyOrg/File-Manager/compare/1.2.2...1.2.3

app/src/main/kotlin/org/fossify/filemanager/activities/MainActivity.kt

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
package org.fossify.filemanager.activities
22

3-
import android.annotation.SuppressLint
43
import android.app.Activity
54
import android.content.ClipData
65
import android.content.Intent
76
import android.graphics.drawable.Drawable
87
import android.media.RingtoneManager
9-
import android.net.Uri
108
import android.os.Bundle
11-
import android.os.Environment
129
import android.os.Handler
13-
import android.provider.Settings
1410
import android.widget.ImageView
1511
import android.widget.TextView
1612
import androidx.viewpager.widget.ViewPager
1713
import com.stericson.RootTools.RootTools
1814
import me.grantland.widget.AutofitHelper
19-
import org.fossify.commons.dialogs.ConfirmationAdvancedDialog
2015
import org.fossify.commons.dialogs.RadioGroupDialog
2116
import org.fossify.commons.extensions.appLaunched
2217
import org.fossify.commons.extensions.appLockManager
@@ -43,7 +38,6 @@ import org.fossify.commons.extensions.launchMoreAppsFromUsIntent
4338
import org.fossify.commons.extensions.onGlobalLayout
4439
import org.fossify.commons.extensions.onTabSelectionChanged
4540
import org.fossify.commons.extensions.sdCardPath
46-
import org.fossify.commons.extensions.showErrorToast
4741
import org.fossify.commons.extensions.toast
4842
import org.fossify.commons.extensions.updateBottomTabItemColors
4943
import org.fossify.commons.extensions.viewBinding
@@ -59,7 +53,6 @@ import org.fossify.commons.helpers.TAB_RECENT_FILES
5953
import org.fossify.commons.helpers.TAB_STORAGE_ANALYSIS
6054
import org.fossify.commons.helpers.VIEW_TYPE_GRID
6155
import org.fossify.commons.helpers.ensureBackgroundThread
62-
import org.fossify.commons.helpers.isRPlus
6356
import org.fossify.commons.models.FAQItem
6457
import org.fossify.commons.models.RadioItem
6558
import org.fossify.commons.models.Release
@@ -84,7 +77,6 @@ import java.io.File
8477
class MainActivity : SimpleActivity() {
8578
companion object {
8679
private const val BACK_PRESS_TIMEOUT = 5000
87-
private const val MANAGE_STORAGE_RC = 201
8880
private const val PICKED_PATH = "picked_path"
8981
}
9082

@@ -281,15 +273,6 @@ class MainActivity : SimpleActivity() {
281273
}
282274
}
283275

284-
@SuppressLint("NewApi")
285-
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
286-
super.onActivityResult(requestCode, resultCode, resultData)
287-
isAskingPermissions = false
288-
if (requestCode == MANAGE_STORAGE_RC && isRPlus()) {
289-
actionOnPermission?.invoke(Environment.isExternalStorageManager())
290-
}
291-
}
292-
293276
private fun updateMenuColors() {
294277
updateStatusbarColor(getProperBackgroundColor())
295278
binding.mainMenu.updateColors()
@@ -323,38 +306,6 @@ class MainActivity : SimpleActivity() {
323306
}
324307
}
325308

326-
@SuppressLint("InlinedApi")
327-
private fun handleStoragePermission(callback: (granted: Boolean) -> Unit) {
328-
actionOnPermission = null
329-
if (hasStoragePermission()) {
330-
callback(true)
331-
} else {
332-
if (isRPlus()) {
333-
ConfirmationAdvancedDialog(this, "", R.string.access_storage_prompt, R.string.ok, 0, false) { success ->
334-
if (success) {
335-
isAskingPermissions = true
336-
actionOnPermission = callback
337-
try {
338-
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
339-
intent.addCategory("android.intent.category.DEFAULT")
340-
intent.data = Uri.parse("package:$packageName")
341-
startActivityForResult(intent, MANAGE_STORAGE_RC)
342-
} catch (e: Exception) {
343-
showErrorToast(e)
344-
val intent = Intent()
345-
intent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
346-
startActivityForResult(intent, MANAGE_STORAGE_RC)
347-
}
348-
} else {
349-
finish()
350-
}
351-
}
352-
} else {
353-
handlePermission(PERMISSION_WRITE_STORAGE, callback)
354-
}
355-
}
356-
}
357-
358309
private fun initFileManager(refreshRecents: Boolean) {
359310
if (intent.action == Intent.ACTION_VIEW && intent.data != null) {
360311
val data = intent.data

app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,21 @@ class SaveAsActivity : SimpleActivity() {
1818
override fun onCreate(savedInstanceState: Bundle?) {
1919
super.onCreate(savedInstanceState)
2020
setContentView(binding.root)
21+
tryInitFileManager()
22+
}
23+
24+
private fun tryInitFileManager() {
25+
handleStoragePermission { granted ->
26+
if (granted) {
27+
saveAsDialog()
28+
} else {
29+
toast(R.string.no_storage_permissions)
30+
finish()
31+
}
32+
}
33+
}
2134

35+
private fun saveAsDialog() {
2236
if (intent.action == Intent.ACTION_SEND && intent.extras?.containsKey(Intent.EXTRA_STREAM) == true) {
2337
FilePickerDialog(this, pickFile = false, showHidden = config.shouldShowHidden(), showFAB = true, showFavoritesButton = true) {
2438
val destination = it

app/src/main/kotlin/org/fossify/filemanager/activities/SimpleActivity.kt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package org.fossify.filemanager.activities
22

33
import android.annotation.SuppressLint
4+
import android.content.Intent
45
import android.os.Environment
6+
import android.provider.Settings
57
import org.fossify.commons.activities.BaseSimpleActivity
8+
import org.fossify.commons.dialogs.ConfirmationAdvancedDialog
69
import org.fossify.commons.extensions.hasPermission
10+
import android.net.Uri
11+
import org.fossify.commons.extensions.showErrorToast
12+
import org.fossify.commons.extensions.toast
713
import org.fossify.commons.helpers.PERMISSION_WRITE_STORAGE
814
import org.fossify.commons.helpers.isRPlus
915
import org.fossify.filemanager.R
@@ -31,10 +37,23 @@ open class SimpleActivity : BaseSimpleActivity() {
3137
R.mipmap.ic_launcher_grey_black
3238
)
3339

40+
companion object {
41+
private const val MANAGE_STORAGE_RC = 201
42+
}
43+
3444
override fun getAppLauncherName() = getString(R.string.app_launcher_name)
3545

3646
override fun getRepositoryName() = "File-Manager"
3747

48+
@SuppressLint("NewApi")
49+
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
50+
super.onActivityResult(requestCode, resultCode, resultData)
51+
isAskingPermissions = false
52+
if (requestCode == MANAGE_STORAGE_RC && isRPlus()) {
53+
actionOnPermission?.invoke(Environment.isExternalStorageManager())
54+
}
55+
}
56+
3857
@SuppressLint("NewApi")
3958
fun hasStoragePermission(): Boolean {
4059
return if (isRPlus()) {
@@ -43,4 +62,39 @@ open class SimpleActivity : BaseSimpleActivity() {
4362
hasPermission(PERMISSION_WRITE_STORAGE)
4463
}
4564
}
65+
66+
@SuppressLint("InlinedApi")
67+
fun handleStoragePermission(callback: (granted: Boolean) -> Unit) {
68+
actionOnPermission = null
69+
if (hasStoragePermission()) {
70+
callback(true)
71+
} else {
72+
if (isRPlus()) {
73+
ConfirmationAdvancedDialog(this, "", R.string.access_storage_prompt, R.string.ok, 0, false) { success ->
74+
if (success) {
75+
isAskingPermissions = true
76+
actionOnPermission = callback
77+
try {
78+
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
79+
intent.addCategory("android.intent.category.DEFAULT")
80+
intent.data = Uri.parse("package:$packageName")
81+
startActivityForResult(intent, MANAGE_STORAGE_RC)
82+
} catch (e: android.content.ActivityNotFoundException) {
83+
showErrorToast(e)
84+
val intent = Intent()
85+
intent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
86+
startActivityForResult(intent, MANAGE_STORAGE_RC)
87+
} catch (e: SecurityException) {
88+
showErrorToast(e)
89+
finish()
90+
}
91+
} else {
92+
finish()
93+
}
94+
}
95+
} else {
96+
handlePermission(PERMISSION_WRITE_STORAGE, callback)
97+
}
98+
}
99+
}
46100
}

0 commit comments

Comments
 (0)