From 3b1c15d7c2393e4b87380e73c4c998de6ca968f3 Mon Sep 17 00:00:00 2001 From: taohebin Date: Thu, 26 Oct 2023 18:32:24 +0800 Subject: [PATCH] feat: uni-installApk --- pages/index/index.vue | 622 +++++++++--------- uni_modules/uni-installApk/changelog.md | 0 uni_modules/uni-installApk/package.json | 93 +++ uni_modules/uni-installApk/readme.md | 6 + .../utssdk/app-android/AndroidManifest.xml | 5 + .../utssdk/app-android/index.uts | 38 ++ .../uni-installApk/utssdk/app-ios/index.uts | 0 .../uni-installApk/utssdk/interface.uts | 65 ++ 8 files changed, 531 insertions(+), 298 deletions(-) create mode 100644 uni_modules/uni-installApk/changelog.md create mode 100644 uni_modules/uni-installApk/package.json create mode 100644 uni_modules/uni-installApk/readme.md create mode 100644 uni_modules/uni-installApk/utssdk/app-android/AndroidManifest.xml create mode 100644 uni_modules/uni-installApk/utssdk/app-android/index.uts create mode 100644 uni_modules/uni-installApk/utssdk/app-ios/index.uts create mode 100644 uni_modules/uni-installApk/utssdk/interface.uts diff --git a/pages/index/index.vue b/pages/index/index.vue index 744af7f..94ee4de 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -1,298 +1,324 @@ - - - - - + + + + + \ No newline at end of file diff --git a/uni_modules/uni-installApk/changelog.md b/uni_modules/uni-installApk/changelog.md new file mode 100644 index 0000000..e69de29 diff --git a/uni_modules/uni-installApk/package.json b/uni_modules/uni-installApk/package.json new file mode 100644 index 0000000..2e8c08f --- /dev/null +++ b/uni_modules/uni-installApk/package.json @@ -0,0 +1,93 @@ +{ + "id": "uni-installApk", + "displayName": "uni-installApk", + "version": "1.0.0", + "description": "uni-installApk", + "keywords": [ + "uni-installApk" + ], + "repository": "", + "engines": { + "HBuilderX": "^3.6.8" + }, + "dcloudext": { + "type": "uts", + "sale": { + "regular": { + "price": "0.00" + }, + "sourcecode": { + "price": "0.00" + } + }, + "contact": { + "qq": "" + }, + "declaration": { + "ads": "", + "data": "", + "permissions": "" + }, + "npmurl": "" + }, + "uni_modules": { + "dependencies": [], + "uni-ext-api": { + "uni": { + "installApk": { + "name": "installApk", + "app": { + "js": false, + "kotlin": true, + "swift": false + } + } + } + }, + "encrypt": [], + "platforms": { + "cloud": { + "tcb": "u", + "aliyun": "u" + }, + "client": { + "Vue": { + "vue2": "u", + "vue3": "u" + }, + "App": { + "app-android": "u", + "app-ios": "u" + }, + "H5-mobile": { + "Safari": "u", + "Android Browser": "u", + "微信浏览器(Android)": "u", + "QQ浏览器(Android)": "u" + }, + "H5-pc": { + "Chrome": "u", + "IE": "u", + "Edge": "u", + "Firefox": "u", + "Safari": "u" + }, + "小程序": { + "微信": "u", + "阿里": "u", + "百度": "u", + "字节跳动": "u", + "QQ": "u", + "钉钉": "u", + "快手": "u", + "飞书": "u", + "京东": "u" + }, + "快应用": { + "华为": "u", + "联盟": "u" + } + } + } + } +} \ No newline at end of file diff --git a/uni_modules/uni-installApk/readme.md b/uni_modules/uni-installApk/readme.md new file mode 100644 index 0000000..9e7caa4 --- /dev/null +++ b/uni_modules/uni-installApk/readme.md @@ -0,0 +1,6 @@ +# uni-installApk +### 开发文档 +[UTS 语法](https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html) +[UTS API插件](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html) +[UTS 组件插件](https://uniapp.dcloud.net.cn/plugin/uts-component.html) +[Hello UTS](https://gitcode.net/dcloud/hello-uts) \ No newline at end of file diff --git a/uni_modules/uni-installApk/utssdk/app-android/AndroidManifest.xml b/uni_modules/uni-installApk/utssdk/app-android/AndroidManifest.xml new file mode 100644 index 0000000..5cf3ce2 --- /dev/null +++ b/uni_modules/uni-installApk/utssdk/app-android/AndroidManifest.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/uni_modules/uni-installApk/utssdk/app-android/index.uts b/uni_modules/uni-installApk/utssdk/app-android/index.uts new file mode 100644 index 0000000..cf5e2cc --- /dev/null +++ b/uni_modules/uni-installApk/utssdk/app-android/index.uts @@ -0,0 +1,38 @@ +import { InstallApkOptions, InstallApkSuccess } from "../interface.uts" +import Intent from 'android.content.Intent'; +import Build from 'android.os.Build'; +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'; + +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 UniError("uni-installApk", -1, "filePath is illegal"); + 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 = { + message: "success" + } + options.success?.(success) + options.complete?.(success) +} \ No newline at end of file diff --git a/uni_modules/uni-installApk/utssdk/app-ios/index.uts b/uni_modules/uni-installApk/utssdk/app-ios/index.uts new file mode 100644 index 0000000..e69de29 diff --git a/uni_modules/uni-installApk/utssdk/interface.uts b/uni_modules/uni-installApk/utssdk/interface.uts new file mode 100644 index 0000000..5a351a7 --- /dev/null +++ b/uni_modules/uni-installApk/utssdk/interface.uts @@ -0,0 +1,65 @@ +export interface Uni { + /** + * installApk() + * @description + * 安装apk + * @param {InstallApkOptions} + * @return {void} + * @uniPlatform { + * "app": { + * "android": { + * "osVer": "4.4", + * "uniVer": "3.96+", + * "unixVer": "3.96+" + * }, + * "ios": { + * "osVer": "x", + * "uniVer": "x", + * "unixVer": "x" + * } + * } + * } + * @example + ```typescript + uni.installApk({ + filePath: "/xx/xx/xx.apk", + complete: (res: any) => { + console.log("complete => " + JSON.stringify(res)); + } + }); + ``` + */ + installApk(options : InstallApkOptions) : void +} +export type InstallApkSuccess = { + /** + * 安装成功消息 + */ + message : string +} +export type InstallApkComplete = any +export type InstallApkSuccessCallback = (res : InstallApkSuccess) => void +export type InstallApkFailCallback = (err : UniError) => void +export type InstallApkCompleteCallback = (res : InstallApkComplete) => void + +export type InstallApkOptions = { + /** + * apk文件地址 + */ + filePath : string, + /** + * 接口调用成功的回调函数 + * @defaultValue null + */ + success ?: InstallApkSuccessCallback | null, + /** + * 接口调用失败的回调函数 + * @defaultValue null + */ + fail ?: InstallApkFailCallback | null, + /** + * 接口调用结束的回调函数(调用成功、失败都会执行) + * @defaultValue null + */ + complete ?: InstallApkCompleteCallback | null +} \ No newline at end of file -- GitLab