add battery to the popup

This commit is contained in:
Kavish Devar
2024-11-30 08:35:02 +05:30
parent 3e0de6f011
commit 4a1d7df82d
4 changed files with 11 additions and 24 deletions

View File

@@ -37,7 +37,7 @@ class AirPodsService: Service() {
fun showPopup(context: Context, name: String) {
val window = Window(context)
window.open(name)
window.open(name, batteryNotification)
}
private object Receiver: BroadcastReceiver() {
@@ -217,7 +217,6 @@ class AirPodsService: Service() {
}
else if (bytesRead == -1) {
Log.d("AirPods Service", "Socket closed (bytesRead = -1)")
this@AirPodsService.stopForeground(STOP_FOREGROUND_REMOVE)
socket.close()
sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DISCONNECTED))
return@launch

View File

@@ -19,7 +19,6 @@ import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
@@ -483,7 +482,7 @@ fun AirPodsSettingsScreen(device: BluetoothDevice?, service: AirPodsService?,
CenterAlignedTopAppBar(
title = {
Text(
text = device!!.name,
text = if (device != null) device.name else "",
color = if (MaterialTheme.colorScheme.surface.luminance() < 0.5) Color.White else Color.Black,
)
},

View File

@@ -7,7 +7,6 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.ServiceConnection
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.util.Log
@@ -70,13 +69,14 @@ fun Main() {
val disconnectReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("MainActivity", "Received DISCONNECTED broadcast")
navController.navigate("notConnected")
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.registerReceiver(disconnectReceiver, IntentFilter(AirPodsNotifications.AIRPODS_DISCONNECTED),
Context.RECEIVER_NOT_EXPORTED)
}
context.registerReceiver(disconnectReceiver, IntentFilter(AirPodsNotifications.AIRPODS_DISCONNECTED),
Context.RECEIVER_NOT_EXPORTED)
// UI logic
NavHost(

View File

@@ -4,10 +4,7 @@ import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.PixelFormat
import android.util.Log
import android.view.Gravity
@@ -56,9 +53,8 @@ class Window @SuppressLint("InflateParams") constructor(
mWindowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
}
@SuppressLint("InlinedApi")
fun open(name: String = "AirPods Pro") {
@SuppressLint("InlinedApi", "SetTextI18n")
fun open(name: String = "AirPods Pro", batteryNotification: AirPodsNotifications.BatteryNotification) {
try {
if (mView.windowToken == null) {
if (mView.parent == null) {
@@ -75,15 +71,8 @@ class Window @SuppressLint("InflateParams") constructor(
// receive battery broadcast and set to R.id.battery
val batteryText = mView.findViewById<TextView>(R.id.battery)
val batteryIntentFilter = IntentFilter(AirPodsNotifications.BATTERY_DATA)
mView.context.registerReceiver(object : BroadcastReceiver() {
@SuppressLint("SetTextI18n")
override fun onReceive(context: Context, intent: Intent) {
val batteryList = intent.getParcelableArrayListExtra("data", Battery::class.java)
batteryText.text = batteryList?.get(0)?.level.toString() + "%" + " " + batteryList?.get(0)?.status + " " + batteryList?.get(1)?.level.toString() + "%" + " " + batteryList?.get(1)?.status + " " + batteryList?.get(2)?.level.toString() + "%" + " " + batteryList?.get(2)?.status
}
}, batteryIntentFilter, Context.RECEIVER_NOT_EXPORTED)
val batteryList = batteryNotification.getBattery()
batteryText.text = "Why are the battery levels zero :( " + batteryList[0].level.toString() + "%" + " " + batteryList[0].status + " " + batteryList[1].level.toString() + "%" + " " + batteryList[1].status + " " + batteryList[2].level.toString() + "%" + " " + batteryList[2].status
// Slide-up animation
val displayMetrics = mView.context.resources.displayMetrics