File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed
OneSignalSDK/onesignal/core/src/main/java/com/onesignal Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import android.net.Uri
1010import android.os.Build
1111import android.os.Bundle
1212import android.os.Looper
13+ import android.os.UserManager
1314import android.text.TextUtils
1415import androidx.annotation.Keep
1516import 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 ,
Original file line number Diff line number Diff 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)
You can’t perform that action at this time.
0 commit comments