diff --git a/.changelog/lang_zh-Hans.json b/.changelog/lang_zh-Hans.json index 3e0c2c99..14cfb9f0 100644 --- a/.changelog/lang_zh-Hans.json +++ b/.changelog/lang_zh-Hans.json @@ -1,13 +1,14 @@ { "$data": { "v6.6.1": { - "released_date": "2024/12/05", + "released_date": "2024/12/07", "feature": [ "pinyin 模块, 用于汉语拼音转换 (参阅 项目文档 > [汉语拼音](https://docs.autojs6.com/#/pinyin))" ], "fix": [ "部分环境因回退版本过低而无法正常编译项目的问题", - "调用不存在的方法时可能出现的 \"非原始类型值\" 异常" + "调用不存在的方法时可能出现的 \"非原始类型值\" 异常", + "部分设备无法正常添加脚本快捷方式的问题 (试修) _[`issue #221`](http://issues.autojs6.com/221)_" ], "improvement": [ "脚本项目识别在 project.json 损坏情况下尽可能还原关键信息", diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index eb73efc0..ee85bf28 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,9 +1,6 @@ \ No newline at end of file diff --git a/app/src/main/java/org/autojs/autojs/ui/common/ScriptOperations.java b/app/src/main/java/org/autojs/autojs/ui/common/ScriptOperations.java index 843394b2..ac01964d 100644 --- a/app/src/main/java/org/autojs/autojs/ui/common/ScriptOperations.java +++ b/app/src/main/java/org/autojs/autojs/ui/common/ScriptOperations.java @@ -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) diff --git a/app/src/main/java/org/autojs/autojs/ui/doc/DocumentationActivity.kt b/app/src/main/java/org/autojs/autojs/ui/doc/DocumentationActivity.kt index 9c7c0239..b1282879 100644 --- a/app/src/main/java/org/autojs/autojs/ui/doc/DocumentationActivity.kt +++ b/app/src/main/java/org/autojs/autojs/ui/doc/DocumentationActivity.kt @@ -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" diff --git a/app/src/main/java/org/autojs/autojs/ui/settings/LauncherShortcutsPreference.kt b/app/src/main/java/org/autojs/autojs/ui/settings/LauncherShortcutsPreference.kt index fbdb9e94..8b4d281a 100644 --- a/app/src/main/java/org/autojs/autojs/ui/settings/LauncherShortcutsPreference.kt +++ b/app/src/main/java/org/autojs/autojs/ui/settings/LauncherShortcutsPreference.kt @@ -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(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(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(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() - } - } diff --git a/app/src/main/java/org/autojs/autojs/ui/shortcut/ShortcutCreateActivity.java b/app/src/main/java/org/autojs/autojs/ui/shortcut/ShortcutCreateActivity.java index 336ac444..80e53089 100644 --- a/app/src/main/java/org/autojs/autojs/ui/shortcut/ShortcutCreateActivity.java +++ b/app/src/main/java/org/autojs/autojs/ui/shortcut/ShortcutCreateActivity.java @@ -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()); } } diff --git a/app/src/main/java/org/autojs/autojs/util/ShortcutUtils.kt b/app/src/main/java/org/autojs/autojs/util/ShortcutUtils.kt index 502aa147..32c4d66a 100644 --- a/app/src/main/java/org/autojs/autojs/util/ShortcutUtils.kt +++ b/app/src/main/java/org/autojs/autojs/util/ShortcutUtils.kt @@ -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() } + } + } \ No newline at end of file diff --git a/version.properties b/version.properties index 9e9ab03a..f1f9e0ff 100644 --- a/version.properties +++ b/version.properties @@ -1,5 +1,5 @@ -#Thu Dec 05 01:41:07 CST 2024 -BUILD_TIME=1733334067701 +#Sat Dec 07 17:46:27 CST 2024 +BUILD_TIME=1733564787201 COMPILE_SDK_VERSION=34 JAVA_VERSION=23 JAVA_VERSION_MIN_RADICAL=0 @@ -17,6 +17,6 @@ RAPID_OCR_OPENCV_MOBILE_LABEL_VERSION=13 RAPID_OCR_OPENCV_MOBILE_VERSION=4.5.3 TARGET_SDK_VERSION=34 TARGET_SDK_VERSION_INRT=29 -VERSION_BUILD=2893 +VERSION_BUILD=2896 VERSION_NAME=6.6.1 Alpha2 VSCODE_EXT_REQUIRED_VERSION=1.0.8