修复安装下载目录空指针、安装会话与流泄漏,下载校验切到 IO 线程
This commit is contained in:
1 parent
53873943b2
commit
a75dada468
4 files changed
+94
-23
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从下载地址中取出文件名,自动去掉 ? 之后的查询参数。
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user