仰望星辰工作室

starmusic-mobile

starmusic-mobile/ common/tv.js 3.9 KB · 105 行 原始文件
1/**
2 * 设备类型检测模块
3 *
4 * 统一识别三类设备,供电视端 / 车机端能力门控使用:
5 * - phone:手机 / 平板(触摸为主,保持现有交互,零改动)
6 * - tv :电视 / 盒子 / 投影(遥控器 D-pad 为主)
7 * - car :车机(触摸 + 物理按键混合)
8 *
9 * 门控约定:所有电视/车机专属能力都必须经过 isRemote() / getDeviceType() 判断,
10 * 手机端永远走原逻辑,不被任何新代码影响。
11 */
12
13/**
14 * 识别设备类型(结果缓存)。
15 * App 端判定依赖 plus.android 反射(runtimeMainActivity / importClass 等),
16 * 单次就是几十毫秒的原生桥往返,此前每个页面 onLoad、每个 tv-nav 挂载都重新判一遍,
17 * 属于纯浪费;设备类型在一次运行内不会变化,缓存后全局只判一次。
18 */
19let deviceTypeCache = null
20
21export function getDeviceType() {
22 if (deviceTypeCache !== null) return deviceTypeCache
23 deviceTypeCache = detectDeviceType()
24 return deviceTypeCache
25}
26
27const detectDeviceType = () => {
28 // #ifdef APP-PLUS
29 // iOS 无 plus.android,直接跳过(否则会抛出并打出噪声日志)
30 if (!plus.os || plus.os.name !== 'Android') return 'phone'
31 // App 端:用 Android 系统特性判断最可靠(AOSP 标准,同时覆盖电视与车机)
32 try {
33 const main = plus.android.runtimeMainActivity()
34 const pm = main.getPackageManager()
35 if (pm) {
36 // 注意:由方法返回值拿到的原生对象必须先 importClass 才能调用其方法,
37 // 否则会抛异常(旧实现把异常静默吞掉,车机被误判为 phone,整套适配失效)
38 plus.android.importClass(pm)
39 if (pm.hasSystemFeature('android.hardware.type.television')) return 'tv'
40 if (pm.hasSystemFeature('android.hardware.type.automotive')) return 'car'
41 }
42 } catch (e) {
43 console.warn('[tv] systemFeature 检测失败', e)
44 }
45 // 兜底:UI 模式为 tv/automotive 也能命中(个别品牌盒子的 feature 声明不完整)
46 try {
47 const mainActivity = plus.android.runtimeMainActivity()
48 const mgr = mainActivity.getSystemService('uimode')
49 if (mgr) {
50 plus.android.importClass(mgr)
51 const type = mgr.getCurrentModeType()
52 if (type === 4) return 'tv' // UI_MODE_TYPE_TELEVISION
53 if (type === 3) return 'car' // UI_MODE_TYPE_AUTOMOTIVE
54 }
55 } catch (e) {
56 console.warn('[tv] UiModeManager 检测失败', e)
57 }
58 return 'phone'
59 // #endif
60 // #ifdef H5
61 // H5:命中电视/车机 UA 才算;日常浏览器都归 phone
62 const ua = String(navigator.userAgent || '')
63 if (/AndroidAuto|AndroidAutomotive|Automotive/i.test(ua)) return 'car'
64 if (/Android TV|SmartTV|Smart-TV|BRAVIA|Web0S|Vizio|Hisense|RokuTV|FireTV|MiTV|NetCast/i.test(ua)) {
65 return 'tv'
66 }
67 // Firefox 之外的电视浏览器 UA 可能不含关键词,再用"无触摸 + 大屏"兜底。
68 // 但桌面浏览器同样无触摸且屏幕 ≥1280,必须先排除,否则普通电脑也会被判成电视端。
69 const isDesktopBrowser = /Windows NT|Macintosh|X11|CrOS/i.test(ua)
70 if (!isDesktopBrowser && typeof window !== 'undefined' && !('ontouchstart' in window) && screen.width >= 1280) {
71 return 'tv'
72 }
73 return 'phone'
74 // #endif
75 // #ifndef APP-PLUS
76 return 'phone'
77 // #endif
78}
79
80/** 是否遥控设备(电视或车机)。true 时启用焦点导航等遥控能力 */
81export function isRemote() {
82 const t = getDeviceType()
83 return t === 'tv' || t === 'car'
84}
85
86/** 是否电视(区别于车机,个别 UI 缩放策略可能不同) */
87export function isTV() {
88 return getDeviceType() === 'tv'
89}
90
91/** 是否车机 */
92export function isCar() {
93 return getDeviceType() === 'car'
94}
95
96/**
97 * 设备类型 → 页根容器挂载的 class。
98 * 页面拿到后加到根元素,全局 CSS 据此做放大 / 安全区 / 焦点态适配。
99 */
100export function deviceClass() {
101 const t = getDeviceType()
102 if (t === 'car') return 'tv-ui car-ui'
103 if (t === 'tv') return 'tv-ui'
104 return ''
105}