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" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity> </activity>
<activity <activity
android:name=".ImporterActivity" android:name=".ImporterActivity"

View File

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

View File

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