From f45c9c8f244fd7747b5f30f36c976f3f4ecf26ed Mon Sep 17 00:00:00 2001 From: marunjar Date: Wed, 20 Nov 2024 21:42:35 +0100 Subject: [PATCH] generalize result methods - generalize methods for reading some preferences - generalize methods for setting source bounds of intents --- .../java/fr/neamar/kiss/result/AppResult.java | 24 +++++-------- .../fr/neamar/kiss/result/CallResult.java | 7 +--- .../fr/neamar/kiss/result/ContactsResult.java | 36 ++++++++----------- .../java/fr/neamar/kiss/result/Result.java | 31 ++++++++++++++++ .../fr/neamar/kiss/result/SearchResult.java | 12 +++---- .../fr/neamar/kiss/result/SettingsResult.java | 11 ++---- .../neamar/kiss/result/ShortcutsResult.java | 17 +++------ 7 files changed, 65 insertions(+), 73 deletions(-) diff --git a/app/src/main/java/fr/neamar/kiss/result/AppResult.java b/app/src/main/java/fr/neamar/kiss/result/AppResult.java index f8c650337..8e70efed9 100644 --- a/app/src/main/java/fr/neamar/kiss/result/AppResult.java +++ b/app/src/main/java/fr/neamar/kiss/result/AppResult.java @@ -15,7 +15,6 @@ import android.os.Build; import android.os.Bundle; import android.os.Handler; -import android.preference.PreferenceManager; import android.provider.Settings; import android.util.Log; import android.view.Menu; @@ -67,8 +66,6 @@ public View display(final Context context, View view, @NonNull ViewGroup parent, view = inflateFromId(context, R.layout.item_app, parent); } - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - TextView appName = view.findViewById(R.id.item_app_name); displayHighlighted(pojo.normalizedName, pojo.getName(), fuzzyScore, appName, context); @@ -78,14 +75,14 @@ public View display(final Context context, View view, @NonNull ViewGroup parent, if (pojo.getTags().isEmpty()) { tagsView.setVisibility(View.GONE); } else if (displayHighlighted(pojo.getNormalizedTags(), pojo.getTags(), - fuzzyScore, tagsView, context) || prefs.getBoolean("tags-visible", true)) { + fuzzyScore, tagsView, context) || isTagsVisible(context)) { tagsView.setVisibility(View.VISIBLE); } else { tagsView.setVisibility(View.GONE); } final ImageView appIcon = view.findViewById(R.id.item_app_icon); - if (!prefs.getBoolean("icons-hide", false)) { + if (!isHideIcons(context)) { if (appIcon.getTag() instanceof ComponentName && className.equals(appIcon.getTag())) { icon = appIcon.getDrawable(); } @@ -448,11 +445,7 @@ public void doLaunch(Context context, View v) { intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setComponent(className); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); - - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - intent.setSourceBounds(getViewBounds(v)); - } - + setSourceBounds(intent, v); context.startActivity(intent); } } catch (ActivityNotFoundException | NullPointerException | SecurityException e) { @@ -463,14 +456,15 @@ public void doLaunch(Context context, View v) { } } - private Rect getViewBounds(View v) { - if (v == null) { + @Override + protected Rect getViewBounds(View view) { + if (view == null) { return null; } - int[] l = new int[2]; - v.getLocationOnScreen(l); - return new Rect(l[0], l[1], l[0] + v.getWidth(), l[1] + v.getHeight()); + int[] location = new int[2]; + view.getLocationOnScreen(location); + return new Rect(location[0], location[1], location[0] + view.getWidth(), location[1] + view.getHeight()); } public void setCustomIcon(long dbId, Drawable drawable) { diff --git a/app/src/main/java/fr/neamar/kiss/result/CallResult.java b/app/src/main/java/fr/neamar/kiss/result/CallResult.java index 828071dfb..d76c1f7cb 100644 --- a/app/src/main/java/fr/neamar/kiss/result/CallResult.java +++ b/app/src/main/java/fr/neamar/kiss/result/CallResult.java @@ -4,7 +4,6 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; -import android.os.Build; import android.view.View; import android.widget.Toast; @@ -24,13 +23,9 @@ public abstract class CallResult extends Result { public void launchCall(Context context, View v, String phone) { Intent phoneIntent = new Intent(Intent.ACTION_CALL); phoneIntent.setData(Uri.parse("tel:" + Uri.encode(phone))); - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - phoneIntent.setSourceBounds(v.getClipBounds()); - } - + setSourceBounds(phoneIntent, v); phoneIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - // Make sure we have permission to call someone as this is considered a dangerous permission if (!Permission.checkPermission(context, Permission.PERMISSION_CALL_PHONE)) { Permission.askPermission(Permission.PERMISSION_CALL_PHONE, new Permission.PermissionResultListener() { diff --git a/app/src/main/java/fr/neamar/kiss/result/ContactsResult.java b/app/src/main/java/fr/neamar/kiss/result/ContactsResult.java index bfc4dbce8..e524f07ea 100644 --- a/app/src/main/java/fr/neamar/kiss/result/ContactsResult.java +++ b/app/src/main/java/fr/neamar/kiss/result/ContactsResult.java @@ -7,7 +7,6 @@ import android.content.pm.PackageManager; import android.graphics.drawable.Drawable; import android.net.Uri; -import android.os.Build; import android.preference.PreferenceManager; import android.provider.ContactsContract; import android.text.TextUtils; @@ -48,12 +47,10 @@ public class ContactsResult extends CallResult { private volatile Drawable appDrawable = null; private Utilities.AsyncRun mLoadIconTask = null; private static final String TAG = ContactsResult.class.getSimpleName(); - private final UserHandle userHandle; ContactsResult(QueryInterface queryInterface, @NonNull ContactsPojo pojo) { super(pojo); this.queryInterface = queryInterface; - this.userHandle = new UserHandle(); } @NonNull @@ -93,8 +90,8 @@ public View display(Context context, View view, @NonNull ViewGroup parent, Fuzzy ImprovedQuickContactBadge contactIcon = view .findViewById(R.id.item_contact_icon); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - if (!prefs.getBoolean("icons-hide", false)) { + boolean hideIcons = isHideIcons(context); + if (!hideIcons) { if (contactIcon.getTag() instanceof ContactsPojo && pojo.equals(contactIcon.getTag())) { icon = contactIcon.getDrawable(); } @@ -139,7 +136,7 @@ public View display(Context context, View view, @NonNull ViewGroup parent, Fuzzy } else if (hasPhone) { messageButton.setVisibility(View.VISIBLE); messageButton.setOnClickListener(v -> { - launchMessaging(v.getContext()); + launchMessaging(v.getContext(), v); recordLaunch(context, queryInterface); }); @@ -154,7 +151,7 @@ public View display(Context context, View view, @NonNull ViewGroup parent, Fuzzy // App icon final ImageView appIcon = view.findViewById(R.id.item_app_icon); - if (pojo.getContactData() != null && !prefs.getBoolean("icons-hide", false)) { + if (pojo.getContactData() != null && !hideIcons && isSubIconVisible(context)) { appIcon.setVisibility(View.VISIBLE); if (appDrawable != null) { appIcon.setImageDrawable(appDrawable); @@ -186,7 +183,8 @@ private Drawable getAppDrawable(Context context) { ComponentName componentName = KissApplication.getMimeTypeCache(context).getComponentName(context, pojo.getContactData().getMimeType()); if (componentName != null) { IconsHandler iconsHandler = KissApplication.getApplication(context).getIconsHandler(); - appDrawable = iconsHandler.getDrawableIconForPackage(PackageManagerUtils.getLaunchingComponent(context, componentName, this.userHandle), this.userHandle); + UserHandle userHandle = new UserHandle(); + appDrawable = iconsHandler.getDrawableIconForPackage(PackageManagerUtils.getLaunchingComponent(context, componentName, userHandle), userHandle); } if (appDrawable == null) { // This should never happen, let's just return the generic activity icon @@ -287,17 +285,12 @@ private void launchContactView(Context context, View v) { viewContact.setData(Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, String.valueOf(pojo.lookupKey))); - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - viewContact.setSourceBounds(v.getClipBounds()); - } - + setSourceBounds(viewContact, v); viewContact.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); viewContact.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); context.startActivity(viewContact); - } - @Override public void doLaunch(Context context, View v) { SharedPreferences settingPrefs = PreferenceManager.getDefaultSharedPreferences(v.getContext()); @@ -310,19 +303,18 @@ public void doLaunch(Context context, View v) { } } - private void launchMessaging(final Context context) { + private void launchMessaging(final Context context, final View view) { String url = "sms:" + Uri.encode(pojo.phone); - Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse(url)); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - context.startActivity(i); + Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url)); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + setSourceBounds(intent, view); + context.startActivity(intent); } - private void launchIm(final Context context, final View v) { + private void launchIm(final Context context, final View view) { Intent intent = MimeTypeUtils.getRegisteredIntentByMimeType(context, pojo.getContactData().getMimeType(), pojo.getContactData().getId(), pojo.getContactData().getIdentifier()); if (intent != null) { - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - intent.setSourceBounds(v.getClipBounds()); - } + setSourceBounds(intent, view); context.startActivity(intent); } } diff --git a/app/src/main/java/fr/neamar/kiss/result/Result.java b/app/src/main/java/fr/neamar/kiss/result/Result.java index 7057fd287..a7fc4beda 100644 --- a/app/src/main/java/fr/neamar/kiss/result/Result.java +++ b/app/src/main/java/fr/neamar/kiss/result/Result.java @@ -2,10 +2,14 @@ import android.annotation.SuppressLint; import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; import android.content.res.TypedArray; import android.graphics.Color; +import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.AsyncTask; +import android.os.Build; import android.os.Handler; import android.preference.PreferenceManager; import android.text.Spannable; @@ -471,4 +475,31 @@ protected void onPostExecute(Drawable drawable) { image.setTag(resultWeakReference.get()); } } + + protected boolean isHideIcons(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + return prefs.getBoolean("icons-hide", false); + } + + protected boolean isTagsVisible(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + return prefs.getBoolean("tags-visible", true); + } + + protected boolean isSubIconVisible(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + return prefs.getBoolean("subicon-visible", true); + } + + protected void setSourceBounds(Intent intent, View view) { + intent.setSourceBounds(getViewBounds(view)); + } + + protected Rect getViewBounds(View view) { + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + return view.getClipBounds(); + } + return null; + } + } diff --git a/app/src/main/java/fr/neamar/kiss/result/SearchResult.java b/app/src/main/java/fr/neamar/kiss/result/SearchResult.java index 6b93a8470..049250eff 100644 --- a/app/src/main/java/fr/neamar/kiss/result/SearchResult.java +++ b/app/src/main/java/fr/neamar/kiss/result/SearchResult.java @@ -5,11 +5,9 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.net.Uri; -import android.preference.PreferenceManager; import android.util.Log; import android.util.Pair; import android.view.View; @@ -57,7 +55,7 @@ public View display(Context context, View view, @NonNull ViewGroup parent, Fuzzy int pos; int len; - boolean hideIcons = getHideIcons(context); + boolean hideIcons = isHideIcons(context); if (hideIcons) { image.setImageDrawable(null); } @@ -196,11 +194,6 @@ private Drawable getIconByPackageName(Context context, String packageName) { return null; } - private boolean getHideIcons(Context context) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - return prefs.getBoolean("icons-hide", false); - } - @Override public void doLaunch(Context context, View v) { switch (pojo.type) { @@ -211,6 +204,7 @@ public void doLaunch(Context context, View v) { Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(SearchManager.QUERY, pojo.query); // query contains search string + setSourceBounds(intent, v); context.startActivity(intent); break; } catch (ActivityNotFoundException e) { @@ -218,6 +212,7 @@ public void doLaunch(Context context, View v) { } } Intent search = createSearchQueryIntent(); + setSourceBounds(search, v); try { context.startActivity(search); } catch (ActivityNotFoundException e) { @@ -230,6 +225,7 @@ public void doLaunch(Context context, View v) { break; case URI_QUERY: Intent intent = createUriQueryIntent(); + setSourceBounds(intent, v); try { context.startActivity(intent); } catch (ActivityNotFoundException e) { diff --git a/app/src/main/java/fr/neamar/kiss/result/SettingsResult.java b/app/src/main/java/fr/neamar/kiss/result/SettingsResult.java index a3341fc1c..ebb54c0be 100644 --- a/app/src/main/java/fr/neamar/kiss/result/SettingsResult.java +++ b/app/src/main/java/fr/neamar/kiss/result/SettingsResult.java @@ -3,11 +3,8 @@ import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.graphics.PorterDuff.Mode; import android.graphics.drawable.Drawable; -import android.os.Build; -import android.preference.PreferenceManager; import android.util.Log; import android.view.View; import android.view.ViewGroup; @@ -38,8 +35,7 @@ public View display(Context context, View view, @NonNull ViewGroup parent, Fuzzy displayHighlighted(pojo.normalizedName, pojo.getName(), fuzzyScore, settingName, context); ImageView settingIcon = view.findViewById(R.id.item_setting_icon); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - if (!prefs.getBoolean("icons-hide", false)) { + if (!isHideIcons(context)) { settingIcon.setImageDrawable(getDrawable(context)); settingIcon.setColorFilter(getThemeFillColor(context), Mode.SRC_IN); } else { @@ -66,10 +62,7 @@ public void doLaunch(Context context, View v) { if (!pojo.packageName.isEmpty()) { intent.setClassName(pojo.packageName, pojo.settingName); } - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - intent.setSourceBounds(v.getClipBounds()); - } - + setSourceBounds(intent, v); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { diff --git a/app/src/main/java/fr/neamar/kiss/result/ShortcutsResult.java b/app/src/main/java/fr/neamar/kiss/result/ShortcutsResult.java index c777c332f..f674ccd3a 100644 --- a/app/src/main/java/fr/neamar/kiss/result/ShortcutsResult.java +++ b/app/src/main/java/fr/neamar/kiss/result/ShortcutsResult.java @@ -6,13 +6,11 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.content.pm.LauncherApps; import android.content.pm.ShortcutInfo; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.UserManager; -import android.preference.PreferenceManager; import android.util.Log; import android.view.View; import android.view.ViewGroup; @@ -63,8 +61,6 @@ public View display(final Context context, View view, @NonNull ViewGroup parent, if (view == null) view = inflateFromId(context, R.layout.item_shortcut, parent); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - TextView shortcutName = view.findViewById(R.id.item_app_name); displayHighlighted(pojo.normalizedName, pojo.getName(), fuzzyScore, shortcutName, context); @@ -75,7 +71,7 @@ public View display(final Context context, View view, @NonNull ViewGroup parent, if (pojo.getTags().isEmpty()) { tagsView.setVisibility(View.GONE); } else if (displayHighlighted(pojo.getNormalizedTags(), pojo.getTags(), - fuzzyScore, tagsView, context) || prefs.getBoolean("tags-visible", true)) { + fuzzyScore, tagsView, context) || isTagsVisible(context)) { tagsView.setVisibility(View.VISIBLE); } else { tagsView.setVisibility(View.GONE); @@ -84,7 +80,7 @@ public View display(final Context context, View view, @NonNull ViewGroup parent, final ImageView shortcutIcon = view.findViewById(R.id.item_shortcut_icon); final ImageView appIcon = view.findViewById(R.id.item_app_icon); - if (!prefs.getBoolean("icons-hide", false)) { + if (!isHideIcons(context)) { // set shortcut icon this.setAsyncDrawable(shortcutIcon); @@ -94,10 +90,8 @@ public View display(final Context context, View view, @NonNull ViewGroup parent, mLoadIconTask = null; } - boolean subIconVisible = prefs.getBoolean("subicon-visible", true); - // Prepare - if (subIconVisible) { + if (isSubIconVisible(context)) { appIcon.setVisibility(View.VISIBLE); if (appDrawable != null) { appIcon.setImageDrawable(getAppDrawable(context)); @@ -220,10 +214,7 @@ protected void doLaunch(Context context, View v) { // Pre-oreo shortcuts try { Intent intent = Intent.parseUri(pojo.intentUri, 0); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - intent.setSourceBounds(v.getClipBounds()); - } - + setSourceBounds(intent, v); context.startActivity(intent); } catch (Exception e) { // Application was just removed?