Skip to content

Commit 4ad653f

Browse files
committed
added battery widget
1 parent 8792e99 commit 4ad653f

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

android/app/src/main/java/me/kavishdevar/aln/BatteryWidget.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package me.kavishdevar.aln
33
import android.appwidget.AppWidgetManager
44
import android.appwidget.AppWidgetProvider
55
import android.content.Context
6-
import android.util.Log
76
import android.widget.RemoteViews
87
import me.kavishdevar.aln.services.ServiceManager
98
import me.kavishdevar.aln.utils.BatteryComponent
@@ -22,11 +21,7 @@ class BatteryWidget : AppWidgetProvider() {
2221
}
2322

2423
override fun onEnabled(context: Context) {
25-
// Enter relevant functionality for when the first widget is created
26-
}
27-
28-
override fun onDisabled(context: Context) {
29-
// Enter relevant functionality for when the last widget is disabled
24+
updateAppWidget(context, AppWidgetManager.getInstance(context), 0)
3025
}
3126
}
3227

@@ -39,7 +34,6 @@ internal fun updateAppWidget(
3934
val batteryList = service?.batteryNotification?.getBattery()
4035

4136
val views = RemoteViews(context.packageName, R.layout.battery_widget)
42-
Log.d("BatteryWidget", "Battery list: $batteryList")
4337

4438
views.setTextViewText(R.id.left_battery_widget,
4539
batteryList?.find { it.component == BatteryComponent.LEFT }?.let {

android/app/src/main/java/me/kavishdevar/aln/services/AirPodsService.kt

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import android.app.Notification
55
import android.app.NotificationChannel
66
import android.app.NotificationManager
77
import android.app.Service
8+
import android.appwidget.AppWidgetManager
89
import android.bluetooth.BluetoothDevice
910
import android.bluetooth.BluetoothManager
1011
import android.bluetooth.BluetoothProfile
1112
import android.bluetooth.BluetoothSocket
1213
import android.content.BroadcastReceiver
14+
import android.content.ComponentName
1315
import android.content.Context
1416
import android.content.Intent
1517
import android.content.IntentFilter
@@ -25,6 +27,7 @@ import kotlinx.coroutines.CoroutineScope
2527
import kotlinx.coroutines.Dispatchers
2628
import kotlinx.coroutines.delay
2729
import kotlinx.coroutines.launch
30+
import me.kavishdevar.aln.BatteryWidget
2831
import me.kavishdevar.aln.R
2932
import me.kavishdevar.aln.utils.AirPodsNotifications
3033
import 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(

android/app/src/main/res/layout-v31/battery_widget.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
style="@style/Widget.ALN.AppWidget.Container"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
6+
android:id="@+id/battery_widget"
67
android:theme="@style/Theme.ALN.AppWidgetContainer">
78
<LinearLayout
89
android:layout_width="match_parent"

android/app/src/main/res/layout/battery_widget.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
style="@style/Widget.ALN.AppWidget.Container"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
6+
android:id="@+id/battery_widget"
67
android:theme="@style/Theme.ALN.AppWidgetContainer">
78
<LinearLayout
89
android:layout_width="match_parent"

0 commit comments

Comments
 (0)