6.6.1 - Alpha2 - 尝试修复脚本快捷方式添加问题

This commit is contained in:
SuperMonster003
2024-12-05 01:07:53 +08:00
parent 124638f9d0
commit eaee908295
8 changed files with 119 additions and 232 deletions

View File

@@ -11,6 +11,7 @@ import android.view.View;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.pm.ShortcutManagerCompat;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.tencent.bugly.crashreport.BuglyLog;
@@ -38,6 +39,7 @@ import org.autojs.autojs.ui.filechooser.FileChooserDialogBuilder;
import org.autojs.autojs.ui.shortcut.ShortcutCreateActivity;
import org.autojs.autojs.ui.timing.TimedTaskSettingActivity;
import org.autojs.autojs.util.EnvironmentUtils;
import org.autojs.autojs.util.ShortcutUtils;
import org.autojs.autojs.util.ViewUtils;
import org.autojs.autojs.util.WorkingDirectoryUtils;
import org.autojs.autojs6.R;
@@ -363,8 +365,14 @@ public class ScriptOperations {
}
public void createShortcut(ScriptFile file) {
mContext.startActivity(new Intent(mContext, ShortcutCreateActivity.class)
.putExtra(ShortcutCreateActivity.EXTRA_FILE, file));
if (!ShortcutManagerCompat.isRequestPinShortcutSupported(mContext)) {
ShortcutUtils.showPinShortcutNotSupportedDialog(mContext);
return;
}
Intent intent = new Intent(mContext, ShortcutCreateActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(ShortcutCreateActivity.EXTRA_FILE, file);
mContext.startActivity(intent);
}
public void delete(final ScriptFile scriptFile) {
@@ -390,7 +398,7 @@ public class ScriptOperations {
return;
}
String content = mContext.getString(R.string.text_old_path) + ": " + oldPath + "\n"
+ mContext.getString(R.string.text_new_path) + ": " + newPath;
+ mContext.getString(R.string.text_new_path) + ": " + newPath;
new MaterialDialog.Builder(mContext)
.title(mContext.getString(R.string.text_prompt))
.content(content)

View File

@@ -12,19 +12,19 @@ import org.autojs.autojs6.databinding.ActivityDocumentationBinding
*/
class DocumentationActivity : BaseActivity() {
private lateinit var binding: ActivityDocumentationBinding
private var binding: ActivityDocumentationBinding? = null
private lateinit var mWebView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityDocumentationBinding.inflate(layoutInflater)
setContentView(binding.root)
setContentView(binding!!.root)
setupViews()
}
private fun setupViews() {
mWebView = binding.ewebView.webView.also {
mWebView = binding!!.ewebView.webView.also {
it.loadUrl(intent.getStringExtra(EXTRA_URL) ?: getUrl("index.html"))
}
}
@@ -40,6 +40,11 @@ class DocumentationActivity : BaseActivity() {
}
}
override fun onDestroy() {
super.onDestroy()
binding = null
}
companion object {
const val EXTRA_URL = "url"

View File

@@ -2,7 +2,7 @@ package org.autojs.autojs.ui.settings
import android.content.Context
import android.util.AttributeSet
import android.widget.LinearLayout
import android.view.LayoutInflater
import androidx.core.content.pm.ShortcutManagerCompat
import com.afollestad.materialdialogs.MaterialDialog
import org.autojs.autojs.theme.preference.MaterialPreference
@@ -10,54 +10,57 @@ import org.autojs.autojs.ui.doc.DocumentationActivity
import org.autojs.autojs.ui.log.LogActivity
import org.autojs.autojs.util.ShortcutUtils
import org.autojs.autojs6.R
import org.autojs.autojs6.databinding.SelectLauncherShortcutBinding
/**
* Created by SuperMonster003 on Sep 25, 2022.
*/
class LauncherShortcutsPreference : MaterialPreference {
private val mDialog = MaterialDialog.Builder(prefContext)
.customView(R.layout.select_launcher_shortcut, true)
.build()
.also { dialog ->
val view = dialog.customView as LinearLayout
private var binding: SelectLauncherShortcutBinding? = null
view.findViewById<LinearLayout?>(R.id.launcher_shortcut_settings).setOnClickListener {
dialog.dismiss()
ShortcutUtils.requestPinShortcut(
prefContext,
R.string.id_launcher_shortcut_settings,
PreferencesActivity::class.java.name,
R.string.text_app_shortcut_settings_long_label,
R.string.text_app_shortcut_settings_short_label,
R.mipmap.ic_app_shortcut_settings_adaptive,
)
}
private val mDialog by lazy {
view.findViewById<LinearLayout?>(R.id.launcher_shortcut_docs).setOnClickListener {
dialog.dismiss()
ShortcutUtils.requestPinShortcut(
prefContext,
R.string.id_launcher_shortcut_docs,
DocumentationActivity::class.java.name,
R.string.text_app_shortcut_docs_long_label,
R.string.text_app_shortcut_docs_short_label,
R.mipmap.ic_app_shortcut_docs_adaptive,
)
}
val binding = SelectLauncherShortcutBinding.inflate(LayoutInflater.from(prefContext)).also { binding = it }
view.findViewById<LinearLayout?>(R.id.launcher_shortcut_log).setOnClickListener {
dialog.dismiss()
ShortcutUtils.requestPinShortcut(
prefContext,
R.string.id_launcher_shortcut_log,
LogActivity::class.java.name,
R.string.text_app_shortcut_log_long_label,
R.string.text_app_shortcut_log_short_label,
R.mipmap.ic_app_shortcut_log_adaptive,
)
MaterialDialog.Builder(prefContext)
.customView(binding.root, true)
.build()
.also { dialog ->
binding.launcherShortcutSettings.setOnClickListener {
ShortcutUtils.requestPinShortcut(
prefContext,
R.string.id_launcher_shortcut_settings,
PreferencesActivity::class.java.name,
R.string.text_app_shortcut_settings_long_label,
R.string.text_app_shortcut_settings_short_label,
R.mipmap.ic_app_shortcut_settings_adaptive,
).also { dialog.dismiss() }
}
binding.launcherShortcutDocs.setOnClickListener {
ShortcutUtils.requestPinShortcut(
prefContext,
R.string.id_launcher_shortcut_docs,
DocumentationActivity::class.java.name,
R.string.text_app_shortcut_docs_long_label,
R.string.text_app_shortcut_docs_short_label,
R.mipmap.ic_app_shortcut_docs_adaptive,
).also { dialog.dismiss() }
}
binding.launcherShortcutLog.setOnClickListener {
ShortcutUtils.requestPinShortcut(
prefContext,
R.string.id_launcher_shortcut_log,
LogActivity::class.java.name,
R.string.text_app_shortcut_log_long_label,
R.string.text_app_shortcut_log_short_label,
R.mipmap.ic_app_shortcut_log_adaptive,
).also { dialog.dismiss() }
}
}
}
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
@@ -71,22 +74,18 @@ class LauncherShortcutsPreference : MaterialPreference {
if (ShortcutManagerCompat.isRequestPinShortcutSupported(prefContext)) {
showShortcutsSelectionDialog()
} else {
showPinShortcutNotSupportedDialog()
ShortcutUtils.showPinShortcutNotSupportedDialog(prefContext)
}
super.onClick()
}
override fun onDetached() {
super.onDetached()
binding = null
}
private fun showShortcutsSelectionDialog() {
if (!mDialog.isShowing) mDialog.show()
}
private fun showPinShortcutNotSupportedDialog() {
MaterialDialog.Builder(prefContext)
.title(R.string.text_prompt)
.content(R.string.text_pin_shortcut_not_unsupported)
.positiveText(R.string.dialog_button_dismiss)
.build()
.show()
}
}

View File

@@ -99,18 +99,34 @@ public class ShortcutCreateActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void createShortcutByShortcutManager() {
CharSequence name = mName.getText();
// @Caution by SuperMonster003 on Dec 7, 2024.
// ! It is necessary to convert to String type here,
// ! rather than retaining the CharSequence type obtained from getText().
// ! This is because on certain devices [such as MIUI 14 (Android 13)],
// ! ShortcutInfoCompat.Builder#setShortLabel and setLongLabel
// ! can only accept String type data returned by getText().
// ! If CharSequence type data is directly passed in, the shortcut will fail to create properly.
// !
// ! zh-CN:
// !
// ! 此处需要转换为 String 类型, 而不能保留 getText() 方法得到的 CharSequence 类型.
// ! 这是因为在一些特殊设备上 [如 MIUI 14 (Android 13)],
// ! ShortcutInfoCompat.Builder#setShortLabel 及 setLongLabel 只能接受 getText() 返回的 String 类型数据,
// ! 如果直接传入 CharSequence 类型数据, 快捷方式将无法正常创建.
// !
// # CharSequence name = mName.getText();
String name = mName.getText().toString();
/* To make each script be able to have its own individual icon. */
String id = ShortcutActivity.class.getName() + "$" + name + "@" + System.currentTimeMillis();
Intent intent = new Intent(this, ShortcutActivity.class)
.putExtra(ScriptIntents.EXTRA_KEY_PATH, mScriptFile.getPath())
.setAction(Intent.ACTION_MAIN);
if (mIsDefaultIcon) {
Icon icon = Icon.createWithResource(this, R.drawable.ic_file_type_js_dark_green);
ShortcutUtils.requestPinShortcut(this, id, intent, name, name, icon);
ShortcutUtils.requestPinShortcut(this, id, intent, name, name, R.drawable.ic_file_type_js_dark_green);
} else {
Bitmap bitmap = BitmapUtils.drawableToBitmap(mIcon.getDrawable());
ShortcutUtils.requestPinShortcut(this, id, intent, name, name, bitmap);
ShortcutUtils.requestPinShortcut(this, id, intent, name, name, mIcon.getDrawable());
}
}

View File

@@ -4,10 +4,13 @@ import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.graphics.drawable.Icon
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat
import com.afollestad.materialdialogs.MaterialDialog
import org.autojs.autojs6.R
/**
* Created by SuperMonster003 on Oct 11, 2022.
@@ -19,6 +22,16 @@ object ShortcutUtils {
requestPinShortcut(context, context.getString(id), className, context.getString(longLabelResId), context.getString(shortLabelResId), IconCompat.createWithResource(context, iconResId))
}
@JvmStatic
fun requestPinShortcut(context: Context, id: String, intent: Intent, longLabel: CharSequence, shortLabel: CharSequence, iconResId: Int) {
requestPinShortcut(context, id, intent, longLabel, shortLabel, IconCompat.createWithResource(context, iconResId))
}
@JvmStatic
fun requestPinShortcut(context: Context, id: String, intent: Intent, longLabel: CharSequence, shortLabel: CharSequence, drawable: Drawable) {
requestPinShortcut(context, id, intent, longLabel, shortLabel, BitmapUtils.drawableToBitmap(drawable))
}
@JvmStatic
fun requestPinShortcut(context: Context, id: String, className: String, longLabel: CharSequence, shortLabel: CharSequence, icon: IconCompat) {
requestPinShortcut(context, id, Intent("android.intent.action.VIEW").setClassName(context, className), longLabel, shortLabel, icon)
@@ -39,7 +52,7 @@ object ShortcutUtils {
// The shortcut must be enabled.
// The "id" for ShortcutInfoCompat.Builder must contain stable, constant strings.
val pinShortcutInfo = ShortcutInfoCompat.Builder(context, id)
.setIntent(intent)
.setIntent(intent.apply { action = action ?: Intent.ACTION_VIEW })
.setLongLabel(longLabel)
.setShortLabel(shortLabel)
.setIcon(icon)
@@ -55,9 +68,18 @@ object ShortcutUtils {
// Configure the intent so that your app's broadcast receiver gets
// the callback successfully.For details, see PendingIntent.getBroadcast().
val successCallback = PendingIntent.getBroadcast(context, 0, pinnedShortcutCallbackIntent, PendingIntent.FLAG_IMMUTABLE)
val successCallback = PendingIntent.getBroadcast(context, 0, pinnedShortcutCallbackIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
ShortcutManagerCompat.requestPinShortcut(context, pinShortcutInfo, successCallback.intentSender)
}
@JvmStatic
fun showPinShortcutNotSupportedDialog(context: Context): MaterialDialog {
return MaterialDialog.Builder(context)
.title(R.string.text_prompt)
.content(R.string.text_pin_shortcut_not_unsupported)
.positiveText(R.string.dialog_button_dismiss)
.build().also { it.show() }
}
}