Skip to content

Conversation

@avazirna
Copy link
Contributor

@avazirna avazirna commented Mar 5, 2025

Product Description

This PR addresses an issue with the GPS widget when the device is in Airplane Mode. Upon pressing the Record Location button, the screen flashes and the user returns to the previous screen.

location_capture_issue.mp4

There is an exception being thrown and here's the stack trace:

com.google.android.gms.common.api.ApiException: 8502: SETTINGS_CHANGE_UNAVAILABLE
13:43:05.638  W  	at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(com.google.android.gms:play-services-base@@18.4.0:3)
13:43:05.638  W  	at com.google.android.gms.common.internal.zap.onComplete(com.google.android.gms:play-services-base@@18.4.0:4)
13:43:05.638  W  	at com.google.android.gms.common.api.internal.BasePendingResult.zab(com.google.android.gms:play-services-base@@18.4.0:7)
13:43:05.638  W  	at com.google.android.gms.common.api.internal.BasePendingResult.setResult(com.google.android.gms:play-services-base@@18.4.0:6)
13:43:05.638  W  	at com.google.android.gms.common.api.internal.BaseImplementation$ApiMethodImpl.setResult(com.google.android.gms:play-services-base@@18.4.0:1)
13:43:05.638  W  	at com.google.android.gms.internal.location.zzbc.zza(Unknown Source:2)
13:43:05.638  W  	at com.google.android.gms.internal.location.zzar.dispatchTransaction(Unknown Source:11)
13:43:05.638  W  	at com.google.android.gms.internal.location.zzb.onTransact(Unknown Source:22)
13:43:05.638  W  	at android.os.Binder.execTransactInternal(Binder.java:1380)
13:43:05.638  W  	at android.os.Binder.execTransact(Binder.java:1311)

According to the documentation, this error occurs when Location settings can't be changed to meet the requirements.

Ticket: https://dimagi.atlassian.net/browse/SAAS-15924

Technical Summary

The approach here was to change the CommCareLocationControllerFactory to use LocationManager when the device is in Airplane mode instead of FuseLocationProvider.

Safety Assurance

Safety story

This was tested locally and it was successful.

Labels and Review

  • Do we need to enhance the manual QA test coverage ? If yes, the "QA Note" label is set correctly
  • Does the PR introduce any major changes worth communicating ? If yes, the "Release Note" label is set and a "Release Note" is specified in PR description.
  • Risk label is set correctly
  • The set of people pinged as reviewers is appropriate for the level of risk of the change

@coderabbitai
Copy link

coderabbitai bot commented Mar 5, 2025

📝 Walkthrough

Walkthrough

This update introduces three primary changes. First, it adds a new localization string (location.capture.cancelled) to the android translatable strings file to support a cancelled location capture message. Second, in the form entry activity, a new conditional branch has been added to handle the cancellation of a location capture request. When the location capture operation is cancelled (indicated by the result code RESULT_CANCELED), a toast message is displayed to inform the user. Third, the location controller factory now performs an additional check for airplane mode before determining which location controller to return. It uses a newly introduced method, isAirplaneModeOn, to query the device settings. If airplane mode is active, the factory returns a specific location controller regardless of the availability of Google Play services.

Sequence Diagram(s)

sequenceDiagram
    participant Activity as FormEntryActivity
    participant System as Android System
    participant UI as User Interface

    System->>Activity: onActivityResult(requestCode, resultCode)
    Activity->>Activity: Check if requestCode == LOCATION_CAPTURE
    Activity->>Activity: Check if resultCode == RESULT_CANCELED
    Activity->>UI: Display toast "Location capture cancelled"
Loading
sequenceDiagram
    participant Caller as Application
    participant Factory as CommCareLocationControllerFactory
    participant Settings as Device Settings
    participant Service as Google Play Services

    Caller->>Factory: getLocationController(context)
    Factory->>Settings: isAirplaneModeOn(context)
    Settings-->>Factory: Return airplane mode status
    alt Airplane mode is ON
      Factory->>Caller: Return CommCareProviderLocationController
    else Airplane mode is OFF
      Factory->>Service: Check for Google Play Services availability
      Service-->>Factory: Return availability status
      Factory->>Caller: Return appropriate LocationController based on status
    end
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c97a152 and 635aa5b.

📒 Files selected for processing (3)
  • app/assets/locales/android_translatable_strings.txt (1 hunks)
  • app/src/org/commcare/activities/FormEntryActivity.java (1 hunks)
  • app/src/org/commcare/location/CommCareLocationControllerFactory.kt (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Lint Code Base
🔇 Additional comments (5)
app/assets/locales/android_translatable_strings.txt (1)

996-996: Well-structured localization string for user feedback.

The addition of this localization string provides appropriate user feedback when a location capture action is cancelled. This enhances the user experience by providing clear information about what happened.

app/src/org/commcare/location/CommCareLocationControllerFactory.kt (3)

4-4: Appropriate import for system settings access.

Adding the Settings import allows proper checking of the device's airplane mode state.


28-30: Well-implemented Airplane mode detection.

This method correctly checks the system setting for Airplane mode using the standard Android API. The method is concise and follows best practices.


18-18:

Details

✅ Verification successful

Good approach for handling location services in Airplane mode.

Adding the check for airplane mode in the condition ensures that the app falls back to CommCareProviderLocationController when the device is in airplane mode, which addresses the API exception that was occurring.

Let's verify this change properly handles the issue by checking if there are any remaining API references in CommCareProviderLocationController that might cause issues in Airplane mode:


🏁 Script executed:

#!/bin/bash
# Description: Check if CommCareProviderLocationController uses any Google Play Services APIs that might cause issues in Airplane mode

# Find the CommCareProviderLocationController file
fd CommCareProviderLocationController.kt

# Check for any Google Play Services imports in the controller
echo "Checking for Google Play Services imports in CommCareProviderLocationController:"
cat $(fd CommCareProviderLocationController.kt) | grep -E "com.google.android.gms"

# Check for potential API calls that might cause issues in Airplane mode
echo "Checking for potential API references that might be problematic in Airplane mode:"
cat $(fd CommCareProviderLocationController.kt) | grep -E "FusedLocationProvider|LocationServices"

Length of output: 774


Location Handling Verified and Approved:
The conditional check in CommCareLocationControllerFactory.kt is correctly updated to consider airplane mode, ensuring that the app falls back to CommCareProviderLocationController when needed. Verification confirms that the fallback controller does not import or call any Google Play Services APIs (e.g., FusedLocationProvider or LocationServices), which eliminates the risk of API exceptions in airplane mode.

  • File: app/src/org/commcare/location/CommCareLocationControllerFactory.kt (Line 18)

    • The condition isPlayServiceAvailable(context) && !isAirplaneModeOn(context) appropriately governs the selection of location controller.
  • File: app/src/org/commcare/location/CommCareProviderLocationController.kt

    • No references to Google Play Services APIs were found, confirming that it is safe for use when the device is in airplane mode.
app/src/org/commcare/activities/FormEntryActivity.java (1)

352-353: Good error handling for cancelled location capture.

Adding this condition improves the user experience by providing feedback when location capture is cancelled. This works in conjunction with the changes in CommCareLocationControllerFactory to properly handle the Airplane mode scenario where the location provider fails to initialize.

The toast message uses the newly added localization string, maintaining proper internationalization support.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@avazirna avazirna merged commit 934c13f into master Mar 7, 2025
4 of 9 checks passed
@avazirna avazirna deleted the fix-location-capture-when-in-airplane-mode branch March 7, 2025 12:16
@avazirna avazirna added this to the 2.57 milestone Apr 11, 2025
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.

3 participants