From e05bf0a938cf15077e4198c9323ba185660246fd Mon Sep 17 00:00:00 2001 From: taohebin Date: Fri, 8 Dec 2023 17:46:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=BC=E5=AE=B9asset=E7=9B=AE?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uni-installApk/changelog.md | 2 + uni_modules/uni-installApk/package.json | 2 +- .../utssdk/app-android/index.uts | 104 +++++++++++++----- 3 files changed, 77 insertions(+), 31 deletions(-) diff --git a/uni_modules/uni-installApk/changelog.md b/uni_modules/uni-installApk/changelog.md index f6c9583..d67eb70 100644 --- a/uni_modules/uni-installApk/changelog.md +++ b/uni_modules/uni-installApk/changelog.md @@ -1,3 +1,5 @@ +## 1.0.4(2023-12-08) +兼容asset目录文件的处理 ## 1.0.3(2023-10-27) 遵循UniError规范 ## 1.0.2(2023-10-27) diff --git a/uni_modules/uni-installApk/package.json b/uni_modules/uni-installApk/package.json index c388920..2270fe2 100644 --- a/uni_modules/uni-installApk/package.json +++ b/uni_modules/uni-installApk/package.json @@ -1,7 +1,7 @@ { "id": "uni-installApk", "displayName": "uni-installApk", - "version": "1.0.3", + "version": "1.0.4", "description": "uni-installApk", "keywords": [ "uni-installApk" diff --git a/uni_modules/uni-installApk/utssdk/app-android/index.uts b/uni_modules/uni-installApk/utssdk/app-android/index.uts index cfdef2e..64e7b6b 100644 --- a/uni_modules/uni-installApk/utssdk/app-android/index.uts +++ b/uni_modules/uni-installApk/utssdk/app-android/index.uts @@ -1,4 +1,4 @@ -import { InstallApkOptions, InstallApkSuccess } from "../interface.uts" +import { InstallApkOptions, InstallApkSuccess } from "../interface.uts" import { InstallApkFailImpl } from "../unierror.uts" import Intent from 'android.content.Intent'; import Build from 'android.os.Build'; @@ -6,34 +6,78 @@ import File from 'java.io.File'; import FileProvider from 'androidx.core.content.FileProvider'; import Context from 'android.content.Context'; import Uri from 'android.net.Uri'; +import FileOutputStream from 'java.io.FileOutputStream'; export function installApk(options : InstallApkOptions) : void { - const context = UTSAndroid.getAppContext() as Context - const filePath = UTSAndroid.convert2AbsFullPath(options.filePath) - const apkFile = new File(filePath) - if (!apkFile.exists() && !apkFile.isFile()) { - let error = new InstallApkFailImpl(1300002); - options.fail?.(error) - options.complete?.(error) - return - } - const intent = new Intent() - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - intent.setAction(Intent.ACTION_VIEW) - - if (Build.VERSION.SDK_INT >= 24) { - const authority = context.getPackageName() + ".dc.fileprovider" - const apkUri = FileProvider.getUriForFile(context, authority, apkFile) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); - } else { - intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); - } - - context.startActivity(intent) - const success : InstallApkSuccess = { - errMsg: "success" - } - options.success?.(success) - options.complete?.(success) -} \ No newline at end of file + const context = UTSAndroid.getAppContext() as Context + var filePath = UTSAndroid.convert2AbsFullPath(options.filePath) + var apkFile : File | null = null; + if (filePath.startsWith("/android_asset/")) { + filePath = filePath.replace("/android_asset/", "") + apkFile = copyAssetFileToPrivateDir(context, filePath) + } else { + apkFile = new File(filePath) + } + + if (apkFile != null && !apkFile.exists() && !apkFile.isFile()) { + let error = new InstallApkFailImpl(1300002); + options.fail?.(error) + options.complete?.(error) + return + } + const intent = new Intent() + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + intent.setAction(Intent.ACTION_VIEW) + + if (Build.VERSION.SDK_INT >= 24) { + const authority = context.getPackageName() + ".dc.fileprovider" + const apkUri = FileProvider.getUriForFile(context, authority, apkFile!!) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); + } else { + intent.setDataAndType(Uri.fromFile(apkFile!!), "application/vnd.android.package-archive"); + } + + context.startActivity(intent) + const success : InstallApkSuccess = { + errMsg: "success" + } + options.success?.(success) + options.complete?.(success) +} + + +function copyAssetFileToPrivateDir(context : Context, fileName : string) : File | null { + try { + const destPath = context.getCacheDir().getPath() + "/apks/" + fileName + const outFile = new File(destPath) + const parentFile = outFile.getParentFile() + if (parentFile != null) { + if (!parentFile.exists()) { + parentFile.mkdirs() + } + } + if (!outFile.exists()) { + outFile.createNewFile() + } + const inputStream = context.getAssets().open(fileName) + const outputStream = new FileOutputStream(outFile) + let buffer = new ByteArray(1024); + do { + let len = inputStream.read(buffer); + if (len == -1) { + break; + } + outputStream.write(buffer, 0, len) + } while (true) + + inputStream.close() + outputStream.close() + + return outFile + } catch (e : Exception) { + e.printStackTrace() + } + + return null +} -- GitLab