Skip to content

Align BODY_SENSORS permission request with API 36 migration guidelines#344

Closed
ithinkihaveacat wants to merge 1 commit intoandroid:mainfrom
ithinkihaveacat:fix-api-36-permissions
Closed

Align BODY_SENSORS permission request with API 36 migration guidelines#344
ithinkihaveacat wants to merge 1 commit intoandroid:mainfrom
ithinkihaveacat:fix-api-36-permissions

Conversation

@ithinkihaveacat
Copy link
Copy Markdown
Contributor

Motivation

This PR updates the permission request logic to align with Wear OS 6 (API level 36) migration guidelines.

The AndroidManifest.xml correctly restricts the legacy BODY_SENSORS permission to maxSdkVersion="35". However, the runtime permission array in PreparingViewModel.kt was requesting BODY_SENSORS on all API levels.

While the Android system gracefully ignores invalid permission requests and still allows the exercise to proceed, requesting a permission that is explicitly restricted in the manifest is incorrect hygiene. It causes the ActivityResultLauncher to return a false result for that specific permission on API 36+, which could break future logic that strictly validates the success of the permission request array.

This PR updates PreparingViewModel.kt to only request Manifest.permission.BODY_SENSORS on API levels 35 and below, ensuring the runtime request matches the manifest declaration.

How to test

Verifying the fix

On API 36+ (Android 16+):

  1. Run the ExerciseSampleCompose app on an Android 16 (API 36) emulator or device.
  2. Tap the "Play" button to prepare an exercise.
  3. Observe that the app correctly requests modern permissions (like READ_HEART_RATE) but no longer attempts to request the legacy BODY_SENSORS permission.
  4. Grant all permissions and verify the exercise starts successfully.
    (Note: Ensure device-level Location is toggled ON in the system settings, otherwise the DataType.LOCATION request will fail).

On API <= 35 (Android 15 and below):

  1. Run the app on an Android 15 (API 35) or lower device/emulator.
  2. Tap the "Play" button.
  3. Verify that the legacy BODY_SENSORS permission is still requested as expected for backward compatibility.
  4. Grant all permissions and verify the exercise starts successfully.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the application's permission request mechanism to ensure compatibility with the latest Android API guidelines, specifically for Wear OS 6 (API 36). By adjusting when the BODY_SENSORS permission is requested at runtime, the change prevents potential issues with permission handling on newer API levels while maintaining backward compatibility for older devices.

Highlights

  • Permission Request Logic Update: Modified PreparingViewModel.kt to conditionally request Manifest.permission.BODY_SENSORS only for API levels 35 (VANILLA_ICE_CREAM) and below, aligning with Wear OS 6 (API 36) migration guidelines.
  • API 36 Compliance: Ensured that the runtime permission request for BODY_SENSORS matches the maxSdkVersion="35" restriction in AndroidManifest.xml, preventing incorrect ActivityResultLauncher results on API 36+.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • health-services/ExerciseSampleCompose/app/src/main/java/com/example/exercisesamplecompose/presentation/preparing/PreparingViewModel.kt
    • Updated the permission request array to conditionally include Manifest.permission.BODY_SENSORS based on the device's API level.
Activity
  • No specific activity (comments, reviews, progress) has been provided in the context.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly aligns the BODY_SENSORS permission request with Android's API 36 migration guidelines by conditionally requesting it only on API levels 35 and below. The change is correct and addresses the issue described. I have one suggestion to improve code readability and maintainability.

Comment on lines +87 to +89
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
add(Manifest.permission.BODY_SENSORS)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

For improved readability and to make the code more self-documenting, consider extracting Build.VERSION_CODES.VANILLA_ICE_CREAM into a named constant. This makes the reason for this specific API level check more explicit, as it relates to the maxSdkVersion for the BODY_SENSORS permission in your manifest.

For example, you could define a constant in the companion object and use it in the check:

private const val MAX_BODY_SENSORS_SDK = Build.VERSION_CODES.VANILLA_ICE_CREAM

// ... inside buildList
if (Build.VERSION.SDK_INT <= MAX_BODY_SENSORS_SDK) {
    add(Manifest.permission.BODY_SENSORS)
}

@M1n9yu23
Copy link
Copy Markdown
Contributor

Hello @ithinkihaveacat,

It looks like this PR covers the same ground as my earlier PR #342 from January.

I’d love to learn if there's anything missing or incorrect in my implementation compared to this one. Could you please review my PR?

@ithinkihaveacat
Copy link
Copy Markdown
Contributor Author

@M1n9yu23 Sorry for the delay. Yes agree this PR is mostly redundant; #342 is basically equivalent. I'll close this one.

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.

2 participants