cache device protected prefs instance

This commit is contained in:
Helium314 2023-12-30 13:38:59 +01:00
parent 98fb94de8d
commit b8a6443a8f

View file

@ -17,18 +17,22 @@ import androidx.annotation.RequiresApi;
public final class DeviceProtectedUtils {
static final String TAG = DeviceProtectedUtils.class.getSimpleName();
private static SharedPreferences prefs;
public static SharedPreferences getSharedPreferences(final Context context) {
if (prefs != null)
return prefs;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
return PreferenceManager.getDefaultSharedPreferences(context);
prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs;
}
Context deviceProtectedContext = getDeviceProtectedContext(context);
SharedPreferences deviceProtectedPreferences = PreferenceManager.getDefaultSharedPreferences(deviceProtectedContext);
if (deviceProtectedPreferences.getAll().isEmpty()) {
prefs = PreferenceManager.getDefaultSharedPreferences(deviceProtectedContext);
if (prefs.getAll().isEmpty()) {
Log.i(TAG, "Device encrypted storage is empty, copying values from credential encrypted storage");
deviceProtectedContext.moveSharedPreferencesFrom(context, PreferenceManager.getDefaultSharedPreferencesName(context));
}
return deviceProtectedPreferences;
return prefs;
}
@RequiresApi(api = Build.VERSION_CODES.N)