Skip to content

Commit 0e9aadd

Browse files
committed
android: clean up a bit of AI gen'd code
1 parent df9f443 commit 0e9aadd

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

android/app/src/main/java/me/kavishdevar/librepods/utils/AACPManager.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class AACPManager {
3434
companion object {
3535
private const val TAG = "AACPManager"
3636

37+
@Suppress("unused")
3738
object Opcodes {
3839
const val SET_FEATURE_FLAGS: Byte = 0x4D
3940
const val REQUEST_NOTIFICATIONS: Byte = 0x0F
@@ -187,7 +188,7 @@ class AACPManager {
187188

188189
var oldConnectedDevices: List<ConnectedDevice> = listOf()
189190
private set
190-
191+
191192
var connectedDevices: List<ConnectedDevice> = listOf()
192193
private set
193194

@@ -735,7 +736,7 @@ class AACPManager {
735736
for (connectedDevice in connectedDevices) {
736737
if (connectedDevice.mac != selfMacAddress) {
737738
Log.d(TAG, "Sending Hijack Request packet to ${connectedDevice.mac}")
738-
success = sendDataPacket(createHijackRequestPacket(connectedDevice.mac)) && success
739+
success = sendDataPacket(createHijackRequestPacket(connectedDevice.mac)) || success
739740
}
740741
}
741742
return success
@@ -872,7 +873,7 @@ class AACPManager {
872873
for (connectedDevice in connectedDevices) {
873874
if (connectedDevice.mac != selfMacAddress) {
874875
Log.d(TAG, "Sending Hijack Reversed packet to ${connectedDevice.mac}")
875-
success = sendDataPacket(createHijackReversedPacket(connectedDevice.mac)) && success
876+
success = sendDataPacket(createHijackReversedPacket(connectedDevice.mac)) || success
876877
}
877878
}
878879
return success
@@ -905,7 +906,6 @@ class AACPManager {
905906
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}"))) {
906907
throw IllegalArgumentException("MAC address must be 6 bytes")
907908
}
908-
909909
Log.d(TAG, "Sending Add TiPi Device packet to $targetMacAddress")
910910
return sendDataPacket(createAddTiPiDevicePacket(selfMacAddress, targetMacAddress))
911911
}

android/app/src/main/java/me/kavishdevar/librepods/utils/IslandWindow.kt

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import android.content.IntentFilter
3333
import android.content.res.Resources
3434
import android.graphics.PixelFormat
3535
import android.graphics.drawable.GradientDrawable
36-
import android.net.Uri
3736
import android.os.Build
3837
import android.os.Handler
3938
import android.os.Looper
@@ -55,6 +54,7 @@ import android.widget.ProgressBar
5554
import android.widget.TextView
5655
import android.widget.VideoView
5756
import androidx.core.content.ContextCompat.getString
57+
import androidx.core.net.toUri
5858
import androidx.dynamicanimation.animation.DynamicAnimation
5959
import androidx.dynamicanimation.animation.SpringAnimation
6060
import androidx.dynamicanimation.animation.SpringForce
@@ -109,7 +109,12 @@ class IslandWindow(private val context: Context) {
109109
private val batteryReceiver = object : BroadcastReceiver() {
110110
override fun onReceive(context: Context?, intent: Intent?) {
111111
if (intent?.action == AirPodsNotifications.BATTERY_DATA) {
112-
val batteryList = intent.getParcelableArrayListExtra<Battery>("data")
112+
val batteryList = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
113+
intent.getParcelableArrayListExtra("data", Battery::class.java)
114+
} else {
115+
@Suppress("DEPRECATION")
116+
intent.getParcelableArrayListExtra<Battery>("data")
117+
}
113118
updateBatteryDisplay(batteryList)
114119
} else if (intent?.action == AirPodsNotifications.DISCONNECT_RECEIVERS) {
115120
try {
@@ -133,8 +138,8 @@ class IslandWindow(private val context: Context) {
133138

134139
val leftLevel = leftBattery?.level ?: 0
135140
val rightLevel = rightBattery?.level ?: 0
136-
val leftStatus = leftBattery?.status ?: BatteryStatus.DISCONNECTED
137-
val rightStatus = rightBattery?.status ?: BatteryStatus.DISCONNECTED
141+
leftBattery?.status ?: BatteryStatus.DISCONNECTED
142+
rightBattery?.status ?: BatteryStatus.DISCONNECTED
138143

139144
val batteryText = islandView.findViewById<TextView>(R.id.island_battery_text)
140145
val batteryProgressBar = islandView.findViewById<ProgressBar>(R.id.island_battery_progress)
@@ -157,7 +162,9 @@ class IslandWindow(private val context: Context) {
157162
}
158163
}
159164

160-
@SuppressLint("SetTextI18s", "ClickableViewAccessibility", "UnspecifiedRegisterReceiverFlag")
165+
@SuppressLint("SetTextI18s", "ClickableViewAccessibility", "UnspecifiedRegisterReceiverFlag",
166+
"SetTextI18n"
167+
)
161168
fun show(name: String, batteryPercentage: Int, context: Context, type: IslandType = IslandType.CONNECTED, reversed: Boolean = false) {
162169
if (ServiceManager.getService()?.islandOpen == true) return
163170
else ServiceManager.getService()?.islandOpen = true
@@ -175,10 +182,10 @@ class IslandWindow(private val context: Context) {
175182
val rightBattery = batteryList.find { it.component == BatteryComponent.RIGHT }
176183

177184
when {
178-
leftBattery?.level ?: 0 > 0 && rightBattery?.level ?: 0 > 0 ->
185+
(leftBattery?.level ?: 0) > 0 && (rightBattery?.level ?: 0) > 0 ->
179186
minOf(leftBattery!!.level, rightBattery!!.level)
180-
leftBattery?.level ?: 0 > 0 -> leftBattery!!.level
181-
rightBattery?.level ?: 0 > 0 -> rightBattery!!.level
187+
(leftBattery?.level ?: 0) > 0 -> leftBattery!!.level
188+
(rightBattery?.level ?: 0) > 0 -> rightBattery!!.level
182189
batteryPercentage > 0 -> batteryPercentage
183190
else -> null
184191
}
@@ -204,16 +211,18 @@ class IslandWindow(private val context: Context) {
204211
if (type == IslandType.MOVED_TO_OTHER_DEVICE && !reversed) {
205212
actionButton.visibility = View.VISIBLE
206213
actionButton.setOnClickListener {
207-
ServiceManager.getService()?.takeOver("reverse")
214+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
215+
ServiceManager.getService()?.takeOver("reverse")
216+
}
208217
close()
209218
}
210-
islandView.findViewById<TextView>(R.id.island_battery_text).visibility = View.GONE
211-
islandView.findViewById<ProgressBar>(R.id.island_battery_progress).visibility = View.GONE
219+
batteryText.visibility = View.GONE
220+
batteryProgressBar.visibility = View.GONE
212221
batteryBg.visibility = View.GONE
213222
} else {
214223
actionButton.visibility = View.GONE
215-
islandView.findViewById<TextView>(R.id.island_battery_text).visibility = View.VISIBLE
216-
islandView.findViewById<ProgressBar>(R.id.island_battery_progress).visibility = View.VISIBLE
224+
batteryText.visibility = View.VISIBLE
225+
batteryProgressBar.visibility = View.VISIBLE
217226
batteryBg.visibility = View.VISIBLE
218227
}
219228

@@ -300,7 +309,7 @@ class IslandWindow(private val context: Context) {
300309

301310
if (isDraggingDown && deltaY > 0) {
302311
val stretchAmount = (deltaY * 0.5f).coerceAtMost(200f)
303-
applyCustomStretchEffect(stretchAmount, deltaY)
312+
applyCustomStretchEffect(stretchAmount)
304313
}
305314
}
306315

@@ -314,7 +323,7 @@ class IslandWindow(private val context: Context) {
314323

315324
if (isBeingDragged) {
316325
val currentTranslationY = containerView.translationY
317-
val significantVelocity = abs(yVelocity) > 800
326+
abs(yVelocity) > 800
318327
val significantDrag = abs(dragDistance) > 80
319328

320329
when {
@@ -361,7 +370,7 @@ class IslandWindow(private val context: Context) {
361370
}
362371

363372
val videoView = islandView.findViewById<VideoView>(R.id.island_video_view)
364-
val videoUri = Uri.parse("android.resource://me.kavishdevar.librepods/${R.raw.island}")
373+
val videoUri = "android.resource://me.kavishdevar.librepods/${R.raw.island}".toUri()
365374
videoView.setVideoURI(videoUri)
366375
videoView.setOnPreparedListener { mediaPlayer ->
367376
mediaPlayer.isLooping = true
@@ -409,13 +418,13 @@ class IslandWindow(private val context: Context) {
409418
}
410419
}
411420

412-
private fun applyCustomStretchEffect(stretchAmount: Float, dragY: Float) {
421+
private fun applyCustomStretchEffect(stretchAmount: Float) {
413422
try {
414423
val mainLayout = islandView.findViewById<LinearLayout>(R.id.island_window_layout)
415-
val connectedText = islandView.findViewById<TextView>(R.id.island_connected_text)
424+
islandView.findViewById<TextView>(R.id.island_connected_text)
416425
val deviceText = islandView.findViewById<TextView>(R.id.island_device_name)
417-
val batteryView = islandView.findViewById<FrameLayout>(R.id.island_battery_container)
418-
val videoView = islandView.findViewById<VideoView>(R.id.island_video_view)
426+
islandView.findViewById<FrameLayout>(R.id.island_battery_container)
427+
islandView.findViewById<VideoView>(R.id.island_video_view)
419428

420429
val stretchFactor = 1f + (stretchAmount / 300f).coerceAtMost(4.0f)
421430
val newMinHeight = (initialHeight * stretchFactor).toInt()
@@ -470,7 +479,7 @@ class IslandWindow(private val context: Context) {
470479
.setDampingRatio(SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY)
471480
.setStiffness(dynamicStiffness)
472481

473-
resetStretchEffects(velocity)
482+
resetStretchEffects()
474483

475484
if (params != null) {
476485
params!!.height = WindowManager.LayoutParams.WRAP_CONTENT
@@ -484,7 +493,7 @@ class IslandWindow(private val context: Context) {
484493
springAnimation.start()
485494
}
486495

487-
private fun resetStretchEffects(velocity: Float) {
496+
private fun resetStretchEffects() {
488497
try {
489498
val mainLayout = islandView.findViewById<LinearLayout>(R.id.island_window_layout)
490499
val deviceText = islandView.findViewById<TextView>(R.id.island_device_name)
@@ -574,7 +583,7 @@ class IslandWindow(private val context: Context) {
574583
stretchAnimator.interpolator = OvershootInterpolator(0.5f)
575584
stretchAnimator.addUpdateListener { animation ->
576585
val progress = animation.animatedValue as Float
577-
animateCustomStretch(progress, expandDuration)
586+
animateCustomStretch(progress)
578587
}
579588

580589
val normalizeAnimator = ValueAnimator.ofFloat(1.0f, 0.0f)
@@ -601,7 +610,7 @@ class IslandWindow(private val context: Context) {
601610
normalizeAnimator.start()
602611
}
603612

604-
private fun animateCustomStretch(progress: Float, duration: Long) {
613+
private fun animateCustomStretch(progress: Float) {
605614
try {
606615
val mainLayout = islandView.findViewById<LinearLayout>(R.id.island_window_layout)
607616
val connectedText = islandView.findViewById<TextView>(R.id.island_connected_text)
@@ -648,7 +657,7 @@ class IslandWindow(private val context: Context) {
648657
ServiceManager.getService()?.islandOpen = false
649658
autoCloseHandler?.removeCallbacks(autoCloseRunnable ?: return)
650659

651-
resetStretchEffects(0f)
660+
resetStretchEffects()
652661

653662
val videoView = islandView.findViewById<VideoView>(R.id.island_video_view)
654663
try {
@@ -712,7 +721,7 @@ class IslandWindow(private val context: Context) {
712721
try {
713722
context.unregisterReceiver(batteryReceiver)
714723
} catch (e: Exception) {
715-
// Silent catch - receiver might already be unregistered
724+
e.printStackTrace()
716725
}
717726

718727
ServiceManager.getService()?.islandOpen = false

0 commit comments

Comments
 (0)