Skip to content

Commit

Permalink
Merge pull request #1901 from OneSignal/fix-v4-to-v5-preference-store…
Browse files Browse the repository at this point in the history
…-migration

Fix issue with migration from v4 to v5 when obfuscation is used by the app
  • Loading branch information
brismithers authored and jinliu9508 committed Feb 6, 2024
2 parents 473ca90 + d2bfa75 commit 79c2ac9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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
Expand Down Expand Up @@ -174,6 +175,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.
Expand Down

0 comments on commit 79c2ac9

Please sign in to comment.