From a660a98539fd5b83c1cc8605188d823744883f76 Mon Sep 17 00:00:00 2001 From: hyb1996 <946994919@qq.com> Date: Sun, 9 Jul 2017 10:08:47 +0800 Subject: [PATCH] fix lag when click shortcut of script with dialog --- .../external/shortcut/Shortcut.java | 41 +++++++++++++++---- .../runtime/api/ui/BlockedMaterialDialog.java | 5 ++- .../autojs/runtime/api/ui/Dialogs.java | 5 ++- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/stardust/scriptdroid/external/shortcut/Shortcut.java b/app/src/main/java/com/stardust/scriptdroid/external/shortcut/Shortcut.java index ce00f41a..9087b4aa 100644 --- a/app/src/main/java/com/stardust/scriptdroid/external/shortcut/Shortcut.java +++ b/app/src/main/java/com/stardust/scriptdroid/external/shortcut/Shortcut.java @@ -2,7 +2,7 @@ package com.stardust.scriptdroid.external.shortcut; import android.content.Context; import android.content.Intent; -import android.content.pm.PackageManager; +import android.graphics.Bitmap; import android.os.Bundle; /** @@ -15,9 +15,10 @@ public class Shortcut { private String mName; private String mTargetClass; private String mTargetPackage; - private Intent.ShortcutIconResource mIcon; + private Intent.ShortcutIconResource mIconRes; private boolean mDuplicate = false; private Intent mLaunchIntent = new Intent(); + private Bitmap mIcon; public Shortcut(Context context) { mContext = context; @@ -50,16 +51,32 @@ public class Shortcut { } - public Shortcut icon(Intent.ShortcutIconResource icon) { + public Shortcut iconRes(Intent.ShortcutIconResource icon) { + if (mIcon != null) { + throw new IllegalStateException("Cannot set both iconRes and icon"); + } + mIconRes = icon; + return this; + } + + public Shortcut iconRes(int resId) { + return iconRes(Intent.ShortcutIconResource.fromContext(mContext, resId)); + } + + + public Shortcut icon(Bitmap icon) { + if (mIconRes != null) { + throw new IllegalStateException("Cannot set both iconRes and icon"); + } mIcon = icon; return this; } - public Shortcut icon(int resId) { - mIcon = Intent.ShortcutIconResource.fromContext(mContext, resId); - return this; + public Intent.ShortcutIconResource getIconRes() { + return mIconRes; } + public Shortcut duplicate(boolean duplicate) { mDuplicate = duplicate; return this; @@ -69,7 +86,7 @@ public class Shortcut { return mName; } - public Intent.ShortcutIconResource getIcon() { + public Bitmap getIcon() { return mIcon; } @@ -86,12 +103,18 @@ public class Shortcut { } public Intent getCreateIntent() { - return new Intent(Intent.ACTION_CREATE_SHORTCUT) + Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT) .putExtra(Intent.EXTRA_SHORTCUT_NAME, getName()) - .putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, getIcon()) .putExtra(Intent.EXTRA_SHORTCUT_INTENT, getLaunchIntent()) .putExtra("duplicate", isDuplicate()) .setAction("com.android.launcher.action.INSTALL_SHORTCUT"); + Bitmap icon = getIcon(); + if (icon == null) { + intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, getIconRes()); + } else { + intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon); + } + return intent; } public Intent getLaunchIntent() { diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/api/ui/BlockedMaterialDialog.java b/autojs/src/main/java/com/stardust/autojs/runtime/api/ui/BlockedMaterialDialog.java index f86978bf..25004e3e 100644 --- a/autojs/src/main/java/com/stardust/autojs/runtime/api/ui/BlockedMaterialDialog.java +++ b/autojs/src/main/java/com/stardust/autojs/runtime/api/ui/BlockedMaterialDialog.java @@ -36,8 +36,9 @@ public class BlockedMaterialDialog extends MaterialDialog { private boolean isActivityContext(Context context) { if (context == null) return false; - if (context instanceof Activity) - return true; + if (context instanceof Activity) { + return !((Activity) context).isFinishing(); + } if (context instanceof ContextWrapper) { return isActivityContext(((ContextWrapper) context).getBaseContext()); } diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/api/ui/Dialogs.java b/autojs/src/main/java/com/stardust/autojs/runtime/api/ui/Dialogs.java index 35d9a5ab..01f5c6bc 100644 --- a/autojs/src/main/java/com/stardust/autojs/runtime/api/ui/Dialogs.java +++ b/autojs/src/main/java/com/stardust/autojs/runtime/api/ui/Dialogs.java @@ -1,5 +1,6 @@ package com.stardust.autojs.runtime.api.ui; +import android.app.Activity; import android.content.Context; import android.content.ContextWrapper; import android.text.TextUtils; @@ -73,7 +74,7 @@ public class Dialogs { private Context getContext() { if (mThemeWrapper != null) return mThemeWrapper; - mThemeWrapper = new ContextThemeWrapper(mUiHandler.getContext(), R.style.Theme_AppCompat_Light); + mThemeWrapper = new ContextThemeWrapper(mUiHandler.getContext().getApplicationContext(), R.style.Theme_AppCompat_Light); return mThemeWrapper; } @@ -115,7 +116,7 @@ public class Dialogs { private BlockedMaterialDialog.Builder dialogBuilder() { Context context = mAppUtils.getCurrentActivity(); - if (context == null) { + if (context == null || ((Activity) context).isFinishing()) { context = getContext(); } return (BlockedMaterialDialog.Builder) new BlockedMaterialDialog.Builder(context, mUiHandler)