|
| 1 | +package com.onesignal.core.internal.preferences |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.os.Build |
| 5 | +import com.onesignal.debug.LogLevel |
| 6 | +import com.onesignal.debug.internal.logging.Logging |
| 7 | +import java.io.File |
| 8 | + |
| 9 | +object PreferenceStoreFix { |
| 10 | + /** |
| 11 | + * Ensure the OneSignal preference store is not using the v4 obfuscated version, if one |
| 12 | + * exists. |
| 13 | + */ |
| 14 | + fun ensureNoObfuscatedPrefStore(context: Context) { |
| 15 | + try { |
| 16 | + // In the v4 version the OneSignal shared preference name was based on the OneSignal |
| 17 | + // class name, which might be minimized/obfuscated if the app is using ProGuard or |
| 18 | + // similar. In order for a device to successfully migrate from v4 to v5 picking |
| 19 | + // up the subscription, we need to copy the shared preferences from the obfuscated |
| 20 | + // version to the static "OneSignal" preference name. We only do this |
| 21 | + // if there isn't already a "OneSignal" preference store. |
| 22 | + val sharedPrefsDir = |
| 23 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 24 | + File(context.dataDir, "shared_prefs") |
| 25 | + } else { |
| 26 | + File(context.filesDir.parentFile, "shared_prefs") |
| 27 | + } |
| 28 | + |
| 29 | + val osPrefsFile = File(sharedPrefsDir, "OneSignal.xml") |
| 30 | + |
| 31 | + if (!sharedPrefsDir.exists() || !sharedPrefsDir.isDirectory || osPrefsFile.exists()) { |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + val prefsFileList = sharedPrefsDir.listFiles() ?: return |
| 36 | + |
| 37 | + // Go through every preference file, looking for the OneSignal preference store. |
| 38 | + for (prefsFile in prefsFileList) { |
| 39 | + val prefsStore = |
| 40 | + context.getSharedPreferences(prefsFile.nameWithoutExtension, Context.MODE_PRIVATE) |
| 41 | + |
| 42 | + if (prefsStore.contains(PreferenceOneSignalKeys.PREFS_LEGACY_PLAYER_ID)) { |
| 43 | + prefsFile.renameTo(osPrefsFile) |
| 44 | + return |
| 45 | + } |
| 46 | + } |
| 47 | + } catch (e: Throwable) { |
| 48 | + Logging.log(LogLevel.ERROR, "error attempting to fix obfuscated preference store", e) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments