This callback will be run on your main thread.
38 | *
39 | * @param encryptedPreferences The {@link EncryptedPreferences} that received
40 | * the change.
41 | * @param key The key of the preference that was changed, added, or
42 | * removed.
43 | */
44 | void onSharedPreferenceChanged(EncryptedPreferences encryptedPreferences, String key);
45 | }
46 |
47 | /**
48 | * Retrieve an {@link EncryptedPreferences} instance with all default settings.
49 | * @deprecated Due to security reasons it's recommended to use {@link Builder} for instance creation instead.
50 | * @param context
51 | * @return default {@link EncryptedPreferences}
52 | */
53 | @Deprecated
54 | public static EncryptedPreferences getInstance(Context context) {
55 | if (encryptedPreferences == null) {
56 | encryptedPreferences = new EncryptedPreferences.Builder(context).build();
57 | }
58 | return encryptedPreferences;
59 | }
60 |
61 | /**
62 | * Retrieve the configured {@link EncryptedPreferences} instance.
63 | * Make sure to call {@link Builder#withSaveAsSingleton(boolean)} to initialize the singleton, otherwise a {@link RuntimeException} will be thrown.
64 | * @return The configured {@link EncryptedPreferences} instance.
65 | */
66 | public static EncryptedPreferences getSingletonInstance() {
67 | if (singletonInstance == null) {
68 | throw new RuntimeException("Singleton instance doesn't exist. Did you forget to set Builder.withSaveAsSingleton(true) ?");
69 | }
70 | return singletonInstance;
71 | }
72 |
73 | private final SharedPreferences sharedPreferences;
74 | private final String cryptoKey;
75 | private final EncryptedEditor encryptedEditor;
76 | private final Utils utils;
77 | private final boolean printDebugMessages;
78 | private final List Note that when two editors are modifying preferences at the same
654 | * time, the last one to call commit wins.
655 | *
656 | * If you don't care about the return value and you're
657 | * using this from your application's main thread, consider
658 | * using {@link #apply} instead.
659 | *
660 | * @return Returns true if the new values were successfully written
661 | * to persistent storage.
662 | */
663 | public boolean commit() {
664 | return editor().commit();
665 | }
666 |
667 | }
668 |
669 | /**
670 | * Class for configuring a new {@link EncryptedPreferences} instance.
671 | */
672 | public static final class Builder {
673 |
674 | private final Context context;
675 | private String encryptionPassword;
676 | private String prefsName;
677 | private boolean singleton = false;
678 | private final List