@@ -39,15 +39,16 @@ private SecurePreferences() {
39
39
public static void setValue (@ NonNull Context context ,
40
40
@ NonNull String key ,
41
41
@ NonNull String value ) throws SecureStorageException {
42
+ Context applicationContext = context .getApplicationContext ();
42
43
if (!KeystoreTool .keyPairExists ()) {
43
- KeystoreTool .generateKeyPair (context );
44
+ KeystoreTool .generateKeyPair (applicationContext );
44
45
}
45
46
46
- String transformedValue = KeystoreTool .encryptMessage (context , value );
47
+ String transformedValue = KeystoreTool .encryptMessage (applicationContext , value );
47
48
if (TextUtils .isEmpty (transformedValue )) {
48
49
throw new SecureStorageException (context .getString (R .string .message_problem_encryption ), null , CRYPTO_EXCEPTION );
49
50
} else {
50
- setSecureValue (context , key , transformedValue );
51
+ setSecureValue (applicationContext , key , transformedValue );
51
52
}
52
53
}
53
54
@@ -90,10 +91,11 @@ public static void setValue(@NonNull Context context,
90
91
public static String getStringValue (@ NonNull Context context ,
91
92
@ NonNull String key ,
92
93
@ Nullable String defValue ) {
93
- String result = getSecureValue (context , key );
94
+ Context applicationContext = context .getApplicationContext ();
95
+ String result = getSecureValue (applicationContext , key );
94
96
try {
95
97
if (!TextUtils .isEmpty (result )) {
96
- return KeystoreTool .decryptMessage (context , result );
98
+ return KeystoreTool .decryptMessage (applicationContext , result );
97
99
} else {
98
100
return defValue ;
99
101
}
@@ -146,33 +148,38 @@ public static Set<String> getStringSetValue(@NonNull Context context,
146
148
147
149
public static boolean contains (@ NonNull Context context ,
148
150
@ NonNull String key ) {
149
- SharedPreferences preferences = context
151
+ Context applicationContext = context .getApplicationContext ();
152
+ SharedPreferences preferences = applicationContext
150
153
.getSharedPreferences (KEY_SHARED_PREFERENCES_NAME , MODE_PRIVATE );
151
154
return preferences .contains (key );
152
155
}
153
156
154
157
public static void removeValue (@ NonNull Context context ,
155
158
@ NonNull String key ) {
156
- removeSecureValue (context , key );
159
+ Context applicationContext = context .getApplicationContext ();
160
+ removeSecureValue (applicationContext , key );
157
161
}
158
162
159
163
public static void clearAllValues (@ NonNull Context context ) throws SecureStorageException {
164
+ Context applicationContext = context .getApplicationContext ();
160
165
if (KeystoreTool .keyPairExists ()) {
161
- KeystoreTool .deleteKeyPair (context );
166
+ KeystoreTool .deleteKeyPair (applicationContext );
162
167
}
163
- clearAllSecureValues (context );
168
+ clearAllSecureValues (applicationContext );
164
169
}
165
170
166
171
public static void registerOnSharedPreferenceChangeListener (@ NonNull Context context ,
167
172
@ NonNull SharedPreferences .OnSharedPreferenceChangeListener listener ) {
168
- SharedPreferences preferences = context
173
+ Context applicationContext = context .getApplicationContext ();
174
+ SharedPreferences preferences = applicationContext
169
175
.getSharedPreferences (KEY_SHARED_PREFERENCES_NAME , MODE_PRIVATE );
170
176
preferences .registerOnSharedPreferenceChangeListener (listener );
171
177
}
172
178
173
179
public static void unregisterOnSharedPreferenceChangeListener (@ NonNull Context context ,
174
180
@ NonNull SharedPreferences .OnSharedPreferenceChangeListener listener ) {
175
- SharedPreferences preferences = context
181
+ Context applicationContext = context .getApplicationContext ();
182
+ SharedPreferences preferences = applicationContext
176
183
.getSharedPreferences (KEY_SHARED_PREFERENCES_NAME , MODE_PRIVATE );
177
184
preferences .unregisterOnSharedPreferenceChangeListener (listener );
178
185
}
0 commit comments