Bladeren bron

fix(update): v1.0.12 自动安装改双保险 + 失败原因可见

## 为什么之前反复修不好(这次用 git 历史定位)

用户关键线索:1.0.2 能自动弹安装,之后就弹不出。

git show c188eb1(1.0.2)证实:那时广播里只有一句
  notifyListeners("downloadComplete") —— 前端收到再去调 install()。
这条路当时是通的,所以能弹。

后来我在广播里改成「原生直接安装」,并且把成功分支的
notifyListeners 移到 else 里 —— 等于只剩原生一条路。
原生那条在部分机型上拿不到文件或权限被拦,就彻底没反应,
前端也收不到事件,连提示都给不了。

## 这版改法

广播里两条路都走:
  · 成功:原生先 launchInstallById(),同时 notifyListeners 发出去
  · 失败:只 notifyListeners,让前端提示

同时核实了前端:app-install.service 里有 500ms 轮询
(getProgress 检测 successful → install(downloadId)),
这条路径是完整的,配合原生形成三重保险。

## 失败原因可见化

安装链路之前完全静默:失败只写 logcat,用户手机上看不到任何东西,
只能反馈「没反应」,我这边全靠猜,来回改了四版都没修对。

现在每个失败分支都弹 toast:
  · 下载服务不可用
  · 找不到安装包文件
  · 请先允许安装未知应用
  · 安装失败:<异常类名>

下次再不弹,用户截图就能直接看到卡在哪一步,不用接 adb。
liuyuyang 5 uur geleden
bovenliggende
commit
6d266fafc0

+ 38 - 6
android/app/src/main/java/cn/fmode/app/AppUpdatePlugin.java

@@ -232,12 +232,30 @@ public class AppUpdatePlugin extends Plugin {
      * 失败时只记日志 —— 此时用户可能已经切走了,弹错误没意义,
      * 通知栏本身还有系统下载完成提示可以点。
      */
+    /**
+     * 轻提示(2026-11-29)
+     *
+     * 安装链路之前完全静默:失败只写 logcat,用户手机上什么也看不到,
+     * 只能反馈「没反应」,排查全靠猜。现在关键失败点都弹一句,
+     * 用户直接截图就能告诉我们卡在哪一步。
+     */
+    private void toast(final String msg) {
+        try {
+            new android.os.Handler(android.os.Looper.getMainLooper()).post(() ->
+                android.widget.Toast.makeText(getContext(), msg, android.widget.Toast.LENGTH_LONG).show()
+            );
+        } catch (Exception ignored) {
+            /* toast 本身失败不影响主流程 */
+        }
+    }
+
     private void launchInstallById(long id) {
         try {
             DownloadManager manager =
                 (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
             if (manager == null) {
                 Log.w(TAG, "install abort: DownloadManager null");
+                toast("安装失败:下载服务不可用");
                 return;
             }
 
@@ -259,6 +277,7 @@ public class AppUpdatePlugin extends Plugin {
             Uri apkUri = manager.getUriForDownloadedFile(id);
             if (apkUri == null) {
                 Log.w(TAG, "install abort: no uri for downloaded file");
+                toast("安装失败:找不到安装包文件");
                 return;
             }
             Log.i(TAG, "install id=" + id + " uri=" + apkUri);
@@ -271,6 +290,7 @@ public class AppUpdatePlugin extends Plugin {
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
                     && !getContext().getPackageManager().canRequestPackageInstalls()) {
                 Log.w(TAG, "install blocked: REQUEST_INSTALL_PACKAGES not granted");
+                toast("请先允许安装未知应用");
                 Intent perm = new Intent(android.provider.Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
                 perm.setData(Uri.parse("package:" + getContext().getPackageName()));
                 perm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
@@ -286,6 +306,7 @@ public class AppUpdatePlugin extends Plugin {
             Log.i(TAG, "install launched");
         } catch (Exception e) {
             Log.w(TAG, "install failed", e);
+            toast("安装失败:" + e.getClass().getSimpleName());
         }
     }
 
@@ -454,15 +475,26 @@ public class AppUpdatePlugin extends Plugin {
                      * 先校验下载状态,确认真的成功了再拉起安装器,避免半截包报错。
                      */
                     int status = downloadStatus(id);
+                    /*
+                     * 双保险:两条路都走(2026-11-29)
+                     *
+                     * 复盘:1.0.2 只有 notifyListeners 一条路(前端收到再调 install),
+                     * 那时自动弹安装是好的。后来我改成「原生直接安装」,
+                     * 把成功分支的 notifyListeners 去掉了 —— 只剩原生一条路。
+                     * 原生那条在部分机型拿不到文件或权限被拦,就彻底没反应,
+                     * 前端也不知道,连提示都给不了。
+                     *
+                     * 现在:成功先由原生拉起安装器,同时把事件也发给前端;
+                     * 失败只发事件。两条路任一条通,用户就能看到安装界面。
+                     * (原生已拉起安装器时,前端再调一次会被系统忽略,无害。)
+                     */
                     if (status == DownloadManager.STATUS_SUCCESSFUL) {
                         launchInstallById(id);
-                    } else {
-                        /* 失败也让前端知道,好给用户提示 */
-                        JSObject data = new JSObject();
-                        data.put("id", id);
-                        data.put("status", status);
-                        notifyListeners("downloadComplete", data);
                     }
+                    JSObject data = new JSObject();
+                    data.put("id", id);
+                    data.put("status", status);
+                    notifyListeners("downloadComplete", data);
                 }
             };
             IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);

+ 1 - 1
prototypes/fmode-download-20261120/index.html

@@ -1331,7 +1331,7 @@ var qrcodegen;
 })(qrcodegen || (qrcodegen = {}));
 
 // 填入 apkUrl 即可开放下载;版本信息等字段按需提供。channel 为渠道标注(如「测试版」),可空。
-const FMODE_SITE = { apkUrl: 'https://ai.fmode.cn/download/fmode-app-v1.0.11.apk', version: '1.0.11', fileSize: '13.5 MB', updatedAt: '2026-11-29', channel: '测试版' };
+const FMODE_SITE = { apkUrl: 'https://ai.fmode.cn/download/fmode-app-v1.0.12.apk', version: '1.0.12', fileSize: '13.5 MB', updatedAt: '2026-11-29', channel: '测试版' };
 const header = document.getElementById('header');
 const menuToggle = document.getElementById('menuToggle');
 const navLinks = document.getElementById('navLinks');

+ 1 - 1
prototypes/fmode-download-20261120/site.js

@@ -1,5 +1,5 @@
 // 填入 apkUrl 即可开放下载;版本信息等字段按需提供。channel 为渠道标注(如「测试版」),可空。
-const FMODE_SITE = { apkUrl: 'https://ai.fmode.cn/download/fmode-app-v1.0.11.apk', version: '1.0.11', fileSize: '13.5 MB', updatedAt: '2026-11-29', channel: '测试版' };
+const FMODE_SITE = { apkUrl: 'https://ai.fmode.cn/download/fmode-app-v1.0.12.apk', version: '1.0.12', fileSize: '13.5 MB', updatedAt: '2026-11-29', channel: '测试版' };
 const header = document.getElementById('header');
 const menuToggle = document.getElementById('menuToggle');
 const navLinks = document.getElementById('navLinks');