From ed0b5724a67a3bbea92d401f74982cfe362abc0a Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Mon, 6 Nov 2023 12:57:17 -0500 Subject: [PATCH 1/2] Fix issue with migration from v4 to v5 when obfuscation was used by the app --- .../preferences/PreferenceStoreFix.kt | 49 +++++++++++++++++++ .../com/onesignal/internal/OneSignalImp.kt | 4 ++ 2 files changed, 53 insertions(+) create mode 100644 OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/preferences/PreferenceStoreFix.kt diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/preferences/PreferenceStoreFix.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/preferences/PreferenceStoreFix.kt new file mode 100644 index 0000000000..16cd4ea174 --- /dev/null +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/preferences/PreferenceStoreFix.kt @@ -0,0 +1,49 @@ +package com.onesignal.core.internal.preferences + +import android.content.Context +import android.os.Build +import com.onesignal.debug.LogLevel +import com.onesignal.debug.internal.logging.Logging +import java.io.File + +object PreferenceStoreFix { + + /** + * Ensure the OneSignal preference store is not using the v4 obfuscated version, if one + * exists. + */ + fun ensureNoObfuscatedPrefStore(context: Context) { + try { + // In the v4 version the OneSignal shared preference name was based on the OneSignal + // class name, which might be minimized/obfuscated if the app is using ProGuard or + // similar. In order for a device to successfully migrate from v4 to v5 picking + // up the subscription, we need to copy the shared preferences from the obfuscated + // version to the static "OneSignal" preference name. We only do this + // if there isn't already a "OneSignal" preference store. + val sharedPrefsDir = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + File(context.dataDir, "shared_prefs") + } else { + File(context.filesDir.parentFile, "shared_prefs") + } + val osPrefsFile = File(sharedPrefsDir, "OneSignal.xml") + + if (!sharedPrefsDir.exists() || !sharedPrefsDir.isDirectory || osPrefsFile.exists()) + return + + val prefsFileList = sharedPrefsDir.listFiles() ?: return + + // Go through every preference file, looking for the OneSignal preference store. + for (prefsFile in prefsFileList) { + val prefsStore = + context.getSharedPreferences(prefsFile.nameWithoutExtension, Context.MODE_PRIVATE) + + if (prefsStore.contains(PreferenceOneSignalKeys.PREFS_LEGACY_PLAYER_ID)) { + prefsFile.renameTo(osPrefsFile) + return + } + } + } catch (e: Throwable) { + Logging.log(LogLevel.ERROR, "error attempting to fix obfuscated preference store", e) + } + } +} diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt index 0f5c437240..94e2356ff1 100644 --- a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt @@ -1,6 +1,7 @@ package com.onesignal.internal import android.content.Context +import android.os.Build import com.onesignal.IOneSignal import com.onesignal.common.IDManager import com.onesignal.common.OneSignalUtils @@ -19,6 +20,7 @@ import com.onesignal.core.internal.config.ConfigModelStore import com.onesignal.core.internal.operations.IOperationRepo import com.onesignal.core.internal.preferences.IPreferencesService import com.onesignal.core.internal.preferences.PreferenceOneSignalKeys +import com.onesignal.core.internal.preferences.PreferenceStoreFix import com.onesignal.core.internal.preferences.PreferenceStores import com.onesignal.core.internal.startup.StartupService import com.onesignal.debug.IDebugManager @@ -174,6 +176,8 @@ internal class OneSignalImp : IOneSignal, IServiceProvider { return true } + PreferenceStoreFix.ensureNoObfuscatedPrefStore(context) + // start the application service. This is called explicitly first because we want // to make sure it has the context provided on input, for all other startable services // to depend on if needed. From d2bfa751874de8dcc69abe81982eaa738be4b1ba Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Tue, 7 Nov 2023 14:59:23 -0500 Subject: [PATCH 2/2] Fix linting errors with PreferenceStoreFix and OneSignalImp --- .../internal/preferences/PreferenceStoreFix.kt | 16 +++++++++------- .../java/com/onesignal/internal/OneSignalImp.kt | 1 - 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/preferences/PreferenceStoreFix.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/preferences/PreferenceStoreFix.kt index 16cd4ea174..773d1e4651 100644 --- a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/preferences/PreferenceStoreFix.kt +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/preferences/PreferenceStoreFix.kt @@ -7,7 +7,6 @@ import com.onesignal.debug.internal.logging.Logging import java.io.File object PreferenceStoreFix { - /** * Ensure the OneSignal preference store is not using the v4 obfuscated version, if one * exists. @@ -20,15 +19,18 @@ object PreferenceStoreFix { // up the subscription, we need to copy the shared preferences from the obfuscated // version to the static "OneSignal" preference name. We only do this // if there isn't already a "OneSignal" preference store. - val sharedPrefsDir = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - File(context.dataDir, "shared_prefs") - } else { - File(context.filesDir.parentFile, "shared_prefs") - } + val sharedPrefsDir = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + File(context.dataDir, "shared_prefs") + } else { + File(context.filesDir.parentFile, "shared_prefs") + } + val osPrefsFile = File(sharedPrefsDir, "OneSignal.xml") - if (!sharedPrefsDir.exists() || !sharedPrefsDir.isDirectory || osPrefsFile.exists()) + if (!sharedPrefsDir.exists() || !sharedPrefsDir.isDirectory || osPrefsFile.exists()) { return + } val prefsFileList = sharedPrefsDir.listFiles() ?: return diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt index 94e2356ff1..f559818b76 100644 --- a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt @@ -1,7 +1,6 @@ package com.onesignal.internal import android.content.Context -import android.os.Build import com.onesignal.IOneSignal import com.onesignal.common.IDManager import com.onesignal.common.OneSignalUtils