|
@@ -6,6 +6,7 @@ import android.content.Context;
|
|
|
import android.content.Intent;
|
|
import android.content.Intent;
|
|
|
import android.content.IntentFilter;
|
|
import android.content.IntentFilter;
|
|
|
import android.database.Cursor;
|
|
import android.database.Cursor;
|
|
|
|
|
+import android.util.Log;
|
|
|
import android.net.Uri;
|
|
import android.net.Uri;
|
|
|
import android.os.Build;
|
|
import android.os.Build;
|
|
|
import android.os.Environment;
|
|
import android.os.Environment;
|
|
@@ -235,31 +236,56 @@ public class AppUpdatePlugin extends Plugin {
|
|
|
try {
|
|
try {
|
|
|
DownloadManager manager =
|
|
DownloadManager manager =
|
|
|
(DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
|
|
(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 intent = new Intent(Intent.ACTION_VIEW);
|
|
|
intent.setDataAndType(apkUri, APK_MIME);
|
|
intent.setDataAndType(apkUri, APK_MIME);
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
getContext().startActivity(intent);
|
|
getContext().startActivity(intent);
|
|
|
|
|
+ Log.i(TAG, "install launched");
|
|
|
} catch (Exception e) {
|
|
} 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(
|
|
Uri apkUri = FileProvider.getUriForFile(
|
|
|
getContext(), getContext().getPackageName() + ".fileprovider", apk);
|
|
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 intent = new Intent(Intent.ACTION_VIEW);
|
|
|
intent.setDataAndType(apkUri, APK_MIME);
|
|
intent.setDataAndType(apkUri, APK_MIME);
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|