Example usage for android.content Context moveSharedPreferencesFrom

List of usage examples for android.content Context moveSharedPreferencesFrom

Introduction

In this page you can find the example usage for android.content Context moveSharedPreferencesFrom.

Prototype

public abstract boolean moveSharedPreferencesFrom(Context sourceContext, String name);

Source Link

Document

Move an existing shared preferences file from the given source storage context to this context.

Usage

From source file:com.android.utils.SharedPreferencesUtils.java

/**
 * Move existing preferences file from credential protected storage to device protected storage.
 * This is used to migrate data between storage locations after an Android upgrade from
 * Build.VERSION < N to Build.VERSION >= N.
 *//* w w w.ja  va 2  s. c  om*/
public static void migrateSharedPreferences(Context context) {
    if (BuildCompat.isAtLeastN()) {
        Context deContext = ContextCompat.createDeviceProtectedStorageContext(context);
        deContext.moveSharedPreferencesFrom(context,
                PreferenceManager.getDefaultSharedPreferencesName(context));
    }
}

From source file:com.android.deskclock.Utils.java

/**
 * Return the default shared preferences.
 *///from w  ww  .ja  v  a  2s .  co m
public static SharedPreferences getDefaultSharedPreferences(Context context) {
    final Context storageContext;
    if (isNOrLater()) {
        // All N devices have split storage areas, but we may need to
        // migrate existing preferences into the new device protected
        // storage area, which is where our data lives from now on.
        final Context deviceContext = context.createDeviceProtectedStorageContext();
        if (!deviceContext.moveSharedPreferencesFrom(context,
                PreferenceManager.getDefaultSharedPreferencesName(context))) {
            LogUtils.wtf("Failed to migrate shared preferences");
        }
        storageContext = deviceContext;
    } else {
        storageContext = context;
    }

    return PreferenceManager.getDefaultSharedPreferences(storageContext);
}

From source file:com.stasbar.knowyourself.Utils.java

/**
 * Returns the default {@link SharedPreferences} instance from the underlying storage context.
 *///  w ww  .j  ava  2 s .  c o  m
@TargetApi(Build.VERSION_CODES.N)
public static SharedPreferences getDefaultSharedPreferences(Context context) {
    final Context storageContext;
    if (isNOrLater()) {
        // All N devices have split storage areas, but we may need to
        // migrate existing preferences into the new device encrypted
        // storage area, which is where our data lives from now on.
        storageContext = context.createDeviceProtectedStorageContext();
        if (!storageContext.moveSharedPreferencesFrom(context,
                PreferenceManager.getDefaultSharedPreferencesName(context))) {
            LogUtils.wtf("Failed to migrate shared preferences");
        }
    } else {
        storageContext = context;
    }

    return PreferenceManager.getDefaultSharedPreferences(storageContext);
}

From source file:com.example.android.directboot.alarms.AlarmStorage.java

public AlarmStorage(Context context) {
    Context storageContext;/*from w ww .  j  a v  a2s .co m*/
    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);
}