|
1 | 1 | package com.v2ray.ang.ui |
2 | 2 |
|
3 | | -import android.Manifest |
4 | 3 | import android.content.Intent |
5 | | -import android.content.pm.PackageManager |
6 | 4 | import android.content.res.ColorStateList |
7 | 5 | import android.net.Uri |
8 | 6 | import android.net.VpnService |
9 | | -import android.os.Build |
10 | 7 | import android.os.Bundle |
11 | 8 | import android.util.Log |
12 | 9 | import android.view.KeyEvent |
@@ -35,6 +32,7 @@ import com.v2ray.ang.handler.MmkvManager |
35 | 32 | import com.v2ray.ang.handler.SettingsChangeManager |
36 | 33 | import com.v2ray.ang.handler.SettingsManager |
37 | 34 | import com.v2ray.ang.handler.V2RayServiceManager |
| 35 | +import com.v2ray.ang.dto.PermissionType |
38 | 36 | import com.v2ray.ang.util.Utils |
39 | 37 | import com.v2ray.ang.viewmodel.MainViewModel |
40 | 38 | import kotlinx.coroutines.Dispatchers |
@@ -65,40 +63,6 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList |
65 | 63 | } |
66 | 64 | } |
67 | 65 |
|
68 | | - // register activity result for requesting permission |
69 | | - private val requestPermissionLauncher = |
70 | | - registerForActivityResult( |
71 | | - ActivityResultContracts.RequestPermission() |
72 | | - ) { isGranted: Boolean -> |
73 | | - if (isGranted) { |
74 | | - when (pendingAction) { |
75 | | - Action.IMPORT_QR_CODE_CONFIG -> |
76 | | - scanQRCodeForConfig.launch(Intent(this, ScannerActivity::class.java)) |
77 | | - |
78 | | - Action.READ_CONTENT_FROM_URI -> |
79 | | - chooseFileForCustomConfig.launch(Intent.createChooser(Intent(Intent.ACTION_GET_CONTENT).apply { |
80 | | - type = "*/*" |
81 | | - addCategory(Intent.CATEGORY_OPENABLE) |
82 | | - }, getString(R.string.title_file_chooser))) |
83 | | - |
84 | | - Action.POST_NOTIFICATIONS -> {} |
85 | | - else -> {} |
86 | | - } |
87 | | - } else { |
88 | | - toast(R.string.toast_permission_denied) |
89 | | - } |
90 | | - pendingAction = Action.NONE |
91 | | - } |
92 | | - |
93 | | - private var pendingAction: Action = Action.NONE |
94 | | - |
95 | | - enum class Action { |
96 | | - NONE, |
97 | | - IMPORT_QR_CODE_CONFIG, |
98 | | - READ_CONTENT_FROM_URI, |
99 | | - POST_NOTIFICATIONS |
100 | | - } |
101 | | - |
102 | 66 | private val chooseFileForCustomConfig = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { |
103 | 67 | val uri = it.data?.data |
104 | 68 | if (it.resultCode == RESULT_OK && uri != null) { |
@@ -148,11 +112,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList |
148 | 112 | setupViewModel() |
149 | 113 | mainViewModel.reloadServerList() |
150 | 114 |
|
151 | | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
152 | | - if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { |
153 | | - pendingAction = Action.POST_NOTIFICATIONS |
154 | | - requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) |
155 | | - } |
| 115 | + checkAndRequestPermission(PermissionType.POST_NOTIFICATIONS) { |
156 | 116 | } |
157 | 117 | } |
158 | 118 |
|
@@ -416,12 +376,8 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList |
416 | 376 | * import config from qrcode |
417 | 377 | */ |
418 | 378 | private fun importQRcode(): Boolean { |
419 | | - val permission = Manifest.permission.CAMERA |
420 | | - if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) { |
| 379 | + checkAndRequestPermission(PermissionType.CAMERA) { |
421 | 380 | scanQRCodeForConfig.launch(Intent(this, ScannerActivity::class.java)) |
422 | | - } else { |
423 | | - pendingAction = Action.IMPORT_QR_CODE_CONFIG |
424 | | - requestPermissionLauncher.launch(permission) |
425 | 381 | } |
426 | 382 | return true |
427 | 383 | } |
@@ -475,7 +431,9 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList |
475 | 431 | */ |
476 | 432 | private fun importConfigLocal(): Boolean { |
477 | 433 | try { |
478 | | - showFileChooser() |
| 434 | + checkAndRequestPermission(PermissionType.READ_STORAGE) { |
| 435 | + showFileChooser() |
| 436 | + } |
479 | 437 | } catch (e: Exception) { |
480 | 438 | Log.e(AppConfig.TAG, "Failed to import config from local file", e) |
481 | 439 | return false |
@@ -596,40 +554,19 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList |
596 | 554 | intent.type = "*/*" |
597 | 555 | intent.addCategory(Intent.CATEGORY_OPENABLE) |
598 | 556 |
|
599 | | - val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
600 | | - Manifest.permission.READ_MEDIA_IMAGES |
601 | | - } else { |
602 | | - Manifest.permission.READ_EXTERNAL_STORAGE |
603 | | - } |
604 | | - |
605 | | - if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) { |
606 | | - pendingAction = Action.READ_CONTENT_FROM_URI |
607 | | - chooseFileForCustomConfig.launch(Intent.createChooser(intent, getString(R.string.title_file_chooser))) |
608 | | - } else { |
609 | | - requestPermissionLauncher.launch(permission) |
610 | | - } |
| 557 | + chooseFileForCustomConfig.launch(Intent.createChooser(intent, getString(R.string.title_file_chooser))) |
611 | 558 | } |
612 | 559 |
|
613 | 560 | /** |
614 | 561 | * read content from uri |
615 | 562 | */ |
616 | 563 | private fun readContentFromUri(uri: Uri) { |
617 | | - val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
618 | | - Manifest.permission.READ_MEDIA_IMAGES |
619 | | - } else { |
620 | | - Manifest.permission.READ_EXTERNAL_STORAGE |
621 | | - } |
622 | | - |
623 | | - if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) { |
624 | | - try { |
625 | | - contentResolver.openInputStream(uri).use { input -> |
626 | | - importBatchConfig(input?.bufferedReader()?.readText()) |
627 | | - } |
628 | | - } catch (e: Exception) { |
629 | | - Log.e(AppConfig.TAG, "Failed to read content from URI", e) |
| 564 | + try { |
| 565 | + contentResolver.openInputStream(uri).use { input -> |
| 566 | + importBatchConfig(input?.bufferedReader()?.readText()) |
630 | 567 | } |
631 | | - } else { |
632 | | - requestPermissionLauncher.launch(permission) |
| 568 | + } catch (e: Exception) { |
| 569 | + Log.e(AppConfig.TAG, "Failed to read content from URI", e) |
633 | 570 | } |
634 | 571 | } |
635 | 572 |
|
|
0 commit comments