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: state lost on orientation change in sensor activities #2576

Conversation

marcnause
Copy link
Contributor

@marcnause marcnause commented Nov 11, 2024

Fixes #2528

Changes

  • refactor: move duplicate code to base class
  • refactor: move duplicates layout components (sensor control dock) to new layout file and replace it with in existing layout files
  • feat: save and restore state of sensor dock, data arrays, Spinners, TextViews and EditTexts on orientation change
  • refactor: improve thread handling

Screenshots / Recordings

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

Refactor sensor activities to extend a new AbstractSensorActivity base class, addressing state loss on orientation change and reducing code duplication. Introduce a new feature to save and restore the state of the sensor dock on orientation change. Enhance layout management by moving duplicate components to a new layout file and using tags.

New Features:

  • Save and restore the state of the sensor dock and data arrays on orientation change.

Bug Fixes:

  • Fix state loss on orientation change in sensor activities.

Enhancements:

  • Refactor sensor activity classes to extend a new AbstractSensorActivity base class, reducing code duplication and improving maintainability.
  • Move duplicate layout components to a new layout file and replace them with in existing layout files.
  • Improve thread handling in sensor activities.

Copy link

sourcery-ai bot commented Nov 11, 2024

Reviewer's Guide by Sourcery

The changes refactor sensor activities to extend a new AbstractSensorActivity base class and improve state handling during orientation changes. The implementation moves common sensor control dock functionality to a shared layout and base class, while adding proper state saving/restoration.

Class diagram for refactored sensor activities

classDiagram
    class AbstractSensorActivity {
        <<abstract>>
        +SensorDataFetch sensorDataFetch
        +getScienceLab() I2C
        +onSaveInstanceState(Bundle)
        +getSensorDataFetch() : SensorDataFetch
        +getLayoutResId() : int
        +getTitleResId() : int
    }
    class SensorMPU925X {
        +SensorDataFetch sensorDataFetch
        +MPU925x sensorMPU925X
        +ArrayList<Entry> entriesAx
        +ArrayList<Entry> entriesAy
        +ArrayList<Entry> entriesAz
        +ArrayList<Entry> entriesGx
        +ArrayList<Entry> entriesGy
        +ArrayList<Entry> entriesGz
        +LineChart mChartAcceleration
        +LineChart mChartGyroscope
        +TextView tvSensorMPU925Xax
        +TextView tvSensorMPU925Xay
        +TextView tvSensorMPU925Xaz
        +TextView tvSensorMPU925Xgx
        +TextView tvSensorMPU925Xgy
        +TextView tvSensorMPU925Xgz
        +TextView tvSensorMPU925Xtemp
        +Spinner spinnerSensorMPU925X1
        +Spinner spinnerSensorMPU925X2
        +Spinner spinnerSensorMPU925X3
        +Spinner spinnerSensorMPU925X4
    }
    class SensorMPU6050 {
        +SensorDataFetch sensorDataFetch
        +MPU6050 sensorMPU6050
        +ArrayList<Entry> entriesAx
        +ArrayList<Entry> entriesAy
        +ArrayList<Entry> entriesAz
        +ArrayList<Entry> entriesGx
        +ArrayList<Entry> entriesGy
        +ArrayList<Entry> entriesGz
        +LineChart mChartAcceleration
        +LineChart mChartGyroscope
        +TextView tvSensorMPU6050ax
        +TextView tvSensorMPU6050ay
        +TextView tvSensorMPU6050az
        +TextView tvSensorMPU6050gx
        +TextView tvSensorMPU6050gy
        +TextView tvSensorMPU6050gz
        +TextView tvSensorMPU6050temp
        +Spinner spinnerSensorMPU60501
        +Spinner spinnerSensorMPU60502
        +Spinner spinnerSensorMPU60503
        +Spinner spinnerSensorMPU60504
    }
    AbstractSensorActivity <|-- SensorMPU925X
    AbstractSensorActivity <|-- SensorMPU6050
    note for AbstractSensorActivity "Base class for sensor activities with state management"
    note for SensorMPU925X "Refactored to extend AbstractSensorActivity"
    note for SensorMPU6050 "Refactored to extend AbstractSensorActivity"
Loading

File-Level Changes

