Skip to content

Commit 2883b97

Browse files
committed
Fix issue with migration from v4 to v5 when obfuscation was used by the app
1 parent ff841f9 commit 2883b97

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
/**
12+
* Ensure the OneSignal preference store is not using the v4 obfuscated version, if one
13+
* exists.
14+
*/
15+
fun ensureNoObfuscatedPrefStore(context: Context) {
16+
try {
17+
// In the v4 version the OneSignal shared preference name was based on the OneSignal
18+
// class name, which might be minimized/obfuscated if the app is using ProGuard or
19+
// similar. In order for a device to successfully migrate from v4 to v5 picking
20+
// up the subscription, we need to copy the shared preferences from the obfuscated
21+
// version to the static "OneSignal" preference name. We only do this
22+
// if there isn't already a "OneSignal" preference store.
23+
val sharedPrefsDir = 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+
val osPrefsFile = File(sharedPrefsDir, "OneSignal.xml")
29+
30+
if (!sharedPrefsDir.exists() || !sharedPrefsDir.isDirectory || osPrefsFile.exists())
31+
return
32+
33+
val prefsFileList = sharedPrefsDir.listFiles() ?: return
34+
35+
// Go through every preference file, looking for the OneSignal preference store.
36+
for (prefsFile in prefsFileList) {
37+
val prefsStore =
38+
context.getSharedPreferences(prefsFile.nameWithoutExtension, Context.MODE_PRIVATE)
39+
40+
if (prefsStore.contains(PreferenceOneSignalKeys.PREFS_LEGACY_PLAYER_ID)) {
41+
prefsFile.renameTo(osPrefsFile)
42+
return
43+
}
44+
}
45+
} catch (e: Throwable) {
46+
Logging.log(LogLevel.ERROR, "error attempting to fix obfuscated preference store", e)
47+
}
48+
}
49+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.onesignal.internal
22

33
import android.content.Context
4+
import android.os.Build
45
import com.onesignal.IOneSignal
56
import com.onesignal.common.IDManager
67
import com.onesignal.common.OneSignalUtils
@@ -19,6 +20,7 @@ import com.onesignal.core.internal.config.ConfigModelStore
1920
import com.onesignal.core.internal.operations.IOperationRepo
2021
import com.onesignal.core.internal.preferences.IPreferencesService
2122
import com.onesignal.core.internal.preferences.PreferenceOneSignalKeys
23+
import com.onesignal.core.internal.preferences.PreferenceStoreFix
2224
import com.onesignal.core.internal.preferences.PreferenceStores
2325
import com.onesignal.core.internal.startup.StartupService
2426
import com.onesignal.debug.IDebugManager
@@ -175,6 +177,8 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
175177
return true
176178
}
177179

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

0 commit comments

Comments
 (0)