Skip to content

Commit cad2c94

Browse files
authored
Merge pull request #2643 from marunjar/settings_in_favorites
Settings in favorites
2 parents bba714f + 3a1d5eb commit cad2c94

11 files changed

Lines changed: 152 additions & 40 deletions

File tree

app/src/main/java/fr/neamar/kiss/DataHandler.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,19 @@ public void reloadSearchProvider() {
831831
}
832832
}
833833

834+
@Nullable
835+
public SettingsProvider getSettingsProvider() {
836+
ProviderEntry entry = this.providers.get("settings");
837+
return (entry != null) ? ((SettingsProvider) entry.provider) : null;
838+
}
839+
840+
public void reloadSettingsProvider() {
841+
SettingsProvider settingsProvider = getSettingsProvider();
842+
if (settingsProvider != null) {
843+
settingsProvider.reload();
844+
}
845+
}
846+
834847
/**
835848
* @return list with favorite ids
836849
*/
@@ -947,6 +960,15 @@ public void resetFavorites() {
947960
setFavoriteIds(Collections.emptyList());
948961
}
949962

963+
/**
964+
* @param id favorite id
965+
* @return true, if id is from favorite
966+
*/
967+
public boolean hasFavorite(String id) {
968+
List<String> favoriteIds = getFavoriteIds();
969+
return favoriteIds.contains(id);
970+
}
971+
950972
/**
951973
* Insert launching activity of package into history
952974
*

app/src/main/java/fr/neamar/kiss/IconsHandler.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -608,17 +608,20 @@ private Drawable getCustomIcon(@NonNull Pojo pojo) {
608608
return null;
609609
}
610610

611-
public Drawable getThemedDrawable(@NonNull Pojo pojo, @DrawableRes int resId, @ColorInt int backgroundColor, @ColorInt int textColor, @ColorInt int themeFillColor) {
611+
@NonNull
612+
public Drawable getThemedDrawable(@NonNull Pojo pojo, @DrawableRes int resId, @ColorInt int backgroundColor, @ColorInt int textColor, @ColorInt int themeFillColor, boolean makeThemedIcon) {
612613
Drawable icon = getCustomIcon(pojo);
613614
if (icon != null) {
614615
return icon;
615616
}
616617

617618
icon = ResourcesCompat.getDrawable(ctx.getResources(), resId, ctx.getTheme());
618-
if (DrawableUtils.isAdaptiveIconDrawable(icon)) {
619+
if (icon == null) {
620+
return PackageManagerUtils.getDefaultActivityIcon(ctx);
621+
} else if (DrawableUtils.isAdaptiveIconDrawable(icon)) {
619622
return icon;
620-
} else if (DrawableUtils.hasThemedIcons() &&
621-
DrawableUtils.isThemedIconEnabled(ctx)) {
623+
} else if (makeThemedIcon ||
624+
(DrawableUtils.hasThemedIcons() && DrawableUtils.isThemedIconEnabled(ctx))) {
622625
Drawable background = getBackgroundDrawable(backgroundColor);
623626
int insetX = (int) (background.getIntrinsicWidth() * 0.15);
624627
int insetY = (int) (background.getIntrinsicHeight() * 0.15);

app/src/main/java/fr/neamar/kiss/SettingsFragment.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ public void onDenied() {
214214
updateNightMode();
215215
} else if ("night-mode".equals(key)) {
216216
InterfaceTweaks.setDefaultNightMode(KissApplication.getApplication(requireContext()));
217+
} else if ("enable-settings".equals(key)) {
218+
getDataHandler().reloadSettingsProvider();
217219
}
218220
}
219221
}

app/src/main/java/fr/neamar/kiss/dataprovider/simpleprovider/SettingsProvider.java

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111

1212
import java.lang.ref.WeakReference;
1313
import java.util.ArrayList;
14+
import java.util.Collections;
1415
import java.util.List;
1516
import java.util.Locale;
17+
import java.util.Set;
1618

19+
import fr.neamar.kiss.KissApplication;
1720
import fr.neamar.kiss.R;
1821
import fr.neamar.kiss.normalizer.StringNormalizer;
1922
import fr.neamar.kiss.pojo.SettingPojo;
@@ -24,12 +27,30 @@
2427

2528
public class SettingsProvider extends SimpleProvider<SettingPojo> {
2629
private final static String SCHEME = "setting://";
27-
private final String settingName;
28-
private final List<SettingPojo> pojos;
30+
private final String settingsPrefix;
31+
private final List<SettingPojo> pojos = new ArrayList<>();
2932
private final WeakReference<Context> contextReference;
3033

3134
public SettingsProvider(Context context) {
32-
pojos = new ArrayList<>();
35+
this.settingsPrefix = context.getString(R.string.settings_prefix).toLowerCase(Locale.ROOT);
36+
this.contextReference = new WeakReference<>(context);
37+
38+
reload();
39+
}
40+
41+
@Override
42+
public void reload() {
43+
pojos.clear();
44+
45+
Context context = contextReference.get();
46+
if (context == null) {
47+
return;
48+
}
49+
50+
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
51+
if (!prefs.getBoolean("enable-settings", true)) {
52+
return;
53+
}
3354

3455
PackageManager pm = context.getPackageManager();
3556
pojos.add(createPojo(context.getString(R.string.settings_airplane),
@@ -58,10 +79,6 @@ public SettingsProvider(Context context) {
5879
}
5980
pojos.add(createPojo(context.getString(R.string.settings_dev),
6081
Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS, R.drawable.setting_dev));
61-
62-
settingName = context.getString(R.string.settings_prefix).toLowerCase(Locale.ROOT);
63-
64-
this.contextReference = new WeakReference<>(context);
6582
}
6683

6784
private void assignName(SettingPojo pojo, String name) {
@@ -87,30 +104,31 @@ private SettingPojo createPojo(String name, String settingName, @DrawableRes int
87104

88105
@Override
89106
public void requestResults(String query, Searcher searcher) {
90-
Context context = contextReference.get();
91-
if (context == null) {
92-
return;
93-
}
94-
95-
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
96-
if (!prefs.getBoolean("enable-settings", true)) {
107+
StringNormalizer.Result queryNormalized = StringNormalizer.normalizeWithResult(query, false);
108+
if (queryNormalized.codePoints.length == 0) {
97109
return;
98110
}
99111

100-
StringNormalizer.Result queryNormalized = StringNormalizer.normalizeWithResult(query, false);
101-
if (queryNormalized.codePoints.length == 0) {
112+
Context context = contextReference.get();
113+
if (context == null) {
102114
return;
103115
}
104116

105117
FuzzyScore fuzzyScore = FuzzyFactory.createFuzzyScore(context, queryNormalized.codePoints);
118+
Set<String> excludedFavoriteIds = KissApplication.getApplication(context).getDataHandler().getExcludedFavorites();
119+
120+
for (SettingPojo pojo : getPojos()) {
121+
// exclude favorites from results
122+
if (excludedFavoriteIds.contains(pojo.getFavoriteId())) {
123+
continue;
124+
}
106125

107-
for (SettingPojo pojo : pojos) {
108126
MatchInfo matchInfo = fuzzyScore.match(pojo.normalizedName.codePoints);
109127
boolean match = pojo.updateMatchingRelevance(matchInfo, false);
110128

111129
if (!match) {
112130
// Match localized setting name
113-
matchInfo = fuzzyScore.match(settingName);
131+
matchInfo = fuzzyScore.match(settingsPrefix);
114132
match = pojo.updateMatchingRelevance(matchInfo, match);
115133
}
116134

@@ -122,15 +140,17 @@ public void requestResults(String query, Searcher searcher) {
122140

123141

124142
/**
125-
* Tells whether or not this provider may be able to find the pojo with
126-
* specified id
127-
*
128-
* @param id id we're looking for
129-
* @return true if the provider can handle the query ; does not guarantee it
130-
* will!
143+
* {@inheritDoc}
131144
*/
132145
public boolean mayFindById(String id) {
133146
return id.startsWith(SCHEME);
134147
}
135148

