android: clean up a bit of AI gen'd code

This commit is contained in:
Kavish Devar
2025-09-10 11:24:51 +05:30
parent df9f443173
commit 0e9aadd672
2 changed files with 39 additions and 30 deletions

View File

@@ -34,6 +34,7 @@ class AACPManager {
companion object {
private const val TAG = "AACPManager"
@Suppress("unused")
object Opcodes {
const val SET_FEATURE_FLAGS: Byte = 0x4D
const val REQUEST_NOTIFICATIONS: Byte = 0x0F
@@ -187,7 +188,7 @@ class AACPManager {
var oldConnectedDevices: List<ConnectedDevice> = listOf()
private set
var connectedDevices: List<ConnectedDevice> = listOf()
private set
@@ -735,7 +736,7 @@ class AACPManager {
for (connectedDevice in connectedDevices) {
if (connectedDevice.mac != selfMacAddress) {
Log.d(TAG, "Sending Hijack Request packet to ${connectedDevice.mac}")
success = sendDataPacket(createHijackRequestPacket(connectedDevice.mac)) && success
success = sendDataPacket(createHijackRequestPacket(connectedDevice.mac)) || success
}
}
return success
@@ -872,7 +873,7 @@ class AACPManager {
for (connectedDevice in connectedDevices) {
if (connectedDevice.mac != selfMacAddress) {
Log.d(TAG, "Sending Hijack Reversed packet to ${connectedDevice.mac}")
success = sendDataPacket(createHijackReversedPacket(connectedDevice.mac)) && success
success = sendDataPacket(createHijackReversedPacket(connectedDevice.mac)) || success
}
}
return success
@@ -905,7 +906,6 @@ class AACPManager {
if (selfMacAddress.length != 17 || !selfMacAddress.matches(Regex("([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}")) || targetMacAddress.length != 17 || !targetMacAddress.matches(Regex("([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}"))) {
throw IllegalArgumentException("MAC address must be 6 bytes")
}
Log.d(TAG, "Sending Add TiPi Device packet to $targetMacAddress")
return sendDataPacket(createAddTiPiDevicePacket(selfMacAddress, targetMacAddress))
}

View File

@@ -33,7 +33,6 @@ import android.content.IntentFilter
import android.content.res.Resources
import android.graphics.PixelFormat
import android.graphics.drawable.GradientDrawable
import android.net.Uri
import android.os.Build
import android.os.Handler
import android.os.Looper
@@ -55,6 +54,7 @@ import android.widget.ProgressBar
import android.widget.TextView
import android.widget.VideoView
import androidx.core.content.ContextCompat.getString
import androidx.core.net.toUri
import androidx.dynamicanimation.animation.DynamicAnimation
import androidx.dynamicanimation.animation.SpringAnimation
import androidx.dynamicanimation.animation.SpringForce
@@ -109,7 +109,12 @@ class IslandWindow(private val context: Context) {
private val batteryReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == AirPodsNotifications.BATTERY_DATA) {
val batteryList = intent.getParcelableArrayListExtra<Battery>("data")
val batteryList = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getParcelableArrayListExtra("data", Battery::class.java)
} else {
@Suppress("DEPRECATION")
intent.getParcelableArrayListExtra<Battery>("data")
}
updateBatteryDisplay(batteryList)
} else if (intent?.action == AirPodsNotifications.DISCONNECT_RECEIVERS) {
try {
@@ -133,8 +138,8 @@ class IslandWindow(private val context: Context) {
val leftLevel = leftBattery?.level ?: 0
val rightLevel = rightBattery?.level ?: 0
val leftStatus = leftBattery?.status ?: BatteryStatus.DISCONNECTED
val rightStatus = rightBattery?.status ?: BatteryStatus.DISCONNECTED
leftBattery?.status ?: BatteryStatus.DISCONNECTED
rightBattery?.status ?: BatteryStatus.DISCONNECTED
val batteryText = islandView.findViewById<TextView>(R.id.island_battery_text)
val batteryProgressBar = islandView.findViewById<ProgressBar>(R.id.island_battery_progress)
@@ -157,7 +162,9 @@ class IslandWindow(private val context: Context) {
}
}
@SuppressLint("SetTextI18s", "ClickableViewAccessibility", "UnspecifiedRegisterReceiverFlag")
@SuppressLint("SetTextI18s", "ClickableViewAccessibility", "UnspecifiedRegisterReceiverFlag",
"SetTextI18n"
)
fun show(name: String, batteryPercentage: Int, context: Context, type: IslandType = IslandType.CONNECTED, reversed: Boolean = false) {
if (ServiceManager.getService()?.islandOpen == true) return
else ServiceManager.getService()?.islandOpen = true
@@ -175,10 +182,10 @@ class IslandWindow(private val context: Context) {
val rightBattery = batteryList.find { it.component == BatteryComponent.RIGHT }
when {
leftBattery?.level ?: 0 > 0 && rightBattery?.level ?: 0 > 0 ->
(leftBattery?.level ?: 0) > 0 && (rightBattery?.level ?: 0) > 0 ->
minOf(leftBattery!!.level, rightBattery!!.level)
leftBattery?.level ?: 0 > 0 -> leftBattery!!.level
rightBattery?.level ?: 0 > 0 -> rightBattery!!.level
(leftBattery?.level ?: 0) > 0 -> leftBattery!!.level
(rightBattery?.level ?: 0) > 0 -> rightBattery!!.level
batteryPercentage > 0 -> batteryPercentage
else -> null
}
@@ -204,16 +211,18 @@ class IslandWindow(private val context: Context) {
if (type == IslandType.MOVED_TO_OTHER_DEVICE && !reversed) {
actionButton.visibility = View.VISIBLE
actionButton.setOnClickListener {
ServiceManager.getService()?.takeOver("reverse")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ServiceManager.getService()?.takeOver("reverse")
}
close()
}
islandView.findViewById<TextView>(R.id.island_battery_text).visibility = View.GONE
islandView.findViewById<ProgressBar>(R.id.island_battery_progress).visibility = View.GONE
batteryText.visibility = View.GONE
batteryProgressBar.visibility = View.GONE
batteryBg.visibility = View.GONE
} else {
actionButton.visibility = View.GONE
islandView.findViewById<TextView>(R.id.island_battery_text).visibility = View.VISIBLE
islandView.findViewById<ProgressBar>(R.id.island_battery_progress).visibility = View.VISIBLE
batteryText.visibility = View.VISIBLE
batteryProgressBar.visibility = View.VISIBLE
batteryBg.visibility = View.VISIBLE
}
@@ -300,7 +309,7 @@ class IslandWindow(private val context: Context) {
if (isDraggingDown && deltaY > 0) {
val stretchAmount = (deltaY * 0.5f).coerceAtMost(200f)
applyCustomStretchEffect(stretchAmount, deltaY)
applyCustomStretchEffect(stretchAmount)
}
}
@@ -314,7 +323,7 @@ class IslandWindow(private val context: Context) {
if (isBeingDragged) {
val currentTranslationY = containerView.translationY
val significantVelocity = abs(yVelocity) > 800
abs(yVelocity) > 800
val significantDrag = abs(dragDistance) > 80
when {
@@ -361,7 +370,7 @@ class IslandWindow(private val context: Context) {
}
val videoView = islandView.findViewById<VideoView>(R.id.island_video_view)
val videoUri = Uri.parse("android.resource://me.kavishdevar.librepods/${R.raw.island}")
val videoUri = "android.resource://me.kavishdevar.librepods/${R.raw.island}".toUri()
videoView.setVideoURI(videoUri)
videoView.setOnPreparedListener { mediaPlayer ->
mediaPlayer.isLooping = true
@@ -409,13 +418,13 @@ class IslandWindow(private val context: Context) {
}
}
private fun applyCustomStretchEffect(stretchAmount: Float, dragY: Float) {
private fun applyCustomStretchEffect(stretchAmount: Float) {
try {
val mainLayout = islandView.findViewById<LinearLayout>(R.id.island_window_layout)
val connectedText = islandView.findViewById<TextView>(R.id.island_connected_text)
islandView.findViewById<TextView>(R.id.island_connected_text)
val deviceText = islandView.findViewById<TextView>(R.id.island_device_name)
val batteryView = islandView.findViewById<FrameLayout>(R.id.island_battery_container)
val videoView = islandView.findViewById<VideoView>(R.id.island_video_view)
islandView.findViewById<FrameLayout>(R.id.island_battery_container)
islandView.findViewById<VideoView>(R.id.island_video_view)
val stretchFactor = 1f + (stretchAmount / 300f).coerceAtMost(4.0f)
val newMinHeight = (initialHeight * stretchFactor).toInt()
@@ -470,7 +479,7 @@ class IslandWindow(private val context: Context) {
.setDampingRatio(SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY)
.setStiffness(dynamicStiffness)
resetStretchEffects(velocity)
resetStretchEffects()
if (params != null) {
params!!.height = WindowManager.LayoutParams.WRAP_CONTENT
@@ -484,7 +493,7 @@ class IslandWindow(private val context: Context) {
springAnimation.start()
}
private fun resetStretchEffects(velocity: Float) {
private fun resetStretchEffects() {
try {
val mainLayout = islandView.findViewById<LinearLayout>(R.id.island_window_layout)
val deviceText = islandView.findViewById<TextView>(R.id.island_device_name)
@@ -574,7 +583,7 @@ class IslandWindow(private val context: Context) {
stretchAnimator.interpolator = OvershootInterpolator(0.5f)
stretchAnimator.addUpdateListener { animation ->
val progress = animation.animatedValue as Float
animateCustomStretch(progress, expandDuration)
animateCustomStretch(progress)
}
val normalizeAnimator = ValueAnimator.ofFloat(1.0f, 0.0f)
@@ -601,7 +610,7 @@ class IslandWindow(private val context: Context) {
normalizeAnimator.start()
}
private fun animateCustomStretch(progress: Float, duration: Long) {
private fun animateCustomStretch(progress: Float) {
try {
val mainLayout = islandView.findViewById<LinearLayout>(R.id.island_window_layout)
val connectedText = islandView.findViewById<TextView>(R.id.island_connected_text)
@@ -648,7 +657,7 @@ class IslandWindow(private val context: Context) {
ServiceManager.getService()?.islandOpen = false
autoCloseHandler?.removeCallbacks(autoCloseRunnable ?: return)
resetStretchEffects(0f)
resetStretchEffects()
val videoView = islandView.findViewById<VideoView>(R.id.island_video_view)
try {
@@ -712,7 +721,7 @@ class IslandWindow(private val context: Context) {
try {
context.unregisterReceiver(batteryReceiver)
} catch (e: Exception) {
// Silent catch - receiver might already be unregistered
e.printStackTrace()
}
ServiceManager.getService()?.islandOpen = false