mirror of
https://github.com/kavishdevar/librepods.git
synced 2026-02-03 00:29:16 +00:00
Update `AirPodsService.kt` and `BatteryWidget.kt` to retain and display the last received battery levels when the case or bud gets disconnected. * **AirPodsService.kt** - Comment out the condition that sets battery levels to an empty string if the status is `BatteryStatus.DISCONNECTED`. * **BatteryWidget.kt** - Comment out the condition that sets battery levels to an empty string if the status is `BatteryStatus.DISCONNECTED`.
83 lines
3.0 KiB
Kotlin
83 lines
3.0 KiB
Kotlin
/*
|
|
* AirPods like Normal (ALN) - Bringing Apple-only features to Linux and Android for seamless AirPods functionality!
|
|
*
|
|
* Copyright (C) 2024 Kavish Devar
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
* by the Free Software Foundation, either version 3 of the License.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
|
|
package me.kavishdevar.aln
|
|
|
|
import android.appwidget.AppWidgetManager
|
|
import android.appwidget.AppWidgetProvider
|
|
import android.content.Context
|
|
import android.widget.RemoteViews
|
|
import me.kavishdevar.aln.services.ServiceManager
|
|
import me.kavishdevar.aln.utils.BatteryComponent
|
|
import me.kavishdevar.aln.utils.BatteryStatus
|
|
|
|
class BatteryWidget : AppWidgetProvider() {
|
|
override fun onUpdate(
|
|
context: Context,
|
|
appWidgetManager: AppWidgetManager,
|
|
appWidgetIds: IntArray
|
|
) {
|
|
// There may be multiple widgets active, so update all of them
|
|
for (appWidgetId in appWidgetIds) {
|
|
updateAppWidget(context, appWidgetManager, appWidgetId)
|
|
}
|
|
}
|
|
|
|
override fun onEnabled(context: Context) {
|
|
updateAppWidget(context, AppWidgetManager.getInstance(context), 0)
|
|
}
|
|
}
|
|
|
|
internal fun updateAppWidget(
|
|
context: Context,
|
|
appWidgetManager: AppWidgetManager,
|
|
appWidgetId: Int
|
|
) {
|
|
val service = ServiceManager.getService()
|
|
val batteryList = service?.batteryNotification?.getBattery()
|
|
|
|
val views = RemoteViews(context.packageName, R.layout.battery_widget)
|
|
|
|
views.setTextViewText(R.id.left_battery_widget,
|
|
batteryList?.find { it.component == BatteryComponent.LEFT }?.let {
|
|
// if (it.status != BatteryStatus.DISCONNECTED) {
|
|
"${if (it.status == BatteryStatus.CHARGING) "⚡" else ""} ${it.level}%"
|
|
// } else {
|
|
// ""
|
|
// }
|
|
} ?: "")
|
|
views.setTextViewText(R.id.right_battery_widget,
|
|
batteryList?.find { it.component == BatteryComponent.RIGHT }?.let {
|
|
// if (it.status != BatteryStatus.DISCONNECTED) {
|
|
"${if (it.status == BatteryStatus.CHARGING) "⚡" else ""} ${it.level}%"
|
|
// } else {
|
|
// ""
|
|
// }
|
|
} ?: "")
|
|
views.setTextViewText(R.id.case_battery_widget,
|
|
batteryList?.find { it.component == BatteryComponent.CASE }?.let {
|
|
// if (it.status != BatteryStatus.DISCONNECTED) {
|
|
"${if (it.status == BatteryStatus.CHARGING) "⚡" else ""} ${it.level}%"
|
|
// } else {
|
|
// ""
|
|
// }
|
|
} ?: "")
|
|
|
|
appWidgetManager.updateAppWidget(appWidgetId, views)
|
|
} |