-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...nesignal/core/src/main/java/com/onesignal/core/internal/preferences/PreferenceStoreFix.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters