From 6c7978ad420c68f366e143c4837263cd6909d874 Mon Sep 17 00:00:00 2001 From: marunjar Date: Fri, 8 Dec 2023 11:00:26 +0100 Subject: [PATCH] load icons async - load icons async for all `exclude ...` settings --- .../preference/ExcludePreferenceScreen.java | 84 +++++++++---------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/fr/neamar/kiss/preference/ExcludePreferenceScreen.java b/app/src/main/java/fr/neamar/kiss/preference/ExcludePreferenceScreen.java index 79eaba4d0..710930794 100644 --- a/app/src/main/java/fr/neamar/kiss/preference/ExcludePreferenceScreen.java +++ b/app/src/main/java/fr/neamar/kiss/preference/ExcludePreferenceScreen.java @@ -4,7 +4,6 @@ import android.content.Context; import android.graphics.drawable.Drawable; import android.os.Build; -import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceScreen; import android.widget.Toolbar; @@ -14,11 +13,14 @@ import java.util.Arrays; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; import fr.neamar.kiss.IconsHandler; import fr.neamar.kiss.KissApplication; +import fr.neamar.kiss.R; import fr.neamar.kiss.pojo.AppPojo; import fr.neamar.kiss.pojo.NameComparator; +import fr.neamar.kiss.utils.Utilities; /** * Normally this would be a subclass of PreferenceScreen but PreferenceScreen is final. @@ -26,9 +28,9 @@ public class ExcludePreferenceScreen { public static PreferenceScreen getInstance( - PreferenceActivity preferenceActivity, - IsExcludedCallback isExcludedCallback, - OnExcludedListener onExcludedListener, + @NonNull PreferenceActivity preferenceActivity, + @NonNull IsExcludedCallback isExcludedCallback, + @NonNull OnExcludedListener onExcludedListener, @StringRes int preferenceTitleResId, final @StringRes int preferenceScreenTitleResId ) { @@ -48,28 +50,20 @@ public static PreferenceScreen getInstance( excludedAppsScreen.setTitle(preferenceTitleResId); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - excludedAppsScreen.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - Toolbar toolbar = PreferenceScreenHelper.findToolbar(excludedAppsScreen); - if(toolbar != null) { - toolbar.setTitle(preferenceScreenTitleResId); - } - return false; + excludedAppsScreen.setOnPreferenceClickListener(preference -> { + Toolbar toolbar = PreferenceScreenHelper.findToolbar(excludedAppsScreen); + if (toolbar != null) { + toolbar.setTitle(preferenceScreenTitleResId); } + return false; }); } - for (AppPojo app : apps) { - final ComponentName componentName = new ComponentName(app.packageName, app.activityName); - - final Drawable icon = iconsHandler.getDrawableIconForPackage(componentName, app.userHandle); - - final boolean showSummary = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP - || preferenceActivity.getResources().getConfiguration().screenWidthDp > 420; + final boolean showSummary = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP + || preferenceActivity.getResources().getConfiguration().screenWidthDp > 420; - SwitchPreference pref = createExcludeAppSwitch(preferenceActivity, icon, app.getName(), - app.getComponentName(), isExcludedCallback.isExcluded(app), app, showSummary, onExcludedListener); + for (AppPojo app : apps) { + SwitchPreference pref = createExcludeAppSwitch(preferenceActivity, iconsHandler, isExcludedCallback, app, showSummary, onExcludedListener); excludedAppsScreen.addPreference(pref); } @@ -79,35 +73,41 @@ public boolean onPreferenceClick(Preference preference) { private static SwitchPreference createExcludeAppSwitch( @NonNull Context context, - @NonNull Drawable icon, - String appName, - String mainActivityName, - boolean isExcluded, + @NonNull IconsHandler iconsHandler, + @NonNull IsExcludedCallback isExcludedCallback, final @NonNull AppPojo app, boolean showSummary, - final OnExcludedListener onExcludedListener + @NonNull final OnExcludedListener onExcludedListener ) { final SwitchPreference switchPreference = new SwitchPreference(context); - switchPreference.setIcon(icon); - switchPreference.setTitle(appName); + + AtomicReference icon = new AtomicReference<>(null); + switchPreference.setIcon(R.drawable.ic_launcher_white); + Utilities.runAsync((task) -> { + final ComponentName componentName = new ComponentName(app.packageName, app.activityName); + icon.set(iconsHandler.getDrawableIconForPackage(componentName, app.userHandle)); + }, (task) -> { + if (!task.isCancelled()) { + switchPreference.setIcon(icon.get()); + } + }); + + switchPreference.setTitle(app.getName()); if (showSummary) { - switchPreference.setSummary(mainActivityName); + switchPreference.setSummary(app.getComponentName()); } - switchPreference.setChecked(isExcluded); + switchPreference.setChecked(isExcludedCallback.isExcluded(app)); switchPreference.setOnPreferenceChangeListener( - new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - boolean becameExcluded = newValue != null && (boolean) newValue; - - if(becameExcluded) { - onExcludedListener.onExcluded(app); - } else { - onExcludedListener.onIncluded(app); - } - - return true; + (preference, newValue) -> { + boolean becameExcluded = newValue != null && (boolean) newValue; + + if (becameExcluded) { + onExcludedListener.onExcluded(app); + } else { + onExcludedListener.onIncluded(app); } + + return true; } ); return switchPreference;