品牌与配置 - 重命名 Bean Mobile → 浮记(DriftLedger),slug 改为 drift-ledger - 所有 Config Plugin 从 app.json 动态读取 appId,替换硬编码 com.beancount.mobile - 预构建时自动从 constants.ts 同步支付包名列表到 Kotlin 端 - size-optimization 插件新增 NDK 版本强制统一(27.1.12297006) - 添加 app icon(direction_a_feather.png) Decimal 精度改造 - 全面消除 parseFloat 用于金额计算的场景,改用 string decimal 运算 - 新增 divideDecimals(除法)、toDateString(Date→YYYY-MM-DD) - negateDecimal 零值特殊处理,避免产生 "-0" - 年报/月报/预算/信用卡/汇率/图表统计等模块全部迁移 - 去重指纹用 parseDecimal 归一化金额格式 - 转账识别增加 normalizeAmount,支持不同精度格式配对(100 vs 100.00) 架构变更 - mobile.bean → main.bean:移除启动时合并迁移逻辑,直接读写 main.bean - TransactionDraft.sourceEventId → sourceEventIds(数组,支持转账双方) - 新增 buildPostings() 纯函数,从 transactionBuilder 提取复式分录构建逻辑 - 新增 csvUtils.ts(引号感知 CSV 行拆分),statements/adapters 共用 - 新增 accessibilityParser.ts:从 automationPipeline 提取微信/支付宝文本解析 - 新增 floatingBillManager.ts:悬浮窗草稿管理(LRU+超时)与排序数据构建 - 新增 txKeyStore.ts:交易级去重键持久化(原子写入+500条LRU) - ledger.ts 新增 resolveIncludes 选项,支持 include 指令递归解析 - metadataStore CRUD 用 createCrudActions 工厂消除7组实体重复代码 安全与持久化 - PIN 哈希改用带盐 SHA-256,兼容旧版无 salt 数据迁移 - storePersistence 从 persistTo 回调改为 subscribe 自动触发 - 持久化增加深比较跳过无变化写入 + 滞后重试 - 新增 flushPersistence(),App 切后台时立即刷盘 - CrashReporter 新增文件持久化(CrashFs 抽象) - settingsStore hydrate 期间跳过自动持久化(skipPersist 标志) 去重与管道 - batchDedup 改用日期索引 Map,候选查找从 O(n) 降至 O(1) - dedupAgainstHistory 按日期分组,仅扫描时间窗口内候选 - OcrProcessor 从布尔标志改为 Promise 队列,不再丢弃并发请求 - 去重缓存改用 Map<hash,timestamp> LRU,阈值超20%时批量淘汰 - 悬浮球全局开关(floatingBallEnabled) + 原生 bridge API 其他改进 - recurring 计算修复月/年溢出(1月31日+1月→2月28日,闰年2月29日) - 预算进度按 posting 分录金额求和(不再仅过滤负数金额) - recurring.ts 用 Date.parse 本地时间替代 toISOString 的 UTC 偏移 - 信用卡账单日/还款日限制不超过28,防止月份溢出 - 类别模糊匹配增加最小长度2,避免单字符误匹配 - 还款检测排除 refund 方向,避免退款被误判为还款 - WebDAV 同步增加 HTTP 日期格式解析 - Git 合并策略改为以 remote 为基础追加 local 独有行 性能优化 - commonStyles 全局 useMemo 缓存 - Button/Card 的 pressIn/pressOut 改用 useCallback - TransactionCard/AccountTreeNode 包裹 React.memo - PostingEditor 用 refs + 函数式更新避免重建 - CreditCard 页预计算账户余额 Map,避免渲染循环重复遍历 - OCR 处理器单例复用,配置变更时才重建 - getLocalAuth/getAccessibilityBridge 结果缓存 测试 - 新增 decimal/constants/channelConfig/transactionBuilder 单测 - 修复金额格式断言(-24.5→-24.50),匹配字符串精度行为 - billPipeline 新增不同格式金额配对、指纹唯一性测试
251 lines
8.9 KiB
Kotlin
251 lines
8.9 KiB
Kotlin
package com.beancount.mobile.accessibility
|
||
|
||
import com.facebook.react.bridge.ReactApplicationContext
|
||
import com.facebook.react.bridge.ReactContextBaseJavaModule
|
||
import com.facebook.react.bridge.ReactMethod
|
||
import com.facebook.react.bridge.Promise
|
||
import com.facebook.react.bridge.WritableNativeArray
|
||
import com.facebook.react.bridge.WritableNativeMap
|
||
import com.facebook.react.bridge.ReadableArray
|
||
|
||
/**
|
||
* 无障碍服务 RN 桥接模块(plan.md「3.6 无障碍服务」JS 接线)。
|
||
*
|
||
* BillingAccessibilityService 是 AccessibilityService 子类(非 RN 模块),
|
||
* 其方法无法直接从 JS 调用。本模块作为中间层,通过 instance 静态引用
|
||
* 把 JS 调用委托给服务实例。
|
||
*
|
||
* 由 Config Plugin 的 withMainApplication 注入 add(AccessibilityBridgePackage())。
|
||
*/
|
||
class AccessibilityBridgeModule(private val reactContext: ReactApplicationContext) :
|
||
ReactContextBaseJavaModule(reactContext) {
|
||
|
||
init {
|
||
ReactContextHolder.context = reactContext
|
||
}
|
||
|
||
override fun invalidate() {
|
||
super.invalidate()
|
||
if (ReactContextHolder.context === reactContext) {
|
||
ReactContextHolder.context = null
|
||
}
|
||
}
|
||
|
||
override fun getName() = "AccessibilityBridge"
|
||
|
||
/** 无障碍服务是否已连接(用户已在系统设置中启用)。 */
|
||
@ReactMethod
|
||
fun isServiceRunning(promise: Promise) {
|
||
promise.resolve(BillingAccessibilityService.instance != null)
|
||
}
|
||
|
||
/**
|
||
* 记住当前页面:把当前顶部 App 的 pkg|activity 加入白名单,
|
||
* 之后该页面内容变化时自动截图 → OCR。
|
||
*/
|
||
@ReactMethod
|
||
fun rememberCurrentPage(promise: Promise) {
|
||
val service = BillingAccessibilityService.instance
|
||
if (service == null) {
|
||
promise.reject("SERVICE_NOT_RUNNING", "无障碍服务未启用")
|
||
return
|
||
}
|
||
val pkg = service.getTopPackage()
|
||
if (pkg == null) {
|
||
promise.reject("NO_TOP_PACKAGE", "当前没有检测到前台 App")
|
||
return
|
||
}
|
||
service.rememberCurrentPage()
|
||
val result = WritableNativeMap()
|
||
result.putString("package", pkg)
|
||
result.putString("activity", service.getTopActivity() ?: "")
|
||
result.putString("signature", "$pkg|${service.getTopActivity() ?: ""}")
|
||
promise.resolve(result)
|
||
}
|
||
|
||
/** 手动触发一次 OCR(截取当前屏幕并发送给 JS 层处理)。 */
|
||
@ReactMethod
|
||
fun triggerManualOcr(promise: Promise) {
|
||
val service = BillingAccessibilityService.instance
|
||
if (service == null) {
|
||
promise.reject("SERVICE_NOT_RUNNING", "无障碍服务未启用")
|
||
return
|
||
}
|
||
try {
|
||
service.triggerManualOcr()
|
||
promise.resolve(true)
|
||
} catch (e: Exception) {
|
||
promise.reject("OCR_TRIGGER_FAIL", e.message)
|
||
}
|
||
}
|
||
|
||
/** 获取所有已记住的页面签名列表。 */
|
||
@ReactMethod
|
||
fun getPageSignatures(promise: Promise) {
|
||
val service = BillingAccessibilityService.instance
|
||
val sigsSet = if (service != null) {
|
||
service.getPageSignatures()
|
||
} else {
|
||
try {
|
||
val prefs = reactContext.getSharedPreferences("billing_accessibility_prefs", android.content.Context.MODE_PRIVATE)
|
||
prefs.getStringSet("page_signatures", emptySet()) ?: emptySet()
|
||
} catch (e: Exception) {
|
||
emptySet()
|
||
}
|
||
}
|
||
val arr = WritableNativeArray()
|
||
for (sig in sigsSet) {
|
||
val parts = sig.split("|", limit = 2)
|
||
val map = WritableNativeMap()
|
||
map.putString("signature", sig)
|
||
map.putString("package", parts.getOrNull(0) ?: "")
|
||
map.putString("activity", parts.getOrNull(1) ?: "")
|
||
arr.pushMap(map)
|
||
}
|
||
promise.resolve(arr)
|
||
}
|
||
|
||
/** 清空所有已记住的页面签名。 */
|
||
@ReactMethod
|
||
fun clearPageSignatures(promise: Promise) {
|
||
val service = BillingAccessibilityService.instance
|
||
if (service != null) {
|
||
service.clearPageSignatures()
|
||
} else {
|
||
try {
|
||
val prefs = reactContext.getSharedPreferences("billing_accessibility_prefs", android.content.Context.MODE_PRIVATE)
|
||
prefs.edit().putStringSet("page_signatures", emptySet()).apply()
|
||
} catch (e: Exception) {
|
||
promise.reject("CLEAR_PREFS_FAIL", e.message)
|
||
return
|
||
}
|
||
}
|
||
promise.resolve(true)
|
||
}
|
||
|
||
/** 删除指定页面签名。 */
|
||
@ReactMethod
|
||
fun removePageSignature(signature: String, promise: Promise) {
|
||
val service = BillingAccessibilityService.instance
|
||
if (service != null) {
|
||
service.removePageSignature(signature)
|
||
} else {
|
||
try {
|
||
val prefs = reactContext.getSharedPreferences("billing_accessibility_prefs", android.content.Context.MODE_PRIVATE)
|
||
val saved = prefs.getStringSet("page_signatures", emptySet()) ?: emptySet()
|
||
val mutable = HashSet(saved)
|
||
if (mutable.remove(signature)) {
|
||
prefs.edit().putStringSet("page_signatures", mutable).apply()
|
||
}
|
||
} catch (e: Exception) {
|
||
promise.reject("REMOVE_PREFS_FAIL", e.message)
|
||
return
|
||
}
|
||
}
|
||
promise.resolve(true)
|
||
}
|
||
|
||
/** 获取支付 App 白名单(供 JS 端展示)。 */
|
||
@ReactMethod
|
||
fun getPaymentPackages(promise: Promise) {
|
||
val arr = WritableNativeArray()
|
||
for (pkg in BillingAccessibilityService.PAYMENT_PACKAGES) {
|
||
arr.pushString(pkg)
|
||
}
|
||
promise.resolve(arr)
|
||
}
|
||
|
||
/** 获取当前顶部 App 信息(供 JS 判断用户是否在支付页面)。 */
|
||
@ReactMethod
|
||
fun getTopApp(promise: Promise) {
|
||
val service = BillingAccessibilityService.instance
|
||
if (service == null) {
|
||
promise.reject("SERVICE_NOT_RUNNING", "无障碍服务未启用")
|
||
return
|
||
}
|
||
val map = WritableNativeMap()
|
||
map.putString("package", service.getTopPackage() ?: "")
|
||
map.putString("activity", service.getTopActivity() ?: "")
|
||
promise.resolve(map)
|
||
}
|
||
|
||
/** 将当前应用拉起至前台,用以在后台识别出账单后,弹窗让用户进行交易确认 */
|
||
@ReactMethod
|
||
fun bringAppToForeground(promise: Promise) {
|
||
val service = BillingAccessibilityService.instance
|
||
if (service == null) {
|
||
promise.reject("SERVICE_NOT_RUNNING", "无障碍服务未启用")
|
||
return
|
||
}
|
||
try {
|
||
service.bringAppToForeground()
|
||
promise.resolve(true)
|
||
} catch (e: Exception) {
|
||
promise.reject("FAIL", e.message)
|
||
}
|
||
}
|
||
|
||
/** 显示账单浮窗(直接在当前其他应用上方渲染,不返回 App 内) */
|
||
@ReactMethod
|
||
fun showFloatingBill(
|
||
amount: String,
|
||
merchant: String,
|
||
time: String,
|
||
packageName: String,
|
||
categories: ReadableArray,
|
||
accounts: ReadableArray,
|
||
direction: String,
|
||
draftId: String,
|
||
promise: Promise
|
||
) {
|
||
val context = reactContext.currentActivity ?: BillingAccessibilityService.instance
|
||
if (context == null) {
|
||
promise.reject("NO_CONTEXT", "无法获取当前前台 Activity 或 AccessibilityService 实例")
|
||
return
|
||
}
|
||
|
||
val categoryList = mutableListOf<Map<String, String>>()
|
||
for (i in 0 until categories.size()) {
|
||
val map = categories.getMap(i)
|
||
categoryList.add(mapOf(
|
||
"id" to (map?.getString("id") ?: ""),
|
||
"name" to (map?.getString("name") ?: ""),
|
||
"account" to (map?.getString("account") ?: ""),
|
||
"type" to (map?.getString("type") ?: "")
|
||
))
|
||
}
|
||
|
||
val accountList = mutableListOf<String>()
|
||
for (i in 0 until accounts.size()) {
|
||
accountList.add(accounts.getString(i) ?: "")
|
||
}
|
||
|
||
android.os.Handler(android.os.Looper.getMainLooper()).post {
|
||
try {
|
||
val floatingView = FloatingBillView(context, draftId, amount, merchant, time, packageName, categoryList, accountList, direction)
|
||
floatingView.show()
|
||
promise.resolve(true)
|
||
} catch (e: Exception) {
|
||
promise.reject("FAIL", e.message)
|
||
}
|
||
}
|
||
}
|
||
|
||
/** 设置悬浮球开关状态。 */
|
||
@ReactMethod
|
||
fun setFloatingBallEnabled(enabled: Boolean, promise: Promise) {
|
||
BillingAccessibilityService.floatingBallEnabled = enabled
|
||
val service = BillingAccessibilityService.instance
|
||
if (service == null) {
|
||
promise.resolve(true)
|
||
return
|
||
}
|
||
if (!enabled) {
|
||
service.dismissFloatingHelper()
|
||
} else {
|
||
service.refreshFloatingHelper()
|
||
}
|
||
promise.resolve(true)
|
||
}
|
||
}
|