Compare commits
16 Commits
v1.9.2
...
76c316783a
| Author | SHA1 | Date | |
|---|---|---|---|
| 76c316783a | |||
| c2fba02048 | |||
| bd15424c5f | |||
| 610ac094ed | |||
| f364ae55d3 | |||
| 036b4d3201 | |||
| 392620e0f1 | |||
| 561d0460e3 | |||
| 15e27db04a | |||
| d1afbd2425 | |||
| c7efd8d2be | |||
| e99da175b5 | |||
| 0bf759ae64 | |||
| 3ef69bc56f | |||
| ab113e5691 | |||
| bcc7ed298f |
@@ -8,6 +8,10 @@ app/build/
|
||||
local.properties
|
||||
.do-session
|
||||
|
||||
# Secrets — never commit
|
||||
gh-token.txt
|
||||
*.token
|
||||
|
||||
# Android Studio / IntelliJ
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
@@ -55,31 +55,41 @@ adb install -r app/build/outputs/apk/release/app-release.apk
|
||||
|
||||
```
|
||||
NotifyListenerService (系统通知入口)
|
||||
→ NotificationProcessor.process(sbn) → Decision(message, reason, code)
|
||||
→ BarkClient.push(msg) → OkHttp POST
|
||||
→ LogStore.addLog(...)
|
||||
失败 → 加入 pending → RetryWorker (WorkManager, 15min 周期)
|
||||
→ NotificationProcessor.process(sbn) → Decision(message?, reason, code, silent)
|
||||
├─ 在线:BarkClient.push(msg) → OkHttp POST;失败入 pending → RetryWorker (15min 周期)
|
||||
└─ 离线:紧要通知(验证码/来电) → SmsFallbackForwarder 走短信兜底
|
||||
→ LogStore.addLog(...)(silent 的静默过滤不写日志)
|
||||
|
||||
旁路(不经通知监听器):
|
||||
PhoneCallMonitor 轮询 getCallState() → 来电/未接构造 Decision 推送(绕过 MIUI 限制)
|
||||
UpdateChecker 查 GitHub Releases → 新版本横幅 → 应用内下载 + 系统安装器
|
||||
```
|
||||
|
||||
### 包结构 `com.a2i.forwarder`
|
||||
|
||||
| 文件 | 职责 |
|
||||
|---|---|
|
||||
| `A2iApp.kt` | `Application` 单例;持有全局 `appScope` 和三个 Store(`settings`、`appRules`、`logStore`)。Store 都用 `MutableStateFlow`,UI 通过 `collectAsState()` 订阅。 |
|
||||
| `A2iApp.kt` | `Application` 单例;持有全局 `appScope`(`SupervisorJob+Dispatchers.Default`)与全局组件:三个 Store(`settings`/`appRules`/`logStore`)+ 监听类(`phoneCallMonitor`/`connectivityMonitor`/`carrierBalanceQuery`/`smsForwarder`/`updateChecker`)。全部经 `A2iApp.instance.xxx` 访问,`onCreate` 构造并 `start()`。UI 通过 `collectAsState()` 订阅 Store 的 `StateFlow`。 |
|
||||
| `service/NotifyListenerService.kt` | `NotificationListenerService` 实现;每条通知调 `processor.process()`,过滤后推送。 |
|
||||
| `core/NotificationProcessor.kt` | **核心决策点**。包含 LRU 去重缓存(5s)、脱敏检测(`*****` → 回退到 `tickerText`)、系统状态消息过滤、广告过滤、验证码提取、特殊 App 解析。返回 `Decision(message?, reason, appLabel, code)`。 |
|
||||
| `core/CodeExtractor.kt` | 三段正则:关键词+数字、数字+关键词、纯数字 4-8 位。检测到全 `*****` 时直接返回 null。 |
|
||||
| `core/AdFilter.kt` | 内置营销关键词 + 用户自定义关键词(`settings.adKeywords`)。 |
|
||||
| `core/AppParsers.kt` | 微信/QQ/Telegram 的 `clickUrl` 映射 + 重要消息关键词(@我、验证码、通话等)。 |
|
||||
| `core/BarkClient.kt` | OkHttp POST 到 `${server}/push`,超时 10/10/15s。 |
|
||||
| `core/BarkClient.kt` | OkHttp POST 到 `${server}/push`,超时 10/10/15s;对 connection closed / 超时 / TLS 中断等瞬时 IOException 自动重试 3 次(退避 600/1200ms),HTTP 4xx/5xx 不重试。 |
|
||||
| `core/RetryWorker.kt` | WorkManager 周期任务,每 15 分钟重发 `pending` 队列中的失败消息。 |
|
||||
| `core/LogStore.kt` | 最近 300 条日志 + 失败重发队列(≤100)。DataStore 持久化。 |
|
||||
| `core/IconExporter.kt` | 一键导出所有已安装 App 图标到用户选择的 SAF 目录(用于部署到 iOS Bark 图床)。 |
|
||||
| `core/PhoneCallMonitor.kt` | 轮询 `TelephonyManager.getCallState()`(800ms)感知来电/未接来电,构造 Decision 推送。绕过 MIUI 不向第三方监听器分发系统电话通知的限制。 |
|
||||
| `core/ConnectivityMonitor.kt` | `ConnectivityManager` + `NetworkCallback`,暴露 `isOnline: StateFlow<Boolean>`,供离线兜底判断。 |
|
||||
| `core/CarrierDetector.kt` | SIM MCC+MNC 映射到运营商(移动/联通/电信/广电),供短信兜底发免费查询码。 |
|
||||
| `core/CarrierBalanceQuery.kt` | 向运营商服务号发查询短信、解析回执余量、余额≤5 自动挂起。 |
|
||||
| `core/SmsFallbackForwarder.kt` | 断网时把紧要通知(验证码/来电)用短信发到 iPhone,5 分钟限频 + 余额检查。 |
|
||||
| `core/UpdateChecker.kt` | 查 GitHub Releases 最新版(24h 节流)→ 语义比较 → 下载 APK → FileProvider 调起系统安装器。 |
|
||||
| `store/SettingsStore.kt` | Bark 服务器列表、全局开关、广告关键词等。`MutableStateFlow` + DataStore。 |
|
||||
| `store/AppRulesStore.kt` | 黑/白名单模式 + 默认黑名单(系统 App + 三星剪贴板/键盘 + GMS)。 |
|
||||
| `store/AppRulesStore.kt` | 黑/白名单模式 + 默认黑名单(系统 App + 输入法 + 三星剪贴板/键盘 + GMS + 录音机/相册/查找/密码管理 + 通话管理/AI通话);默认黑名单带版本号 `bl_version`,升级时把新增噪音包合并进老用户黑名单(只增不删)。 |
|
||||
| `ui/nav/AppNav.kt` | 4 个底部 Tab + 日志页。 |
|
||||
| `ui/theme/*` | 固定品牌配色(`Color.kt`)、自定义 `Typography`(`Type.kt`),`A2iTheme` 关闭动态取色、统一深浅色方案(`Theme.kt`)。 |
|
||||
| `ui/screens/*.kt` | HomeScreen(品牌横幅 + 运行状态面板 + 统计指标)、AppManageScreen、FilterScreen、SettingsScreen、LogScreen。 |
|
||||
| `ui/screens/*.kt` | HomeScreen(品牌横幅 + 运行状态 + 统计 + 更新横幅 + 短信挂起提醒)、AppManageScreen、FilterScreen、SettingsScreen(Bark 服务 / 应用图标 / 断网短信兜底 / 关于与更新)、LogScreen、UpdateDialog(应用内更新弹窗)。 |
|
||||
| `ui/Components.kt` | 共享 UI 组件层:`ScreenTitle`、`SectionCard`、`SwitchRow`、`ClickRow`、`IconBox`、`StatusBadge`、`MetricBlock`,以及 `appVersionName(context)`、`drawableToImageBitmap(d)`、`isNotificationListenerEnabled(context)` 工具函数。 |
|
||||
|
||||
### 重要约定
|
||||
@@ -87,9 +97,9 @@ NotifyListenerService (系统通知入口)
|
||||
- **Store 全局单例**:所有 Store 通过 `A2iApp.instance.xxx` 访问。**不要**在 Compose 中创建新的 Store 实例。
|
||||
- **协程作用域**:跨屏调用用 `app.appScope.launch { ... }`(Application 级别的 `SupervisorJob + Dispatchers.Default`)。
|
||||
- **过滤流水线**(顺序很重要,在 `NotificationProcessor.process` 中):
|
||||
1. 全局开关 → 2. ongoing 过滤 → 3. App 黑/白名单 → 4. 空内容/脱敏 → 5. 噪音包名 → 6. 系统状态消息 → 7. 近空内容 → 8. 广告过滤 → **9. 去重检查(5s 窗口)** → 10. 验证码提取 → 11. 特殊 App 解析 → 12. 构造 BarkMessage
|
||||
1. 全局开关 → 2. ongoing 过滤(静默)→ 3. App 黑/白名单 → 4. 空内容/脱敏 → 5. 噪音包名 → 6. 系统状态消息 → 7. 近空内容 → 8. 广告过滤 → 9. 已发短信(仅收不发)→ 10. 运营商余额回执拦截 → **11. 去重检查(5s 窗口)** → 12. 验证码提取 → 13. 特殊 App 解析 → 14. 构造 BarkMessage
|
||||
- **Bark API** 字段映射在 `BarkMessage.kt`(`@SerialName`):`device_key`/`autoCopy`/`isArchive` 用下划线/驼峰别名,其他用蛇形。
|
||||
- **版本号**:`app/build.gradle.kts` 的 `versionCode`/`versionName`,运行时通过 `appVersionName(context)` 读取,首页底部居中显示 `v{version}`。改完代码记得同步递增。
|
||||
- **版本号**:`app/build.gradle.kts` 的 `versionCode`/`versionName`,运行时通过 `appVersionName(context)` 读取,首页底部居中显示 `v{version}`。**改完代码同步递增 versionCode/versionName,并更新本文档「版本与发布」一节的当前版本。**
|
||||
|
||||
### 通知监听授权
|
||||
|
||||
@@ -101,8 +111,11 @@ NotifyListenerService (系统通知入口)
|
||||
|
||||
## 版本与发布
|
||||
|
||||
- 当前版本:`versionCode = 3`、`versionName = "1.2.0"`(位于 `app/build.gradle.kts`)。改完代码记得同步递增。
|
||||
- 当前版本:`versionCode = 9`、`versionName = "1.5.1"`(位于 `app/build.gradle.kts`)。**每次改动后同步递增 versionCode/versionName,并同步更新本节。**
|
||||
- 远端仓库:`https://github.com/lsxf/a2i.git`,默认分支 `main`。
|
||||
- 提交策略:`.gitignore` 已排除 `app/build`、`.gradle`、`.kotlin`、`local.properties`、`.claude/`、`do.ps1`、截图与临时文件,**不要**提交它们。
|
||||
- 发版流程:本地打 tag(`git tag -a v1.2.0 -m "..."`)→ `git push origin v1.2.0` → 在 GitHub Releases 基于 tag 发布,并上传 `app-release.apk` 作为二进制附件。
|
||||
- 提交策略:`.gitignore` 已排除 `app/build`、`.gradle`、`.kotlin`、`local.properties`、`.claude/`、`do.ps1`、`gh-token.txt`(发版用 token)、`*.token`、截图与临时文件,**不要**提交它们。
|
||||
- 发版流程:
|
||||
1. 递增 `versionCode`/`versionName` → `./gradlew assembleRelease`(产物 `app/build/outputs/apk/release/app-release.apk`)
|
||||
2. `git tag -a v{versionName} -m "..."` → `git push origin v{versionName}`
|
||||
3. 本机无 `gh` CLI:用 python(urllib) 调 GitHub API 创建 Release + 上传 APK。token 放仓库根 `gh-token.txt`(已 gitignore),脚本里 `open('gh-token.txt')` 读取,**勿**把 token 写进命令参数。注意 Git Bash 的 `/tmp` 与 Windows python 路径不一致,脚本内一律用项目相对路径。
|
||||
- UI 现状:统一品牌科技感主题(固定品牌色 + 自定义 Typography + 共享组件层),深浅色双方案,关闭动态取色;新组件统一通过 `ui/Components.kt` 复用,避免各页面各拼一套样式。
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# a2i
|
||||
# A2i ·安到果(按倒过[呲牙])
|
||||
|
||||
把安卓手机上的通知(短信、验证码、微信、QQ、Telegram 等)实时转发到 iOS,通过 [Bark](https://github.com/Finb/Bark) 推送到 iPhone / iPad。在 iOS 上点击通知,还能一键跳转到对应的 App。
|
||||
把安卓手机上的通知(短信、验证码、电话“有电话进来这个通知,不是电话呼叫转移”、微信、QQ、Telegram 等)实时转发到 iOS,通过 [Bark](https://github.com/Finb/Bark) 推送到 iPhone / iPad。在 iOS 上点击通知,还能一键跳转到对应的 App。
|
||||
|
||||
适用于把主力安卓机的消息同步到 iOS 设备,不漏验证码、不漏重要聊天。
|
||||
适用于把安卓备机的消息同步到 iOS 设备,不漏验证码、不漏重要聊天。
|
||||
|
||||
## 主要功能
|
||||
|
||||
@@ -17,7 +17,13 @@
|
||||
- **应用级规则**:黑名单 / 白名单两种模式,可按 App 单独控制是否转发。
|
||||
- **失败自动重试**:发送失败的通知进入待重发队列,由 WorkManager 定期重试,也可手动触发。
|
||||
- **本地日志**:保留最近 300 条处理记录,方便排查为什么某条通知被过滤或发送失败。
|
||||
- **图标导出**:一键导出已安装 App 的图标,便于部署到 Bark 图床,让通知带上来源图标。
|
||||
- **电话来电通知**:通过轮询 `getCallState()` 实时感知来电和未接来电(绕过 MIUI 对系统电话通知的限制,需授予"电话"权限)。
|
||||
- **断网短信兜底**:无网络时自动把验证码、来电用短信发到 iPhone。
|
||||
- 自动识别运营商(移动/联通/电信/广电),发免费查询短信拿套餐内短信余量
|
||||
- 余额 ≤5 条自动停用,下次打开 App 提醒
|
||||
- 每 5 分钟最多发 1 条,避免刷屏
|
||||
- 自动解析失败时回退手动额度计数
|
||||
- **应用内更新**:每天自动检查 GitHub 新版本,发现新版后一键下载并通过系统安装器升级,无需手动去网页下载。
|
||||
|
||||
## 快速开始
|
||||
|
||||
@@ -50,6 +56,8 @@
|
||||
- **想同步聊天**:保持黑名单模式(默认),在「过滤」页开启「微信 / QQ / Telegram 优化」。
|
||||
- **通知没推过来**:先看「日志」页,每条通知(无论转发、过滤还是失败)都会记录原因。
|
||||
- **想让通知带图标**:参考下方「应用图标」一节。
|
||||
- **电话通知不工作**:MIUI 会把系统电话通知限制为不向第三方通知监听器分发,本 App 通过轮询 `getCallonyState()` 绕过此限制。需要授予「电话」权限(设置 → 应用 → a2i → 权限 → 电话)。
|
||||
- **断网收不到通知**:开启「设置 → 断网短信兜底」,填好 iPhone 号码、授予短信权限、设好手动额度。断网时验证码和来电会走短信(消耗套餐额度,5 分钟限 1 条)。
|
||||
|
||||
## 应用图标
|
||||
|
||||
@@ -63,6 +71,38 @@ Bark 的 `icon` 字段需要一个公网可访问的图片 URL。a2i 支持把
|
||||
|
||||
留空前缀则不显示图标。
|
||||
|
||||
## 自建 Bark 服务(可选)
|
||||
|
||||
a2i 默认用 Bark 官方服务器(`https://api.day.app`),免费够用。如果想完全自控、不限速、不依赖第三方,可以自建 Bark Server。下面是三种零成本方案:
|
||||
|
||||
### 方案 1:Vercel 云托管(最适合新手)
|
||||
|
||||
把 Bark 后端跑在 Vercel 的 Serverless 上,每次推送触发一个云函数,不挂进程、完全免费。
|
||||
|
||||
- 平台:[Vercel](https://vercel.com) · 代码:[GitHub](https://github.com)
|
||||
- 在 GitHub 搜索 `bark-server-vercel`,项目 README 里通常有 **Deploy with Vercel** 按钮,一键克隆部署
|
||||
|
||||
### 方案 2:Cloudflare Workers(最稳定)
|
||||
|
||||
把轻量 JS 转发代码部署到 Cloudflare 全球边缘节点,免费额度每天 10 万次请求,个人用属于「溢出」状态。
|
||||
|
||||
- 平台:[Cloudflare](https://www.cloudflare.com)
|
||||
- 在 GitHub 搜索 `bark-server-cloudflare` 或 `bark cloudflare worker`,在 Cloudflare 后台新建 Worker 粘贴脚本保存即可
|
||||
|
||||
### 方案 3:免费 VPS + Docker(最高自主权)
|
||||
|
||||
在永久免费的 Linux 虚拟机上用 Docker 跑官方 bark-server。
|
||||
|
||||
- 官方服务端:[finab/bark-server](https://github.com/finab/bark-server)
|
||||
- 反向代理 + 免费 HTTPS 证书:[Nginx Proxy Manager](https://nginxproxymanager.com)
|
||||
- 免费 VPS:[Oracle Cloud 永久免费层](https://www.oracle.com/cloud/free/)、[AWS 一年免费套餐](https://aws.amazon.com/free/)
|
||||
|
||||
### ⚠️ 防坑提示
|
||||
|
||||
国内网络下,Vercel 的 `*.vercel.app` 和 Cloudflare 的 `*.workers.dev` 二级域名可能间歇性无法访问。**终极方案**:注册一个便宜的个人域名(或找免费域名),解析托管到 Cloudflare,然后把 Vercel / Worker 绑到**自定义域名**上——既稳定又秒达。
|
||||
|
||||
自建完成后,在「设置 → Bark 服务器」里填自建地址即可。
|
||||
|
||||
## 技术栈
|
||||
|
||||
- Kotlin + Jetpack Compose(Material 3)
|
||||
|
||||
@@ -13,8 +13,8 @@ android {
|
||||
applicationId = "com.a2i.forwarder"
|
||||
minSdk = 34
|
||||
targetSdk = 36
|
||||
versionCode = 3
|
||||
versionName = "1.2.0"
|
||||
versionCode = 9
|
||||
versionName = "1.5.1"
|
||||
}
|
||||
|
||||
signingConfigs {
|
||||
|
||||
@@ -4,11 +4,14 @@
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
|
||||
<uses-permission android:name="android.permission.SEND_SMS" />
|
||||
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
|
||||
tools:ignore="QueryAllPackagesPermission" />
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
|
||||
|
||||
<application
|
||||
android:name=".A2iApp"
|
||||
@@ -43,5 +46,15 @@
|
||||
</intent-filter>
|
||||
</service>
|
||||
|
||||
<provider
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
android:authorities="${applicationId}.fileprovider"
|
||||
android:exported="false"
|
||||
android:grantUriPermissions="true">
|
||||
<meta-data
|
||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/file_paths" />
|
||||
</provider>
|
||||
|
||||
</application>
|
||||
</manifest>
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package com.a2i.forwarder
|
||||
|
||||
import android.app.Application
|
||||
import com.a2i.forwarder.core.CarrierBalanceQuery
|
||||
import com.a2i.forwarder.core.CarrierDetector
|
||||
import com.a2i.forwarder.core.ConnectivityMonitor
|
||||
import com.a2i.forwarder.core.LogStore
|
||||
import com.a2i.forwarder.core.PhoneCallMonitor
|
||||
import com.a2i.forwarder.core.SmsFallbackForwarder
|
||||
import com.a2i.forwarder.core.UpdateChecker
|
||||
import com.a2i.forwarder.store.AppRulesStore
|
||||
import com.a2i.forwarder.store.SettingsStore
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
@@ -17,6 +23,16 @@ class A2iApp : Application() {
|
||||
private set
|
||||
lateinit var logStore: LogStore
|
||||
private set
|
||||
lateinit var phoneCallMonitor: PhoneCallMonitor
|
||||
private set
|
||||
lateinit var connectivityMonitor: ConnectivityMonitor
|
||||
private set
|
||||
lateinit var carrierBalanceQuery: CarrierBalanceQuery
|
||||
private set
|
||||
lateinit var smsForwarder: SmsFallbackForwarder
|
||||
private set
|
||||
lateinit var updateChecker: UpdateChecker
|
||||
private set
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
@@ -24,6 +40,14 @@ class A2iApp : Application() {
|
||||
settings = SettingsStore(this, appScope)
|
||||
appRules = AppRulesStore(this, appScope)
|
||||
logStore = LogStore(this, appScope)
|
||||
phoneCallMonitor = PhoneCallMonitor(this)
|
||||
phoneCallMonitor.start()
|
||||
connectivityMonitor = ConnectivityMonitor(this)
|
||||
connectivityMonitor.start()
|
||||
carrierBalanceQuery = CarrierBalanceQuery(this)
|
||||
smsForwarder = SmsFallbackForwarder(this)
|
||||
updateChecker = UpdateChecker(this)
|
||||
updateChecker.start()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.a2i.forwarder.core
|
||||
import com.a2i.forwarder.model.BarkMessage
|
||||
import com.a2i.forwarder.model.BarkServer
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
@@ -10,6 +11,7 @@ import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import java.io.IOException
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class BarkClient(private val server: BarkServer) {
|
||||
@@ -24,16 +26,37 @@ class BarkClient(private val server: BarkServer) {
|
||||
suspend fun push(msg: BarkMessage): Result<Unit> = withContext(Dispatchers.IO) {
|
||||
runCatching {
|
||||
require(server.deviceKey.isNotBlank()) { "device key 为空" }
|
||||
val body = json.encodeToString(msg).toRequestBody(JSON)
|
||||
val url = server.server.trim().removeSuffix("/") + "/push"
|
||||
val req = Request.Builder().url(url).post(body).build()
|
||||
http.newCall(req).execute().use { res ->
|
||||
if (!res.isSuccessful) error("HTTP ${res.code}")
|
||||
doPushWithRetry(msg)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 带重试的推送:对 connection closed / 超时 / TLS 中断等瞬时 IOException 自动重试。
|
||||
* OkHttp 的 retryOnConnectionFailure 不覆盖 POST 读响应阶段的连接中断,故在此手动补。
|
||||
* HTTP 4xx/5xx 是服务端拒绝,重试无意义,直接抛出。
|
||||
*/
|
||||
private suspend fun doPushWithRetry(msg: BarkMessage) {
|
||||
val body = json.encodeToString(msg).toRequestBody(JSON)
|
||||
val url = server.server.trim().removeSuffix("/") + "/push"
|
||||
val req = Request.Builder().url(url).post(body).build()
|
||||
var lastError: IOException? = null
|
||||
repeat(MAX_ATTEMPTS) { attempt ->
|
||||
try {
|
||||
http.newCall(req).execute().use { res ->
|
||||
if (res.isSuccessful) return
|
||||
error("HTTP ${res.code}")
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
lastError = e
|
||||
if (attempt < MAX_ATTEMPTS - 1) delay(BACKOFF_MS.getOrElse(attempt) { 1000L })
|
||||
}
|
||||
}
|
||||
throw lastError ?: error("推送失败")
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val JSON = "application/json; charset=utf-8".toMediaType()
|
||||
private const val MAX_ATTEMPTS = 3
|
||||
private val BACKOFF_MS = longArrayOf(600L, 1200L)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.a2i.forwarder.core
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.telephony.SmsManager
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.a2i.forwarder.A2iApp
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* 运营商短信余额查询:
|
||||
* - refresh():向运营商服务号发免费查询短信(若运营商支持),回执由 NotificationProcessor 解析。
|
||||
* - 手动模式:递减 smsManualQuota 作为余额。
|
||||
* - parseReply():从运营商回执短信正文中提取剩余短信条数,供 NotificationProcessor 调用。
|
||||
*/
|
||||
class CarrierBalanceQuery(private val context: Context) {
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
|
||||
private val settings get() = A2iApp.instance.settings
|
||||
|
||||
/** 触发一次余额查询(自动模式发短信,手动模式递减额度)。 */
|
||||
suspend fun refresh() {
|
||||
val carrier = currentCarrier()
|
||||
// 自动模式:发查询短信到运营商(免费,不走限频)
|
||||
if (carrier.serviceNumber != null && carrier.queryCmd != null) {
|
||||
sendSms(carrier.serviceNumber, carrier.queryCmd)
|
||||
return
|
||||
}
|
||||
// 手动模式:余额 = 手动额度,不额外查询
|
||||
}
|
||||
|
||||
/** 转发成功后扣减余额(手动模式 / 自动解析失败时)。 */
|
||||
suspend fun decrementManual() {
|
||||
val q = settings.smsManualQuota.value
|
||||
if (q > 0) {
|
||||
val newQ = q - 1
|
||||
settings.setSmsManualQuota(newQ)
|
||||
settings.setSmsBalance(newQ)
|
||||
checkSuspend(newQ)
|
||||
}
|
||||
}
|
||||
|
||||
/** 解析运营商回执,提取剩余短信条数。返回 null 表示解析失败。供 NotificationProcessor 调用。 */
|
||||
fun parseReply(text: String): Int? {
|
||||
// 匹配 "短信剩余XX条" / "短信:XX条" / "短信余量XX条" 等
|
||||
val patterns = listOf(
|
||||
Regex("""短信[^0-9]*(\d+)\s*条"""),
|
||||
Regex("""短信余[额度][^0-9]*(\d+)"""),
|
||||
)
|
||||
for (p in patterns) {
|
||||
val m = p.find(text) ?: continue
|
||||
val n = m.groupValues.getOrNull(1)?.toIntOrNull() ?: continue
|
||||
if (n in 0..9999) return n
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/** 回执解析成功后更新余额并检查是否需要挂起。 */
|
||||
suspend fun applyParsedBalance(count: Int) {
|
||||
settings.setSmsBalance(count)
|
||||
checkSuspend(count)
|
||||
}
|
||||
|
||||
private suspend fun checkSuspend(balance: Int) {
|
||||
if (balance in 0..5 && !settings.smsSuspended.value) {
|
||||
settings.setSmsSuspended(true)
|
||||
}
|
||||
}
|
||||
|
||||
fun currentCarrier(): CarrierDetector.Carrier {
|
||||
val stored = settings.smsCarrier.value
|
||||
return if (stored == "auto") CarrierDetector.detect(context)
|
||||
else CarrierDetector.Carrier.fromName(stored)
|
||||
}
|
||||
|
||||
private fun sendSms(dest: String, body: String) {
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.SEND_SMS)
|
||||
!= PackageManager.PERMISSION_GRANTED
|
||||
) return
|
||||
scope.launch {
|
||||
runCatching {
|
||||
SmsManager.getDefault().sendTextMessage(dest, null, body, null, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.a2i.forwarder.core
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.telephony.TelephonyManager
|
||||
|
||||
/**
|
||||
* 运营商检测:根据 SIM 的 MCC+MNC 映射到国内运营商。
|
||||
* 提供余额查询用的服务号 + 查询指令。
|
||||
*/
|
||||
object CarrierDetector {
|
||||
|
||||
enum class Carrier(
|
||||
val displayName: String,
|
||||
val serviceNumber: String?, // 余额查询服务号(免费)
|
||||
val queryCmd: String?, // 查询指令(发送到此服务号)
|
||||
) {
|
||||
CHINA_MOBILE("中国移动", "10086", "CXDX"),
|
||||
CHINA_UNICOM("中国联通", "10010", "CXTC"),
|
||||
CHINA_TELECOM("中国电信", "10001", "108"),
|
||||
CHINA_BROADCASTING("中国广电", "10099", null), // 无统一查询指令
|
||||
UNKNOWN("未知运营商", null, null);
|
||||
|
||||
companion object {
|
||||
fun fromName(name: String?): Carrier =
|
||||
entries.firstOrNull { it.name == name } ?: UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
// MCC+MNC → 运营商(中国大陆 460)
|
||||
private val mncMap = mapOf(
|
||||
// 中国移动
|
||||
"46000" to Carrier.CHINA_MOBILE, "46002" to Carrier.CHINA_MOBILE,
|
||||
"46004" to Carrier.CHINA_MOBILE, "46007" to Carrier.CHINA_MOBILE,
|
||||
// 中国联通
|
||||
"46001" to Carrier.CHINA_UNICOM, "46006" to Carrier.CHINA_UNICOM,
|
||||
"46009" to Carrier.CHINA_UNICOM,
|
||||
// 中国电信
|
||||
"46003" to Carrier.CHINA_TELECOM, "46005" to Carrier.CHINA_TELECOM,
|
||||
"46011" to Carrier.CHINA_TELECOM, "46012" to Carrier.CHINA_TELECOM,
|
||||
// 中国广电
|
||||
"46015" to Carrier.CHINA_BROADCASTING, "46018" to Carrier.CHINA_BROADCASTING,
|
||||
)
|
||||
|
||||
fun detect(context: Context): Carrier {
|
||||
return runCatching {
|
||||
val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager
|
||||
?: return Carrier.UNKNOWN
|
||||
val simOperator = tm.simOperator // 形如 "46000"
|
||||
if (simOperator.isNullOrBlank() || simOperator.length < 5) return Carrier.UNKNOWN
|
||||
mncMap[simOperator] ?: Carrier.UNKNOWN
|
||||
}.getOrDefault(Carrier.UNKNOWN)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.a2i.forwarder.core
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.Network
|
||||
import android.net.NetworkCapabilities
|
||||
import android.net.NetworkRequest
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
|
||||
/**
|
||||
* 监听网络连通性(是否能访问互联网)。
|
||||
* 暴露 isOnline: StateFlow<Boolean>,断网时短信兜底转发器据此降级。
|
||||
*/
|
||||
class ConnectivityMonitor(private val context: Context) {
|
||||
private val _isOnline = MutableStateFlow(false)
|
||||
val isOnline = _isOnline.asStateFlow()
|
||||
|
||||
private var registered = false
|
||||
|
||||
private val callback = object : ConnectivityManager.NetworkCallback() {
|
||||
override fun onAvailable(network: Network) { refresh() }
|
||||
override fun onLost(network: Network) { refresh() }
|
||||
override fun onCapabilitiesChanged(network: Network, caps: NetworkCapabilities) { refresh() }
|
||||
}
|
||||
|
||||
fun start() {
|
||||
if (registered) return
|
||||
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
|
||||
?: return
|
||||
val request = NetworkRequest.Builder()
|
||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
.build()
|
||||
runCatching { cm.registerNetworkCallback(request, callback) }
|
||||
registered = true
|
||||
refresh()
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
if (!registered) return
|
||||
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
|
||||
runCatching { cm?.unregisterNetworkCallback(callback) }
|
||||
registered = false
|
||||
}
|
||||
|
||||
private fun refresh() {
|
||||
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager ?: return
|
||||
val active = cm.activeNetwork
|
||||
val caps = active?.let { cm.getNetworkCapabilities(it) }
|
||||
_isOnline.value = caps != null &&
|
||||
caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
|
||||
caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ class NotificationProcessor(private val context: Context) {
|
||||
val reason: String,
|
||||
val appLabel: String,
|
||||
val code: String?,
|
||||
val silent: Boolean = false, // true = 静默过滤,不写日志
|
||||
)
|
||||
|
||||
// ========== 去重缓存 ==========
|
||||
@@ -88,10 +89,10 @@ class NotificationProcessor(private val context: Context) {
|
||||
if (!settings.forwardingEnabled.value)
|
||||
return Decision(null, "全局转发已关", appLabel, null)
|
||||
|
||||
// ---- 持续性通知过滤 ----
|
||||
// ---- 持续性通知过滤(静默,不进日志)----
|
||||
val isOngoing = n.flags and Notification.FLAG_ONGOING_EVENT != 0
|
||||
if (settings.skipOngoing.value && isOngoing)
|
||||
return Decision(null, "持续性通知", appLabel, null)
|
||||
return Decision(null, "持续性通知", appLabel, null, silent = true)
|
||||
|
||||
// ---- App 黑白名单 ----
|
||||
if (!rules.isForwardingOn(pkg))
|
||||
@@ -122,6 +123,16 @@ class NotificationProcessor(private val context: Context) {
|
||||
if (settings.adFilterEnabled.value && AdFilter.isAd("$title $content $sub", settings.adKeywords.value))
|
||||
return Decision(null, "广告过滤", appLabel, null)
|
||||
|
||||
// ---- 短信"发出"过滤:只保留收到的短信 ----
|
||||
if (isSentSmsNotification(pkg, title, content))
|
||||
return Decision(null, "已发短信(仅收不发)", appLabel, null)
|
||||
|
||||
// ---- 运营商余额回执拦截:解析短信余量,不转发 ----
|
||||
val balanceParsed = tryParseCarrierBalance(pkg, title, content)
|
||||
if (balanceParsed != null) {
|
||||
return Decision(null, "运营商余额回执(${balanceParsed}条)", appLabel, null)
|
||||
}
|
||||
|
||||
// ---- 去重检查 ----
|
||||
val dedupKey = "$pkg|${title.take(80)}|${content.take(80)}"
|
||||
val lastTime = dedupCache[dedupKey]
|
||||
@@ -188,4 +199,47 @@ class NotificationProcessor(private val context: Context) {
|
||||
private fun isSystemStatusMessage(text: String): Boolean {
|
||||
return systemStatusPatterns.any { it.containsMatchIn(text) }
|
||||
}
|
||||
|
||||
/** 短信 App 列表 */
|
||||
private val smsPackages = setOf(
|
||||
"com.android.mms",
|
||||
"com.google.android.apps.messaging",
|
||||
"com.samsung.android.messaging",
|
||||
"com.android.messaging",
|
||||
)
|
||||
|
||||
/** 发出的短信通知关键词 */
|
||||
private val sentSmsPatterns = listOf(
|
||||
Regex("""发送中"""),
|
||||
Regex("""正在发送"""),
|
||||
Regex("""已发送"""),
|
||||
Regex("""发送成功"""),
|
||||
Regex("""短信已发出"""),
|
||||
Regex("""消息已发出"""),
|
||||
Regex("""sending""", RegexOption.IGNORE_CASE),
|
||||
Regex("""message sent""", RegexOption.IGNORE_CASE),
|
||||
Regex("""sent$""", RegexOption.IGNORE_CASE),
|
||||
)
|
||||
|
||||
/** 判断是否为发出的短信通知(而非收到的) */
|
||||
private fun isSentSmsNotification(pkg: String, title: String, content: String): Boolean {
|
||||
if (pkg !in smsPackages) return false
|
||||
val blob = "$title $content"
|
||||
return sentSmsPatterns.any { it.containsMatchIn(blob) }
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截运营商余额回执短信:title 是运营商服务号(10086/10010/10001/10099),
|
||||
* 解析正文中的剩余短信条数,写入设置并按"过滤"处理。
|
||||
* 返回解析到的条数(已处理),返回 null 表示不是回执或解析失败。
|
||||
*/
|
||||
private fun tryParseCarrierBalance(pkg: String, title: String, content: String): Int? {
|
||||
if (pkg !in smsPackages) return null
|
||||
val serviceNumbers = setOf("10086", "10010", "10001", "10099")
|
||||
if (title.trim() !in serviceNumbers) return null
|
||||
val app = A2iApp.instance
|
||||
val count = app.carrierBalanceQuery.parseReply("$title $content") ?: return null
|
||||
kotlinx.coroutines.runBlocking { app.carrierBalanceQuery.applyParsedBalance(count) }
|
||||
return count
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
package com.a2i.forwarder.core
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.telephony.TelephonyManager
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.a2i.forwarder.A2iApp
|
||||
import com.a2i.forwarder.model.BarkMessage
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.util.UUID
|
||||
|
||||
/**
|
||||
* 通过轮询 TelephonyManager.getCallState() 监听来电状态。
|
||||
* MIUI 不会把系统电话通知分发给第三方监听器,TelephonyCallback 也不生效,
|
||||
* 所以这里用最原始但最可靠的方式:每 800ms 读一次 call state。
|
||||
*
|
||||
* getCallState() 只是读内存中的 int,不涉及硬件操作,耗电极低。
|
||||
*/
|
||||
class PhoneCallMonitor(private val context: Context) {
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
private val json = Json { encodeDefaults = true; ignoreUnknownKeys = true }
|
||||
|
||||
private var lastState = TelephonyManager.CALL_STATE_IDLE
|
||||
private var ringing = false
|
||||
private var offhook = false
|
||||
private var running = false
|
||||
|
||||
/** 上一次推送"来电"的时间,防止重复推送 */
|
||||
private var lastRingTime = 0L
|
||||
|
||||
fun start() {
|
||||
if (running) return
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
|
||||
!= PackageManager.PERMISSION_GRANTED
|
||||
) return
|
||||
|
||||
running = true
|
||||
lastState = TelephonyManager.CALL_STATE_IDLE
|
||||
ringing = false
|
||||
offhook = false
|
||||
|
||||
scope.launch {
|
||||
val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager
|
||||
if (tm == null) { running = false; return@launch }
|
||||
|
||||
while (isActive && running) {
|
||||
val state = tm.callState
|
||||
if (state != lastState) {
|
||||
handleTransition(lastState, state)
|
||||
lastState = state
|
||||
}
|
||||
delay(800)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
running = false
|
||||
}
|
||||
|
||||
private fun handleTransition(from: Int, to: Int) {
|
||||
val app = A2iApp.instance
|
||||
val settings = app.settings
|
||||
if (!settings.forwardingEnabled.value) return
|
||||
val server = settings.currentServer.value ?: return
|
||||
if (server.deviceKey.isBlank()) return
|
||||
|
||||
when {
|
||||
// IDLE → RINGING:来电
|
||||
from == TelephonyManager.CALL_STATE_IDLE && to == TelephonyManager.CALL_STATE_RINGING -> {
|
||||
ringing = true
|
||||
offhook = false
|
||||
val now = System.currentTimeMillis()
|
||||
if (now - lastRingTime > 3000) {
|
||||
lastRingTime = now
|
||||
val msg = BarkMessage(
|
||||
deviceKey = server.deviceKey,
|
||||
title = "来电",
|
||||
body = "有电话进来",
|
||||
level = "timeSensitive",
|
||||
group = "phone_call",
|
||||
isArchive = "1",
|
||||
)
|
||||
pushToServer(server, msg, app, "来电")
|
||||
}
|
||||
}
|
||||
|
||||
// RINGING → OFFHOOK:接听
|
||||
from == TelephonyManager.CALL_STATE_RINGING && to == TelephonyManager.CALL_STATE_OFFHOOK -> {
|
||||
offhook = true
|
||||
}
|
||||
|
||||
// RINGING → IDLE:未接(响了但没接)
|
||||
from == TelephonyManager.CALL_STATE_RINGING && to == TelephonyManager.CALL_STATE_IDLE -> {
|
||||
if (!offhook && ringing) {
|
||||
val msg = BarkMessage(
|
||||
deviceKey = server.deviceKey,
|
||||
title = "未接来电",
|
||||
body = "未接来电",
|
||||
level = "timeSensitive",
|
||||
group = "phone_call",
|
||||
isArchive = "1",
|
||||
)
|
||||
pushToServer(server, msg, app, "未接来电")
|
||||
}
|
||||
ringing = false
|
||||
offhook = false
|
||||
}
|
||||
|
||||
// OFFHOOK → IDLE:正常挂断,不推送
|
||||
from == TelephonyManager.CALL_STATE_OFFHOOK && to == TelephonyManager.CALL_STATE_IDLE -> {
|
||||
ringing = false
|
||||
offhook = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun pushToServer(
|
||||
server: com.a2i.forwarder.model.BarkServer,
|
||||
msg: BarkMessage,
|
||||
app: A2iApp,
|
||||
logBody: String,
|
||||
) {
|
||||
scope.launch {
|
||||
val result = BarkClient(server).push(msg)
|
||||
val now = System.currentTimeMillis()
|
||||
if (result.isSuccess) {
|
||||
app.logStore.addLog(
|
||||
ForwardLog(now, "phone_call", "电话", msg.title, logBody, "sent", "")
|
||||
)
|
||||
} else {
|
||||
val reason = result.exceptionOrNull()?.message ?: "错误"
|
||||
app.logStore.addLog(
|
||||
ForwardLog(now, "phone_call", "电话", msg.title, logBody, "failed", reason)
|
||||
)
|
||||
app.logStore.addPending(
|
||||
PendingPush(UUID.randomUUID().toString(), now, server.id, json.encodeToString(msg))
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.a2i.forwarder.core
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.telephony.SmsManager
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.a2i.forwarder.A2iApp
|
||||
import com.a2i.forwarder.core.NotificationProcessor.Decision
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
* 短信兜底转发器:断网时把紧要通知(验证码、来电)用短信发到 iPhone。
|
||||
* 限频:每 5 分钟最多 1 条转发短信。余额 ≤5 时自动挂起。
|
||||
*/
|
||||
class SmsFallbackForwarder(private val context: Context) {
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
private val RATE_LIMIT_MS = 5 * 60 * 1000L
|
||||
|
||||
private val app get() = A2iApp.instance
|
||||
private val settings get() = app.settings
|
||||
|
||||
/** 判断该通知是否值得走短信(断网资源宝贵)。 */
|
||||
fun isSmsWorthy(d: Decision): Boolean {
|
||||
if (d.code != null) return true // 验证码
|
||||
val msg = d.message ?: return false
|
||||
if (msg.group == "phone_call") return true // 来电/未接来电
|
||||
val t = msg.title.orEmpty()
|
||||
return t.contains("来电") || t.contains("未接")
|
||||
}
|
||||
|
||||
/** 尝试用短信转发。返回 true 表示已处理(无论成功失败)。 */
|
||||
fun forward(d: Decision) {
|
||||
scope.launch {
|
||||
if (!settings.smsFallbackEnabled.value) return@launch
|
||||
if (settings.smsSuspended.value) {
|
||||
log(d, "failed", "短信已挂起(余额不足)")
|
||||
return@launch
|
||||
}
|
||||
if (!isSmsWorthy(d)) return@launch
|
||||
val target = settings.smsTargetNumber.value.trim()
|
||||
if (target.isBlank()) {
|
||||
log(d, "failed", "未设置短信目标号")
|
||||
return@launch
|
||||
}
|
||||
// 限频
|
||||
val now = System.currentTimeMillis()
|
||||
val elapsed = now - settings.lastSmsSentTime.value
|
||||
if (elapsed < RATE_LIMIT_MS) {
|
||||
log(d, "filtered", "限频(${RATE_LIMIT_MS - elapsed}ms 内)")
|
||||
return@launch
|
||||
}
|
||||
// 权限
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.SEND_SMS)
|
||||
!= PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
log(d, "failed", "无 SEND_SMS 权限")
|
||||
return@launch
|
||||
}
|
||||
|
||||
val body = compose(d).take(70)
|
||||
val result = runCatching {
|
||||
SmsManager.getDefault().sendTextMessage(target, null, body, null, null)
|
||||
}
|
||||
if (result.isSuccess) {
|
||||
settings.setLastSmsSentTime(now)
|
||||
log(d, "sent", "")
|
||||
// 转发后刷新余额:自动模式发查询短信,手动模式递减
|
||||
val carrier = app.carrierBalanceQuery.currentCarrier()
|
||||
if (carrier.serviceNumber != null && carrier.queryCmd != null) {
|
||||
app.carrierBalanceQuery.refresh()
|
||||
} else {
|
||||
app.carrierBalanceQuery.decrementManual()
|
||||
}
|
||||
} else {
|
||||
val reason = result.exceptionOrNull()?.message ?: "短信发送失败"
|
||||
log(d, "failed", reason)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun compose(d: Decision): String {
|
||||
val code = d.code
|
||||
return if (code != null) {
|
||||
"[验证码]${d.appLabel}: $code"
|
||||
} else {
|
||||
val m = d.message!!
|
||||
"${m.title.orEmpty().ifBlank { d.appLabel }}: ${m.body}".trim()
|
||||
}
|
||||
}
|
||||
|
||||
private fun log(d: Decision, status: String, reason: String) {
|
||||
scope.launch {
|
||||
val now = System.currentTimeMillis()
|
||||
val title = d.code?.let { "验证码" } ?: d.message?.title ?: d.appLabel
|
||||
val content = d.code ?: d.message?.body ?: ""
|
||||
app.logStore.addLog(
|
||||
ForwardLog(now, "sms_fallback", d.appLabel, title, content, status, reason)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
package com.a2i.forwarder.core
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.Settings
|
||||
import androidx.core.content.FileProvider
|
||||
import com.a2i.forwarder.A2iApp
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import org.json.JSONObject
|
||||
import java.io.File
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
data class UpdateInfo(
|
||||
val latestVersion: String,
|
||||
val apkUrl: String,
|
||||
val apkSize: Long,
|
||||
val releaseNotes: String,
|
||||
val releasePageUrl: String,
|
||||
)
|
||||
|
||||
/**
|
||||
* 应用内更新:拉取 GitHub Releases 最新版 → 版本比较 → 下载 APK → 调起系统安装器。
|
||||
* Android targetSdk≥24 须用 FileProvider,targetSdk≥26 安装未知来源 APK 须 REQUEST_INSTALL_PACKAGES。
|
||||
*/
|
||||
class UpdateChecker(private val context: Context) {
|
||||
private val app get() = A2iApp.instance
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
private val http = OkHttpClient.Builder()
|
||||
.connectTimeout(10, TimeUnit.SECONDS)
|
||||
.readTimeout(30, TimeUnit.SECONDS)
|
||||
.retryOnConnectionFailure(true)
|
||||
.build()
|
||||
|
||||
val updateInfo = MutableStateFlow<UpdateInfo?>(null)
|
||||
val downloading = MutableStateFlow(false)
|
||||
val downloadProgress = MutableStateFlow(0) // 0..100
|
||||
val lastError = MutableStateFlow<String?>(null)
|
||||
|
||||
fun start() {
|
||||
scope.launch { check(force = false) }
|
||||
}
|
||||
|
||||
suspend fun check(force: Boolean): Result<UpdateInfo?> = withContext(Dispatchers.IO) {
|
||||
runCatching {
|
||||
val settings = app.settings
|
||||
val now = System.currentTimeMillis()
|
||||
if (!force) {
|
||||
// 自动检查被关闭或未满 24h:保持现状,不发请求
|
||||
if (!settings.updateCheckEnabled.value) return@runCatching updateInfo.value
|
||||
if (now - settings.lastUpdateCheckTime.value < CHECK_INTERVAL_MS) return@runCatching updateInfo.value
|
||||
}
|
||||
val req = Request.Builder()
|
||||
.url(RELEASES_API)
|
||||
.header("Accept", "application/vnd.github+json")
|
||||
.header("User-Agent", "a2i-android")
|
||||
.get()
|
||||
.build()
|
||||
val info = http.newCall(req).execute().use { res ->
|
||||
if (!res.isSuccessful) error("HTTP ${res.code}")
|
||||
val body = res.body?.string() ?: error("空响应")
|
||||
parseRelease(body)
|
||||
}
|
||||
settings.setLastUpdateCheck(now)
|
||||
val current = currentVersionName()
|
||||
val available = info != null &&
|
||||
isNewer(info.latestVersion, current) &&
|
||||
info.latestVersion != settings.ignoredVersion.value
|
||||
updateInfo.value = if (available) info else null
|
||||
lastError.value = null
|
||||
updateInfo.value
|
||||
}.onFailure { lastError.value = it.message ?: "检查失败" }
|
||||
}
|
||||
|
||||
private fun parseRelease(body: String): UpdateInfo? {
|
||||
val json = JSONObject(body)
|
||||
val tag = json.optString("tag_name").removePrefix("v").trim()
|
||||
if (tag.isEmpty()) return null
|
||||
val assets = json.optJSONArray("assets") ?: return null
|
||||
var apkUrl = ""
|
||||
var apkSize = 0L
|
||||
for (i in 0 until assets.length()) {
|
||||
val a = assets.optJSONObject(i) ?: continue
|
||||
if (a.optString("name").endsWith(".apk", ignoreCase = true)) {
|
||||
apkUrl = a.optString("browser_download_url")
|
||||
apkSize = a.optLong("size")
|
||||
break
|
||||
}
|
||||
}
|
||||
if (apkUrl.isEmpty()) return null
|
||||
return UpdateInfo(
|
||||
latestVersion = tag,
|
||||
apkUrl = apkUrl,
|
||||
apkSize = apkSize,
|
||||
releaseNotes = json.optString("body").orEmpty(),
|
||||
releasePageUrl = json.optString("html_url").orEmpty(),
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun download(info: UpdateInfo): File? = withContext(Dispatchers.IO) {
|
||||
downloading.value = true
|
||||
downloadProgress.value = 0
|
||||
lastError.value = null
|
||||
try {
|
||||
val req = Request.Builder().url(info.apkUrl).get().build()
|
||||
http.newCall(req).execute().use { res ->
|
||||
if (!res.isSuccessful) { lastError.value = "HTTP ${res.code}"; return@use null }
|
||||
val body = res.body ?: run { lastError.value = "空响应"; return@use null }
|
||||
val total = body.contentLength()
|
||||
val dir = context.getExternalFilesDir(null) ?: context.cacheDir
|
||||
val target = File(dir, "a2i-${info.latestVersion}.apk")
|
||||
target.outputStream().use { sink ->
|
||||
val input = body.byteStream()
|
||||
val buf = ByteArray(8 * 1024)
|
||||
var read = 0L
|
||||
while (true) {
|
||||
val n = input.read(buf)
|
||||
if (n <= 0) break
|
||||
sink.write(buf, 0, n)
|
||||
read += n
|
||||
if (total > 0) downloadProgress.value = (read * 100 / total).toInt()
|
||||
}
|
||||
}
|
||||
downloadProgress.value = 100
|
||||
target
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
lastError.value = e.message ?: "下载失败"
|
||||
null
|
||||
} finally {
|
||||
downloading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
fun install(apk: File) {
|
||||
// 国产 ROM 需用户先在系统设置开启「安装未知应用」
|
||||
if (!context.packageManager.canRequestPackageInstalls()) {
|
||||
val intent = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).apply {
|
||||
data = Uri.parse("package:${context.packageName}")
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
runCatching { context.startActivity(intent) }
|
||||
return
|
||||
}
|
||||
val uri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", apk)
|
||||
val intent = Intent(Intent.ACTION_VIEW).apply {
|
||||
setDataAndType(uri, "application/vnd.android.package-archive")
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
runCatching { context.startActivity(intent) }
|
||||
}
|
||||
|
||||
fun ignoreVersion(v: String) {
|
||||
scope.launch {
|
||||
app.settings.setIgnoredVersion(v)
|
||||
updateInfo.value = null
|
||||
}
|
||||
}
|
||||
|
||||
private fun currentVersionName(): String {
|
||||
return runCatching {
|
||||
@Suppress("DEPRECATION")
|
||||
context.packageManager.getPackageInfo(context.packageName, 0).versionName ?: "0"
|
||||
}.getOrDefault("0")
|
||||
}
|
||||
|
||||
/** 语义版本比较:按 "." 分段逐段比,段数不等时短的补 0。 */
|
||||
fun isNewer(latest: String, current: String): Boolean {
|
||||
val l = latest.split(".").map { it.substringBefore("-").toIntOrNull() ?: 0 }
|
||||
val c = current.split(".").map { it.substringBefore("-").toIntOrNull() ?: 0 }
|
||||
val n = maxOf(l.size, c.size)
|
||||
for (i in 0 until n) {
|
||||
val lv = l.getOrElse(i) { 0 }
|
||||
val cv = c.getOrElse(i) { 0 }
|
||||
if (lv != cv) return lv > cv
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val RELEASES_API = "https://api.github.com/repos/lsxf/a2i/releases/latest"
|
||||
const val CHECK_INTERVAL_MS = 24L * 60 * 60 * 1000
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,21 @@ class NotifyListenerService : NotificationListenerService() {
|
||||
val msg = d.message
|
||||
|
||||
if (msg == null) {
|
||||
app.logStore.addLog(ForwardLog(now, sbn.packageName, d.appLabel, "", "", "filtered", d.reason))
|
||||
// 静默过滤(如持续性通知)不写日志
|
||||
if (!d.silent) {
|
||||
app.logStore.addLog(ForwardLog(now, sbn.packageName, d.appLabel, "", "", "filtered", d.reason))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ---- 断网兜底:离线时紧要通知走短信,其余不转发避免离线堆积 ----
|
||||
val online = app.connectivityMonitor.isOnline.value
|
||||
if (!online) {
|
||||
if (app.smsForwarder.isSmsWorthy(d)) {
|
||||
app.smsForwarder.forward(d)
|
||||
} else {
|
||||
app.logStore.addLog(ForwardLog(now, sbn.packageName, d.appLabel, msg.title, msg.body, "filtered", "离线非紧要通知"))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.intPreferencesKey
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.core.stringSetPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
@@ -25,6 +26,7 @@ class AppRulesStore(
|
||||
val MODE = stringPreferencesKey("mode")
|
||||
val BLACK = stringSetPreferencesKey("blacklist")
|
||||
val WHITE = stringSetPreferencesKey("whitelist")
|
||||
val BL_VERSION = intPreferencesKey("bl_version")
|
||||
}
|
||||
|
||||
val mode = MutableStateFlow(RuleMode.BLACKLIST)
|
||||
@@ -35,7 +37,14 @@ class AppRulesStore(
|
||||
scope.launch {
|
||||
ds.data.collect { p ->
|
||||
mode.value = runCatching { RuleMode.valueOf(p[K.MODE] ?: "BLACKLIST") }.getOrDefault(RuleMode.BLACKLIST)
|
||||
blacklist.value = p[K.BLACK] ?: defaultBlacklist
|
||||
// 默认黑名单版本迁移:把新增的噪音包补进老用户的黑名单(只增不删)
|
||||
val ver = p[K.BL_VERSION] ?: 1
|
||||
var black = p[K.BLACK] ?: defaultBlacklist
|
||||
if (ver < DEFAULT_BL_VERSION) {
|
||||
black = black + blAdded
|
||||
ds.edit { it[K.BLACK] = black; it[K.BL_VERSION] = DEFAULT_BL_VERSION }
|
||||
}
|
||||
blacklist.value = black
|
||||
whitelist.value = p[K.WHITE] ?: emptySet()
|
||||
}
|
||||
}
|
||||
@@ -69,13 +78,26 @@ class AppRulesStore(
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val DEFAULT_BL_VERSION = 3
|
||||
|
||||
// 默认黑名单增量(累积,用于老用户版本迁移,只补不删)
|
||||
private val blAdded = setOf(
|
||||
// v2
|
||||
"com.android.soundrecorder", // 录音机
|
||||
"com.google.android.apps.photos", // 相册
|
||||
"com.xiaomi.finddevice", // 查找手机
|
||||
"com.miui.passwords", // 密码管理
|
||||
// v3:通话过程中的系统状态通知,会与来电(PhoneCallMonitor)重复
|
||||
"com.android.server.telecom", // 通话管理
|
||||
"com.xiaomi.aiasst.service", // AI通话
|
||||
)
|
||||
|
||||
val defaultBlacklist = setOf(
|
||||
// Android 系统
|
||||
"com.android.systemui",
|
||||
"com.android.settings",
|
||||
"com.android.permissioncontroller",
|
||||
"com.android.phone",
|
||||
"com.android.incallui",
|
||||
// 注意:com.android.phone / com.android.incallui 不在黑名单中,电话通知需要转发
|
||||
// 输入法
|
||||
"com.google.android.inputmethod",
|
||||
"com.google.android.inputmethod.latin",
|
||||
@@ -92,6 +114,14 @@ class AppRulesStore(
|
||||
"com.google.android.gms.persistent",
|
||||
// a2i 自身
|
||||
"com.a2i.forwarder",
|
||||
// [v2] 纯噪音 App:转发无意义,默认屏蔽
|
||||
"com.android.soundrecorder", // 录音机
|
||||
"com.google.android.apps.photos", // 相册
|
||||
"com.xiaomi.finddevice", // 查找手机
|
||||
"com.miui.passwords", // 密码管理
|
||||
// [v3] 通话状态噪音:来电由 PhoneCallMonitor 独立处理,这两个会重复
|
||||
"com.android.server.telecom", // 通话管理
|
||||
"com.xiaomi.aiasst.service", // AI通话
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.booleanPreferencesKey
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.intPreferencesKey
|
||||
import androidx.datastore.preferences.core.longPreferencesKey
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import com.a2i.forwarder.model.BarkServer
|
||||
@@ -37,6 +39,18 @@ class SettingsStore(
|
||||
val SPECIAL = booleanPreferencesKey("special")
|
||||
val SKIP_ONGOING = booleanPreferencesKey("skip_ongoing")
|
||||
val AD_KW = stringPreferencesKey("ad_keywords")
|
||||
// 短信兜底
|
||||
val SMS_FALLBACK = booleanPreferencesKey("sms_fallback")
|
||||
val SMS_TARGET = stringPreferencesKey("sms_target")
|
||||
val SMS_CARRIER = stringPreferencesKey("sms_carrier")
|
||||
val SMS_QUOTA = intPreferencesKey("sms_quota")
|
||||
val SMS_BALANCE = intPreferencesKey("sms_balance")
|
||||
val SMS_SUSPENDED = booleanPreferencesKey("sms_suspended")
|
||||
val SMS_LAST_SENT = longPreferencesKey("sms_last_sent")
|
||||
// 应用更新
|
||||
val UPDATE_CHECK_AUTO = booleanPreferencesKey("update_check_auto")
|
||||
val LAST_UPDATE_CHECK = longPreferencesKey("last_update_check")
|
||||
val IGNORED_VERSION = stringPreferencesKey("ignored_version")
|
||||
}
|
||||
|
||||
private val defaultServers = listOf(
|
||||
@@ -49,7 +63,7 @@ class SettingsStore(
|
||||
private val _currentServerId = MutableStateFlow(ID_OFFICIAL)
|
||||
val currentServer = MutableStateFlow<BarkServer?>(defaultServers.first())
|
||||
|
||||
val iconPrefix = MutableStateFlow("")
|
||||
val iconPrefix = MutableStateFlow(DEFAULT_ICON_PREFIX)
|
||||
val forwardingEnabled = MutableStateFlow(true)
|
||||
val codeEnabled = MutableStateFlow(true)
|
||||
val adFilterEnabled = MutableStateFlow(true)
|
||||
@@ -57,6 +71,20 @@ class SettingsStore(
|
||||
val skipOngoing = MutableStateFlow(true)
|
||||
val adKeywords = MutableStateFlow<List<String>>(emptyList()) // 用户自定义关键词
|
||||
|
||||
// 短信兜底
|
||||
val smsFallbackEnabled = MutableStateFlow(false)
|
||||
val smsTargetNumber = MutableStateFlow("")
|
||||
val smsCarrier = MutableStateFlow("auto") // "auto" 或 Carrier.name
|
||||
val smsManualQuota = MutableStateFlow(0) // 手动额度(自动解析失败时回退用)
|
||||
val smsBalance = MutableStateFlow(-1) // 缓存余额,-1=未知
|
||||
val smsSuspended = MutableStateFlow(false) // 余额 ≤5 自动挂起
|
||||
val lastSmsSentTime = MutableStateFlow(0L) // 限频时间戳
|
||||
|
||||
// 应用更新
|
||||
val updateCheckEnabled = MutableStateFlow(true) // 自动检查更新开关
|
||||
val lastUpdateCheckTime = MutableStateFlow(0L) // 上次检查时间戳
|
||||
val ignoredVersion = MutableStateFlow("") // 用户忽略的版本号
|
||||
|
||||
init {
|
||||
scope.launch {
|
||||
ds.data.collect { p ->
|
||||
@@ -66,7 +94,7 @@ class SettingsStore(
|
||||
_servers.value = list
|
||||
_currentServerId.value = p[K.CURRENT] ?: ID_OFFICIAL
|
||||
currentServer.value = list.firstOrNull { it.id == _currentServerId.value } ?: list.firstOrNull()
|
||||
iconPrefix.value = p[K.ICON_PREFIX] ?: ""
|
||||
iconPrefix.value = p[K.ICON_PREFIX] ?: DEFAULT_ICON_PREFIX
|
||||
forwardingEnabled.value = p[K.FWD] ?: true
|
||||
codeEnabled.value = p[K.CODE] ?: true
|
||||
adFilterEnabled.value = p[K.AD] ?: true
|
||||
@@ -75,6 +103,18 @@ class SettingsStore(
|
||||
adKeywords.value = p[K.AD_KW]
|
||||
?.let { runCatching { json.decodeFromString<List<String>>(it) }.getOrNull() }
|
||||
?: emptyList()
|
||||
// 短信兜底
|
||||
smsFallbackEnabled.value = p[K.SMS_FALLBACK] ?: false
|
||||
smsTargetNumber.value = p[K.SMS_TARGET] ?: ""
|
||||
smsCarrier.value = p[K.SMS_CARRIER] ?: "auto"
|
||||
smsManualQuota.value = p[K.SMS_QUOTA] ?: 0
|
||||
smsBalance.value = p[K.SMS_BALANCE] ?: -1
|
||||
smsSuspended.value = p[K.SMS_SUSPENDED] ?: false
|
||||
lastSmsSentTime.value = p[K.SMS_LAST_SENT] ?: 0L
|
||||
// 应用更新
|
||||
updateCheckEnabled.value = p[K.UPDATE_CHECK_AUTO] ?: true
|
||||
lastUpdateCheckTime.value = p[K.LAST_UPDATE_CHECK] ?: 0L
|
||||
ignoredVersion.value = p[K.IGNORED_VERSION] ?: ""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -128,9 +168,22 @@ class SettingsStore(
|
||||
adKeywords.value = list
|
||||
}
|
||||
|
||||
suspend fun setSmsFallback(v: Boolean) { edit { it[K.SMS_FALLBACK] = v }; smsFallbackEnabled.value = v }
|
||||
suspend fun setSmsTargetNumber(v: String) { edit { it[K.SMS_TARGET] = v }; smsTargetNumber.value = v }
|
||||
suspend fun setSmsCarrier(v: String) { edit { it[K.SMS_CARRIER] = v }; smsCarrier.value = v }
|
||||
suspend fun setSmsManualQuota(v: Int) { edit { it[K.SMS_QUOTA] = v }; smsManualQuota.value = v }
|
||||
suspend fun setSmsBalance(v: Int) { edit { it[K.SMS_BALANCE] = v }; smsBalance.value = v }
|
||||
suspend fun setSmsSuspended(v: Boolean) { edit { it[K.SMS_SUSPENDED] = v }; smsSuspended.value = v }
|
||||
suspend fun setLastSmsSentTime(v: Long) { edit { it[K.SMS_LAST_SENT] = v }; lastSmsSentTime.value = v }
|
||||
|
||||
suspend fun setUpdateCheckEnabled(v: Boolean) { edit { it[K.UPDATE_CHECK_AUTO] = v }; updateCheckEnabled.value = v }
|
||||
suspend fun setLastUpdateCheck(v: Long) { edit { it[K.LAST_UPDATE_CHECK] = v }; lastUpdateCheckTime.value = v }
|
||||
suspend fun setIgnoredVersion(v: String) { edit { it[K.IGNORED_VERSION] = v }; ignoredVersion.value = v }
|
||||
|
||||
suspend fun snapshot() = ds.data.first()
|
||||
|
||||
companion object {
|
||||
const val ID_OFFICIAL = "official"
|
||||
const val DEFAULT_ICON_PREFIX = "https://raw.githubusercontent.com/lsxf/a2i/main/icons/"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import androidx.compose.material.icons.filled.Notifications
|
||||
import androidx.compose.material.icons.filled.PowerSettingsNew
|
||||
import androidx.compose.material.icons.filled.Settings
|
||||
import androidx.compose.material.icons.filled.WarningAmber
|
||||
import androidx.compose.material.icons.filled.SystemUpdate
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.FilledTonalButton
|
||||
import androidx.compose.material3.Icon
|
||||
@@ -65,6 +66,9 @@ fun HomeScreen(onOpenLog: () -> Unit, onOpenSettings: () -> Unit) {
|
||||
val sent by app.logStore.sentCount.collectAsState()
|
||||
val filtered by app.logStore.filteredCount.collectAsState()
|
||||
val failed by app.logStore.failedCount.collectAsState()
|
||||
val smsSuspended by app.settings.smsSuspended.collectAsState()
|
||||
val update by app.updateChecker.updateInfo.collectAsState()
|
||||
var showUpdateDialog by remember { mutableStateOf(false) }
|
||||
|
||||
var granted by remember { mutableStateOf(isNotificationListenerEnabled(context)) }
|
||||
val lifecycleOwner = LocalLifecycleOwner.current
|
||||
@@ -89,6 +93,51 @@ fun HomeScreen(onOpenLog: () -> Unit, onOpenSettings: () -> Unit) {
|
||||
)
|
||||
}
|
||||
|
||||
val info = update
|
||||
if (info != null) {
|
||||
item {
|
||||
SectionCard {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
IconBox(icon = Icons.Filled.SystemUpdate, tint = MaterialTheme.colorScheme.primary)
|
||||
Spacer(Modifier.width(12.dp))
|
||||
Column(Modifier.weight(1f)) {
|
||||
Text("发现新版本 v${info.latestVersion}", style = MaterialTheme.typography.titleMedium)
|
||||
Text(
|
||||
"安装包 ${formatSize(info.apkSize)} · 点击立即更新",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
StatusBadge(text = "新", tint = MaterialTheme.colorScheme.primary)
|
||||
}
|
||||
Spacer(Modifier.size(12.dp))
|
||||
Button(onClick = { showUpdateDialog = true }, modifier = Modifier.fillMaxWidth()) { Text("立即更新") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (smsSuspended) {
|
||||
item {
|
||||
SectionCard {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
IconBox(
|
||||
icon = Icons.Filled.WarningAmber,
|
||||
tint = MaterialTheme.colorScheme.error,
|
||||
)
|
||||
Spacer(Modifier.width(12.dp))
|
||||
Column(Modifier.weight(1f)) {
|
||||
Text("短信兜底已停用", style = MaterialTheme.typography.titleMedium)
|
||||
Text(
|
||||
"短信余额不足,断网转发已关闭。请在「设置 → 断网短信兜底」补充额度",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
item {
|
||||
SectionCard {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
@@ -177,6 +226,11 @@ fun HomeScreen(onOpenLog: () -> Unit, onOpenSettings: () -> Unit) {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (showUpdateDialog) {
|
||||
val info = update
|
||||
if (info != null) UpdateDialog(info, onDismiss = { showUpdateDialog = false })
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
||||
@@ -16,6 +16,7 @@ import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.ArrowBack
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
@@ -25,7 +26,9 @@ import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
@@ -51,6 +54,7 @@ fun LogScreen(onBack: () -> Unit) {
|
||||
val logs by app.logStore.logs.collectAsState()
|
||||
val pending by app.logStore.pending.collectAsState()
|
||||
val df = remember { SimpleDateFormat("MM-dd HH:mm:ss", Locale.getDefault()) }
|
||||
var showClearDialog by remember { mutableStateOf(false) }
|
||||
|
||||
Column(Modifier.fillMaxSize()) {
|
||||
Surface(
|
||||
@@ -66,7 +70,7 @@ fun LogScreen(onBack: () -> Unit) {
|
||||
Text("转发日志", style = MaterialTheme.typography.titleLarge)
|
||||
Text("最近 300 条通知处理记录", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
}
|
||||
TextButton(onClick = { app.appScope.launch { app.logStore.clearLogs() } }) { Text("清空") }
|
||||
TextButton(onClick = { showClearDialog = true }) { Text("清空") }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,6 +107,29 @@ fun LogScreen(onBack: () -> Unit) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (showClearDialog) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { showClearDialog = false },
|
||||
title = { Text("清空日志") },
|
||||
text = { Text("选择清空范围:") },
|
||||
confirmButton = {
|
||||
TextButton(onClick = {
|
||||
showClearDialog = false
|
||||
app.appScope.launch { app.logStore.clearLogs() }
|
||||
}) { Text("仅清空日志") }
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = {
|
||||
showClearDialog = false
|
||||
app.appScope.launch {
|
||||
app.logStore.clearLogs()
|
||||
app.logStore.resetCounters()
|
||||
}
|
||||
}) { Text("日志+计数器") }
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
||||
@@ -26,6 +26,8 @@ import androidx.compose.material.icons.filled.Edit
|
||||
import androidx.compose.material.icons.filled.History
|
||||
import androidx.compose.material.icons.filled.RestartAlt
|
||||
import androidx.compose.material.icons.filled.Settings
|
||||
import androidx.compose.material.icons.filled.SystemUpdateAlt
|
||||
import androidx.compose.material.icons.filled.Update
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
@@ -60,6 +62,7 @@ import com.a2i.forwarder.model.BarkServer
|
||||
import com.a2i.forwarder.model.OFFICIAL_BARK_SERVER
|
||||
import com.a2i.forwarder.ui.ClickRow
|
||||
import com.a2i.forwarder.ui.ScreenTitle
|
||||
import com.a2i.forwarder.ui.appVersionName
|
||||
import com.a2i.forwarder.ui.SectionCard
|
||||
import com.a2i.forwarder.ui.StatusBadge
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -80,6 +83,29 @@ fun SettingsScreen(onOpenLog: () -> Unit) {
|
||||
var showAdd by remember { mutableStateOf(false) }
|
||||
var editing by remember { mutableStateOf<BarkServer?>(null) }
|
||||
|
||||
// 应用更新状态
|
||||
val updateAuto by app.settings.updateCheckEnabled.collectAsState()
|
||||
val updateInfo by app.updateChecker.updateInfo.collectAsState()
|
||||
var checking by remember { mutableStateOf(false) }
|
||||
var checkMsg by remember { mutableStateOf<String?>(null) }
|
||||
|
||||
// 短信兜底状态
|
||||
val smsFallback by app.settings.smsFallbackEnabled.collectAsState()
|
||||
val smsTarget by app.settings.smsTargetNumber.collectAsState()
|
||||
val smsCarrier by app.settings.smsCarrier.collectAsState()
|
||||
val smsQuota by app.settings.smsManualQuota.collectAsState()
|
||||
val smsBalance by app.settings.smsBalance.collectAsState()
|
||||
val smsSuspended by app.settings.smsSuspended.collectAsState()
|
||||
var smsTargetInput by remember(smsTarget) { mutableStateOf(smsTarget) }
|
||||
var smsQuotaInput by remember(smsQuota) { mutableStateOf(if (smsQuota == 0) "" else smsQuota.toString()) }
|
||||
val detectedCarrier = remember { com.a2i.forwarder.core.CarrierDetector.detect(context) }
|
||||
|
||||
val smsPermLauncher = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { }
|
||||
val hasSmsPerm = remember {
|
||||
androidx.core.content.ContextCompat.checkSelfPermission(context, android.Manifest.permission.SEND_SMS) ==
|
||||
android.content.pm.PackageManager.PERMISSION_GRANTED
|
||||
}
|
||||
|
||||
val treeLauncher = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri ->
|
||||
if (uri != null) {
|
||||
runCatching {
|
||||
@@ -197,6 +223,125 @@ fun SettingsScreen(onOpenLog: () -> Unit) {
|
||||
}
|
||||
}
|
||||
|
||||
item {
|
||||
SectionCard("断网短信兜底", subtitle = "无网络时把验证码、来电用短信发到 iPhone(每5分钟1条)") {
|
||||
com.a2i.forwarder.ui.SwitchRow(
|
||||
title = "启用短信兜底",
|
||||
subtitle = if (smsFallback) "断网时紧要通知走短信" else "当前关闭",
|
||||
checked = smsFallback,
|
||||
) { v -> app.appScope.launch { app.settings.setSmsFallback(v) } }
|
||||
|
||||
Spacer(Modifier.size(8.dp))
|
||||
OutlinedTextField(
|
||||
value = smsTargetInput,
|
||||
onValueChange = { smsTargetInput = it },
|
||||
label = { Text("iPhone 手机号") },
|
||||
supportingText = { Text("短信转发的目标号码") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
)
|
||||
Spacer(Modifier.size(4.dp))
|
||||
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
OutlinedButton(onClick = {
|
||||
app.appScope.launch { app.settings.setSmsTargetNumber(smsTargetInput.trim()) }
|
||||
}, modifier = Modifier.weight(1f)) { Text("保存号码") }
|
||||
if (!hasSmsPerm) {
|
||||
OutlinedButton(onClick = {
|
||||
smsPermLauncher.launch(android.Manifest.permission.SEND_SMS)
|
||||
}, modifier = Modifier.weight(1f)) { Text("授予短信权限") }
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(Modifier.size(8.dp))
|
||||
Text(
|
||||
"检测到运营商:${detectedCarrier.displayName}" +
|
||||
if (detectedCarrier.serviceNumber == null) "(无自动查询,将用手动额度)" else "(${detectedCarrier.serviceNumber})",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
Spacer(Modifier.size(8.dp))
|
||||
OutlinedTextField(
|
||||
value = smsQuotaInput,
|
||||
onValueChange = { smsQuotaInput = it.filter { c -> c.isDigit() } },
|
||||
label = { Text("手动短信额度") },
|
||||
supportingText = { Text("自动查询失败时按此递减;余额≤5自动停用") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
)
|
||||
Spacer(Modifier.size(4.dp))
|
||||
OutlinedButton(onClick = {
|
||||
val q = smsQuotaInput.trim().toIntOrNull() ?: 0
|
||||
app.appScope.launch {
|
||||
app.settings.setSmsManualQuota(q)
|
||||
app.settings.setSmsBalance(q)
|
||||
if (q > 5) app.settings.setSmsSuspended(false)
|
||||
}
|
||||
}) { Text("保存额度并解除挂起") }
|
||||
|
||||
Spacer(Modifier.size(8.dp))
|
||||
val balanceText = when {
|
||||
smsSuspended -> "已挂起(余额不足)"
|
||||
smsBalance < 0 -> "余额未知"
|
||||
else -> "剩余约 ${smsBalance} 条"
|
||||
}
|
||||
Text(
|
||||
"当前状态:$balanceText",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = if (smsSuspended) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
item {
|
||||
SectionCard("关于与更新", subtitle = "版本检查(数据来自 GitHub Releases)") {
|
||||
com.a2i.forwarder.ui.SwitchRow(
|
||||
title = "自动检查更新",
|
||||
subtitle = "启动时检查,且距上次满 24 小时再次检查",
|
||||
checked = updateAuto,
|
||||
icon = Icons.Filled.Update,
|
||||
) { v -> app.appScope.launch { app.settings.setUpdateCheckEnabled(v) } }
|
||||
|
||||
ClickRow(
|
||||
title = "检查更新",
|
||||
subtitle = "当前 v${appVersionName(context)}" +
|
||||
(updateInfo?.let { " · 已发现 v${it.latestVersion}" } ?: " · 点击立即检查"),
|
||||
icon = Icons.Filled.SystemUpdateAlt,
|
||||
onClick = {
|
||||
app.appScope.launch {
|
||||
checking = true
|
||||
checkMsg = null
|
||||
val r = app.updateChecker.check(force = true)
|
||||
checking = false
|
||||
checkMsg = when {
|
||||
!r.isSuccess -> "检查失败:${r.exceptionOrNull()?.message}"
|
||||
r.getOrNull() != null -> "发现新版本,请到首页更新"
|
||||
else -> "已是最新版本"
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
if (checking) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
CircularProgressIndicator(Modifier.size(16.dp), strokeWidth = 2.dp)
|
||||
Spacer(Modifier.width(8.dp))
|
||||
Text("正在检查…", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
}
|
||||
}
|
||||
checkMsg?.let {
|
||||
Text(
|
||||
it,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = if (it.startsWith("已是")) MaterialTheme.colorScheme.tertiary else MaterialTheme.colorScheme.error,
|
||||
modifier = Modifier.padding(start = 50.dp, top = 2.dp, bottom = 4.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
item {
|
||||
SectionCard("维护", subtitle = "日志查看和本地统计清理") {
|
||||
ClickRow("转发日志", "查看 / 清空最近记录", icon = Icons.Filled.History, onClick = onOpenLog)
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.a2i.forwarder.ui.screens
|
||||
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.LinearProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.a2i.forwarder.A2iApp
|
||||
import com.a2i.forwarder.core.UpdateInfo
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.File
|
||||
import java.util.Locale
|
||||
|
||||
@Composable
|
||||
fun UpdateDialog(info: UpdateInfo, onDismiss: () -> Unit) {
|
||||
val app = A2iApp.instance
|
||||
val downloading by app.updateChecker.downloading.collectAsState()
|
||||
val progress by app.updateChecker.downloadProgress.collectAsState()
|
||||
var downloadedFile by remember { mutableStateOf<File?>(null) }
|
||||
|
||||
AlertDialog(
|
||||
onDismissRequest = { if (!downloading) onDismiss() },
|
||||
title = { Text("发现新版本 v${info.latestVersion}") },
|
||||
text = {
|
||||
Column {
|
||||
Text(
|
||||
"安装包大小:${formatSize(info.apkSize)}",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
Spacer(Modifier.size(8.dp))
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.heightIn(max = 240.dp)
|
||||
.verticalScroll(rememberScrollState()),
|
||||
) {
|
||||
val notes = info.releaseNotes.ifBlank { "(作者未提供更新说明)" }
|
||||
Text(notes, style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
if (downloading) {
|
||||
Spacer(Modifier.size(12.dp))
|
||||
LinearProgressIndicator(
|
||||
progress = { progress / 100f },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
Spacer(Modifier.size(4.dp))
|
||||
Text(
|
||||
"$progress%",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
downloadedFile?.let {
|
||||
Spacer(Modifier.size(8.dp))
|
||||
Text(
|
||||
"下载完成,点击「安装」继续",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.tertiary,
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
if (downloadedFile == null) {
|
||||
Button(
|
||||
onClick = {
|
||||
app.appScope.launch {
|
||||
val f = app.updateChecker.download(info)
|
||||
if (f != null) downloadedFile = f
|
||||
}
|
||||
},
|
||||
enabled = !downloading,
|
||||
) {
|
||||
if (downloading) {
|
||||
CircularProgressIndicator(Modifier.size(16.dp), strokeWidth = 2.dp)
|
||||
} else {
|
||||
Text("下载并安装")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Button(onClick = { app.updateChecker.install(downloadedFile!!) }) { Text("安装") }
|
||||
}
|
||||
},
|
||||
dismissButton = {
|
||||
Row {
|
||||
TextButton(
|
||||
onClick = { app.updateChecker.ignoreVersion(info.latestVersion); onDismiss() },
|
||||
enabled = !downloading,
|
||||
) { Text("忽略此版本") }
|
||||
TextButton(onClick = onDismiss, enabled = !downloading) { Text("稍后") }
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fun formatSize(bytes: Long): String {
|
||||
if (bytes <= 0) return "未知"
|
||||
val kb = bytes / 1024.0
|
||||
return if (kb >= 1024) String.format(Locale.US, "%.1f MB", kb / 1024) else String.format(Locale.US, "%.0f KB", kb)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<paths>
|
||||
<external-files-path name="updates" path="." />
|
||||
<cache-path name="cache" path="." />
|
||||
</paths>
|
||||
|
After Width: | Height: | Size: 4.3 KiB |
|
After Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 6.8 KiB |
|
After Width: | Height: | Size: 31 KiB |
|
After Width: | Height: | Size: 3.5 KiB |
|
After Width: | Height: | Size: 7.1 KiB |
|
After Width: | Height: | Size: 2.0 KiB |
|
After Width: | Height: | Size: 24 KiB |
|
After Width: | Height: | Size: 17 KiB |