Skip to content

Commit e62629e

Browse files
committed
fix: end initialization early if device storage is locked (#2520)
1 parent 300b4c1 commit e62629e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/AndroidUtils.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.net.Uri
1010
import android.os.Build
1111
import android.os.Bundle
1212
import android.os.Looper
13+
import android.os.UserManager
1314
import android.text.TextUtils
1415
import androidx.annotation.Keep
1516
import androidx.core.app.NotificationManagerCompat
@@ -41,6 +42,27 @@ object AndroidUtils {
4142
return hasToken && insetsAttached
4243
}
4344

45+
/**
46+
* Retrieve whether the device user is accessible.
47+
*
48+
* On Android 7.0+ (API 24+), encrypted user data is inaccessible until the user unlocks
49+
* the device for the first time after boot. This includes:
50+
* * getSharedPreferences()
51+
* * Any file-based storage in the default credential-encrypted context
52+
*
53+
* Apps that auto-run on boot or background services triggered early may hit this issue.
54+
*/
55+
fun isAndroidUserUnlocked(appContext: Context): Boolean {
56+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
57+
// Prior to API 24, the device booted into an unlocked state by default
58+
return true
59+
}
60+
61+
val userManager = appContext.getSystemService(Context.USER_SERVICE) as? UserManager
62+
// assume user is unlocked if the Android UserManager is null
63+
return userManager?.isUserUnlocked ?: true
64+
}
65+
4466
fun hasConfigChangeFlag(
4567
activity: Activity,
4668
configChangeFlag: Int,

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
192192
return true
193193
}
194194

195+
// Check whether current Android user is accessible.
196+
// Return early if it is inaccessible, as we are unable to complete initialization without access
197+
// to device storage like SharedPreferences.
198+
if (!AndroidUtils.isAndroidUserUnlocked(context)) {
199+
Logging.warn("initWithContext called when device storage is locked, no user data is accessible!")
200+
return false
201+
}
202+
195203
Logging.log(LogLevel.DEBUG, "initWithContext: SDK initializing")
196204

197205
PreferenceStoreFix.ensureNoObfuscatedPrefStore(context)

0 commit comments

Comments
 (0)