2021-05-04 10:57:20 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
2023-10-17 13:44:01 +02:00
|
|
|
* modified
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 AND GPL-3.0-only
|
2021-05-04 10:57:20 -04:00
|
|
|
*/
|
|
|
|
|
2024-01-31 18:32:43 +01:00
|
|
|
package helium314.keyboard.latin.utils;
|
2021-05-04 10:57:20 -04:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
2021-08-12 13:59:57 -04:00
|
|
|
import android.os.Build;
|
2021-05-04 12:56:35 -04:00
|
|
|
|
2024-01-28 18:05:38 +01:00
|
|
|
import androidx.preference.PreferenceManager;
|
2021-08-12 13:59:57 -04:00
|
|
|
|
2024-02-13 08:23:07 +01:00
|
|
|
import java.io.File;
|
|
|
|
|
2021-05-04 10:57:20 -04:00
|
|
|
public final class DeviceProtectedUtils {
|
|
|
|
|
2021-05-04 12:56:35 -04:00
|
|
|
static final String TAG = DeviceProtectedUtils.class.getSimpleName();
|
2023-12-30 13:38:59 +01:00
|
|
|
private static SharedPreferences prefs;
|
2021-05-04 10:57:20 -04:00
|
|
|
|
2021-05-04 11:25:34 -04:00
|
|
|
public static SharedPreferences getSharedPreferences(final Context context) {
|
2023-12-30 13:38:59 +01:00
|
|
|
if (prefs != null)
|
|
|
|
return prefs;
|
2021-08-12 13:59:57 -04:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
2023-12-30 13:38:59 +01:00
|
|
|
prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
return prefs;
|
2021-08-12 13:59:57 -04:00
|
|
|
}
|
2021-05-06 10:32:19 -04:00
|
|
|
Context deviceProtectedContext = getDeviceProtectedContext(context);
|
2025-01-25 22:07:56 +01:00
|
|
|
if (deviceProtectedContext == null) { // not relevant in practice, but happens when compose previews access shared preferences
|
|
|
|
prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
return prefs;
|
|
|
|
}
|
2023-12-30 13:38:59 +01:00
|
|
|
prefs = PreferenceManager.getDefaultSharedPreferences(deviceProtectedContext);
|
|
|
|
if (prefs.getAll().isEmpty()) {
|
2021-05-04 12:56:35 -04:00
|
|
|
Log.i(TAG, "Device encrypted storage is empty, copying values from credential encrypted storage");
|
2024-01-28 18:05:38 +01:00
|
|
|
deviceProtectedContext.moveSharedPreferencesFrom(context, android.preference.PreferenceManager.getDefaultSharedPreferencesName(context));
|
2021-05-04 12:56:35 -04:00
|
|
|
}
|
2023-12-30 13:38:59 +01:00
|
|
|
return prefs;
|
2021-05-04 11:25:34 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 08:23:07 +01:00
|
|
|
// keep this private to avoid accidental use of device protected context anywhere in the app
|
|
|
|
private static Context getDeviceProtectedContext(final Context context) {
|
2024-01-28 18:05:38 +01:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return context;
|
|
|
|
return context.isDeviceProtectedStorage() ? context : context.createDeviceProtectedStorageContext();
|
2021-05-04 10:57:20 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 08:23:07 +01:00
|
|
|
public static File getFilesDir(final Context context) {
|
|
|
|
return getDeviceProtectedContext(context).getFilesDir();
|
|
|
|
}
|
|
|
|
|
2021-05-04 10:57:20 -04:00
|
|
|
private DeviceProtectedUtils() {
|
|
|
|
// This utility class is not publicly instantiable.
|
|
|
|
}
|
|
|
|
}
|