Back to project page unknown-pleasures.
The source code is released under:
Creative Commons Attribution NonCommercial NoDerivs (CC-NC-ND) THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTE...
If you think the Android project unknown-pleasures listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * Prefs.java/*from www .ja va2 s . c om*/ * Author: marek.brodziak@gmail.com * Created: Jun 11, 2014 * Copyright 2014 by miniti */ package pl.miniti.android.pleasures.preferences; import java.util.Map; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; /** * Simplifies usage of {@link SharedPreferences} and provides compatibility * between older version of the application */ public class Prefs { /** * Enumeration of all application preferences */ public static enum V { /** * Controls dimming of the animation edges */ DIM("dim", Boolean.class), /** * Makes the animation lines stronger */ STRONG_LINE("strong", Boolean.class), /** * Color palette for the animation */ PALETTE("palette", String.class), /** * Animation speed */ SPEED("speed", Integer.class), /** * Class responsible for rendering the animation */ ANIMATOR("animator", String.class), /** * Determines if full screen width is used or not */ FULL_WIDTH("full", Boolean.class), /** * Controls the background transparency */ TRANSPARENCY("transparency", Integer.class), /** * Number of lines in the animation */ QUANTITY("quantity", Integer.class); /** * Key of the preference */ public final String key; /** * Currently applicable type of the preference */ public final Class<?> clazz; /** * Private enum constructor * * @param key * key of the preference * @param clazz * currently applicable type of the preference */ private V(String key, Class<?> clazz) { this.key = key; this.clazz = clazz; } } /** * Wrapped preferences object */ private SharedPreferences prefs; /** * Factory method for constructing instances of this class * * @param sp * application preferences * @return wrapped preferences object */ public static final Prefs of(SharedPreferences sp) { return new Prefs(sp); } /** * Private constructor. Use factory method instead. * * @param sp * application preferences */ private Prefs(SharedPreferences sp) { this.prefs = sp; } /** * Does preferences validation by checking existing user settings against * types currently stored in the system * * @param prefs * application preferences */ public static void validate(SharedPreferences prefs) { Editor editor = prefs.edit(); Map<String, ?> all = prefs.getAll(); for (Prefs.V pref : V.values()) { if (all.containsKey(pref.key)) { Object obj = all.get(pref.key); if (!obj.getClass().isAssignableFrom(pref.clazz)) { editor.remove(pref.key); } } } editor.commit(); } /** * Fetch integer preference or default value in case of issues * * @param p * preference * @param def * default value * @return safely retrieved preference */ public int i(V p, int def) { try { return prefs.getInt(p.key, def); } catch (ClassCastException ignore) { } catch (NullPointerException ignore) { } return def; } /** * Fetch boolean preference or default value in case of issues * * @param p * preference * @param def * default value * @return safely retrieved preference */ public boolean b(V p, boolean def) { try { return prefs.getBoolean(p.key, def); } catch (ClassCastException ignore) { } catch (NullPointerException ignore) { } return def; } /** * Fetch String preference or default value in case of issues * * @param p * preference * @param def * default value * @return safely retrieved preference */ public String s(V p, String def) { try { return prefs.getString(p.key, def); } catch (ClassCastException ignore) { } catch (NullPointerException ignore) { } return def; } }