Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -175,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.
Expand Down