diff --git a/packages/core/application-settings/index.android.ts b/packages/core/application-settings/index.android.ts index b5a14a1551..fa5f1fd872 100644 --- a/packages/core/application-settings/index.android.ts +++ b/packages/core/application-settings/index.android.ts @@ -1,10 +1,28 @@ import * as common from './application-settings-common'; import { getNativeApplication } from '../application'; +import { Trace } from '../trace'; +import { Device } from '../platform'; +import lazy from '../utils/lazy'; +const sdkVersion = lazy(() => parseInt(Device.sdkVersion)); +const DB_KEY = 'prefs.db'; let sharedPreferences: android.content.SharedPreferences; function ensureSharedPreferences() { + let context = getNativeApplication().getApplicationContext(); if (!sharedPreferences) { - sharedPreferences = (getNativeApplication()).getApplicationContext().getSharedPreferences('prefs.db', 0); + if (sdkVersion() >= 24) { + const deviceContext = context.createDeviceProtectedStorageContext(); + if (deviceContext && !deviceContext.moveSharedPreferencesFrom(context, DB_KEY)) { + const warnMessage = 'Failed to migrate Application Settings to Device Protected Storage'; + if (Trace.isEnabled()) { + Trace.write(warnMessage, Trace.categories.Debug, Trace.messageType.warn); + } else { + console.log(warnMessage); + } + } + context = deviceContext; + } + sharedPreferences = context.getSharedPreferences(DB_KEY, android.content.Context.MODE_PRIVATE); } }