added battery widget

This commit is contained in:
Kavish Devar
2024-12-24 03:45:01 +05:30
parent 8792e992da
commit 4ad653f064
4 changed files with 48 additions and 9 deletions

View File

@@ -3,7 +3,6 @@ package me.kavishdevar.aln
import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider import android.appwidget.AppWidgetProvider
import android.content.Context import android.content.Context
import android.util.Log
import android.widget.RemoteViews import android.widget.RemoteViews
import me.kavishdevar.aln.services.ServiceManager import me.kavishdevar.aln.services.ServiceManager
import me.kavishdevar.aln.utils.BatteryComponent import me.kavishdevar.aln.utils.BatteryComponent
@@ -22,11 +21,7 @@ class BatteryWidget : AppWidgetProvider() {
} }
override fun onEnabled(context: Context) { override fun onEnabled(context: Context) {
// Enter relevant functionality for when the first widget is created updateAppWidget(context, AppWidgetManager.getInstance(context), 0)
}
override fun onDisabled(context: Context) {
// Enter relevant functionality for when the last widget is disabled
} }
} }
@@ -39,7 +34,6 @@ internal fun updateAppWidget(
val batteryList = service?.batteryNotification?.getBattery() val batteryList = service?.batteryNotification?.getBattery()
val views = RemoteViews(context.packageName, R.layout.battery_widget) val views = RemoteViews(context.packageName, R.layout.battery_widget)
Log.d("BatteryWidget", "Battery list: $batteryList")
views.setTextViewText(R.id.left_battery_widget, views.setTextViewText(R.id.left_battery_widget,
batteryList?.find { it.component == BatteryComponent.LEFT }?.let { batteryList?.find { it.component == BatteryComponent.LEFT }?.let {

View File

@@ -5,11 +5,13 @@ import android.app.Notification
import android.app.NotificationChannel import android.app.NotificationChannel
import android.app.NotificationManager import android.app.NotificationManager
import android.app.Service import android.app.Service
import android.appwidget.AppWidgetManager
import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager import android.bluetooth.BluetoothManager
import android.bluetooth.BluetoothProfile import android.bluetooth.BluetoothProfile
import android.bluetooth.BluetoothSocket import android.bluetooth.BluetoothSocket
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
@@ -25,6 +27,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import me.kavishdevar.aln.BatteryWidget
import me.kavishdevar.aln.R import me.kavishdevar.aln.R
import me.kavishdevar.aln.utils.AirPodsNotifications import me.kavishdevar.aln.utils.AirPodsNotifications
import me.kavishdevar.aln.utils.Battery import me.kavishdevar.aln.utils.Battery
@@ -138,6 +141,47 @@ class AirPodsService: Service() {
} }
} }
fun updateBatteryWidget() {
val appWidgetManager = AppWidgetManager.getInstance(this)
val componentName = ComponentName(this, BatteryWidget::class.java)
val widgetIds = appWidgetManager.getAppWidgetIds(componentName)
val remoteViews = RemoteViews(packageName, R.layout.battery_widget).also {
it.setTextViewText(
R.id.left_battery_widget,
batteryNotification.getBattery().find { it.component == BatteryComponent.LEFT }?.let {
if (it.status != BatteryStatus.DISCONNECTED) {
"${if (it.status == BatteryStatus.CHARGING) "⚡" else ""} ${it.level}%"
} else {
""
}
} ?: ""
)
it.setTextViewText(
R.id.right_battery_widget,
batteryNotification.getBattery().find { it.component == BatteryComponent.RIGHT }?.let {
if (it.status != BatteryStatus.DISCONNECTED) {
"${if (it.status == BatteryStatus.CHARGING) "⚡" else ""} ${it.level}%"
} else {
""
}
} ?: ""
)
it.setTextViewText(
R.id.case_battery_widget,
batteryNotification.getBattery().find { it.component == BatteryComponent.CASE }?.let {
if (it.status != BatteryStatus.DISCONNECTED) {
"${if (it.status == BatteryStatus.CHARGING) "⚡" else ""} ${it.level}%"
} else {
""
}
} ?: ""
)
}
Log.d("AirPodsService", "Updating battery widget")
appWidgetManager.updateAppWidget(widgetIds, remoteViews)
}
fun updateNotificationContent(connected: Boolean, airpodsName: String? = null, batteryList: List<Battery>? = null) { fun updateNotificationContent(connected: Boolean, airpodsName: String? = null, batteryList: List<Battery>? = null) {
val notificationManager = getSystemService(NotificationManager::class.java) val notificationManager = getSystemService(NotificationManager::class.java)
val textColor = this.getSharedPreferences("settings", MODE_PRIVATE).getLong("textColor", 0) val textColor = this.getSharedPreferences("settings", MODE_PRIVATE).getLong("textColor", 0)
@@ -296,7 +340,6 @@ class AirPodsService: Service() {
if (isConnected != true) { if (isConnected != true) {
try { try {
Log.d("aikooo7", "trying first method")
socket = HiddenApiBypass.newInstance( socket = HiddenApiBypass.newInstance(
BluetoothSocket::class.java, BluetoothSocket::class.java,
3, 3,
@@ -310,7 +353,6 @@ class AirPodsService: Service() {
e: Exception e: Exception
) { ) {
e.printStackTrace() e.printStackTrace()
Log.d("aikooo7", "first method didn't work; trying second method")
try { try {
socket = HiddenApiBypass.newInstance( socket = HiddenApiBypass.newInstance(
BluetoothSocket::class.java, BluetoothSocket::class.java,
@@ -497,6 +539,7 @@ class AirPodsService: Service() {
ArrayList(batteryNotification.getBattery()) ArrayList(batteryNotification.getBattery())
) )
}) })
updateBatteryWidget()
updateNotificationContent( updateNotificationContent(
true, true,
this@AirPodsService.getSharedPreferences( this@AirPodsService.getSharedPreferences(

View File

@@ -3,6 +3,7 @@
style="@style/Widget.ALN.AppWidget.Container" style="@style/Widget.ALN.AppWidget.Container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:id="@+id/battery_widget"
android:theme="@style/Theme.ALN.AppWidgetContainer"> android:theme="@style/Theme.ALN.AppWidgetContainer">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@@ -3,6 +3,7 @@
style="@style/Widget.ALN.AppWidget.Container" style="@style/Widget.ALN.AppWidget.Container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:id="@+id/battery_widget"
android:theme="@style/Theme.ALN.AppWidgetContainer"> android:theme="@style/Theme.ALN.AppWidgetContainer">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"