Forráskód Böngészése

fix(update): v1.0.7 修复下载完成后不自动安装

根因:下载存盘名与安装查找名不一致。

- download() 存盘用前端传入的 fileName = `fmode-<version>.apk`(如 fmode-1.0.7.apk)
- install() 却用 URL 末段反推 = `fmode-app-v1.0.7.apk`
- 两者永远对不上 → apk.exists() 为 false → 静默 reject
- 用户端表现:「下载完了什么也没发生」

修复:
- install() 支持直接传 downloadId,原生侧用 DownloadManager 查真实落盘路径,
  不再靠猜文件名
- 前端 install() 优先传 downloadId,没有才退回 url
- install() 保留文件名路径时增加固定名兜底
- 广播里的自动安装改用 launchInstallById

另:下载页 index.html 之前多次上传未生效(上传返回 200 但桶内文件未变),
本次上传后回读校验,确认线上 FMODE_SITE 已指向 v1.0.7。
liuyuyang 5 órája
szülő
commit
3dc09ec63d

+ 39 - 5
android/app/src/main/java/cn/fmode/app/AppUpdatePlugin.java

@@ -49,6 +49,8 @@ public class AppUpdatePlugin extends Plugin {
 
     private static final String APK_MIME = "application/vnd.android.package-archive";
     private static final String DOWNLOAD_DIR = "fmode-update";
+    /** 下载落盘的固定文件名(与 download() 里 setDestinationInExternalFilesDir 一致) */
+    private static final String DOWNLOAD_FILE = "fmode-update.apk";
     /** 日志标签:自动安装失败时不打扰用户,只记 logcat */
     private static final String TAG = "FmodeUpdate";
 
@@ -229,7 +231,7 @@ public class AppUpdatePlugin extends Plugin {
      * 失败时只记日志 —— 此时用户可能已经切走了,弹错误没意义,
      * 通知栏本身还有系统下载完成提示可以点。
      */
-    private void launchInstall(long id) {
+    private void launchInstallById(long id) {
         try {
             DownloadManager manager =
                 (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
@@ -263,6 +265,32 @@ public class AppUpdatePlugin extends Plugin {
 
     @PluginMethod
     public void install(PluginCall call) {
+        /*
+         * 安装方式(2026-11-28 修「下载完不弹安装」)
+         *
+         * 原来的实现用 URL 末段拼文件名去下载目录里找:
+         *   String fileName = Uri.parse(url).getLastPathSegment();
+         *   File apk = new File(downloadDir(), fileName);
+         *
+         * 但 download() 是把文件存成固定名 DOWNLOAD_FILE 的,
+         * 两者永远对不上 → apk.exists() == false → 静默 reject,
+         * 用户端表现就是「下载完了什么也没发生」。
+         *
+         * 正确做法:按 downloadId 向 DownloadManager 查真实落盘路径。
+         * 调用方没给 id 时,才退回按文件名找(保持向后兼容)。
+         */
+        Long id = call.getLong("id");
+        if (id != null && id > 0) {
+            int status = downloadStatus(id);
+            if (status != DownloadManager.STATUS_SUCCESSFUL) {
+                call.reject("下载尚未完成");
+                return;
+            }
+            launchInstallById(id);
+            call.resolve();
+            return;
+        }
+
         String url = call.getString("url");
         if (url == null || url.isEmpty()) {
             call.reject("缺少安装包地址");
@@ -273,14 +301,20 @@ public class AppUpdatePlugin extends Plugin {
             String fileName = Uri.parse(url).getLastPathSegment();
             if (fileName == null) {
                 //noinspection SpellCheckingInspection
-                fileName = "fmode-update.apk";
+                fileName = DOWNLOAD_FILE;
             }
 
             File dir = downloadDir();
             File apk = new File(dir, fileName);
             if (!apk.exists()) {
-                call.reject("安装包不存在,请重新下载");
-                return;
+                /* 兜底:按固定名再找一次 —— 下载时用的就是这个名字 */
+                File fallback = new File(dir, DOWNLOAD_FILE);
+                if (fallback.exists()) {
+                    apk = fallback;
+                } else {
+                    call.reject("安装包不存在,请重新下载");
+                    return;
+                }
             }
 
             /*
@@ -384,7 +418,7 @@ public class AppUpdatePlugin extends Plugin {
                      */
                     int status = downloadStatus(id);
                     if (status == DownloadManager.STATUS_SUCCESSFUL) {
-                        launchInstall(id);
+                        launchInstallById(id);
                     } else {
                         /* 失败也让前端知道,好给用户提示 */
                         JSObject data = new JSObject();

+ 12 - 3
src/app/core/app-install.service.ts

@@ -46,8 +46,8 @@ interface AppUpdatePluginApi {
     progress: number;
   }>;
 
-  /** 拉起系统安装器 */
-  install(options: { url: string }): Promise<void>;
+  /** 拉起系统安装器(优先传 downloadId,原生侧据此查真实落盘路径) */
+  install(options: { url?: string; id?: number }): Promise<void>;
 
   /** 检查「安装未知应用」权限是否已授予 */
   canInstall(): Promise<{ granted: boolean }>;
@@ -187,7 +187,16 @@ export class AppInstallService {
    */
   async install(url: string): Promise<void> {
     try {
-      await AppUpdate.install({ url });
+      /*
+       * 优先按 downloadId 安装(2026-11-28 修)
+       * 原生侧用 DownloadManager 查真实落盘路径,比按 URL 猜文件名可靠。
+       * id 不存在时才退回 url 方式。
+       */
+      if (this.downloadId != null) {
+        await AppUpdate.install({ id: this.downloadId });
+      } else {
+        await AppUpdate.install({ url });
+      }
     } catch (e: unknown) {
       this.fail(e instanceof Error ? e.message : '无法启动安装');
     }