@@ -24,8 +24,12 @@ import android.animation.AnimatorListenerAdapter
2424import android.animation.ObjectAnimator
2525import android.animation.PropertyValuesHolder
2626import android.annotation.SuppressLint
27+ import android.content.BroadcastReceiver
2728import android.content.Context
29+ import android.content.Intent
30+ import android.content.IntentFilter
2831import android.graphics.PixelFormat
32+ import android.os.Build
2933import android.os.Handler
3034import android.os.Looper
3135import android.util.Log
@@ -51,6 +55,7 @@ class PopupWindow(
5155 private var isClosing = false
5256 private var autoCloseHandler = Handler (Looper .getMainLooper())
5357 private var autoCloseRunnable: Runnable ? = null
58+ private var batteryUpdateReceiver: BroadcastReceiver ? = null
5459
5560 @Suppress(" DEPRECATION" )
5661 private val mParams: WindowManager .LayoutParams = WindowManager .LayoutParams ().apply {
@@ -145,6 +150,8 @@ class PopupWindow(
145150 interpolator = DecelerateInterpolator ()
146151 start()
147152 }
153+
154+ registerBatteryUpdateReceiver()
148155
149156 autoCloseRunnable = Runnable { close() }
150157 autoCloseHandler.postDelayed(autoCloseRunnable!! , 12000 )
@@ -155,31 +162,59 @@ class PopupWindow(
155162 }
156163 }
157164
158- @SuppressLint(" SetTextI18n" )
159- fun updateBatteryStatus (batteryNotification : AirPodsNotifications .BatteryNotification ) {
160- val batteryStatus = batteryNotification.getBattery()
165+ private fun registerBatteryUpdateReceiver () {
166+ batteryUpdateReceiver = object : BroadcastReceiver () {
167+ override fun onReceive (context : Context ? , intent : Intent ? ) {
168+ if (intent?.action == AirPodsNotifications .BATTERY_DATA ) {
169+ val batteryList = intent.getParcelableArrayListExtra<Battery >(" data" )
170+ if (batteryList != null ) {
171+ updateBatteryStatusFromList(batteryList)
172+ }
173+ }
174+ }
175+ }
161176
177+ val filter = IntentFilter (AirPodsNotifications .BATTERY_DATA )
178+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
179+ context.registerReceiver(batteryUpdateReceiver, filter, Context .RECEIVER_EXPORTED )
180+ } else {
181+ context.registerReceiver(batteryUpdateReceiver, filter)
182+ }
183+ }
184+
185+ private fun unregisterBatteryUpdateReceiver () {
186+ batteryUpdateReceiver?.let {
187+ try {
188+ context.unregisterReceiver(it)
189+ batteryUpdateReceiver = null
190+ } catch (e: Exception ) {
191+ Log .e(" PopupWindow" , " Error unregistering battery receiver: ${e.message} " )
192+ }
193+ }
194+ }
195+
196+ private fun updateBatteryStatusFromList (batteryList : List <Battery >) {
162197 val batteryLeftText = mView.findViewById<TextView >(R .id.left_battery)
163198 val batteryRightText = mView.findViewById<TextView >(R .id.right_battery)
164199 val batteryCaseText = mView.findViewById<TextView >(R .id.case_battery)
165200
166- batteryLeftText.text = batteryStatus .find { it.component == BatteryComponent .LEFT }?.let {
201+ batteryLeftText.text = batteryList .find { it.component == BatteryComponent .LEFT }?.let {
167202 if (it.status != BatteryStatus .DISCONNECTED ) {
168203 " \uDBC3\uDC8E ${it.level} %"
169204 } else {
170205 " "
171206 }
172207 } ? : " "
173208
174- batteryRightText.text = batteryStatus .find { it.component == BatteryComponent .RIGHT }?.let {
209+ batteryRightText.text = batteryList .find { it.component == BatteryComponent .RIGHT }?.let {
175210 if (it.status != BatteryStatus .DISCONNECTED ) {
176211 " \uDBC3\uDC8D ${it.level} %"
177212 } else {
178213 " "
179214 }
180215 } ? : " "
181216
182- batteryCaseText.text = batteryStatus .find { it.component == BatteryComponent .CASE }?.let {
217+ batteryCaseText.text = batteryList .find { it.component == BatteryComponent .CASE }?.let {
183218 if (it.status != BatteryStatus .DISCONNECTED ) {
184219 " \uDBC3\uDE6C ${it.level} %"
185220 } else {
@@ -188,12 +223,19 @@ class PopupWindow(
188223 } ? : " "
189224 }
190225
226+ @SuppressLint(" SetTextI18s" )
227+ fun updateBatteryStatus (batteryNotification : AirPodsNotifications .BatteryNotification ) {
228+ val batteryStatus = batteryNotification.getBattery()
229+ updateBatteryStatusFromList(batteryStatus)
230+ }
231+
191232 fun close () {
192233 try {
193234 if (isClosing) return
194235 isClosing = true
195236
196237 autoCloseRunnable?.let { autoCloseHandler.removeCallbacks(it) }
238+ unregisterBatteryUpdateReceiver()
197239
198240 val vid = mView.findViewById<VideoView >(R .id.video)
199241 vid.stopPlayback()
0 commit comments