From 04a2d7fb1050fa3fc57ecca54738e2f2b5896afc Mon Sep 17 00:00:00 2001 From: zhy200308 <2258391712@qq.com> Date: Wed, 12 Aug 2026 16:12:26 +0800 Subject: [PATCH] fix: adapt phone share QR to window size --- README.md | 3 +- android/app/build.gradle.kts | 4 +-- .../main/java/com/qft/optical/MainActivity.kt | 20 ++++++++--- .../java/com/qft/optical/PhoneQrLayout.kt | 19 ++++++++++ .../java/com/qft/optical/PhoneQrLayoutTest.kt | 35 +++++++++++++++++++ docs/ANDROID_PHONE_TRANSFER.md | 10 ++++-- docs/PROTOCOL.md | 2 +- scripts/android_compat_selftest.py | 3 ++ scripts/phone_share_selftest.py | 4 +++ 9 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 android/app/src/main/java/com/qft/optical/PhoneQrLayout.kt create mode 100644 android/app/src/test/java/com/qft/optical/PhoneQrLayoutTest.kt diff --git a/README.md b/README.md index 5f0d530..e16cd22 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ v0.4.16 提供两套互不依赖的传输方式: > v0.4.16 已完成协议、图像合成解码、ROI、双向并发传输、专用 Wi-Fi 快速发现、队列拖动稳定性和关键 Kotlin 源码测试。当前生成环境没有真实 Windows 显示器、Android 相机或 Wi‑Fi Direct 适配器,因此物理 30/45/60 FPS 和真实扫码配网仍需在你的设备上跑 `VERIFY_WINDOWS_HOST.ps1` 后做最终验收;详见 `TEST_REPORT.md`。 -## v0.5.0-dev1:Android → Android 手机互传 +## v0.5.0-dev2:Android → Android 手机互传 当前开发版新增 Android 手机之间的扫码直传闭环: @@ -26,6 +26,7 @@ v0.4.16 提供两套互不依赖的传输方式: - 二维码只携带一次性会话和网络身份,文件队列通过会话认证后读取,避免多文件让二维码过密; - 每个文件复用现有最高 8 路 HTTP Range、断点位图和 SHA-256 校验,按发送队列顺序保存到 `Downloads/QFT`; - 发送页面必须保持打开,返回首页会立即关闭发送服务和临时热点。 +- 发送二维码按当前应用窗口的实际可用宽高动态生成并居中,手机、平板和分屏窗口都不会再被 `match_parent` 放大溢出。 此阶段只实现 Android → Android;Android ↔ iOS 尚未接入。 diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 2543914..dd1a542 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -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" } diff --git a/android/app/src/main/java/com/qft/optical/MainActivity.kt b/android/app/src/main/java/com/qft/optical/MainActivity.kt index a4ed96c..118d640 100644 --- a/android/app/src/main/java/com/qft/optical/MainActivity.kt +++ b/android/app/src/main/java/com/qft/optical/MainActivity.kt @@ -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(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 临时网络已就绪;另一台手机扫码后会自动连接并开始接收。", diff --git a/android/app/src/main/java/com/qft/optical/PhoneQrLayout.kt b/android/app/src/main/java/com/qft/optical/PhoneQrLayout.kt new file mode 100644 index 0000000..77374bb --- /dev/null +++ b/android/app/src/main/java/com/qft/optical/PhoneQrLayout.kt @@ -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) +} diff --git a/android/app/src/test/java/com/qft/optical/PhoneQrLayoutTest.kt b/android/app/src/test/java/com/qft/optical/PhoneQrLayoutTest.kt new file mode 100644 index 0000000..ef02621 --- /dev/null +++ b/android/app/src/test/java/com/qft/optical/PhoneQrLayoutTest.kt @@ -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)) + } +} diff --git a/docs/ANDROID_PHONE_TRANSFER.md b/docs/ANDROID_PHONE_TRANSFER.md index 372a2f8..84e838a 100644 --- a/docs/ANDROID_PHONE_TRANSFER.md +++ b/docs/ANDROID_PHONE_TRANSFER.md @@ -1,4 +1,4 @@ -# Android → Android 真机验收(v0.5.0-dev1) +# Android → Android 真机验收(v0.5.0-dev2) ## 构建约束 @@ -11,7 +11,7 @@ cd android ## 首轮验收 -1. 两台 Android 安装同一个 `v0.5.0-dev1` APK。 +1. 两台 Android 安装同一个 `v0.5.0-dev2` APK。 2. 发送手机进入“手机 → 手机”,选择至少两个文件并调整一次顺序。 3. 点击“创建发送二维码”,允许附近 Wi-Fi/位置权限。 4. 接收手机进入“扫码直传”,扫描发送手机二维码并确认系统连接提示。 @@ -27,6 +27,12 @@ cd android - 手动停止发送端热点:接收端应报连接中断,而不是无限等待。 - 传输中返回发送端首页:服务和临时热点应立即关闭。 +## 二维码布局组合 + +- 普通手机:二维码、状态、网络信息和停止按钮应在首屏完整显示。 +- 10–14 英寸平板:二维码按窗口宽高取较小限制,不应拉伸到全宽后挤出下方按钮。 +- 分屏/小窗:重新进入发送二维码页后,应按当前应用窗口而非物理屏幕分辨率计算尺寸。 + ## 当前边界 - 仅文件,不包含文件夹。 diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index 7d0d706..258548a 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -99,7 +99,7 @@ The sender returns `206 Partial Content`, the matching `Content-Range`, and the For both direct-transfer directions, the Windows listener may accept connections non-blockingly for shutdown responsiveness, but each accepted file socket is switched to blocking I/O with explicit timeouts before reading or writing request bodies. This prevents `WSAEWOULDBLOCK (10035)` from turning an in-progress parallel transfer into an HTTP 500 failure. -## 6. Android → Android (`QFTPHONE2`, v0.5.0-dev1) +## 6. Android → Android (`QFTPHONE2`, v0.5.0-dev2) The sender starts an authenticated local HTTP service and encodes only session, port, sender identity and optional LocalOnlyHotspot credentials in the QR. The diff --git a/scripts/android_compat_selftest.py b/scripts/android_compat_selftest.py index ac94560..3a1d3a4 100644 --- a/scripts/android_compat_selftest.py +++ b/scripts/android_compat_selftest.py @@ -25,6 +25,9 @@ assert 'QFTPHONE2:' in (ROOT/'android/app/src/main/java/com/qft/optical/PairingP assert 'startLocalOnlyHotspot' in phone_hotspot and 'PhoneShareServer.create' in main assert '/qft/phone/manifest' in phone_server and '/qft/phone/download/' in phone_server assert 'startDownloadsFromPhone' in main and 'downloadPath = item.path' in main +assert 'phoneQrSidePx(windowWidth, windowHeight' in main +assert 'LinearLayout.LayoutParams(qrSide, qrSide)' in main +assert 'PhoneQr.render(encoded, 720)' not in main assert 'if (mode == ScannerMode.PAIR) Size(960, 720) else Size(640, 480)' in main and 'Size(960, 540)' not in main assert 'Camera2Interop.Extender' in main and 'CONTROL_AE_TARGET_FPS_RANGE' in main and 'STRATEGY_KEEP_ONLY_LATEST' in main assert '设备拒绝显式高帧率请求' in main and 'fallbackAnalysis' in main diff --git a/scripts/phone_share_selftest.py b/scripts/phone_share_selftest.py index 9c4dd83..7360119 100644 --- a/scripts/phone_share_selftest.py +++ b/scripts/phone_share_selftest.py @@ -9,6 +9,7 @@ ROOT = Path(__file__).resolve().parents[1] pairing = (ROOT / "android/app/src/main/java/com/qft/optical/PairingProtocol.kt").read_text() server = (ROOT / "android/app/src/main/java/com/qft/optical/PhoneShareServer.kt").read_text() download = (ROOT / "android/app/src/main/java/com/qft/optical/DownloadClient.kt").read_text() +layout = (ROOT / "android/app/src/main/java/com/qft/optical/PhoneQrLayout.kt").read_text() session = base64.urlsafe_b64encode(bytes(range(24))).decode().rstrip("=") payload = { @@ -47,4 +48,7 @@ assert 'headers["x-qft-token"] != session' in server assert 'Range: bytes=-' not in server # implementation, not documentation placeholder assert 'baseUrl + pairing.downloadPath' in download assert 'digest(downloadPath.toByteArray' in download +assert 'windowWidthPx - horizontalPagePadding' in layout +assert 'windowHeightPx - nonQrVerticalUi' in layout +assert 'min(widthLimit, heightLimit)' in layout print(f"PASS Android phone share: compact QR={len(qr)} chars, authenticated fixed manifest paths, per-file partial isolation, SHA-256={sha[:12]}…")