下面列出了android.content.Context#createDeviceProtectedStorageContext ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public AlarmStorage(Context context) {
Context storageContext;
if (BuildCompat.isAtLeastN()) {
// All N devices have split storage areas, but we may need to
// move the existing preferences to the new device protected
// storage area, which is where the data lives from now on.
final Context deviceContext = context.createDeviceProtectedStorageContext();
if (!deviceContext.moveSharedPreferencesFrom(context,
ALARM_PREFERENCES_NAME)) {
Log.w(TAG, "Failed to migrate shared preferences.");
}
storageContext = deviceContext;
} else {
storageContext = context;
}
mSharedPreferences = storageContext
.getSharedPreferences(ALARM_PREFERENCES_NAME, Context.MODE_PRIVATE);
}
@Override
public boolean onInit(@NonNull Context context, @NonNull Shell shell) {
Context de = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
? context.createDeviceProtectedStorageContext() : context;
File lib = new File(de.getApplicationInfo().nativeLibraryDir, "libbusybox.so");
File bbPath = new File(de.getFilesDir().getParentFile(), "busybox");
File bb = new File(bbPath, "busybox");
bbPath.mkdir();
shell.newJob().add(
"rm -f " + bbPath + "/*",
"ln -sf " + lib + " " + bb,
bb + " --install -s " + bbPath,
"export PATH=" + bbPath + ":$PATH"
).exec();
return true;
}
public AlarmStorage(Context context) {
Context storageContext;
if (BuildCompat.isAtLeastN()) {
// All N devices have split storage areas, but we may need to
// move the existing preferences to the new device protected
// storage area, which is where the data lives from now on.
final Context deviceContext = context.createDeviceProtectedStorageContext();
if (!deviceContext.moveSharedPreferencesFrom(context,
ALARM_PREFERENCES_NAME)) {
Log.w(TAG, "Failed to migrate shared preferences.");
}
storageContext = deviceContext;
} else {
storageContext = context;
}
mSharedPreferences = storageContext
.getSharedPreferences(ALARM_PREFERENCES_NAME, Context.MODE_PRIVATE);
}
BackupScheme(Context context) {
mFullBackupContent = context.getApplicationInfo().fullBackupContent;
mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
mPackageManager = context.getPackageManager();
mPackageName = context.getPackageName();
// System apps have control over where their default storage context
// is pointed, so we're always explicit when building paths.
final Context ceContext = context.createCredentialProtectedStorageContext();
FILES_DIR = ceContext.getFilesDir();
DATABASE_DIR = ceContext.getDatabasePath("foo").getParentFile();
ROOT_DIR = ceContext.getDataDir();
SHAREDPREF_DIR = ceContext.getSharedPreferencesPath("foo").getParentFile();
CACHE_DIR = ceContext.getCacheDir();
NOBACKUP_DIR = ceContext.getNoBackupFilesDir();
final Context deContext = context.createDeviceProtectedStorageContext();
DEVICE_FILES_DIR = deContext.getFilesDir();
DEVICE_DATABASE_DIR = deContext.getDatabasePath("foo").getParentFile();
DEVICE_ROOT_DIR = deContext.getDataDir();
DEVICE_SHAREDPREF_DIR = deContext.getSharedPreferencesPath("foo").getParentFile();
DEVICE_CACHE_DIR = deContext.getCacheDir();
DEVICE_NOBACKUP_DIR = deContext.getNoBackupFilesDir();
if (android.os.Process.myUid() != Process.SYSTEM_UID) {
EXTERNAL_DIR = context.getExternalFilesDir(null);
} else {
EXTERNAL_DIR = null;
}
}
/**
* Creates a storage context that is protected by device-specific credentials.
*
* <p>This method only has an effect on API levels 24 and above.
*/
Context getDeviceProtectedStorageContext(Context context) {
if (VERSION.SDK_INT >= VERSION_CODES.N) {
return context.createDeviceProtectedStorageContext();
}
return context;
}
public static byte[] loadPasswordResetTokenFromPreference(Context context) {
Context directBootContext = context.createDeviceProtectedStorageContext();
SharedPreferences settings = directBootContext.getSharedPreferences(PREFS_NAME, 0);
String tokenString = settings.getString(TOKEN_NAME, null);
if (tokenString != null) {
return Base64.getDecoder().decode(tokenString.getBytes(StandardCharsets.UTF_8));
} else {
return null;
}
}
public static SharedPreferences getProtecredSharedPreferences(Context context) {
SharedPreferences preferences;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Context protectedContext = context.createDeviceProtectedStorageContext();
preferences = protectedContext.getSharedPreferences(PropUtil.PREFERENCE_NAME, Context.MODE_PRIVATE);
}
else {
preferences = context.getSharedPreferences(PropUtil.PREFERENCE_NAME, Context.MODE_PRIVATE);
}
return preferences;
}
@RequiresApi(api = Build.VERSION_CODES.N)
public static Context getProtectedContext(Context context) {
return context.isDeviceProtectedStorage() ? context
: context.createDeviceProtectedStorageContext();
}
public SharedPrefDriver(Context context) {
this.context = context;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
DPStorageContext = context.createDeviceProtectedStorageContext();
}
}
private static Context getDeviceProtectedStorageContext(Context context) {
if (VERSION.SDK_INT >= VERSION_CODES.N) {
return context.createDeviceProtectedStorageContext();
}
return context;
}