dubrowgn / wattz

An Android battery power status bar indicator
MIT License
192 stars 9 forks source link

[BUG] Charging status flipped. #12

Closed THElegend5 closed 2 years ago

THElegend5 commented 2 years ago

Wattz shows the charging status as true when the device isn't charging and as false when it is charging. Screenshot_2022-09-24-02-20-10-662_dubrowgn.wattz.png Screenshot_2022-09-24-02-19-58-582_dubrowgn.wattz.png

Device : Redmi Note 9 Pro Max Android Version : 11 App Version : 1.8

dubrowgn commented 2 years ago

It looks like your device reports amperage with reverse sign. I don't think there is a reliable way to work around this.

xlash123 commented 2 years ago

I'm also having this issue. Is there a way to add a manual toggle switch in application settings to invert the logic to work with phones that report amperage with a negative sign?

specs32 commented 2 years ago

It looks like your device reports amperage with reverse sign. I don't think there is a reliable way to work around this.

https://www.geeksforgeeks.org/how-to-check-if-the-battery-is-charging-or-not-in-android-programmatically/

that looks reasonable.. checks property of intend ..

dubrowgn commented 2 years ago

The problem with ACTION_POWER_CONNECTED is that the app must be alive to receive it. If you plug in your phone, and then open the app, there is no way for it to know that event already happened in the past. The way you're supposed to do it is through BatteryManager, but BatteryManager is currently lying to Wattz on your devices.

The only 100% reliable way to fix this is to report the issue to your phone manufacturer, and have them fix it. I highly encourage you to do this.

That being said, I'm mulling over some settings options, but the situation is greatly complicated by the fact that the status service lives in a separate process, necessitating the design and implementation of an entire IPC protocol of some kind to communicate settings between it and the app.

specs32 commented 2 years ago

there are battery apps that work without issue on my phone.. but I like yours better :)

I thought the battery manager holds that information you just weren't asking for it ? like in :

val status: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1
val isCharging: Boolean = status == BatteryManager.BATTERY_STATUS_CHARGING
        || status == BatteryManager.BATTERY_STATUS_FULL

// How are we charging?
val chargePlug: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) ?: -1
val usbCharge: Boolean = chargePlug == BatteryManager.BATTERY_PLUGGED_USB
val acCharge: Boolean = chargePlug == BatteryManager.BATTERY_PLUGGED_AC

https://developer.android.com/training/monitoring-device-state/battery-monitoring (this is probably a more reliable source.)

I am tempted to set up an android ide and try for myself :D ^^

dubrowgn commented 2 years ago

there are battery apps that work without issue on my phone

And this app works without issue on my phone. Do those other apps work without issue on every phone? If other apps work on your phone, it's because they have implemented workarounds that work on your phone. Chances are, those same workarounds break other phones that would have worked correctly without them.

I thought the battery manager holds that information you just weren't asking for it

I am using BatteryManager. Your phone is not correctly conforming to the BatteryManager API.

I am tempted to set up an android ide and try for myself :D ^^

Knock yourself out. Keep in mind, however, just because it works on your phone, doesn't mean it works on all phones, so I can't necessarily integrate any changes you make.

THElegend5 commented 2 years ago

@dubrowgn could the app do it dynamically, so for ex it follows the current system which is +ve amp if discharging and -ve amp charging by default. But if it sees that the phone is charging but the charging status is false it reverses charging status indicator for that phone and saves this too storage to always show the status in reverse for that phome from that point on?

dubrowgn commented 2 years ago

Theoretically, but the devil is in the details. The app already handles phones that don't properly report charging status by looking at amperage. What you want to do is the reverse, but how do you tell a phone is charging if the charging signal is unreliable?

THElegend5 commented 2 years ago

Theoretically, but the devil is in the details. The app already handles phones that don't properly report charging status by looking at amperage. What you want to do is the reverse, but how do you tell a phone is charging if the charging signal is unreliable?

In my second picture it was showing charging since just fine, if it already knows that why not just trust that.

dubrowgn commented 2 years ago

In this case, yes.

THElegend5 commented 2 years ago

In this case, yes.

So it could reverse if it sees charging since but the charging indicator is false or are there some phones that don't show charging since?

dubrowgn commented 2 years ago

You'll also notice the BatteryManager is telling us it is not charging.

THElegend5 commented 2 years ago

You'll also notice the BatteryManager is telling us it is not charging.

I don't know anything about how that works or how the app works really, I was just going by what was showen on the screen. This would be fixed atleast for my phone after it detects it charging once as I can speak only about mine. Will this not work with other phones that are having this issue?

dubrowgn commented 2 years ago

Will this not work with other phones that are having this issue?

No, it will not. Samsung phones, for example, do not correctly report charging status, so amperage is used as a fallback mechanism. Your phone would use the same fallback mechanism, if it didn't also incorrectly report amperage. So, using charging to fix amperage, and that amperage to fix charging, creates a dependency cycle. Which value do you trust? Given an arbitrary phone, you can't know. "Charging since" (which consumes the ACTION_POWER_CONNECTED event) may seem to be working in your example, but as I mentioned previously in this thread, it is also unreliable because the app must be running to receive the event. There is also no guarantee that all phones produce this event correctly. So, we have 3 different ways to detect "is charging", and all 3 have cases where they don't work.

Ultimately, the root cause is an incorrect BatteryManager implementation by your phone's manufacture. Please report these issues to them, as they are the only ones capable of resolving these issues with 100% reliability. Anything else we may do here can only work if conditions are right.

dubrowgn commented 2 years ago

Yesterday, I discovered/realized you can use Intents and BroadcastReceivers to easily implement pub/sub for IPC. This simplifies things quite a bit, and I used it to build out an initial settings page containing a switch to invert current sign in https://github.com/dubrowgn/wattz/commit/21f6fafb2d26288addc83784c31fe34a0bc856d4. It's working well so far, but I want to flesh it out with a few more workaround settings before I publish a new version.

settings

dubrowgn commented 2 years ago

@THElegend5 I have a release candidate ready, if you would like to see if it resolves your issue.

wattz-1.9.zip

THElegend5 commented 2 years ago

@THElegend5 I have a release candidate ready, if you would like to see if it resolves your issue.

wattz-1.9.zip

Looks pretty nice, the only thing I would say is that the bottom half looks really empty now. Maybe show the stats in the middle of the screen instead of the top?

dubrowgn commented 2 years ago

I'm still not completely happy with the look, but I'm going to roll with it for the time being. I'm more interested in if the technical functionality is working correctly.

THElegend5 commented 2 years ago

I'm still not completely happy with the look, but I'm going to roll with it for the time being. I'm more interested in if the technical functionality is working correctly.

Yep so far is working as intended.

xlash123 commented 2 years ago

I can also confirm the workaround is working for me.

dubrowgn commented 2 years ago

Awesome, thanks for the feedback. I just cut a v1.9 release with the same apk, which should end up on F-Droid in a few days.