From 9afad08801dbfeab0be76656a0d43786513c090e Mon Sep 17 00:00:00 2001 From: duqingquan Date: Thu, 13 Jul 2023 16:57:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86utsAndroid=20=20=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/SyntaxCase/utsAndroid.vue | 199 ++++++--- .../utssdk/app-android/index.uts | 384 +++++++++++++++--- 2 files changed, 476 insertions(+), 107 deletions(-) diff --git a/pages/SyntaxCase/utsAndroid.vue b/pages/SyntaxCase/utsAndroid.vue index 759437f..cd86e46 100644 --- a/pages/SyntaxCase/utsAndroid.vue +++ b/pages/SyntaxCase/utsAndroid.vue @@ -1,105 +1,212 @@ \ No newline at end of file diff --git a/uni_modules/uts-platform-api/utssdk/app-android/index.uts b/uni_modules/uts-platform-api/utssdk/app-android/index.uts index a90cbce..21d0dae 100644 --- a/uni_modules/uts-platform-api/utssdk/app-android/index.uts +++ b/uni_modules/uts-platform-api/utssdk/app-android/index.uts @@ -1,112 +1,374 @@ import { UTSAndroid } from "io.dcloud.uts"; - +import MediaStore from "android.provider.MediaStore"; +import Activity from "android.app.Activity"; +import Bitmap from "android.graphics.Bitmap"; +import FileOutputStream from "java.io.FileOutputStream"; +import Intent from 'android.content.Intent'; /** - * UTSAndroid.getAppContext + * UTSAndroid.getAppContext 测试示例 + * https://uniapp.dcloud.net.cn/plugin/uts-for-android.html#_4-1-application-%E4%B8%8A%E4%B8%8B%E6%96%87%E7%9B%B8%E5%85%B3 */ -export function getAppContextTest():boolean{ - if(UTSAndroid.getAppContext() == null){ +export function getAppContextTest() : boolean { + if (UTSAndroid.getAppContext() == null) { console.log("UTSAndroid.getAppContext() error") return false } - if(UTSAndroid.getUniActivity() == null){ + if (UTSAndroid.getUniActivity() == null) { console.log("UTSAndroid.getUniActivity() error") return false } - if(UTSAndroid.getUniActivity()!.getApplicationContext() == UTSAndroid.getAppContext()){ - console.log(UTSAndroid.getUniActivity()!.getApplicationContext()) - console.log(UTSAndroid.getAppContext()) + if (UTSAndroid.getUniActivity()!.getApplicationContext() != UTSAndroid.getAppContext()) { return false } - console.log("getAppContextTest success") return true } -export function getUniActivityTest(){ - console.log(UTSAndroid.getUniActivity()) +/** + * UTSAndroid.getUniActivity 测试示例 + * https://uniapp.dcloud.net.cn/plugin/uts-for-android.html#_4-2-1-getuniactivity + */ +export function getUniActivityTest() : boolean { + if (UTSAndroid.getUniActivity() == null) { + return false + } + return true } - -export function getAppTempPathTest(){ - console.log(UTSAndroid.getAppTempPath()) +/** + * UTSAndroid.getAppTempPath 测试示例 + */ +export function getAppTempPathTest() : boolean { + if (UTSAndroid.getAppTempPath()!.contains("data/io.dcloud.HBuilder/apps/HBuilder/temp")) { + return true + } + return false } -export function typeofClickTest():boolean{ - - if(UTSAndroid.typeof('hello wrold') != 'string'){ +/** + * UTSAndroid.typeof 测试示例 + */ +export function typeofClickTest() : boolean { + + if (UTSAndroid.typeof('hello wrold') != 'string') { return false } - if(UTSAndroid.typeof(3.1415926) != 'number'){ + if (UTSAndroid.typeof(3.1415926) != 'number') { return false } - if(UTSAndroid.typeof(-100) != 'number'){ + if (UTSAndroid.typeof(-100) != 'number') { return false } - if(UTSAndroid.typeof(false) != 'boolean'){ + if (UTSAndroid.typeof(false) != 'boolean') { return false } - if(UTSAndroid.typeof(true) != 'boolean'){ + if (UTSAndroid.typeof(true) != 'boolean') { return false } - if(UTSAndroid.typeof(new Date()) != 'object'){ + if (UTSAndroid.typeof(new Date()) != 'object') { return false } - + return true - } -export function checkSystemPermissionGrantedTest():boolean{ - let ret = UTSAndroid.checkSystemPermissionGranted(UTSAndroid.getUniActivity()!,utsArrayOf('Manifest.permission.RECORD_AUDIO')) - console.log(ret) - let ret3 = UTSAndroid.checkSystemPermissionGranted(UTSAndroid.getUniActivity()!,utsArrayOf('Manifest.permission.ACCESS_WIFI_STATE','Manifest.permission.WRITE_CALENDAR','Manifest.permission.BLUETOOTH')) - console.log(ret3) - - return true +/** + * 手动跳转用户授权管理界面 + */ +export function gotoSystemPermissionActivityTest() { + /** + * 用户手动授权录音权限 + */ + UTSAndroid.gotoSystemPermissionActivity(UTSAndroid.getUniActivity()!, utsArrayOf('Manifest.permission.RECORD_AUDIO')) } +/** + * 跳转系统拍照界面,并触发相关生命周期测试 + */ +export function gotoCameraTake(imageDone : (event : string) => void) : boolean { + + let permissionNeed = utsArrayOf("android.permission.CAMERA") + + UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight : boolean, _grantedList : string[]) { + if (allRight) { + // 交给目前的location 引擎,真实执行 + UTSAndroid.onAppActivityResult((requestCode : Int, resultCode : Int, data ?: Intent) => { + + if ((requestCode == 1001) && (resultCode == Activity.RESULT_OK)) { + if (data != null) { + let bundle = data.getExtras(); + let mImageBitmap = bundle!.get("data") as Bitmap; + let bitmapPath = UTSAndroid.getUniActivity()!.getExternalCacheDir()!.getPath() + "/photo.png" + console.log(bitmapPath); + try { + mImageBitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(bitmapPath)) + } catch (e) { + } + imageDone(bitmapPath); + } + } + }); + + let takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + //resolveActivity 返回可处理 Intent 的第一个 Activity 组件 + if (takePictureIntent.resolveActivity(UTSAndroid.getUniActivity()!.getPackageManager()) != null) { + UTSAndroid.getUniActivity()!.startActivityForResult(takePictureIntent, 1001); + } + } else { + //callback(false,"用户拒绝了部分权限") + } + }, function (_doNotAskAgain : boolean, _grantedList : string[]) { + //callback(false,"用户拒绝了部分权限") + }) + + return true; -export function getSystemPermissionDeniedTest():boolean{ - let ret = UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!,utsArrayOf('Manifest.permission.RECORD_AUDIO')) - console.log(ret) - let ret3 = UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!,utsArrayOf('Manifest.permission.ACCESS_WIFI_STATE','Manifest.permission.WRITE_CALENDAR','Manifest.permission.BLUETOOTH')) - console.log(ret3) - - return true } -export function gotoSystemPermissionActivityTest(){ - UTSAndroid.gotoSystemPermissionActivity(UTSAndroid.getUniActivity()!,utsArrayOf('Manifest.permission.RECORD_AUDIO')) +/** + * 初始化生命周期相关 + */ +export function initAppLifecycle(onLifecycleChange : (event : string) => void) { + + /** + * application 内存不足的回调函数 + * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onapptrimmemory + */ + UTSAndroid.onAppTrimMemory((level : Number) => { + let eventName = "onAppTrimMemory - " + level; + onLifecycleChange(eventName); + console.log(eventName); + }); + + /** + * application 状态改变的回调函数 + * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onAppConfigChange + */ + UTSAndroid.onAppConfigChange((ret : UTSJSONObject) => { + let eventName = "onAppConfigChange - " + JSON.stringify(ret); + onLifecycleChange(eventName); + console.log(eventName); + }); + + + /** + * activity 销毁生命周期回调 + * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitydestroy + */ + UTSAndroid.onAppActivityDestroy(() => { + let eventName = "onAppActivityDestroy"; + onLifecycleChange(eventName); + console.log(eventName); + }); + + + /** + * activity 失去焦点生命周期回调 + * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitypause + */ + UTSAndroid.onAppActivityPause(() => { + let eventName = "onAppActivityPause"; + onLifecycleChange(eventName); + console.log(eventName); + }); + /** + * activity 得到焦点的周期回调 + * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityresume + */ + UTSAndroid.onAppActivityResume(() => { + let eventName = "onAppActivityResume"; + onLifecycleChange(eventName); + console.log(eventName); + }); + /** + * activity 回退物理按键事件回调 + * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityback + */ + UTSAndroid.onAppActivityBack(() => { + let eventName = "onAppActivityBack"; + onLifecycleChange(eventName); + console.log(eventName); + }); + } -export function requestSystemPermission(){ - - // UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!,utsArrayOf('Manifest.permission.RECORD_AUDIO')) - - // let permissionNeed : string[] = utsArrayOf("android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"); - // UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight:boolean,_grantedList:string[]) { - // if (allRight) { - // // 交给目前的location 引擎,真实执行 - // console.log("all right:") - // } - // }, function (_doNotAskAgain:boolean,_grantedList:string[]) { - // console.log("用户拒绝了部分权限:") - - // }) - - + +/** + * 取消注册生命周期函数 + */ +export function unRegLifecycle() { + + /** + * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onapptrimmemory + */ + UTSAndroid.offAppTrimMemory(); + + /** + * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onAppConfigChange + */ + UTSAndroid.offAppConfigChange(); + + /** + * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitydestroy + */ + UTSAndroid.offAppActivityDestroy(); + + /** + * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitypause + */ + UTSAndroid.offAppActivityPause(); + + /** + * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityresume + */ + UTSAndroid.offAppActivityResume(); + /** + * activity 回退物理按键事件回调 + */ + UTSAndroid.offAppActivityBack(); + } + + /** * 一组权限,申请流程测试 */ -export function arrayPermissionFlowTest():boolean{ - // if(!UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!,utsArrayOf("android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"))){ - // // 此时预期 - // return false - // } +export function arrayPermissionFlowTest(callback : (ret : boolean, desc : string) => void) { + + /** + * 同时申请日历和联系人权限 + */ + let permissionNeed = utsArrayOf("android.permission.READ_CALENDAR", "android.permission.WRITE_CALENDAR", "android.permission.READ_CONTACTS", "android.permission.WRITE_CONTACTS", "android.permission.GET_ACCOUNTS") + + if (UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!, permissionNeed).isEmpty()) { + callback(false, "预期当前不具备 读写日历/联系人的权限") + return; + } + + UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight : boolean, _grantedList : string[]) { + if (allRight) { + // 交给目前的location 引擎,真实执行 + // 测试权限api 返回值 + if (!UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!, permissionNeed).isEmpty()) { + // 预期当前基座具备了读写日历的权限 + callback(false, "权限请求完成,getSystemPermissionDenied 失败") + return; + } + if (!UTSAndroid.checkSystemPermissionGranted(UTSAndroid.getUniActivity()!, permissionNeed)) { + callback(false, "权限请求完成,checkSystemPermissionGranted 失败") + return; + } + callback(true, "") + } else { + callback(false, "用户拒绝了部分权限") + } + }, function (_doNotAskAgain : boolean, _grantedList : string[]) { + callback(false, "用户拒绝了部分权限") + }) + +} + + + +/** + * 任务分发测试 + */ +export function dispatchAsyncTest(callback : (ret : boolean, desc : string) => void) { + UTSAndroid.dispatchAsync("main",function(res){ + if(Thread.currentThread().name != 'main'){ + callback(false,"main thread error") + return + } + UTSAndroid.dispatchAsync("dom",function(res){ + /** + * dom 参数,只在2.0生效,1.0会自动切换到main线程 + */ + if(Thread.currentThread().name != 'main' && Thread.currentThread().name != 'io_dcloud_uniapp_dom'){ + callback(false,"dom thread error") + return + } + UTSAndroid.dispatchAsync("io",function(res){ + /** + * dom 参数,只在2.0生效,1.0会自动切换到main线程 + */ + if(!Thread.currentThread().name.startWith("DefaultDispatcher")){ + callback(false,"io thread error") + return + } + callback(true) + },null) + + },null) + },null) + +} + +/** + * 路径转换测试 + */ +export function convert2AbsFullPathTest() : boolean { + + let resourcePath = UTSAndroid.convert2AbsFullPath("static/logo.png") + console.log(resourcePath) + let resourcePath2 = UTSAndroid.getResourcePath("static/logo.png") + console.log(resourcePath2) + if (resourcePath != resourcePath2) { + console.log("convert2AbsFullPath 与 getResourcePath 结果不一致") + return false; + } + + if (!resourcePath.contains("Android/data/io.dcloud")) { + console.log("路径转换结果错误") + return false; + } + + let sdcardPath = UTSAndroid.convert2AbsFullPath(resourcePath) + console.log(sdcardPath) + + if (resourcePath != sdcardPath) { + console.log("绝对路径转换结果错误") + return false; + } + return true +} + + + +/** + * 单个权限,申请流程测试 + */ +export function singlePermissionFlowTest(callback : (ret : boolean, desc : string) => void) { + + let permissionNeed = utsArrayOf("android.permission.RECORD_AUDIO") + if (UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!, permissionNeed).isEmpty()) { + callback(false, "预期当前不具备 录音的权限") + return; + } + + UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight : boolean, _grantedList : string[]) { + if (allRight) { + // 交给目前的location 引擎,真实执行 + // 测试权限api 返回值 + if (!UTSAndroid.getSystemPermissionDenied(UTSAndroid.getUniActivity()!, permissionNeed).isEmpty()) { + // 预期当前基座具备了读写日历的权限 + callback(false, "权限请求完成,getSystemPermissionDenied 失败") + return; + } + if (!UTSAndroid.checkSystemPermissionGranted(UTSAndroid.getUniActivity()!, permissionNeed)) { + callback(false, "权限请求完成,checkSystemPermissionGranted 失败") + return; + } + callback(true, "") + } else { + callback(false, "用户拒绝了部分权限") + } + }, function (_doNotAskAgain : boolean, _grantedList : string[]) { + callback(false, "用户拒绝了部分权限") + }) + } \ No newline at end of file -- GitLab