Skip to content

Commit dcc3860

Browse files
authored
Merge pull request #1901 from OneSignal/fix-v4-to-v5-preference-store-migration
Fix issue with migration from v4 to v5 when obfuscation is used by the app
2 parents ff841f9 + aa9ce40 commit dcc3860

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.onesignal.core.internal.config.ConfigModelStore
1919
import com.onesignal.core.internal.operations.IOperationRepo
2020
import com.onesignal.core.internal.preferences.IPreferencesService
2121
import com.onesignal.core.internal.preferences.PreferenceOneSignalKeys
22+
import com.onesignal.core.internal.preferences.PreferenceStoreFix
2223
import com.onesignal.core.internal.preferences.PreferenceStores
2324
import com.onesignal.core.internal.startup.StartupService
2425
import com.onesignal.debug.IDebugManager
@@ -175,6 +176,8 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
175176
return true
176177
}
177178

179+
PreferenceStoreFix.ensureNoObfuscatedPrefStore(context)
180+
178181
// start the application service. This is called explicitly first because we want
179182
// to make sure it has the context provided on input, for all other startable services
180183
// to depend on if needed.

0 commit comments

Comments
 (0)