feat(tool): 添加存储权限请求功能并优化代码

- 在 GlobalMethod.kt 中添加了请求存储权限的功能
- 优化了代码格式,调整了部分缩进和换行
- 移除了 shortcuts.xml 文件
- 在 strings.xml 中添加了权限请求相关的字符串资源
This commit is contained in:
Cold-Mint 2025-02-08 22:59:16 +08:00
parent c65c684997
commit 31084d4793
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
4 changed files with 57 additions and 63 deletions

View File

@ -89,9 +89,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
<activity
android:name=".ImporterActivity"

View File

@ -5,6 +5,7 @@ import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Context.CLIPBOARD_SERVICE
import android.content.DialogInterface
import android.content.res.ColorStateList
import android.content.res.Resources
import android.graphics.Bitmap
@ -112,14 +113,14 @@ object GlobalMethod {
//请求设置
val requestOptions = if (transformations.isNotEmpty()) {
val multi = MultiTransformation<Bitmap>(
transformations
transformations
)
RequestOptions.bitmapTransform(multi)
} else {
RequestOptions()
}
requestOptions.placeholder(R.drawable.image)
.error(R.drawable.image_not_supported)
.error(R.drawable.image_not_supported)
return requestOptions
}
@ -131,10 +132,10 @@ object GlobalMethod {
val context = view.context;
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
PopupMenu(
context,
view,
Gravity.NO_GRAVITY,
0, com.google.android.material.R.style.Widget_Material3_PopupMenu
context,
view,
Gravity.NO_GRAVITY,
0, com.google.android.material.R.style.Widget_Material3_PopupMenu
)
} else {
PopupMenu(context, view)
@ -165,24 +166,24 @@ object GlobalMethod {
* @param func Function1<String, Unit>
*/
fun showColorPickerDialog(
context: Context,
useARGB: Boolean = false, func: ((String) -> Unit)
context: Context,
useARGB: Boolean = false, func: ((String) -> Unit)
) {
ColorPickerDialogBuilder
.with(context).showAlphaSlider(useARGB)
.setTitle(context.getString(R.string.choose_color))
.initialColor(Color.WHITE)
.wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
.density(12)
.setOnColorSelectedListener {
//toast("onColorSelected: 0x" + Integer.toHexString(selectedColor));
}
.setPositiveButton(R.string.dialog_ok) { dialog, selectedColor, allColors ->
func.invoke(colorToString(selectedColor, useARGB))
}
.setNegativeButton(R.string.dialog_cancel) { dialog, which -> }
.build()
.show()
.with(context).showAlphaSlider(useARGB)
.setTitle(context.getString(R.string.choose_color))
.initialColor(Color.WHITE)
.wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
.density(12)
.setOnColorSelectedListener {
//toast("onColorSelected: 0x" + Integer.toHexString(selectedColor));
}
.setPositiveButton(R.string.dialog_ok) { dialog, selectedColor, allColors ->
func.invoke(colorToString(selectedColor, useARGB))
}
.setNegativeButton(R.string.dialog_cancel) { dialog, which -> }
.build()
.show()
}
/**
@ -243,11 +244,11 @@ object GlobalMethod {
stringBuilder.append("\n\n------\n\n")
}
val title =
context.getString(R.string.update_record) + "(" + data.size + ")"
context.getString(R.string.update_record) + "(" + data.size + ")"
MaterialAlertDialogBuilder(context).setTitle(title)
.setMessage(stringBuilder.toString()).setCancelable(false)
.setPositiveButton(R.string.dialog_ok) { i, i2 ->
}.show()
.setMessage(stringBuilder.toString()).setCancelable(false)
.setPositiveButton(R.string.dialog_ok) { i, i2 ->
}.show()
} else {
Toast.makeText(context, t.message, Toast.LENGTH_SHORT).show()
}
@ -261,9 +262,9 @@ object GlobalMethod {
override fun onFailure(e: Exception) {
Toast.makeText(
context,
context.getString(R.string.network_error),
Toast.LENGTH_SHORT
context,
context.getString(R.string.network_error),
Toast.LENGTH_SHORT
).show()
}
@ -278,19 +279,29 @@ object GlobalMethod {
fun requestStoragePermissions(activity: FragmentActivity, requestCompleted: (Boolean) -> Unit) {
val list = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
listOf(
Manifest.permission.MANAGE_EXTERNAL_STORAGE
Manifest.permission.MANAGE_EXTERNAL_STORAGE
)
} else {
listOf(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
}
XXPermissions.with(activity).permission(
list
).request { _, allGranted ->
requestCompleted.invoke(allGranted)
if (XXPermissions.isGranted(activity, list)) {
return
}
MaterialAlertDialogBuilder(activity).setTitle(R.string.permission_request_title)
.setMessage(R.string.permission_request_message)
.setPositiveButton(
R.string.dialog_ok
) { _, _ ->
XXPermissions.with(activity).permission(
list
).request { _, allGranted ->
requestCompleted.invoke(allGranted)
}
}.setNegativeButton(R.string.dialog_cancel, null).setCancelable(false).show()
}
@ -325,14 +336,14 @@ object GlobalMethod {
clipboardManager.setPrimaryClip(ClipData.newPlainText("", text))
if (showView != null) {
if (AppSettings.getValue(
AppSettings.Setting.ClipboardCue,
Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2
)
AppSettings.Setting.ClipboardCue,
Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2
)
) {
Snackbar.make(
showView,
String.format(context.getText(R.string.copy_complete).toString(), text),
Snackbar.LENGTH_SHORT
showView,
String.format(context.getText(R.string.copy_complete).toString(), text),
Snackbar.LENGTH_SHORT
).show()
}
}
@ -350,8 +361,8 @@ object GlobalMethod {
// When setting the clip board text.
clipboardManager.setPrimaryClip(ClipData.newPlainText("", text))
Toast.makeText(
context,
"已复制", Toast.LENGTH_SHORT
context,
"已复制", Toast.LENGTH_SHORT
).show()
}

View File

@ -616,4 +616,6 @@
<string name="clipboard_cue_tip">在安卓13及以上系统应用将内容复制到剪贴板时弹出提示。</string>
<string name="share">分享</string>
<string name="variable_name">变量名</string>
<string name="permission_request_title">请求权限</string>
<string name="permission_request_message">我们需要存储权限以访问您的模组。</string>
</resources>

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@drawable/table"
android:shortcutDisabledMessage="@string/code_table"
android:shortcutId="id1"
android:shortcutLongLabel="@string/code_table"
android:shortcutShortLabel="@string/code_table">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.coldmint.rust.pro.CodeTableActivity"
android:targetPackage="com.coldmint.rust.pro" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>