The problem with calculation thru the currentNow is it fluctuate very much. Android has been designed to burst the CPU frequency in short moment to finish the task ASAP, then go to sleep or low frequency idling afterward. During the checking of the condition battery level, the current consumption will surge and give higher reading than the supposed current without the check, skewing the calculation result. Similar to observer effect in quantum mechanic.
I don't know how android calculate the current battery left time, it will be interesting to know the formula. Or at least if someone know which file to read the estimates time, screen on time, and battery usage time. (root required)
Simple manual calculation
Here is my simple guideline to interpolate the time left. I usually check my battery at certain random percentage and see the usage time and Screen On Time (SOT). If I am lazy, I simply dividing out the percentage used to the SOT and get how many percent/hour. Then use the remaining percentage to divide this percent/hour, I will get the remaining potential SOT. But for more detail, you still need to know the behaviour and basic value for your phone.
Example Case :
I use the phone until 71%, with 13 hours usage, all full wifi and have 4 hours 40 minutes SOT. My phone has 0,5%/hour on wifi idling. That means :
29% = 4 hours 40 minutes (4,66 hours) with 13 hours usage. Looking at 0,5%/hour in idle wifi connected, I would get 13 hours wifi = 0,5 x 13 = 6,5%. Leaving 29 - 6,5 = 22,5%. 22,5% / 4,66 hours = 4,82%/hour of SOT, round up to 5%/hour.
If I used my phone until 1%, that mean 70% can be used. Let's say I use the phone again for the next 40 hours, which will use up 0,5 x 40 = 20%. Leaving 50%. By calculating the 5%/hour, I can have another additional 10 hours SOT after this. Which mean at 71% left, I have 40 hours usage left with potential 10 hours SOT. But of course, this is estimation. It will change if I change the behaviour of using the phone, such as playing game, using it outdoor, turn on 4G, etc. In fact I ended having 50 hours usage with 13 hours SOT
. It is lower (estimation should be 14 hours 40 minutes) because I use the phone outdoor (higher brightness) and 4G afterward, hence faster drain. It is a good estimation. I do this manually on mental calculation, just to get rough estimation.
Interpolation from last 5 %
Using the same interpolation method, we can estimate the battery left by interpolating the last 5 (or 10) percentage change. Because it makes sense to say "if you keep using your phone with current drain speed, you have xx hours left". So we need to log the last 5 percentage change (or 10 to be longer and less prone to surge usage) to the glovar. We only need the time and percentage, no other values needed. You can include them if you want, and do another calculation. But this method only care about the percentage change and the time it takes to have the change.
Use trigger Battery Level - lower than 100%, so it triggers at every percentage change.
- When it drop to 99%, let say in 5 minutes. Then log this to the glovar. To calculate the time left, the speed of the drain is 1% in 5 minutes. Hence 99% left is 99 / 1 x 5 = 495 minutes left (8 hours 15 minutes left).
- You don't use the phone, and after another 25 minutes (30 minutes from full), it drops to 98%. Log this to glovar, speed of drain is 2% in 30 minutes. Hence 98% left is 98 / 2 x 30 minutes = 1470 minutes (24 hours 30 minutes left)
- You use the phone, and after 10 minutes (40 minutes from full), it drops to 97%. Log, speed is 3% in 40 minutes. 97% left is 97 / 3 x 40 minutes = 1293 minutes (21 hours 33 minutes left)
- this continue until 94% and delete the first 100% data. Or better don't delete, just refer to the index 5 from the current one.
- So at 90%, you use the time from the 95%. If that 5% drain in 1 hour, then at 90% left, 90 / 5 x 1 hour = 18 hours left
This continues to take only last 5 (or last 10) data. And the flow should disabled when you charge the phone and resetted once you unplugged from the charger. So the cycle begin again. When you play game, the drain wil be much faster, let say the last 5% drain happened in 10 minutes. So with 75% left, you will only have 75 / 5 * 10 = 150 minutes = 2 hours 30 minutes. It is reasonable, since if 5% in 10 minutes, that means 30% in an hour. With 75% / 30%/hour = 2,5 hours; same result.
The above method doesn't estimate the SOT. To be more accurate, you can log the Display on/off state too. So you can estimate even the SOT and the usage time.