Skip to content

Commit 99345c3

Browse files
Refactor BatteryReceiver to improve reliability and efficiency
The `BatteryReceiver` has been refactored to be more robust and efficient. It now takes the `FontAppCompatTextView` and `Prefs` as constructor parameters, removing the need to look up the view on each `onReceive` call. The battery level calculation has been improved to use `BatteryManager.BATTERY_PROPERTY_CAPACITY` as a fallback if the intent extras are not available. In `HomeFragment`, the receiver is now registered with a sticky intent, allowing the battery UI to be updated immediately upon registration instead of waiting for the next battery change event.
1 parent 2f64613 commit 99345c3

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed
Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,57 @@
11
package com.github.codeworkscreativehub.mlauncher.helper.receivers
22

3-
import android.app.Activity
43
import android.content.BroadcastReceiver
54
import android.content.Context
65
import android.content.Intent
76
import android.os.BatteryManager
87
import androidx.core.content.ContextCompat
9-
import com.github.creativecodecat.components.views.FontAppCompatTextView
108
import com.github.codeworkscreativehub.mlauncher.R
119
import com.github.codeworkscreativehub.mlauncher.data.Prefs
10+
import com.github.creativecodecat.components.views.FontAppCompatTextView
1211

13-
class BatteryReceiver : BroadcastReceiver() {
14-
15-
private lateinit var prefs: Prefs
12+
class BatteryReceiver(
13+
private val batteryTextView: FontAppCompatTextView,
14+
private val prefs: Prefs
15+
) : BroadcastReceiver() {
1616

1717
override fun onReceive(context: Context, intent: Intent) {
18-
prefs = Prefs(context)
18+
19+
// Try to get level/scale from the intent
1920
val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
2021
val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
2122

22-
val contextBattery = context as? Activity
23-
val batteryTextView = (contextBattery)?.findViewById<FontAppCompatTextView>(R.id.battery)
24-
25-
val batteryLevel = level * 100 / scale.toFloat()
23+
val batteryLevel: Float = if (level >= 0 && scale > 0) {
24+
level * 100f / scale
25+
} else {
26+
// Fallback using BatteryManager
27+
val bm = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
28+
bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY).toFloat()
29+
}
2630

31+
// Select battery drawable based on percentage
2732
val batteryDrawable = when {
28-
batteryLevel >= 76 -> ContextCompat.getDrawable(
29-
context,
30-
R.drawable.app_battery100
31-
)
32-
33-
batteryLevel >= 51 -> ContextCompat.getDrawable(
34-
context,
35-
R.drawable.app_battery75
36-
)
37-
38-
batteryLevel >= 26 -> ContextCompat.getDrawable(
39-
context,
40-
R.drawable.app_battery50
41-
)
42-
33+
batteryLevel >= 76 -> ContextCompat.getDrawable(context, R.drawable.app_battery100)
34+
batteryLevel >= 51 -> ContextCompat.getDrawable(context, R.drawable.app_battery75)
35+
batteryLevel >= 26 -> ContextCompat.getDrawable(context, R.drawable.app_battery50)
4336
else -> ContextCompat.getDrawable(context, R.drawable.app_battery25)
4437
}
4538

39+
// Update drawable
4640
batteryDrawable?.let {
47-
// Resize the drawable to match the text size
48-
val textSize = batteryTextView?.textSize?.toInt()
41+
val textSize = batteryTextView.textSize.toInt()
4942
if (prefs.showBatteryIcon) {
50-
textSize?.let { bottom -> it.setBounds(0, 0, textSize, bottom) }
51-
batteryTextView?.setCompoundDrawables(it, null, null, null)
43+
it.setBounds(0, 0, textSize, textSize)
44+
batteryTextView.setCompoundDrawables(it, null, null, null)
5245
} else {
5346
it.setBounds(0, 0, 0, 0)
54-
batteryTextView?.setCompoundDrawables(null, null, null, null)
47+
batteryTextView.setCompoundDrawables(null, null, null, null)
5548
}
5649
}
5750

5851
val batteryLevelInt = batteryLevel.toInt()
59-
batteryTextView?.text = buildString {
52+
batteryTextView.text = buildString {
6053
append(batteryLevelInt)
6154
append("%")
6255
}
63-
6456
}
6557
}
66-

app/src/main/java/com/github/codeworkscreativehub/mlauncher/ui/HomeFragment.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class HomeFragment : BaseFragment(), View.OnClickListener, View.OnLongClickListe
118118

119119
val view = binding.root
120120
prefs = Prefs(requireContext())
121-
batteryReceiver = BatteryReceiver()
121+
batteryReceiver = BatteryReceiver(binding.battery, prefs)
122122
dialogBuilder = DialogManager(requireContext(), requireActivity())
123123
if (PrivateSpaceManager(requireContext()).isPrivateSpaceSupported()) {
124124
privateSpaceReceiver = PrivateSpaceReceiver()
@@ -188,8 +188,14 @@ class HomeFragment : BaseFragment(), View.OnClickListener, View.OnLongClickListe
188188

189189
// Register battery receiver
190190
context?.let { ctx ->
191-
batteryReceiver = BatteryReceiver()
192-
ctx.registerReceiver(batteryReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
191+
binding.battery.let { textView ->
192+
batteryReceiver = BatteryReceiver(textView, prefs)
193+
val intentFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
194+
// Register receiver
195+
val stickyIntent = ctx.registerReceiver(batteryReceiver, intentFilter)
196+
// Immediately update UI with sticky intent
197+
stickyIntent?.let { batteryReceiver.onReceive(ctx, it) }
198+
}
193199

194200
// Register private space receiver if supported
195201
if (PrivateSpaceManager(ctx).isPrivateSpaceSupported()) {

0 commit comments

Comments
 (0)