index.uts 3.0 KB
Newer Older
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
1
import { InstallApkOptions, InstallApkSuccess } from "../interface.uts"
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
2
import { InstallApkFailImpl } from "../unierror.uts"
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
3 4 5 6 7 8
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';
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
9 10
import FileOutputStream from 'java.io.FileOutputStream';
import IOException from 'java.io.IOException';
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
11 12

export function installApk(options : InstallApkOptions) : void {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  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");
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
38
  } else {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    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()
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
63
    }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
64 65 66 67 68 69 70 71 72 73 74 75
    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()
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
76 77 78 79 80 81 82
    outputStream.close()
	
	
	if (Build.VERSION.SDK_INT < 24) {
		changePermissionRecursive(new File(context.getCacheDir().getPath() + "/apks/"))
	}
	
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
83 84 85 86 87 88 89
    return outFile
  } catch (e : Exception) {
    e.printStackTrace()
  }

  return null
}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
90 91 92 93 94 95 96 97 98

function changePermissionRecursive(file: File){
	const cmd = "chmod -R 777 " + file.getAbsolutePath()
	const runtime = Runtime.getRuntime()
	try {
		runtime.exec(cmd)
	} catch (e: IOException) {
	}
}