Browse Source

fix(update): v1.0.9 彻底修复下载完成不自动安装

真因(前两次修错了地方):
DownloadManager 在 targetSdk 36 下,COLUMN_LOCAL_URI 返回的可能是
content:// 形式。原代码 new File(Uri.parse(localUri).getPath())
解出来的不是文件系统真实路径,于是 apk.exists() 恒为 false,
静默 return,永远走不到安装那一步。

这也是为什么 1.0.7 加了「按 downloadId 查路径」仍然没用 ——
查路径这一步本身就取不到有效路径。

修复:
- 改用 DownloadManager.getUriForDownloadedFile(id)
  直接拿系统给的可安装 content:// URI,一步到位,
  不做路径映射、也不经 FileProvider
- 试过 openDownloadedFile(id),但它返回 ParcelFileDescriptor 不是 File,
  包 uri 容易踩授权问题,已放弃并在注释里记下
- 新增安装权限主动检查:无「安装未知应用」权限时直接跳转设置页,
  而不是装完被系统拦下、用户以为没反应
- 每个失败分支都写了 Log.i/Log.w,logcat 可一眼定位卡在哪一步

教训:前两次都是靠读代码推断,没有验证 DownloadManager 的实际返回形态。
Android 分区存储之后,凡是「拿路径再 new File」的写法都要怀疑。
liuyuyang 3 giờ trước cách đây
mục cha
commit
60b565d67e

+ 52 - 15
android/app/src/main/java/cn/fmode/app/AppUpdatePlugin.java

@@ -6,6 +6,7 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.database.Cursor;
+import android.util.Log;
 import android.net.Uri;
 import android.os.Build;
 import android.os.Environment;
@@ -235,31 +236,56 @@ public class AppUpdatePlugin extends Plugin {
         try {
             DownloadManager manager =
                 (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
-            if (manager == null) return;
-
-            DownloadManager.Query q = new DownloadManager.Query().setFilterById(id);
-            String localUri = null;
-            try (Cursor c = manager.query(q)) {
-                if (c != null && c.moveToFirst()) {
-                    int idx = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
-                    if (idx >= 0) localUri = c.getString(idx);
-                }
+            if (manager == null) {
+                Log.w(TAG, "install abort: DownloadManager null");
+                return;
             }
-            if (localUri == null) return;
 
-            File apk = new File(Uri.parse(localUri).getPath());
-            if (!apk.exists()) return;
+            /*
+             * 🔴 关键修复(2026-11-28,自动安装一直不生效的真因)
+             *
+             * 原来用 COLUMN_LOCAL_URI 拿路径再 new File(uri.getPath()):
+             *   targetSdk 36 下这个值可能是 content:// 形式,
+             *   getPath() 解出来的不是文件系统真实路径,
+             *   于是 apk.exists() 恒为 false → 静默 return → 永不安装。
+             *
+             * 试过 openDownloadedFile() —— 它返回 ParcelFileDescriptor 不是 File,
+             * 在 PFD 上包 Format/file uri 很容易踩授权问题,放弃。
+             *
+             * 最终采用 getUriForDownloadedFile(id):
+             *   DownloadManager 直接给出可用的 content:// uri,
+             *   一步到位,不用自己做路径映射,也不用再过 FileProvider。
+             */
+            Uri apkUri = manager.getUriForDownloadedFile(id);
+            if (apkUri == null) {
+                Log.w(TAG, "install abort: no uri for downloaded file");
+                return;
+            }
+            Log.i(TAG, "install id=" + id + " uri=" + apkUri);
 
-            Uri apkUri = FileProvider.getUriForFile(
-                getContext(), getContext().getPackageName() + ".fileprovider", apk);
+            /*
+             * 安装权限检查(2026-11-28)
+             * 没有「安装未知应用」权限时,startActivity 会被系统拦下,
+             * 用户看到的是「没反应」。这里主动把用户送到开启页面。
+             */
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
+                    && !getContext().getPackageManager().canRequestPackageInstalls()) {
+                Log.w(TAG, "install blocked: REQUEST_INSTALL_PACKAGES not granted");
+                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);
+                getContext().startActivity(perm);
+                return;
+            }
 
             Intent intent = new Intent(Intent.ACTION_VIEW);
             intent.setDataAndType(apkUri, APK_MIME);
             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
             getContext().startActivity(intent);
+            Log.i(TAG, "install launched");
         } catch (Exception e) {
-            android.util.Log.w(TAG, "自动安装失败", e);
+            Log.w(TAG, "install failed", e);
         }
     }
 
@@ -324,6 +350,17 @@ public class AppUpdatePlugin extends Plugin {
             Uri apkUri = FileProvider.getUriForFile(
                 getContext(), getContext().getPackageName() + ".fileprovider", apk);
 
+            /* 安装权限检查(2026-11-28,与 launchInstallById 保持一致) */
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
+                    && !getContext().getPackageManager().canRequestPackageInstalls()) {
+                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);
+                getContext().startActivity(perm);
+                call.reject("需要开启「安装未知应用」权限,已为你跳转设置");
+                return;
+            }
+
             Intent intent = new Intent(Intent.ACTION_VIEW);
             intent.setDataAndType(apkUri, APK_MIME);
             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

+ 5 - 5
android/capacitor.settings.gradle

@@ -1,15 +1,15 @@
 // DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN
 include ':capacitor-android'
-project(':capacitor-android').projectDir = new File('../node_modules/.pnpm/@capacitor+android@8.6.2_@capacitor+core@8.6.2/node_modules/@capacitor/android/capacitor')
+project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/android/capacitor')
 
 include ':capacitor-app'
-project(':capacitor-app').projectDir = new File('../node_modules/.pnpm/@capacitor+app@8.1.2_@capacitor+core@8.6.2/node_modules/@capacitor/app/android')
+project(':capacitor-app').projectDir = new File('../node_modules/@capacitor/app/android')
 
 include ':capacitor-local-notifications'
-project(':capacitor-local-notifications').projectDir = new File('../node_modules/.pnpm/@capacitor+local-notifications@8.3.1_@capacitor+core@8.6.2/node_modules/@capacitor/local-notifications/android')
+project(':capacitor-local-notifications').projectDir = new File('../node_modules/@capacitor/local-notifications/android')
 
 include ':capacitor-share'
-project(':capacitor-share').projectDir = new File('../node_modules/.pnpm/@capacitor+share@8.0.3_@capacitor+core@8.6.2/node_modules/@capacitor/share/android')
+project(':capacitor-share').projectDir = new File('../node_modules/@capacitor/share/android')
 
 include ':capacitor-status-bar'
-project(':capacitor-status-bar').projectDir = new File('../node_modules/.pnpm/@capacitor+status-bar@8.0.4_@capacitor+core@8.6.2/node_modules/@capacitor/status-bar/android')
+project(':capacitor-status-bar').projectDir = new File('../node_modules/@capacitor/status-bar/android')