Returns a filter that can be used to constrain data with a filtering 114 | * pattern.
115 | * 116 | *This method is usually implemented by {@link android.widget.Adapter} 117 | * classes.
118 | * 119 | * @return a filter used to constrain data 120 | */ 121 | @Override 122 | public Filter getFilter() { 123 | Filter filter = new Filter() { 124 | @Override 125 | protected FilterResults performFiltering(CharSequence constraint) { 126 | ArrayListtrue
if you want to add this item in backstack. pass false
otherwise
35 | */
36 | public void startFragment(Fragment fragment, String tag, boolean canAddToBackstack) {
37 | FragmentTransaction transaction = getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, fragment);
38 | if (canAddToBackstack) {
39 | transaction.addToBackStack(tag);
40 | }
41 | transaction.commit();
42 |
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/sharedpreferenceinspector/src/main/java/com/ceelites/sharedpreferenceinspector/Utils/SharedPreferenceUtils.java:
--------------------------------------------------------------------------------
1 | package com.ceelites.sharedpreferenceinspector.Utils;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.content.SharedPreferences;
6 | import android.preference.PreferenceManager;
7 | import android.text.TextUtils;
8 | import android.view.Menu;
9 | import android.view.MenuInflater;
10 | import android.view.MenuItem;
11 | import com.ceelites.sharedpreferenceinspector.R;
12 | import com.ceelites.sharedpreferenceinspector.SharedPrefsBrowser;
13 | import java.util.Map;
14 |
15 | /**
16 | * Helper class to create shared preferences files. It will also help you to read from and write to shared preferences. Created by Prasham on
17 | * 17-01-2015.
18 | */
19 | public class SharedPreferenceUtils {
20 | public static final String keyTestMode = "test_mode_";
21 | private static SharedPreferenceUtils sharedPreferenceUtils;
22 | private SharedPreferences sharedPreferences;
23 |
24 | public static final String STRING = "String";
25 | public static final String INT = "Integer";
26 | public static final String FLOAT = "Float";
27 | public static final String LONG = "Long";
28 | public static final String BOOLEAN = "Boolean";
29 |
30 | public static final int SPINNER_STRING = 0;
31 | public static final int SPINNER_INT = 1;
32 | public static final int SPINNER_LONG = 2;
33 | public static final int SPINNER_BOOLEAN = 3;
34 | public static final int SPINNER_FLOAT = 4;
35 |
36 | /**
37 | * Init SharedPreferenceUtils with SharedPreferences file
38 | *
39 | * @param sharedPreferences
40 | * : SharedPreferences object to init with.
41 | *
42 | * @return : SharedPreferenceUtils object. It will store the sharedPreferences value with given SharedPreferences.
43 | */
44 | public static SharedPreferenceUtils initWith(SharedPreferences sharedPreferences) {
45 |
46 | if (sharedPreferenceUtils == null) {
47 | sharedPreferenceUtils = new SharedPreferenceUtils();
48 | }
49 | sharedPreferenceUtils.sharedPreferences = sharedPreferences;
50 | return sharedPreferenceUtils;
51 | }
52 |
53 | /**
54 | * Init SharedPreferences with context and a SharedPreferences name
55 | *
56 | * @param context:
57 | * Context to init SharedPreferences
58 | * @param name:
59 | * Name of SharedPreferences file. If you pass null
it will create default SharedPrefernces
60 | *
61 | * @return: SharedPreferenceUtils object. It will store given the sharedPreferences value with given SharedPreferences.
62 | */
63 | public static SharedPreferenceUtils initWith(Context context, String name) {
64 | if (sharedPreferenceUtils == null) {
65 | sharedPreferenceUtils = new SharedPreferenceUtils();
66 | }
67 | if (isEmptyString(name)) {
68 | sharedPreferenceUtils.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
69 | } else {
70 | sharedPreferenceUtils.sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
71 | }
72 | return sharedPreferenceUtils;
73 | }
74 |
75 | /**
76 | * This method checks if the string is empty or having null value.
77 | *
78 | * @param string
79 | * : Charsequence string to check.
80 | *
81 | * @return true
if string is null, blank or having "null" as value
82 | */
83 | public static boolean isEmptyString(CharSequence string) {
84 | return (TextUtils.isEmpty(string) || string.toString().equalsIgnoreCase("null"));
85 | }
86 |
87 | /**
88 | * Extract number from string, failsafe. If the string is not a proper number it will always return 0;
89 | *
90 | * @param string
91 | * : String that should be converted into a number
92 | *
93 | * @return : 0 if conversion to number is failed anyhow, otherwise converted number is returned
94 | */
95 | public static int getNumber(CharSequence string) {
96 | int number = 0;
97 | if (!isEmptyString(string)) {
98 | if (TextUtils.isDigitsOnly(string)) {
99 |
100 | number = Integer.parseInt(string.toString());
101 | }
102 | }
103 | return number;
104 | }
105 |
106 | /**
107 | * Extract number from string, failsafe. If the string is not a proper number it will always return 0.0f;
108 | *
109 | * @param string
110 | * : String that should be converted into a number
111 | *
112 | * @return : 0.0f if conversion to number is failed anyhow, otherwise converted number is returned
113 | */
114 | public static float getNumberFloat(CharSequence string) {
115 | float number = 0.0f;
116 | try {
117 | if (!isEmptyString(string)) {
118 | number = Float.parseFloat(string.toString());
119 | }
120 | } catch (NumberFormatException e) {
121 | e.printStackTrace();
122 | }
123 | return number;
124 | }
125 |
126 | /**
127 | * Extract number from string, failsafe. If the string is not a proper number it will always return 0l;
128 | *
129 | * @param string
130 | * : String that should be converted into a number
131 | *
132 | * @return : 0l if conversion to number is failed anyhow, otherwise converted number is returned
133 | */
134 |
135 | public static long getNumberLong(CharSequence string) {
136 | long number = 0l;
137 | try {
138 | if (!isEmptyString(string)) {
139 | if (TextUtils.isDigitsOnly(string)) {
140 |
141 | number = Long.parseLong(string.toString());
142 | }
143 | }
144 | } catch (NumberFormatException e) {
145 | e.printStackTrace();
146 | }
147 | return number;
148 | }
149 |
150 | /**
151 | * Gets all KeyValue pairs from Given Shared Preferences.
152 | *
153 | * @return : Map of KeyValue pair from Given SharedPreferences
154 | */
155 | public Maptrue
if debug menu item is clicked, it will automatically Start the SharedPrefsBrowser activity. If debug menu item is
180 | * not clicked, you can handle rest menu items in onOptionsItemSelcted code.
181 | */
182 | public boolean isDebugHandled(Context context, MenuItem item) {
183 | int id = item.getItemId();
184 | if (id == R.id.action_debug) {
185 | startActivity(context);
186 | return true;
187 | }
188 | return false;
189 | }
190 |
191 | /**
192 | * Start the SharedPrefsBrowser activity.
193 | *
194 | * @param context
195 | * : : context to start activity.
196 | */
197 | public static void startActivity(Context context) {
198 | context.startActivity(new Intent(context, SharedPrefsBrowser.class));
199 | }
200 |
201 | /**
202 | * Check if value exists for key.
203 | *
204 | * @param key
205 | * : Key for which we need to check the value exists or not.
206 | *
207 | * @return : true
if the value exists and it's other than default data.
208 | */
209 | public boolean isValueExistForKey(String key) {
210 | boolean isValueExists;
211 | try {
212 | String string = getString(key, "");
213 | isValueExists = !string.equalsIgnoreCase("");
214 | } catch (ClassCastException e) {
215 | try {
216 | int anInt = getInt(key, 0);
217 | isValueExists = anInt != 0;
218 | } catch (ClassCastException e1) {
219 | try {
220 | long aLong = getLong(key, 0);
221 | isValueExists = aLong != 0;
222 | } catch (ClassCastException e2) {
223 | try {
224 | float aFloat = getFloat(key, 0f);
225 | isValueExists = aFloat != 0;
226 | } catch (ClassCastException e3) {
227 | try {
228 | boolean aBoolean = getBoolean(key, false);
229 | isValueExists = !aBoolean;
230 | } catch (Exception e4) {
231 | isValueExists = false;
232 | e.printStackTrace();
233 | }
234 | }
235 |
236 | }
237 |
238 | }
239 | } catch (Exception e) {
240 | isValueExists = false;
241 | }
242 | return isValueExists;
243 | }
244 |
245 | /**
246 | * Retrieve a String value from the preferences.
247 | *
248 | * @param key
249 | * The name of the preference to retrieve.
250 | * @param defaultValue
251 | * value to return when a key does not exists.
252 | *
253 | * @return the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a String.
254 | *
255 | * @Throws ClassCastException : When a value exists in given key but it's not a string
256 | * @see android.content.SharedPreferences#getString(String, String)
257 | */
258 | public String getString(String key, String defaultValue) throws ClassCastException {
259 |
260 | return sharedPreferences.getString(key, (defaultValue == null) ? "" : defaultValue);
261 | }
262 |
263 | /**
264 | * Retrieve an int value from the preferences.
265 | *
266 | * @param key
267 | * The name of the preference to retrieve.
268 | * @param defaultValue
269 | * value to return when a key does not exists.
270 | *
271 | * @return the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not an int.
272 | *
273 | * @Throws ClassCastException : When a value exists in given key but it's not a integer
274 | * @see android.content.SharedPreferences#getInt(String, int)
275 | */
276 | public int getInt(String key, int defaultValue) throws ClassCastException {
277 |
278 | return sharedPreferences.getInt(key, defaultValue);
279 | }
280 |
281 |
282 | /**
283 | * Retrieve a long value from the preferences.
284 | *
285 | * @param key
286 | * The name of the preference to retrieve.
287 | * @param defaultValue
288 | * value to return when a key does not exists.
289 | *
290 | * @return the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a long.
291 | *
292 | * @Throws ClassCastException : When a value exists in given key but it's not a long
293 | * @see android.content.SharedPreferences#getLong(String, long)
294 | */
295 | public long getLong(String key, long defaultValue) throws ClassCastException {
296 |
297 | return sharedPreferences.getLong(key, defaultValue);
298 | }
299 |
300 | /**
301 | * Retrieve a float value from the preferences.
302 | *
303 | * @param key
304 | * The name of the preference to retrieve.
305 | * @param defaultValue
306 | * value to return when a key does not exists.
307 | *
308 | * @return the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a long.
309 | *
310 | * @Throws ClassCastException : When a value exists in given key but it's not a float
311 | * @see android.content.SharedPreferences#getFloat(String, float)
312 | */
313 | public float getFloat(String key, float defaultValue) throws ClassCastException {
314 |
315 | return sharedPreferences.getFloat(key, defaultValue);
316 | }
317 |
318 | /**
319 | * Retrieve a boolean value from the preferences.
320 | *
321 | * @param key
322 | * The name of the preference to retrieve.
323 | * @param defaultValue
324 | * value to return when a key does not exists.
325 | *
326 | * @return the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a
327 | * boolean.
328 | *
329 | * @Throws ClassCastException : When a value exists in given key but it's not a boolean
330 | * @see android.content.SharedPreferences#getBoolean(String, boolean)
331 | */
332 | public boolean getBoolean(String key, boolean defaultValue) throws ClassCastException {
333 |
334 | return sharedPreferences.getBoolean(key, defaultValue);
335 | }
336 |
337 | /**
338 | * Restore the original value for the key after it has been changed in test mode.
339 | *
340 | * @param key
341 | * : Key whose value is to be restored
342 | */
343 | public void restoreKey(String key) {
344 | if (!key.equalsIgnoreCase("test_mode_opened")) {
345 | String originalKey = key.substring(keyTestMode.length());
346 | Object value = get(key);
347 | put(originalKey, value);
348 | clear(key);
349 | }
350 | }
351 |
352 | /**
353 | * Get the value of the key.
354 | *
355 | * @param key
356 | * The name of the preference to retrieve.
357 | *
358 | * @return value of the preferences
359 | */
360 | public Object get(String key) {
361 | try {
362 | return getString(key, null);
363 | } catch (ClassCastException e) {
364 | try {
365 | return getInt(key, 0);
366 | } catch (ClassCastException e1) {
367 | try {
368 | return getLong(key, 0);
369 | } catch (ClassCastException e2) {
370 | try {
371 | return getFloat(key, 0f);
372 | } catch (ClassCastException e3) {
373 | try {
374 | return getBoolean(key, false);
375 | } catch (Exception e4) {
376 | e.printStackTrace();
377 | }
378 | }
379 |
380 | }
381 |
382 | }
383 | }
384 | return null;
385 | }
386 |
387 | /**
388 | * Put the value in given shared preference
389 | *
390 | * @param key
391 | * the name of the preference to save
392 | * @param value
393 | * the value to save
394 | */
395 | public void put(String key, Object value) {
396 | if (value.getClass().equals(String.class)) {
397 | putString(key, value.toString());
398 | } else if (value.getClass().equals(Integer.class)) {
399 | putInt(key, (Integer) value);
400 | } else if (value.getClass().equals(Float.class)) {
401 | putFloat(key, (Float) value);
402 | } else if (value.getClass().equals(Long.class)) {
403 | putLong(key, (Long) value);
404 | } else if (value.getClass().equals(Boolean.class)) {
405 | putBoolean(key, (Boolean) value);
406 | } else {
407 | putString(key, value.toString());
408 | }
409 | }
410 |
411 | /**
412 | * remove the preference
413 | *
414 | * @param key
415 | * the name of the preference to remove
416 | */
417 | public void clear(String key) {
418 | sharedPreferences.edit().remove(key).commit();
419 | }
420 |
421 | /**
422 | * put the string value to shared preference
423 | *
424 | * @param key
425 | * the name of the preference to save
426 | * @param value
427 | * the name of the preference to modify.
428 | *
429 | * @see android.content.SharedPreferences#edit()#putString(String, String)
430 | */
431 | public void putString(String key, String value) {
432 | sharedPreferences.edit().putString(key, value).commit();
433 | }
434 |
435 | /**
436 | * put the int value to shared preference
437 | *
438 | * @param key
439 | * the name of the preference to save
440 | * @param value
441 | * the name of the preference to modify.
442 | *
443 | * @see android.content.SharedPreferences#edit()#putInt(String, int)
444 | */
445 | public void putInt(String key, int value) {
446 | sharedPreferences.edit().putInt(key, value).commit();
447 | }
448 |
449 |
450 | /**
451 | * put the float value to shared preference
452 | *
453 | * @param key
454 | * the name of the preference to save
455 | * @param value
456 | * the name of the preference to modify.
457 | *
458 | * @see android.content.SharedPreferences#edit()#putFloat(String, float)
459 | */
460 | public void putFloat(String key, float value) {
461 | sharedPreferences.edit().putFloat(key, value).commit();
462 | }
463 |
464 |
465 | /**
466 | * put the int value to shared preference
467 | *
468 | * @param key
469 | * the name of the preference to save
470 | * @param value
471 | * the name of the preference to modify.
472 | *
473 | * @see android.content.SharedPreferences#edit()#putLong(String, long)
474 | */
475 | public void putLong(String key, long value) {
476 | sharedPreferences.edit().putLong(key, value).commit();
477 | }
478 |
479 |
480 | /**
481 | * put the int value to shared preference
482 | *
483 | * @param key
484 | * the name of the preference to save
485 | * @param value
486 | * the name of the preference to modify.
487 | *
488 | * @see android.content.SharedPreferences#edit()#putBoolean(String, boolean)
489 | */
490 | public void putBoolean(String key, boolean value) {
491 | sharedPreferences.edit().putBoolean(key, value).commit();
492 | }
493 |
494 | }
495 |
--------------------------------------------------------------------------------
/sharedpreferenceinspector/src/main/java/com/ceelites/sharedpreferenceinspector/fragments/SharedPreferencesItem.java:
--------------------------------------------------------------------------------
1 | package com.ceelites.sharedpreferenceinspector.fragments;
2 |
3 |
4 | import android.content.DialogInterface;
5 | import android.content.DialogInterface.OnClickListener;
6 | import android.os.Bundle;
7 | import android.support.annotation.NonNull;
8 | import android.support.v4.app.ListFragment;
9 | import android.support.v4.util.Pair;
10 | import android.support.v4.view.MenuItemCompat;
11 | import android.support.v7.app.AlertDialog;
12 | import android.support.v7.app.AlertDialog.Builder;
13 | import android.support.v7.widget.SearchView;
14 | import android.support.v7.widget.SearchView.OnQueryTextListener;
15 | import android.support.v7.widget.SwitchCompat;
16 | import android.text.Editable;
17 | import android.text.InputType;
18 | import android.view.LayoutInflater;
19 | import android.view.Menu;
20 | import android.view.MenuInflater;
21 | import android.view.MenuItem;
22 | import android.view.View;
23 | import android.widget.AdapterView;
24 | import android.widget.AdapterView.OnItemClickListener;
25 | import android.widget.AdapterView.OnItemSelectedListener;
26 | import android.widget.Button;
27 | import android.widget.CompoundButton;
28 | import android.widget.CompoundButton.OnCheckedChangeListener;
29 | import android.widget.EditText;
30 | import android.widget.ListView;
31 | import android.widget.Spinner;
32 | import android.widget.Toast;
33 | import com.ceelites.devutils.ConstantMethods;
34 | import com.ceelites.sharedpreferenceinspector.PrefsAdapter;
35 | import com.ceelites.sharedpreferenceinspector.R;
36 | import com.ceelites.sharedpreferenceinspector.Utils.SharedPreferenceUtils;
37 | import java.util.ArrayList;
38 | import java.util.Map;
39 | import java.util.Set;
40 |
41 | /**
42 | * Lists all the shared preferences stored in a given shared preference file. Created by Prasham on 25-01-2015.
43 | */
44 | public class SharedPreferencesItem
45 | extends ListFragment
46 | implements OnItemClickListener {
47 |
48 | private final String testModeOpened = "test_mode_opened";
49 | private SharedPreferenceUtils preferenceUtils;
50 | private PrefsAdapter prefsAdapter;
51 | private int typePosition = 0;
52 | private MenuItem testMode;
53 |
54 | Toast toast;
55 |
56 | /**
57 | * Instantiates new fragment.
58 | *
59 | * @param bundle
60 | * : Any extra data to be passed to fragment, pass null if you don't want to pass anything.
61 | *
62 | * @return : Instance of current fragment.
63 | */
64 | public static SharedPreferencesItem newInstance(@NonNull Bundle bundle) {
65 | SharedPreferencesItem sharedPreferencesItem = new SharedPreferencesItem();
66 | sharedPreferencesItem.setArguments(bundle);
67 | return sharedPreferencesItem;
68 | }
69 |
70 | @Override
71 | public void onCreate(Bundle savedInstanceState) {
72 | super.onCreate(savedInstanceState);
73 | setHasOptionsMenu(true);
74 | }
75 |
76 | @Override
77 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
78 | super.onCreateOptionsMenu(menu, inflater);
79 | inflater.inflate(R.menu.menu_shared_preference_browser, menu);
80 | testMode = menu.findItem(R.id.action_testmode);
81 | }
82 |
83 | @Override
84 | public void onPrepareOptionsMenu(Menu menu) {
85 | super.onPrepareOptionsMenu(menu);
86 | if (!preferenceUtils.getBoolean(testModeOpened, false)) {
87 | testMode.setTitle(R.string.action_test_mode_enter);
88 | } else {
89 | testMode.setTitle(R.string.action_test_mode_exit);
90 |
91 | }
92 | }
93 |
94 | @Override
95 | public boolean onOptionsItemSelected(MenuItem item) {
96 | int id = item.getItemId();
97 |
98 | if (id == R.id.action_testmode) {
99 | getActivity().supportInvalidateOptionsMenu();
100 | changeTestMode();
101 |
102 | }
103 | if (id == R.id.action_search) {
104 | SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
105 | if (searchView != null) {
106 | searchView.setQueryHint(getString(R.string.searchHint));
107 | searchView.setOnQueryTextListener(new OnQueryTextListener() {
108 | @Override
109 | public boolean onQueryTextSubmit(String s) {
110 | search(s);
111 | return true;
112 | }
113 |
114 | @Override
115 | public boolean onQueryTextChange(String s) {
116 | search(s);
117 | return true;
118 | }
119 | });
120 | }
121 | }
122 |
123 | return super.onOptionsItemSelected(item);
124 | }
125 |
126 | /**
127 | * Toggle test modes. If test mode is already open, it will restore the original data before toggling.
128 | */
129 | private void changeTestMode() {
130 | boolean testMode = preferenceUtils.getBoolean(testModeOpened, false);
131 | if (testMode) {
132 | restoreData();
133 | }
134 | preferenceUtils.putBoolean(testModeOpened, !testMode);
135 | }
136 |
137 | private void search(String s) {
138 | prefsAdapter.getFilter().filter(s);
139 | }
140 |
141 | /**
142 | * Restore values of original keys stored.
143 | */
144 | private void restoreData() {
145 | Map