From a75dada468207e525b96dded3b7468b860d65539 Mon Sep 17 00:00:00 2001 From: tongtongstudio Date: Wed, 25 Nov 2026 17:50:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AE=89=E8=A3=85=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E7=9B=AE=E5=BD=95=E7=A9=BA=E6=8C=87=E9=92=88=E3=80=81?= =?UTF-8?q?=E5=AE=89=E8=A3=85=E4=BC=9A=E8=AF=9D=E4=B8=8E=E6=B5=81=E6=B3=84?= =?UTF-8?q?=E6=BC=8F=EF=BC=8C=E4=B8=8B=E8=BD=BD=E6=A0=A1=E9=AA=8C=E5=88=87?= =?UTF-8?q?=E5=88=B0=20IO=20=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fuying/sn/platform/app/ApkUtils.java | 90 ++++++++++++++----- .../fuying/sn/platform/system/JgyUtils.java | 15 +++- .../runtime/receiver/ApkInstallReceiver.java | 5 ++ .../sn/runtime/service/LogcatService.java | 7 +- 4 files changed, 94 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/fuying/sn/platform/app/ApkUtils.java b/app/src/main/java/com/fuying/sn/platform/app/ApkUtils.java index 3504331..775e6af 100644 --- a/app/src/main/java/com/fuying/sn/platform/app/ApkUtils.java +++ b/app/src/main/java/com/fuying/sn/platform/app/ApkUtils.java @@ -33,6 +33,7 @@ import com.fuying.sn.platform.system.JgyUtils; import com.google.gson.JsonObject; import java.io.BufferedReader; +import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -334,37 +335,48 @@ public class ApkUtils { boolean success = false; File apkFile = new File(apkFilePath); PackageInstaller.Session session = null; + OutputStream out = null; + FileInputStream input = null; try { session = pi.openSession(sessionId); - OutputStream out = session.openWrite("app.apk", 0, apkFile.length()); - FileInputStream input = new FileInputStream(apkFile); - int read = 0; + out = session.openWrite("app.apk", 0, apkFile.length()); + input = new FileInputStream(apkFile); + int read; byte[] buffer = new byte[65536]; -// while (read != -1) { -// read = input.read(buffer); -// out.write(buffer, 0, read); -// } - while (true) { - read = input.read(buffer); - if (read == -1) { - session.fsync(out); - success = true; - out.close(); - input.close(); - break; - } + while ((read = input.read(buffer)) != -1) { out.write(buffer, 0, read); } + session.fsync(out); + success = true; } catch (IOException e) { Log.e(TAG, "copyApkFile: " + e.getMessage(), e); + } finally { + // 失败时也必须关闭流与会话,否则会持续泄漏文件描述符 + closeQuietly(input); + closeQuietly(out); + if (session != null) { + session.close(); + } } return success; } + private static void closeQuietly(Closeable closeable) { + if (closeable == null) { + return; + } + try { + closeable.close(); + } catch (IOException e) { + Log.e(TAG, "closeQuietly: " + e.getMessage(), e); + } + } + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private static void install(PackageInstaller packageInstaller, int sessionId, Context context) { + PackageInstaller.Session session = null; try { - PackageInstaller.Session session = packageInstaller.openSession(sessionId); + session = packageInstaller.openSession(sessionId); Intent intent = new Intent(context, InstallResultReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( context, @@ -374,6 +386,11 @@ public class ApkUtils { session.commit(pendingIntent.getIntentSender()); } catch (IOException e) { Log.e(TAG, "install: " + e.getMessage(), e); + } finally { + // commit 之后仍需关闭会话对象,否则会泄漏 + if (session != null) { + session.close(); + } } } @@ -652,6 +669,21 @@ public class ApkUtils { * @param jsonObject 后端下发的应用信息,用于取 MD5 与透传给下载引擎 */ public static void ariaDownload(Context context, String url, JsonObject jsonObject) { + if (context == null) { + return; + } + Context appContext = context.getApplicationContext(); + // 校验整包 MD5、触发安装或下载都属于耗时操作,统一放到 IO 线程,避免调用方在主线程时卡顿 + Schedulers.io().scheduleDirect(() -> { + try { + doAriaDownload(appContext, url, jsonObject); + } catch (Exception e) { + Log.e(TAG, "ariaDownload: " + e.getMessage(), e); + } + }); + } + + private static void doAriaDownload(Context context, String url, JsonObject jsonObject) { String fileName = getFileNamefromURL(url); if (TextUtils.isEmpty(fileName)) { Log.e(TAG, "ariaDownload: 无法从下载地址解析文件名 " + url); @@ -678,9 +710,8 @@ public class ApkUtils { } Log.e(TAG, "ariaDownload: fileName=" + fileName + " expectedMD5=" + expectedMD5); - String downloadPath = context.getExternalFilesDirs(Environment.DIRECTORY_DOWNLOADS)[0].getAbsolutePath() - + File.separator; - File file = new File(downloadPath, fileName); + File downloadDir = resolveDownloadDir(context); + File file = new File(downloadDir, fileName); if (file.isFile() && !TextUtils.isEmpty(expectedMD5)) { String fileMD5 = EncryptUtils.encryptMD5File2String(file); Log.e(TAG, "ariaDownload: fileMD5=" + fileMD5 + " expectedMD5=" + expectedMD5); @@ -697,6 +728,25 @@ public class ApkUtils { .create(); // 启动下载 } + /** + * 下载目录:外部存储未挂载时 getExternalFilesDirs 可能返回空数组或空元素, + * 直接取 [0] 会空指针,这里回退到应用内部目录。 + */ + private static File resolveDownloadDir(Context context) { + File[] dirs = context.getExternalFilesDirs(Environment.DIRECTORY_DOWNLOADS); + File dir = null; + if (dirs != null && dirs.length > 0) { + dir = dirs[0]; + } + if (dir == null) { + dir = context.getFilesDir(); + } + if (dir == null) { + dir = context.getCacheDir(); + } + return dir; + } + /** * 从下载地址中取出文件名,自动去掉 ? 之后的查询参数。 * diff --git a/app/src/main/java/com/fuying/sn/platform/system/JgyUtils.java b/app/src/main/java/com/fuying/sn/platform/system/JgyUtils.java index 1c0ac03..b595b63 100644 --- a/app/src/main/java/com/fuying/sn/platform/system/JgyUtils.java +++ b/app/src/main/java/com/fuying/sn/platform/system/JgyUtils.java @@ -242,8 +242,19 @@ public class JgyUtils { public String getDownLoadPath() { - String path = ContextCompat.getExternalFilesDirs(mContext, Environment.DIRECTORY_DOWNLOADS)[0].getAbsolutePath(); - return path + File.separator; + File[] dirs = ContextCompat.getExternalFilesDirs(mContext, Environment.DIRECTORY_DOWNLOADS); + File dir = null; + if (dirs != null && dirs.length > 0) { + dir = dirs[0]; + } + if (dir == null) { + // 外部存储未挂载时退回内部目录,避免空指针 + dir = mContext.getFilesDir(); + } + if (dir == null) { + dir = mContext.getCacheDir(); + } + return dir.getAbsolutePath() + File.separator; } /** diff --git a/app/src/main/java/com/fuying/sn/runtime/receiver/ApkInstallReceiver.java b/app/src/main/java/com/fuying/sn/runtime/receiver/ApkInstallReceiver.java index 3f1eccd..a69ef26 100644 --- a/app/src/main/java/com/fuying/sn/runtime/receiver/ApkInstallReceiver.java +++ b/app/src/main/java/com/fuying/sn/runtime/receiver/ApkInstallReceiver.java @@ -51,6 +51,11 @@ public class ApkInstallReceiver extends BroadcastReceiver { Log.e(TAG, "handleBroadcast: action is empty "); return; } + if (TextUtils.isEmpty(dataString)) { + // 广播过滤器要求带 package: scheme,缺失时无法解析包名,直接忽略 + Log.e(TAG, "handleBroadcast: dataString is empty, action = " + action); + return; + } String packageName = dataString.replace("package:", ""); String actionDesc; switch (action) { diff --git a/app/src/main/java/com/fuying/sn/runtime/service/LogcatService.java b/app/src/main/java/com/fuying/sn/runtime/service/LogcatService.java index 6b5c4f2..04f43ce 100644 --- a/app/src/main/java/com/fuying/sn/runtime/service/LogcatService.java +++ b/app/src/main/java/com/fuying/sn/runtime/service/LogcatService.java @@ -64,7 +64,12 @@ public class LogcatService extends BaseService { @Override public void onCreate() { super.onCreate(); - logFilePath = getExternalCacheDir() + File.separator + "LOG" + File.separator; + // 外部存储未挂载时 getExternalCacheDir 会返回 null,回退到内部缓存目录,避免拼出 "null/LOG/" + File cacheDir = getExternalCacheDir(); + if (cacheDir == null) { + cacheDir = getCacheDir(); + } + logFilePath = cacheDir + File.separator + "LOG" + File.separator; File file = new File(logFilePath); if (!file.exists()) { file.mkdirs();