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: Trigger active value not correct in some cases #48

Merged
merged 9 commits into from
Mar 6, 2024

Conversation

KCeh
Copy link
Collaborator

@KCeh KCeh commented Feb 13, 2024

📄 Context

Fix for the issue #46

📝 Changes

There were 2 issues:

In SensorTrigger start() trigger would always be set true.
IMO we should remember what active value was for the trigger (overridden by the user in settings or manifest). This is why I introduced activeHolder (name could be probably better).
Example of how to replicate a related bug is to turn off the trigger (in settings for example) and put app in background. Open the app from the background again. Then trigger is turned on.
I am not sure if this is an oversight or a feature 😅

Another issue is that the override value from the manifest wouldn't be loaded into memory properly on the first (clean with no cache) app start. When overrides for triggers are loaded we just invoke repository.save which saves DAO. But this doesn't update cache/in memory object (flow onEach for loaded entities or something like that is also not invoked). I just added a call to updateCache when DAO is saved. This resolves this issue and trigger's active value is properly reflected in memory.

🛠️ How to test

You can clone the repo and try out a sample app with manifest overrides for triggers.

⏱️ Next steps

More fixes for other issues 😄

@KCeh KCeh added the bug Something isn't working label Feb 13, 2024
@KCeh KCeh requested review from bojankoma and AsimRibo February 13, 2024 09:41
@KCeh KCeh self-assigned this Feb 13, 2024
@KCeh
Copy link
Collaborator Author

KCeh commented Feb 13, 2024

CI is broken
I will push lint fixes and open a new PR with Java version fix for Github action

@KCeh KCeh changed the base branch from master to develop February 13, 2024 10:04
@KCeh KCeh requested a review from simicmarin February 14, 2024 09:53
@KCeh KCeh changed the base branch from develop to master March 1, 2024 13:46
@KCeh KCeh changed the base branch from master to develop March 1, 2024 13:46
@AsimRibo AsimRibo requested review from antunflas and removed request for simicmarin March 4, 2024 14:54
Comment on lines 27 to 32
this.active = true
if (activeHolder == null) {
activeHolder = active
}
this.active = activeHolder ?: true // use remembered active state
Copy link
Member

Choose a reason for hiding this comment

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

What's the whole idea of updating the active flag based on the availability of a sensor? Why don't we just leave it as is, if there is no sensor, we won't receive a change anyway 🤷‍♂️

Otherwise, by setting it to true and false in the start and stop we overwrite the user's value, which is forever lost.

Even with this change, I don't see how this does anything different than not updating the flag. If active was false, the activeHolder would be false and the active flag would stay false. If the active was true, the activeHolder would be true and the active flag would stay true. Note that activeHolder cannot be null, since if it was, on L30 it would be set to a non-null value, there is just a case of concurrency which I assume we aren't relying on here.

However, if active was true, the sensor started and then the active flag was changed to false, in that case stopping the trigger would change the active flag to false (but activeHolder would still be true), and then when started again, activeHolder won't be reset, meaning the active flag would be true out of nowhere.

I have a feeling changing the active flag in here is just wrong and probably there was an idea to set it to false in case the sensor is not available. We can still keep that part if there is a reason, but I would prefer if we remove every active toggling from the sensor itself.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not that this is code for only SensorTrigger (proximity & shake).

There is an issue with the initialization of the Sensor and that is why code this.active = true exist. Logic is a bit well, unlogical at first.
I added activeHolder logic to avoid the issue of the sensor always becoming active after start() has been invoked regardless of the user's settings.
I see your point about L30.

I will try to revert this change and see what is happening from user's perspective.

Copy link
Collaborator Author

@KCeh KCeh Mar 6, 2024

Choose a reason for hiding this comment

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

So this is problematic part of the code:

override fun start() {
           ...
            this.active = true
}

This is result with code with reverted SensorTrigger.kt changes.
Imagine I am activating proximity sensor after I am bringing app from background

Record_2024-03-06-09-07-23.mp4

It is more of an annoyance than anything else

Copy link
Member

Choose a reason for hiding this comment

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

My suggestion was to completely remove the flipping of the active flag. So start and stop method would look something like this. And the flag setting would be removed from other methods as well 🤷‍♂️

    override fun start() {
        sensorManager = (context.getSystemService(Context.SENSOR_SERVICE) as? SensorManager)
        sensorManager?.let {
            registerSensor(it)
        }
    }

    override fun stop() {
        queue?.clear()
        unregisterSensor()
        sensorManager = null
    }

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I tried this, but I found out that flipping of flag is necessary

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I guess we can remove active flipping and see if anyone reports issue 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

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

registerSensor also has the flipping to false. I'd remove every occurrence of writing to that flag from this class, otherwise the flag would be flipped incorrectly again in some cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well I wouldn't change that because it is fallback if sensor manager can't find that sensor

Copy link
Member

Choose a reason for hiding this comment

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

Okay, that case is not that important, but what would be the downside of removing also that? The trigger would appear to be active but it wouldn't report anything as the sensor is not working.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe only it would be confusing to the user in Sentinel UI?
Not sure

@@ -19,6 +19,7 @@ internal class TriggersRepository(
override suspend fun save(input: TriggerParameters) =
input.entity?.let {
dao.save(it)
updateCache(it)
Copy link
Member

Choose a reason for hiding this comment

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

This change is crucial and probably the fix for all the problems. Good work 👏

@KCeh
Copy link
Collaborator Author

KCeh commented Mar 6, 2024

@antunflas do you think we are good to go?

Copy link
Member

@antunflas antunflas left a comment

Choose a reason for hiding this comment

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

Let's go 🚀

@KCeh KCeh merged commit 6877def into develop Mar 6, 2024
5 checks passed
@KCeh KCeh mentioned this pull request Mar 6, 2024
@KCeh KCeh deleted the fix/trigger-init-not-working-properly-on-first-start branch March 6, 2024 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants