Skip to content

Commit

Permalink
load icons async
Browse files Browse the repository at this point in the history
- load icons async for all `exclude ...` settings
  • Loading branch information
marunjar committed Dec 8, 2023
1 parent 853bb2e commit 6c7978a
Showing 1 changed file with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,21 +13,24 @@

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.
*/
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
) {
Expand All @@ -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);
}
Expand All @@ -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<Drawable> 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;
Expand Down

0 comments on commit 6c7978a

Please sign in to comment.