Java Code Examples for android.content.Context#createDeviceProtectedStorageContext()

The following examples show how to use android.content.Context#createDeviceProtectedStorageContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AlarmStorage.java    From security-samples with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: BusyBoxInstaller.java    From libsu with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 3
Source File: AlarmStorage.java    From android-DirectBoot with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: FullBackup.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 5
Source File: Utils.java    From capillary with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: ResetPasswordWithTokenFragment.java    From android-testdpc with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 7
Source File: PropUtil.java    From LocationReportEnabler with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: ContextUtils.java    From XMiTools with GNU General Public License v3.0 4 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.N)
public static Context getProtectedContext(Context context) {
    return context.isDeviceProtectedStorage() ? context
            : context.createDeviceProtectedStorageContext();
}
 
Example 9
Source File: SharedPrefDriver.java    From pandora with Apache License 2.0 4 votes vote down vote up
public SharedPrefDriver(Context context) {
    this.context = context;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        DPStorageContext = context.createDeviceProtectedStorageContext();
    }
}
 
Example 10
Source File: Utils.java    From capillary with Apache License 2.0 4 votes vote down vote up
private static Context getDeviceProtectedStorageContext(Context context) {
  if (VERSION.SDK_INT >= VERSION_CODES.N) {
    return context.createDeviceProtectedStorageContext();
  }
  return context;
}