Change Details Files
Created a new AbstractSensorActivity base class to handle common sensor functionality
  • Implemented common sensor control dock UI handling
  • Added thread handling with HandlerThread for sensor data collection
  • Added state saving/restoration for sensor parameters
  • Implemented common lifecycle methods (onCreate, onDestroy, etc.)
  • Created abstract methods for sensor-specific implementations
app/src/main/java/io/pslab/sensors/AbstractSensorActivity.java
Refactored sensor activities to extend AbstractSensorActivity
  • Removed duplicate code by inheriting from base class
  • Implemented sensor-specific data collection and UI updates
  • Added state saving/restoration for sensor-specific data
  • Improved error handling with proper logging
app/src/main/java/io/pslab/sensors/SensorMPU925X.java
app/src/main/java/io/pslab/sensors/SensorMPU6050.java
app/src/main/java/io/pslab/sensors/SensorAPDS9960.java
app/src/main/java/io/pslab/sensors/SensorTSL2561.java
app/src/main/java/io/pslab/sensors/SensorBMP180.java
app/src/main/java/io/pslab/sensors/SensorMLX90614.java
app/src/main/java/io/pslab/sensors/SensorHMC5883L.java
app/src/main/java/io/pslab/sensors/SensorCCS811.java
app/src/main/java/io/pslab/sensors/SensorSHT21.java
app/src/main/java/io/pslab/sensors/SensorVL53L0X.java
app/src/main/java/io/pslab/sensors/SensorADS1115.java
Moved sensor control dock layout to a separate reusable file
  • Created new sensor_control_dock.xml layout file
  • Replaced duplicate layout code with tag in sensor layouts
  • Updated layout references in sensor activities
app/src/main/res/layout/sensor_control_dock.xml
app/src/main/res/layout/sensor_tsl2561.xml
app/src/main/res/layout/sensor_ads1115.xml
app/src/main/res/layout/sensor_apds9960.xml
app/src/main/res/layout/sensor_bmp180.xml
app/src/main/res/layout/sensor_ccs811.xml
app/src/main/res/layout/sensor_hmc5883l.xml
app/src/main/res/layout/sensor_mlx90614.xml
app/src/main/res/layout/sensor_mpu6050.xml
app/src/main/res/layout/sensor_mpu925x.xml
app/src/main/res/layout/sensor_sht21.xml
app/src/main/res/layout/sensor_vl53l0x.xml

Assessment against linked issues

Issue Objective Addressed Explanation
#2528 Maintain screen state during orientation change in sensor activities

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

@marcnause marcnause added Enhancement Improvement to an existing feature Instrument: Sensors java Pull requests that update Java code labels Nov 11, 2024
Copy link

github-actions bot commented Nov 11, 2024

@marcnause marcnause added the In Progress Developer is working on the problem label Nov 11, 2024
sourcery-ai[bot]

This comment was marked as outdated.

@marcnause
Copy link
Contributor Author

@sourcery-ai review

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: 1 issue found
  • 🟢 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.

@marcnause marcnause force-pushed the 2528-sensor-activities-are-not-designed-for-orientation-change branch from c8639ec to 89f41f3 Compare November 19, 2024 23:04
@marcnause marcnause marked this pull request as ready for review December 1, 2024 15:47
@marcnause marcnause changed the title WIP: fix: state lost on orientation change in sensor activities fix: state lost on orientation change in sensor activities Dec 1, 2024
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 found some issues that need to be addressed.

Blocking issues:

  • Incorrect keys used for restoring Y and Z entries from saved state (link)

Overall Comments:

  • Consider using SavedStateHandle from ViewModel instead of manual state restoration for a more modern approach to handling configuration changes
Here's what I looked at during the review
  • 🔴 General issues: 1 blocking issue, 2 other issues
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 2 issues found
  • 🟢 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.

@marcnause marcnause removed the In Progress Developer is working on the problem label Dec 1, 2024
@marcnause
Copy link
Contributor Author

Sorry for this huge PR. One thing led to another.

@AsCress
Copy link
Contributor

AsCress commented Dec 10, 2024

