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

View File

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