1111
1212import java .lang .ref .WeakReference ;
1313import java .util .ArrayList ;
14+ import java .util .Collections ;
1415import java .util .List ;
1516import java .util .Locale ;
17+ import java .util .Set ;
1618
19+ import fr .neamar .kiss .KissApplication ;
1720import fr .neamar .kiss .R ;
1821import fr .neamar .kiss .normalizer .StringNormalizer ;
1922import fr .neamar .kiss .pojo .SettingPojo ;
2427
2528public 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}
0 commit comments