@@ -5,11 +5,13 @@ import android.app.Notification
55import android.app.NotificationChannel
66import android.app.NotificationManager
77import android.app.Service
8+ import android.appwidget.AppWidgetManager
89import android.bluetooth.BluetoothDevice
910import android.bluetooth.BluetoothManager
1011import android.bluetooth.BluetoothProfile
1112import android.bluetooth.BluetoothSocket
1213import android.content.BroadcastReceiver
14+ import android.content.ComponentName
1315import android.content.Context
1416import android.content.Intent
1517import android.content.IntentFilter
@@ -25,6 +27,7 @@ import kotlinx.coroutines.CoroutineScope
2527import kotlinx.coroutines.Dispatchers
2628import kotlinx.coroutines.delay
2729import kotlinx.coroutines.launch
30+ import me.kavishdevar.aln.BatteryWidget
2831import me.kavishdevar.aln.R
2932import me.kavishdevar.aln.utils.AirPodsNotifications
3033import me.kavishdevar.aln.utils.Battery
@@ -138,6 +141,47 @@ class AirPodsService: Service() {
138141 }
139142 }
140143
144+ fun updateBatteryWidget () {
145+ val appWidgetManager = AppWidgetManager .getInstance(this )
146+ val componentName = ComponentName (this , BatteryWidget ::class .java)
147+ val widgetIds = appWidgetManager.getAppWidgetIds(componentName)
148+
149+ val remoteViews = RemoteViews (packageName, R .layout.battery_widget).also {
150+ it.setTextViewText(
151+ R .id.left_battery_widget,
152+ batteryNotification.getBattery().find { it.component == BatteryComponent .LEFT }?.let {
153+ if (it.status != BatteryStatus .DISCONNECTED ) {
154+ " ${if (it.status == BatteryStatus .CHARGING ) " ⚡" else " " } ${it.level} %"
155+ } else {
156+ " "
157+ }
158+ } ? : " "
159+ )
160+ it.setTextViewText(
161+ R .id.right_battery_widget,
162+ batteryNotification.getBattery().find { it.component == BatteryComponent .RIGHT }?.let {
163+ if (it.status != BatteryStatus .DISCONNECTED ) {
164+ " ${if (it.status == BatteryStatus .CHARGING ) " ⚡" else " " } ${it.level} %"
165+ } else {
166+ " "
167+ }
168+ } ? : " "
169+ )
170+ it.setTextViewText(
171+ R .id.case_battery_widget,
172+ batteryNotification.getBattery().find { it.component == BatteryComponent .CASE }?.let {
173+ if (it.status != BatteryStatus .DISCONNECTED ) {
174+ " ${if (it.status == BatteryStatus .CHARGING ) " ⚡" else " " } ${it.level} %"
175+ } else {
176+ " "
177+ }
178+ } ? : " "
179+ )
180+ }
181+ Log .d(" AirPodsService" , " Updating battery widget" )
182+ appWidgetManager.updateAppWidget(widgetIds, remoteViews)
183+ }
184+
141185 fun updateNotificationContent (connected : Boolean , airpodsName : String? = null, batteryList : List <Battery >? = null) {
142186 val notificationManager = getSystemService(NotificationManager ::class .java)
143187 val textColor = this .getSharedPreferences(" settings" , MODE_PRIVATE ).getLong(" textColor" , 0 )
@@ -296,7 +340,6 @@ class AirPodsService: Service() {
296340
297341 if (isConnected != true ) {
298342 try {
299- Log .d(" aikooo7" , " trying first method" )
300343 socket = HiddenApiBypass .newInstance(
301344 BluetoothSocket ::class .java,
302345 3 ,
@@ -310,7 +353,6 @@ class AirPodsService: Service() {
310353 e: Exception
311354 ) {
312355 e.printStackTrace()
313- Log .d(" aikooo7" , " first method didn't work; trying second method" )
314356 try {
315357 socket = HiddenApiBypass .newInstance(
316358 BluetoothSocket ::class .java,
@@ -497,6 +539,7 @@ class AirPodsService: Service() {
497539 ArrayList (batteryNotification.getBattery())
498540 )
499541 })
542+ updateBatteryWidget()
500543 updateNotificationContent(
501544 true ,
502545 this @AirPodsService.getSharedPreferences(
0 commit comments