mirror of
https://github.com/2dust/v2rayNG.git
synced 2026-10-29 06:31:54 +00:00
Refactor file and permission helpers, update usage
This commit is contained in:
1 parent
f6bded88dd
commit
41fd237a87
7 files changed
+18
-25
No files matched your search
+5
-9
@@ -1,5 +1,6 @@
|
||||
package com.v2ray.ang.util
|
||||
package com.v2ray.ang.helper
|
||||
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
@@ -13,7 +14,7 @@ import com.v2ray.ang.extension.toast
|
||||
/**
|
||||
* Helper for choosing files using ACTION_GET_CONTENT intent.
|
||||
*
|
||||
*/
|
||||
*/
|
||||
|
||||
class FileChooserHelper(private val activity: AppCompatActivity) {
|
||||
private var fileChooserCallback: ((Uri?) -> Unit)? = null
|
||||
@@ -50,16 +51,11 @@ class FileChooserHelper(private val activity: AppCompatActivity) {
|
||||
fileChooserLauncher.launch(
|
||||
Intent.createChooser(intent, activity.getString(R.string.title_file_chooser))
|
||||
)
|
||||
} catch (ex: android.content.ActivityNotFoundException) {
|
||||
} catch (ex: ActivityNotFoundException) {
|
||||
Log.e(AppConfig.TAG, "File chooser activity not found", ex)
|
||||
activity.toast(R.string.toast_require_file_manager)
|
||||
fileChooserCallback?.invoke(null)
|
||||
fileChooserCallback = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension function to create a FileChooserHelper instance.
|
||||
*/
|
||||
fun AppCompatActivity.fileChooserHelper() = FileChooserHelper(this)
|
||||
}
|
||||
+3
-5
@@ -1,4 +1,4 @@
|
||||
package com.v2ray.ang.util
|
||||
package com.v2ray.ang.helper
|
||||
|
||||
import android.content.pm.PackageManager
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
@@ -12,7 +12,7 @@ import com.v2ray.ang.extension.toast
|
||||
/**
|
||||
* Helper for requesting permissions.
|
||||
*/
|
||||
class PermissionRequester(private val activity: AppCompatActivity) {
|
||||
class PermissionHelper(private val activity: AppCompatActivity) {
|
||||
private var permissionCallback: ((Boolean) -> Unit)? = null
|
||||
|
||||
private val permissionLauncher: ActivityResultLauncher<String> =
|
||||
@@ -43,6 +43,4 @@ class PermissionRequester(private val activity: AppCompatActivity) {
|
||||
permissionLauncher.launch(permission)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun AppCompatActivity.permissionRequester() = PermissionRequester(this)
|
||||
}
|
||||
@@ -154,9 +154,8 @@ class BackupActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
private fun showFileChooser() {
|
||||
launchFileChooser("*/*") { uri ->
|
||||
launchFileChooser { uri ->
|
||||
if (uri == null) {
|
||||
toastError(R.string.toast_failure)
|
||||
return@launchFileChooser
|
||||
}
|
||||
try {
|
||||
|
||||
@@ -21,9 +21,9 @@ import com.v2ray.ang.handler.SettingsManager
|
||||
import com.v2ray.ang.helper.CustomDividerItemDecoration
|
||||
import com.v2ray.ang.util.MyContextWrapper
|
||||
import com.v2ray.ang.dto.PermissionType
|
||||
import com.v2ray.ang.helper.FileChooserHelper
|
||||
import com.v2ray.ang.helper.PermissionHelper
|
||||
import com.v2ray.ang.util.Utils
|
||||
import com.v2ray.ang.util.fileChooserHelper
|
||||
import com.v2ray.ang.util.permissionRequester
|
||||
|
||||
|
||||
/**
|
||||
@@ -40,11 +40,14 @@ import com.v2ray.ang.util.permissionRequester
|
||||
abstract class BaseActivity : AppCompatActivity() {
|
||||
// Progress indicator that sits at the bottom of the toolbar
|
||||
private var progressBar: LinearProgressIndicator? = null
|
||||
private val permissionRequester = permissionRequester()
|
||||
private val fileChooser = fileChooserHelper()
|
||||
private lateinit var fileChooser : FileChooserHelper
|
||||
private lateinit var permissionRequester : PermissionHelper
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
fileChooser = FileChooserHelper(this)
|
||||
permissionRequester = PermissionHelper(this)
|
||||
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
if (!Utils.getDarkModeStatus(this)) {
|
||||
WindowCompat.getInsetsController(window, window.decorView).apply {
|
||||
|
||||
@@ -544,9 +544,8 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
|
||||
* show file chooser
|
||||
*/
|
||||
private fun showFileChooser() {
|
||||
launchFileChooser("*/*") { uri ->
|
||||
launchFileChooser { uri ->
|
||||
if (uri == null) {
|
||||
toastError(R.string.toast_failure)
|
||||
return@launchFileChooser
|
||||
}
|
||||
|
||||
|
||||
@@ -85,7 +85,6 @@ class ScannerActivity : BaseActivity() {
|
||||
private fun showFileChooser() {
|
||||
launchFileChooser("image/*") { uri ->
|
||||
if (uri == null) {
|
||||
toastError(R.string.toast_failure)
|
||||
return@launchFileChooser
|
||||
}
|
||||
try {
|
||||
|
||||
@@ -97,9 +97,8 @@ class UserAssetActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
private fun showFileChooser2() {
|
||||
launchFileChooser("*/*") { uri ->
|
||||
launchFileChooser { uri ->
|
||||
if (uri == null) {
|
||||
toastError(R.string.toast_failure)
|
||||
return@launchFileChooser
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user