@marcnause This is fantastic! Sorry for the late review. I tested with the following three sensors: APDS9960, BMP180 and VL53L0X. Following were my observations:-

  1. The VL53L0X performs well with the new changes.
  2. The BMP180 sensor isn't working, gives vague readings for some strange reason. I then tested it by building the development branch, wherein it still gives the same error. This indicates that it hasn't been broken by this PR, but something else. This is quite strange as I had fixed it during my GSoC project (feat: updated I2C API and fixed BMP180 #2527) (or maybe my sensor has broken). Nonetheless, it would be nice if you could test this sensor @marcnause.
  3. The APDS9960 performs well, however, doesn't preserve state on screen rotation.
  4. With all of the sensors, if I change orientation while the sensor is fetching readings, the communication desyncs and the sensors stop working.

I only had these three available with me so I could test with only three of them.
@marcnause It would be great if we could once test this with the maximum amount of sensors that are available to us and then merge this.
Do inform me if I can be of any more help :))
Edit :- Apparently in the case of BMP180, my sensor seems to be broken 'cause it's giving weird values even with the python library.

@marcnause marcnause force-pushed the 2528-sensor-activities-are-not-designed-for-orientation-change branch from 4711495 to 4cce908 Compare December 10, 2024 20:46
@marcnause
Copy link
Contributor Author

marcnause commented Dec 10, 2024

@AsCress, I tested with the following sensors tonight: VL53L0X, BMP180, APDS9960, CCS811

I found one bug and fixed it (df39058).

The CCS811 and the APDS9960 need some time to output valid values after initialization. The sensors are initialized every time the orientation is changed. As a consequence the first few values are 0 and they overwrite the values which were stored and re-created on orientation change.

The gap you noticed is also a consequence of the destruction and new setup of the thread which gets the data from the sensor.

Both problems could be solved by moving the fetching of the data to a service which would keep running while the Activities are destroyed and re-created during orientation change. I think we should create an extra ticket for that.

edit: Extra ticket created: #2583

@marcnause marcnause force-pushed the 2528-sensor-activities-are-not-designed-for-orientation-change branch from 0c00b9b to e1e84df Compare December 22, 2024 21:52
@marcnause marcnause enabled auto-merge (rebase) December 22, 2024 21:57
@marcnause marcnause disabled auto-merge December 22, 2024 21:57
@marcnause marcnause enabled auto-merge (squash) December 22, 2024 21:58
@marcnause marcnause disabled auto-merge December 22, 2024 21:58
@marcnause marcnause force-pushed the 2528-sensor-activities-are-not-designed-for-orientation-change branch from e1e84df to bd66db6 Compare December 22, 2024 22:12
@marcnause marcnause enabled auto-merge (squash) December 22, 2024 22:12
@marcnause marcnause disabled auto-merge December 22, 2024 22:12
@marcnause marcnause enabled auto-merge (rebase) December 22, 2024 22:13
Copy link
Contributor

@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 I built the app from your branch today. It all goes as we discussed yesterday. I tested with four sensors: the ADS1115, APDS9960, VL53L0X, BMP180, all of them work fine now.
The only issue I noticed was a small misalignment of text in the sensor activity for the VL53L0X. Attaching screenshots for your reference -

Screenshot_20241223_134723
Screenshot_20241223_134745

Rest, we are good to go.
How do you think we should go about this now ? Shall we wait and test with all the sensors ? Or should we just merge this ?

@marcnause
Copy link
Contributor Author

The only issue I noticed was a small misalignment of text in the sensor activity for the VL53L0X. Attaching screenshots for your reference -

Good catch! Fixed in 786e6f5.

Rest, we are good to go. How do you think we should go about this now ? Shall we wait and test with all the sensors ? Or should we just merge this ?

Since we both don't have all the sensors yet, I'd propose to merge it now and test all sensors later. I have created an issue to test all supported sensors: #2594

@marcnause marcnause disabled auto-merge December 23, 2024 22:24
@marcnause marcnause enabled auto-merge (squash) December 23, 2024 22:24
@marcnause marcnause force-pushed the 2528-sensor-activities-are-not-designed-for-orientation-change branch from 786e6f5 to d63bccf Compare December 23, 2024 22:52
Copy link
Contributor

@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 Perfect then! We are good to go with this one.

@marcnause marcnause merged commit 5414051 into fossasia:development Dec 25, 2024
3 checks passed
@marcnause marcnause deleted the 2528-sensor-activities-are-not-designed-for-orientation-change branch December 30, 2024 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Improvement to an existing feature Instrument: Sensors java Pull requests that update Java code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sensor Activities are not designed for orientation change
3 participants