@@ -120,6 +120,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener,
120120 registerForActivityResult(ActivityResultContracts .StartActivityForResult ()) {
121121 // 当从应用设置返回时,重新检查权限
122122 if (hasLegacyPermissions()) {
123+ MGConfig (this ).save()
123124 showOptions()
124125 } else {
125126 // 如果用户仍然没有授予权限,可以再次显示请求或提示
@@ -133,6 +134,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener,
133134 if (isGranted) {
134135 // 情况1: 权限被授予
135136 // 用户同意了权限,执行你的操作
137+ MGConfig (this ).save()
136138 showOptions()
137139 } else {
138140 // 情况2&3: 权限被拒绝
@@ -238,16 +240,15 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener,
238240 }
239241 }
240242
241- private fun hasLegacyPermissions (): Boolean {
242- return ActivityCompat .checkSelfPermission(
243+ private fun hasLegacyPermissions (): Boolean =
244+ ActivityCompat .checkSelfPermission(
243245 this ,
244246 Manifest .permission.READ_EXTERNAL_STORAGE
245247 ) == PackageManager .PERMISSION_GRANTED &&
246248 ContextCompat .checkSelfPermission(
247249 this ,
248250 Manifest .permission.WRITE_EXTERNAL_STORAGE
249251 ) == PackageManager .PERMISSION_GRANTED
250- }
251252
252253 override fun onResume () {
253254 super .onResume()
@@ -540,8 +541,8 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener,
540541 private fun checkPermissionSilently () {
541542 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
542543 MGDirectoryUri = folderPermissionManager.getMGFolderUri()
543- val config = MGConfig .loadConfig( this )
544- if (config != null && MGDirectoryUri != null ) {
544+ if ( MGDirectoryUri != null ) {
545+ ( MGConfig .loadConfig( this ) ? : MGConfig ( this )).save()
545546 showOptions()
546547 } else {
547548 hideOptions()
@@ -567,20 +568,19 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener,
567568 )
568569 .setPositiveButton(R .string.dialog_positive) { _, _ ->
569570 val intent = Intent (Intent .ACTION_OPEN_DOCUMENT_TREE )
570- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
571- // 1. 获取外部存储文档提供者的根 URI
572- val rootUri = DocumentsContract .buildTreeDocumentUri(
573- " com.android.externalstorage.documents" ,
574- " primary" // "primary" 通常代表内部共享存储
575- )
571+ // 1. 获取外部存储文档提供者的根 URI
572+ val rootUri = DocumentsContract .buildTreeDocumentUri(
573+ " com.android.externalstorage.documents" ,
574+ " primary" // "primary" 通常代表内部共享存储
575+ )
576576
577- // 2. 在根 URI 的基础上构建指向 "MG" 文件夹的 Document-URI
578- // Document ID 的格式是 "root:path"
579- val folderDocumentId = " primary:MG"
580- val initialUri = DocumentsContract .buildDocumentUriUsingTree(rootUri, folderDocumentId)
577+ // 2. 在根 URI 的基础上构建指向 "MG" 文件夹的 Document-URI
578+ // Document ID 的格式是 "root:path"
579+ val folderDocumentId = " primary:MG"
580+ val initialUri =
581+ DocumentsContract .buildDocumentUriUsingTree(rootUri, folderDocumentId)
581582
582- intent.putExtra(DocumentsContract .EXTRA_INITIAL_URI , initialUri)
583- }
583+ intent.putExtra(DocumentsContract .EXTRA_INITIAL_URI , initialUri)
584584 safLauncher.launch(intent)
585585 }
586586 .setNegativeButton(R .string.dialog_negative) { dialog, _ -> dialog.dismiss() }
0 commit comments