Files
ShortX_ToolHub/code/th_02_core.js
linshenjianlu 9ad01b436d fix: 代码审查6项修复
- 689处空catch块补全日志
- eval远程代码增加SHA256校验
- 删除ToolHubLogger重复定义
- getParentFile()增加null保护
- 提取buildButtonEditorPanelView内通用工具函数到文件级
- 修复HandlerThread/ValueAnimator资源泄漏
2026-04-21 07:42:23 +08:00

124 lines
3.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// @version 1.0.0
function FloatBallAppWM(logger) {
this.L = logger || null;
// # 加载配置
this.config = ConfigManager.loadSettings();
this.currentPanelKey = "main";
this.panels = { main: ConfigManager.loadButtons() };
// # 更新 Logger 配置(因为 Logger 初始化时是默认值)
if (this.L) this.L.updateConfig(this.config);
this.state = {
receivers: [], // 存储广播接收器引用,用于 close 时注销
wm: null,
dm: null,
density: 1.0,
screen: { w: 0, h: 0 },
lastRotation: -1,
lastMonitorTs: 0,
ht: null,
h: null,
addedBall: false,
addedPanel: false,
addedSettings: false,
addedViewer: false,
addedMask: false,
ballRoot: null,
ballContent: null,
ballLp: null,
panel: null,
panelLp: null,
settingsPanel: null,
settingsPanelLp: null,
viewerPanel: null,
viewerPanelLp: null,
mask: null,
maskLp: null,
loadedPos: null,
lastSaveTs: 0,
dragging: false,
rawX: 0,
rawY: 0,
downX: 0,
downY: 0,
docked: false,
dockSide: null,
lastMotionTs: 0,
idleDockRunnable: null,
longPressArmed: false,
longPressTriggered: false,
longPressRunnable: null,
displayListener: null,
// # 设置面板:临时编辑缓存
pendingUserCfg: null,
pendingDirty: false,
closing: false
};
// # 创建实例独立的 UI 工具对象,避免多实例共享颜色状态
this.ui = {};
var protoUi = FloatBallAppWM.prototype.ui;
for (var _uiKey in protoUi) {
this.ui[_uiKey] = protoUi[_uiKey];
}
this.ui.colors = {};
// # 初始化莫奈动态配色(传入当前主题避免重复检测)
try { this.refreshMonetColors(this.isDarkTheme()); } catch(eM) { safeLog(null, 'e', "catch " + String(eM)); }
}
// =======================【工具dp/now/clamp】======================
FloatBallAppWM.prototype.dp = function(v) { return Math.floor(Number(v) * this.state.density); };
FloatBallAppWM.prototype.sp = function(v) { try { return Math.floor(Number(v) * context.getResources().getDisplayMetrics().scaledDensity); } catch (e) { return Math.floor(Number(v) * this.state.density); } };
FloatBallAppWM.prototype.now = function() { return new Date().getTime(); };
FloatBallAppWM.prototype.clamp = function(v, min, max) { if (v < min) return min; if (v > max) return max; return v; };
FloatBallAppWM.prototype.rectIntersect = function(ax, ay, aw, ah, bx, by, bw, bh) {
return !(ax + aw <= bx || bx + bw <= ax || ay + ah <= by || by + bh <= ay);
};
// # 这段代码的主要内容/用途:安全地在 UI 线程执行代码(用于后台线程回调更新 UI
FloatBallAppWM.prototype.runOnUiThreadSafe = function(fn) {
try {
if (!fn) return;
var self = this;
// 优先使用 Activity 的 runOnUiThread否则使用 View.post
if (this.state && this.state.ballRoot) {
this.state.ballRoot.post(new java.lang.Runnable({
run: function() { try { fn.call(self); } catch(e) { safeLog(null, 'e', "catch " + String(e)); } }
}));
} else {
// 兜底:直接执行(如果已经在 UI 线程)或尝试使用 Handler
try {
var h = new android.os.Handler(android.os.Looper.getMainLooper());
h.post(new java.lang.Runnable({
run: function() { try { fn.call(self); } catch(e) { safeLog(null, 'e', "catch " + String(e)); } }
}));
} catch(e) {
// 最后兜底:直接执行
try { fn.call(self); } catch(e2) { safeLog(null, 'e', "catch " + String(e2)); }
}
}
} catch(e) { safeLog(null, 'e', "catch " + String(e)); }
};
// # 这段代码的主要内容/用途统一图标缓存LRU减少反复解码/反复走 PackageManager降低卡顿与内存波动不改变 UI 与功能)
// 优化后的图标缓存(带 Bitmap 回收,防止内存泄漏)