首次完整推送,
V:1.20240808.006
This commit is contained in:
@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
|
||||
package="io.dcloud.uni.installApk">
|
||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
|
||||
</manifest>
|
98
uni_modules/uni-installApk/utssdk/app-android/index.uts
Normal file
98
uni_modules/uni-installApk/utssdk/app-android/index.uts
Normal file
@ -0,0 +1,98 @@
|
||||
import { InstallApkOptions, InstallApkSuccess } from "../interface.uts"
|
||||
import { InstallApkFailImpl } from "../unierror.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';
|
||||
import FileOutputStream from 'java.io.FileOutputStream';
|
||||
import IOException from 'java.io.IOException';
|
||||
|
||||
export function installApk(options : InstallApkOptions) : void {
|
||||
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()
|
||||
|
||||
|
||||
if (Build.VERSION.SDK_INT < 24) {
|
||||
changePermissionRecursive(new File(context.getCacheDir().getPath() + "/apks/"))
|
||||
}
|
||||
|
||||
return outFile
|
||||
} catch (e : Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
function changePermissionRecursive(file: File){
|
||||
const cmd = "chmod -R 777 " + file.getAbsolutePath()
|
||||
const runtime = Runtime.getRuntime()
|
||||
try {
|
||||
runtime.exec(cmd)
|
||||
} catch (e: IOException) {
|
||||
}
|
||||
}
|
78
uni_modules/uni-installApk/utssdk/index.d.ts
vendored
Normal file
78
uni_modules/uni-installApk/utssdk/index.d.ts
vendored
Normal file
@ -0,0 +1,78 @@
|
||||
declare namespace UniNamespace {
|
||||
|
||||
interface InstallApkSuccess {
|
||||
/**
|
||||
* 安装成功消息
|
||||
*/
|
||||
errMsg : string
|
||||
}
|
||||
|
||||
type InstallApkErrorCode = 1300002
|
||||
interface InstallApkFail {
|
||||
errCode : InstallApkErrorCode
|
||||
}
|
||||
|
||||
type InstallApkComplete = any
|
||||
|
||||
type InstallApkSuccessCallback = (res : InstallApkSuccess) => void
|
||||
type InstallApkFailCallback = (err : InstallApkFail) => void
|
||||
type InstallApkCompleteCallback = (res : InstallApkComplete) => void
|
||||
|
||||
interface InstallApkOptions {
|
||||
/**
|
||||
* apk文件地址
|
||||
*/
|
||||
filePath : string,
|
||||
/**
|
||||
* 接口调用成功的回调函数
|
||||
* @defaultValue null
|
||||
*/
|
||||
success ?: InstallApkSuccessCallback | null,
|
||||
/**
|
||||
* 接口调用失败的回调函数
|
||||
* @defaultValue null
|
||||
*/
|
||||
fail ?: InstallApkFailCallback | null,
|
||||
/**
|
||||
* 接口调用结束的回调函数(调用成功、失败都会执行)
|
||||
* @defaultValue null
|
||||
*/
|
||||
complete ?: InstallApkCompleteCallback | null
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
declare interface Uni {
|
||||
/**
|
||||
* installApk()
|
||||
* @description
|
||||
* 安装apk
|
||||
* @param {InstallApkOptions}
|
||||
* @return {void}
|
||||
* @uniPlatform {
|
||||
* "app": {
|
||||
* "android": {
|
||||
* "osVer": "5.0",
|
||||
* "uniVer": "3.94+",
|
||||
* "unixVer": "3.94+"
|
||||
* },
|
||||
* "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 : UniNamespace.InstallApkOptions) : void
|
||||
}
|
80
uni_modules/uni-installApk/utssdk/interface.uts
Normal file
80
uni_modules/uni-installApk/utssdk/interface.uts
Normal file
@ -0,0 +1,80 @@
|
||||
export interface Uni {
|
||||
/**
|
||||
* installApk()
|
||||
* @description
|
||||
* 安装apk
|
||||
* @param {InstallApkOptions}
|
||||
* @return {void}
|
||||
* @uniPlatform {
|
||||
* "app": {
|
||||
* "android": {
|
||||
* "osVer": "5.0",
|
||||
* "uniVer": "3.94+",
|
||||
* "unixVer": "3.94+"
|
||||
* },
|
||||
* "ios": {
|
||||
* "osVer": "x",
|
||||
* "uniVer": "x",
|
||||
* "unixVer": "x"
|
||||
* }
|
||||
* },
|
||||
* "web": {
|
||||
* "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 = {
|
||||
/**
|
||||
* 安装成功消息
|
||||
*/
|
||||
errMsg : string
|
||||
}
|
||||
export type InstallApkComplete = any
|
||||
export type InstallApkSuccessCallback = (res : InstallApkSuccess) => void
|
||||
/**
|
||||
* 错误码
|
||||
* - 1300002 找不到文件
|
||||
*/
|
||||
export type InstallApkErrorCode = 1300002
|
||||
/**
|
||||
* 网络请求失败的错误回调参数
|
||||
*/
|
||||
export interface InstallApkFail extends IUniError {
|
||||
errCode : InstallApkErrorCode
|
||||
};
|
||||
export type InstallApkFailCallback = (err : InstallApkFail) => 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
|
||||
}
|
25
uni_modules/uni-installApk/utssdk/unierror.uts
Normal file
25
uni_modules/uni-installApk/utssdk/unierror.uts
Normal file
@ -0,0 +1,25 @@
|
||||
import { InstallApkErrorCode, InstallApkFail } from "./interface.uts"
|
||||
|
||||
/**
|
||||
* 错误主题
|
||||
*/
|
||||
export const UniErrorSubject = 'uni-installApk';
|
||||
/**
|
||||
* 错误码
|
||||
* @UniError
|
||||
*/
|
||||
export const UniErrors : Map<InstallApkErrorCode, string> = new Map([
|
||||
/**
|
||||
* 找不到文件
|
||||
*/
|
||||
[1300002, 'No such file'],
|
||||
]);
|
||||
|
||||
export class InstallApkFailImpl extends UniError implements InstallApkFail {
|
||||
constructor(errCode : InstallApkErrorCode) {
|
||||
super();
|
||||
this.errSubject = UniErrorSubject;
|
||||
this.errCode = errCode;
|
||||
this.errMsg = UniErrors[errCode] ?? "";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user