149+
/**
150+
* {@inheritDoc}
151+
*/
152+
@Override
153+
public List<SettingPojo> getPojos() {
154+
return Collections.unmodifiableList(pojos);
155+
}
136156
}

app/src/main/java/fr/neamar/kiss/dataprovider/simpleprovider/SimpleProvider.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,52 @@
1111
* so whenever we can, we avoid using them.
1212
*/
1313
public abstract class SimpleProvider<T extends Pojo> implements IProvider<T> {
14+
15+
/**
16+
* {@inheritDoc}
17+
*/
1418
@Override
1519
public void reload() {
16-
// Simple providers can't be reloaded
20+
// Simple providers doesn't reload anything by default
1721
}
1822

23+
/**
24+
* Indicate whether this provider has already loaded it's data.
25+
*
26+
* @return always true for simple provider
27+
*/
1928
@Override
2029
public final boolean isLoaded() {
2130
return true;
2231
}
2332

33+
/**
34+
* {@inheritDoc}
35+
*/
2436
@Override
2537
public boolean mayFindById(String id) {
2638
return false;
2739
}
2840

41+
/**
42+
* {@inheritDoc}
43+
*/
2944
@Override
3045
public T findById(String id) {
46+
List<T> pojos = getPojos();
47+
if (pojos != null) {
48+
for (T pojo : pojos) {
49+
if (pojo.id.equals(id)) {
50+
return pojo;
51+
}
52+
}
53+
}
3154
return null;
3255
}
3356

57+
/**
58+
* {@inheritDoc}
59+
*/
3460
@Override
3561
public List<T> getPojos() {
3662
return null;

app/src/main/java/fr/neamar/kiss/preference/ImportSettingsPreference.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public void onDialogClosed(Context context, boolean positiveResult) {
126126
dataHandler.reloadApps();
127127
dataHandler.reloadShortcuts();
128128
dataHandler.reloadSearchProvider();
129+
dataHandler.reloadSettingsProvider();
129130
dataHandler.reloadContactsProvider();
130131

131132
Toast.makeText(context, R.string.import_settings_done, Toast.LENGTH_SHORT).show();

app/src/main/java/fr/neamar/kiss/result/ContactsResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private Drawable getAppDrawable(Context context) {
163163
}
164164
if (appDrawable == null) {
165165
// This should never happen, let's just return the generic activity icon
166-
appDrawable = context.getPackageManager().getDefaultActivityIcon();
166+
appDrawable = PackageManagerUtils.getDefaultActivityIcon(context);
167167
}
168168
}
169169
}

app/src/main/java/fr/neamar/kiss/result/Result.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,7 @@ protected void buildPopupMenu(Context context, ArrayAdapter<ListPopup.Item> adap
287287
if (isAllowedAsFavorite()) {
288288
// If app already pinned, do not display the "add to favorite" option
289289
// otherwise don't show the "remove favorite button"
290-
String favApps = PreferenceManager.getDefaultSharedPreferences(context).
291-
getString("favorite-apps-list", "");
292-
if (!favApps.contains(this.pojo.id + ";")) {
290+
if (!KissApplication.getApplication(context).getDataHandler().hasFavorite(this.pojo.getFavoriteId())) {
293291
adapter.add(new ListPopup.Item(context, R.string.menu_favorites_add));
294292
} else {
295293
adapter.add(new ListPopup.Item(context, R.string.menu_favorites_remove));
@@ -538,7 +536,7 @@ int getThemeFillColor(Context context) {
538536
}
539537

540538
protected Drawable getThemedDrawable(@NonNull Context context, @NonNull Pojo pojo, @DrawableRes int resId) {
541-
return KissApplication.getApplication(context).getIconsHandler().getThemedDrawable(pojo, resId, getBackgroundColor(context), getTextColor(context), getThemeFillColor(context));
539+
return KissApplication.getApplication(context).getIconsHandler().getThemedDrawable(pojo, resId, getBackgroundColor(context), getTextColor(context), getThemeFillColor(context), makeThemedIcon());
542540
}
543541

544542
@ColorInt
@@ -614,4 +612,11 @@ protected void setTranscriptModeDisabled(RecordAdapter adapter) {
614612
public final String getCustomIconId() {
615613
return pojo.getCustomIconId();
616614
}
615+
616+
/**
617+
* @return true, if icon should be put onto shaped background and follow theme colors
618+
*/
619+
protected boolean makeThemedIcon() {
620+
return false;
621+
}
617622
}

app/src/main/java/fr/neamar/kiss/result/SettingsResult.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
import fr.neamar.kiss.icons.IconPack;
1717
import fr.neamar.kiss.pojo.SettingPojo;
1818
import fr.neamar.kiss.utils.Log;
19+
import fr.neamar.kiss.utils.PackageManagerUtils;
1920
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;
2021

2122
public class SettingsResult extends Result<SettingPojo> {
2223
private static final String TAG = SettingsResult.class.getSimpleName();
2324

25+
private volatile Drawable icon = null;
26+
2427
SettingsResult(@NonNull SettingPojo pojo) {
2528
super(pojo);
2629
}
@@ -44,13 +47,30 @@ public View display(Context context, View view, @NonNull ViewGroup parent, Fuzzy
4447
return view;
4548
}
4649

50+
@Override
51+
boolean isDrawableCached() {
52+
return icon != null;
53+
}
54+
55+
@Override
56+
void setDrawableCache(Drawable drawable) {
57+
icon = drawable;
58+
}
59+
4760
@Override
4861
public Drawable getDrawable(Context context) {
49-
if (pojo.icon != -1) {
50-
return getThemedDrawable(context, pojo, pojo.icon);
62+
if (icon == null) {
63+
synchronized (this) {
64+
if (icon == null) {
65+
icon = getThemedDrawable(context, pojo, pojo.icon);
66+
if (icon == null) {
67+
// This should never happen, let's just return the generic activity icon
68+
icon = PackageManagerUtils.getDefaultActivityIcon(context);
69+
}
70+
}
71+
}
5172
}
52-
53-
return null;
73+
return icon;
5474
}
5575

5676
@Override
@@ -84,4 +104,12 @@ protected boolean canRemoveFromHistory(Context context) {
84104
protected boolean canHaveCustomIcon(Context context, IconPack iconPack) {
85105
return true;
86106
}
107+
108+
/**
109+
* {@inheritDoc}
110+
*/
111+
@Override
112+
protected boolean makeThemedIcon() {
113+
return true;
114+
}
87115
}

app/src/main/java/fr/neamar/kiss/utils/DrawableUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ public static boolean hasThemedIcons() {
349349
* @param drawable
350350
* @return themed drawable
351351
*/
352+
@NonNull
352353
public static Drawable getThemedDrawable(@NonNull Context ctx, @NonNull Drawable drawable) {
353354
if (isAdaptiveIconDrawable(drawable) &&
354355
hasThemedIcons() &&

0 commit comments

Comments
 (0)