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

fix: crash when trying to read data from sensor #2615

Merged
merged 3 commits into from
Jan 22, 2025

Conversation

marcnause
Copy link
Contributor

@marcnause marcnause commented Jan 19, 2025

Fixes #2608

Changes

  • make sure that UI changes happen on UI thread
  • add method to check if sensor data was read again (prevents crash on orientation change)

Screenshots / Recordings

N/A

Checklist:

  • No hard coding: I have used resources from strings.xml, dimens.xml and colors.xml without hard coding any value.
  • No end of file edits: No modifications done at end of resource files strings.xml, dimens.xml or colors.xml.
  • Code reformatting: I have reformatted code and fixed indentation in every file included in this pull request.
  • No extra space: My code does not contain any extra lines or extra spaces than the ones that are necessary.

Summary by Sourcery

Bug Fixes:

  • Fix crash on orientation change when reading sensor data.

@marcnause marcnause added Bug Unexpected problem or unintended behavior in app Instrument: Sensors labels Jan 19, 2025
Copy link

sourcery-ai bot commented Jan 19, 2025

Reviewer's Guide by Sourcery

This pull request addresses a crash that occurs when trying to read data from a sensor. The fix ensures that UI updates happen on the UI thread and adds a check to prevent crashes during orientation changes by verifying if sensor data was successfully acquired.

Sequence diagram for sensor data reading and UI update flow

sequenceDiagram
    participant S as Sensor
    participant SF as SensorDataFetch
    participant UI as UI Thread

    SF->>S: getSensorData()
    alt Success
        S-->>SF: return data
        SF->>SF: isSensorDataAcquired = true
        SF->>UI: runOnUiThread(updateUi)
        UI->>UI: check isSensorDataAcquired
        alt Data Acquired
            UI->>UI: update sensor values
            UI->>UI: update charts
        end
    else Failure
        S-->>SF: return error
        SF->>SF: isSensorDataAcquired = false
    end
Loading

Class diagram showing AbstractSensorActivity and SensorDataFetch changes

classDiagram
    class AbstractSensorActivity {
        #class SensorDataFetch
    }

    class SensorDataFetch {
        -boolean isSensorDataAcquired
        #boolean isSensorDataAcquired()
        #float getTimeElapsed()
        #void execute()
        #abstract boolean getSensorData()
        #abstract void updateUi()
    }

    AbstractSensorActivity *-- SensorDataFetch

    note for SensorDataFetch "Added isSensorDataAcquired flag
and made methods protected"
Loading

File-Level Changes

Change Details Files
Modified sensor data fetching to update UI on the UI thread.
  • The updateUi method is now executed on the UI thread using runOnUiThread.
app/src/main/java/io/pslab/sensors/AbstractSensorActivity.java
Added a check to ensure sensor data was acquired before updating the UI.
  • Added a boolean flag isSensorDataAcquired to track if sensor data was successfully read.
  • The updateUi method now checks isSensorDataAcquired before updating the UI elements.
app/src/main/java/io/pslab/sensors/SensorMPU6050.java
app/src/main/java/io/pslab/sensors/SensorMPU925X.java
app/src/main/java/io/pslab/sensors/SensorAPDS9960.java
app/src/main/java/io/pslab/sensors/SensorBMP180.java
app/src/main/java/io/pslab/sensors/SensorHMC5883L.java
app/src/main/java/io/pslab/sensors/SensorTSL2561.java
app/src/main/java/io/pslab/sensors/SensorCCS811.java
app/src/main/java/io/pslab/sensors/SensorMLX90614.java
app/src/main/java/io/pslab/sensors/SensorSHT21.java
app/src/main/java/io/pslab/sensors/SensorADS1115.java
app/src/main/java/io/pslab/sensors/SensorVL53L0X.java
Changed the access modifier of getSensorData and updateUi methods to protected.
  • The getSensorData method is now protected.
  • The updateUi method is now protected.
app/src/main/java/io/pslab/sensors/SensorMPU6050.java
app/src/main/java/io/pslab/sensors/SensorMPU925X.java
app/src/main/java/io/pslab/sensors/SensorAPDS9960.java
app/src/main/java/io/pslab/sensors/SensorBMP180.java
app/src/main/java/io/pslab/sensors/SensorHMC5883L.java
app/src/main/java/io/pslab/sensors/SensorTSL2561.java
app/src/main/java/io/pslab/sensors/SensorCCS811.java
app/src/main/java/io/pslab/sensors/SensorMLX90614.java
app/src/main/java/io/pslab/sensors/SensorSHT21.java
app/src/main/java/io/pslab/sensors/SensorADS1115.java
app/src/main/java/io/pslab/sensors/SensorVL53L0X.java

Assessment against linked issues

Issue Objective Addressed Explanation
#2608 Prevent app from crashing when reading data from APDS9960 sensor
#2608 Ensure UI updates happen on the main/UI thread
#2608 Handle potential sensor data reading failures gracefully

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @marcnause - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

Copy link
Collaborator

@AsCress AsCress left a comment

Choose a reason for hiding this comment

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

@marcnause LGTM! We're good to go with this one.

@AsCress AsCress merged commit 87625b2 into fossasia:development Jan 22, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Unexpected problem or unintended behavior in app Instrument: Sensors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

APDS9960: App crashes when trying to read data from sensor
2 participants