Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Paywalls V2] Handles configuring while the device is locked #2077

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

JayShortway
Copy link
Member

Description

As the title says. The strategy is as follows:

  1. If the device is unlocked, there's no change in behavior.
  2. If the device is locked, we use the provided Context directly to get SharedPreferences, instead of the Application. This is done so developers can pass a specially-prepared Context using createDeviceProtectedStorageContext(). This is required by Android to get SharedPreferences while the device is locked.
  3. If we fail to get the SharedPreferences, we check if the device is locked. If so, we provide further instructions in the error.

Note: labeled this as a fix, let me know what you think!

@JayShortway JayShortway added the pr:fix A bug fix label Jan 23, 2025
@JayShortway JayShortway requested a review from a team January 23, 2025 12:19
@JayShortway JayShortway self-assigned this Jan 23, 2025
Comment on lines -59 to +63
private val mockApplication = mockk<Application>(relaxed = true).apply {
every { applicationContext } returns this
}
protected val mockContext = mockk<Context>(relaxed = true).apply {
every {
applicationContext
} returns mockApplication
protected val mockContext = InstrumentationRegistry.getInstrumentation().targetContext
private val mockApplication = (mockContext.applicationContext as Application).apply {
shadowOf(this).grantPermissions(Manifest.permission.INTERNET)
Copy link
Member Author

@JayShortway JayShortway Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required, because a mocked Context doesn't return anything for getSystemService(), which is now used to get the UserManager and check whether the device is locked.

Copy link

emerge-tools bot commented Jan 23, 2025

📸 Snapshot Test

179 unchanged

Name Added Removed Modified Renamed Unchanged Errored Approval
TestPurchasesUIAndroidCompatibility
com.revenuecat.testpurchasesuiandroidcompatibility
0 0 0 0 179 5 N/A

🛸 Powered by Emerge Tools

val prefs = PreferenceManager.getDefaultSharedPreferences(application)
val isUserUnlocked = UserManagerCompat.isUserUnlocked(context)
val prefs = try {
PreferenceManager.getDefaultSharedPreferences(if (isUserUnlocked) application else context)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have only one concern with this PR... That is, if the shared preferences we obtain with the given context is different than the one we obtain through the application, there is a chance of us losing the entire cached shared preferences right? This could result in using anonymous ids inadvertently (unless the developer always passes us the user id when configuring), losing entitlements until the cache can be refreshed, or losing subscriber attributes.

I'm just wondering if we should try to store this data in files, instead of shared preferences to avoid these possible issues...

Copy link
Member Author

@JayShortway JayShortway Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this back to draft, as this is indeed an issue. Good one! I'm not entirely sure if moving the data in a non-sharedprefs file is a solution, as I believe the data directory is different for a "regular" context vs a "device-protected-storage" context. So we'd still end up with 2 copies, depending on when the SDK is configured.

Thinking about this, I'm starting to think we should not try to be smart about it, and just pass the Context directly to getDefaultSharedPreferences(). That way app developers that need this can make sure to always give us a consistent one (e.g. always a "device-protected-storage" context).

@JayShortway JayShortway marked this pull request as draft January 23, 2025 12:30
Copy link

codecov bot commented Jan 23, 2025

Codecov Report

Attention: Patch coverage is 22.22222% with 7 lines in your changes missing coverage. Please review.

Project coverage is 81.18%. Comparing base (3faca9f) to head (efaaf1f).

Files with missing lines Patch % Lines
...otlin/com/revenuecat/purchases/PurchasesFactory.kt 22.22% 6 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2077      +/-   ##
==========================================
- Coverage   81.24%   81.18%   -0.07%     
==========================================
  Files         267      267              
  Lines        8867     8875       +8     
  Branches     1259     1261       +2     
==========================================
+ Hits         7204     7205       +1     
- Misses       1144     1150       +6     
- Partials      519      520       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:fix A bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants