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

Crash on bindService with broadcast receiver context #1935

Merged

Conversation

jinliu9508
Copy link
Contributor

@jinliu9508 jinliu9508 commented Dec 7, 2023

Description

One Line Summary

Change broadcast receivers that calls initWithContext with the context from onReceive.

Details

The context carried by BroadcastReceiver.onReceive on certain older Android devices may be ReceiverRestrictedContext, disallowed from invoking bindService. However, this context type no longer exists in newer Android API levels, in which the method now carries the application context. Therefore, it explained why the observed crash affects some, but not all, devices. Accessing context.applicationContext retrieves the actual application context that registered the receiver and is permitted to call bindService.

Motivation

Fixing a critical bug that causes the client app to crash.

Scope

Customers that were not affected will continue remain unaffected because context.applicationContext is the same as context in devices with newer Android versions

Testing

Unit testing

Unable to reproduce the issue, since ReceiverRestrictedContext is no longer accessible

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
    • If it is hard to explain how any codes changes are related to each other then it most likely needs to be more than one PR
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
    • Simplify with less code, followed by splitting up code into well named functions and variables, followed by adding comments to the code.
  • I have reviewed this PR myself, ensuring it meets each checklist item
    • WIP (Work In Progress) is ok, but explain what is still in progress and what you would like feedback on. Start the PR title with "WIP" to indicate this.

This change is Reviewable

Copy link
Member

@jkasten2 jkasten2 left a comment

Choose a reason for hiding this comment

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

Changes themselves look good!, but there are some commits in this PR that just add and then later remove code that we should clean up before merging.

@jinliu9508 jinliu9508 force-pushed the crash-bindService-with-Broadcast-Context branch from bf255f1 to 9e3c8b8 Compare December 7, 2023 18:25
Copy link
Contributor Author

@jinliu9508 jinliu9508 left a comment

Choose a reason for hiding this comment

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

Ok I just cleaned them up and place them into one commit

Reviewable status: 0 of 4 files reviewed, all discussions resolved (waiting on @brismithers and @emawby)

Copy link
Member

@jkasten2 jkasten2 left a comment

Choose a reason for hiding this comment

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

Forgot to bring this up before, but could you add a comment to TrackGooglePurchases of why applicationContext is used? This way if this code is refactored in the future they will know applicationContext is something that can't be removed.

@jinliu9508 jinliu9508 force-pushed the crash-bindService-with-Broadcast-Context branch from 9e3c8b8 to 8e6fb1f Compare December 7, 2023 22:42
@jinliu9508
Copy link
Contributor Author

Forgot to bring this up before, but could you add a comment to TrackGooglePurchases of why applicationContext is used? This way if this code is refactored in the future they will know applicationContext is something that can't be removed.

@jkasten2 Good point, I have added the comment and explain the usage of applicationContext

Copy link
Member

@jkasten2 jkasten2 left a comment

Choose a reason for hiding this comment

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

Thanks for all the fixups, looks good now 👍

Copy link
Contributor

@brismithers brismithers left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 4 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @emawby)

@jinliu9508 jinliu9508 merged commit 49fbe04 into user-model/main Dec 13, 2023
3 checks passed
@jinliu9508 jinliu9508 deleted the crash-bindService-with-Broadcast-Context branch December 13, 2023 16:40
@jennantilla jennantilla mentioned this pull request Dec 28, 2023
jinliu9508 added a commit that referenced this pull request Jan 31, 2024
…ast-Context

Crash on bindService with broadcast receiver context
jinliu9508 added a commit that referenced this pull request Jan 31, 2024
…ast-Context

Crash on bindService with broadcast receiver context
jinliu9508 added a commit that referenced this pull request Feb 6, 2024
…ast-Context

Crash on bindService with broadcast receiver context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants