fix: adapt phone share QR to window size
This commit is contained in:
@@ -12,8 +12,8 @@ android {
|
||||
applicationId = "com.qft.optical"
|
||||
minSdk = 24
|
||||
targetSdk = 35
|
||||
versionCode = 24
|
||||
versionName = "0.5.0-dev1"
|
||||
versionCode = 25
|
||||
versionName = "0.5.0-dev2"
|
||||
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
@@ -228,7 +228,7 @@ class MainActivity : ComponentActivity() {
|
||||
) { showReceivedFiles() })
|
||||
root.addView(space(22))
|
||||
root.addView(text("本机设备:$receiverDeviceLabel", 13f, MUTED))
|
||||
root.addView(text("QFT v0.5.0-dev1 · Android 7+ 到最新", 12f, MUTED))
|
||||
root.addView(text("QFT v0.5.0-dev2 · Android 7+ 到最新", 12f, MUTED))
|
||||
setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) })
|
||||
}
|
||||
|
||||
@@ -993,17 +993,27 @@ class MainActivity : ComponentActivity() {
|
||||
phoneShareStatusText?.text = "无法生成配对二维码:${e.message}"
|
||||
return
|
||||
}
|
||||
val qr = PhoneQr.render(encoded, 720)
|
||||
// Use the measured app content area. `resources.displayMetrics` is only
|
||||
// a fallback for the short interval before the decor has completed a
|
||||
// layout pass; using the physical display alone breaks tablets and
|
||||
// split-screen windows because it ignores the actual app bounds.
|
||||
val content = findViewById<View>(android.R.id.content)
|
||||
val windowWidth = content?.width?.takeIf { it > 0 } ?: resources.displayMetrics.widthPixels
|
||||
val windowHeight = content?.height?.takeIf { it > 0 } ?: resources.displayMetrics.heightPixels
|
||||
val qrSide = phoneQrSidePx(windowWidth, windowHeight, resources.displayMetrics.density)
|
||||
val qr = PhoneQr.render(encoded, qrSide)
|
||||
val root = pageColumn()
|
||||
root.addView(text("等待另一台 Android 扫码", 27f, Color.WHITE, true))
|
||||
root.addView(text("$receiverDeviceLabel · ${selectedFiles.size} 个文件", 15f, ACCENT))
|
||||
root.addView(space(12))
|
||||
root.addView(ImageView(this).apply {
|
||||
setImageBitmap(qr)
|
||||
adjustViewBounds = true
|
||||
adjustViewBounds = false
|
||||
scaleType = ImageView.ScaleType.CENTER
|
||||
setBackgroundColor(Color.WHITE)
|
||||
setPadding(dp(8), dp(8), dp(8), dp(8))
|
||||
}, LinearLayout.LayoutParams(-1, -2))
|
||||
}, LinearLayout.LayoutParams(qrSide, qrSide).apply {
|
||||
gravity = Gravity.CENTER_HORIZONTAL
|
||||
})
|
||||
root.addView(space(12))
|
||||
phoneShareStatusText = text(
|
||||
note ?: "QFT 临时网络已就绪;另一台手机扫码后会自动连接并开始接收。",
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.qft.optical
|
||||
|
||||
import kotlin.math.min
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/**
|
||||
* Size a square pairing QR against the *current app window*, not a fixed phone
|
||||
* resolution. The vertical reserve covers page padding, titles, connection
|
||||
* details and the stop button, keeping the whole sender page visible on phones,
|
||||
* tablets and split-window layouts.
|
||||
*/
|
||||
internal fun phoneQrSidePx(windowWidthPx: Int, windowHeightPx: Int, density: Float): Int {
|
||||
val safeDensity = density.takeIf { it.isFinite() && it > 0f } ?: 1f
|
||||
val horizontalPagePadding = (40f * safeDensity).roundToInt()
|
||||
val nonQrVerticalUi = (300f * safeDensity).roundToInt()
|
||||
val widthLimit = (windowWidthPx - horizontalPagePadding).coerceAtLeast(1)
|
||||
val heightLimit = (windowHeightPx - nonQrVerticalUi).coerceAtLeast(1)
|
||||
return min(widthLimit, heightLimit)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.qft.optical
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class PhoneQrLayoutTest {
|
||||
@Test
|
||||
fun phoneUsesWidthWithoutOverflowingTheRemainingHeight() {
|
||||
// 360 × 800 dp at 3× density: width is the limiting dimension.
|
||||
assertEquals(960, phoneQrSidePx(1080, 2400, 3f))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun tabletQrDoesNotExpandToTheFullContentWidth() {
|
||||
// 800 × 1280 dp portrait tablet at 2×: width limits the QR before overflow.
|
||||
val side = phoneQrSidePx(1600, 2560, 2f)
|
||||
assertEquals(1520, side)
|
||||
assertTrue(side + 600 <= 2560)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shortOrSplitWindowPrioritizesNoOverflow() {
|
||||
// 320 × 480 dp at 2×: height becomes the limiting dimension.
|
||||
val side = phoneQrSidePx(640, 960, 2f)
|
||||
assertEquals(360, side)
|
||||
assertTrue(side <= 640)
|
||||
assertTrue(side + 600 <= 960)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun invalidMetricsStillReturnASafePositiveSize() {
|
||||
assertEquals(1, phoneQrSidePx(0, 0, Float.NaN))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user