Skip to content

Commit

Permalink
Merge pull request #2344 from marunjar/generalize_result_methods
Browse files Browse the repository at this point in the history
generalize result methods
  • Loading branch information
marunjar authored Nov 20, 2024
2 parents d57a785 + f45c9c8 commit 94ffdc9
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 73 deletions.
24 changes: 9 additions & 15 deletions app/src/main/java/fr/neamar/kiss/result/AppResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
7 changes: 1 addition & 6 deletions app/src/main/java/fr/neamar/kiss/result/CallResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,13 +23,9 @@ public abstract class CallResult<T extends Pojo> extends Result<T> {
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() {
Expand Down
36 changes: 14 additions & 22 deletions app/src/main/java/fr/neamar/kiss/result/ContactsResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,12 +47,10 @@ public class ContactsResult extends CallResult<ContactsPojo> {
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
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
});

Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand All @@ -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);
}
}
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/fr/neamar/kiss/result/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

}
12 changes: 4 additions & 8 deletions app/src/main/java/fr/neamar/kiss/result/SearchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand All @@ -211,13 +204,15 @@ 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) {
// Google app not found, fall back to default method
}
}
Intent search = createSearchQueryIntent();
setSourceBounds(search, v);
try {
context.startActivity(search);
} catch (ActivityNotFoundException e) {
Expand All @@ -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) {
Expand Down
11 changes: 2 additions & 9 deletions app/src/main/java/fr/neamar/kiss/result/SettingsResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
17 changes: 4 additions & 13 deletions app/src/main/java/fr/neamar/kiss/result/ShortcutsResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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));
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit 94ffdc9

Please sign in to comment.