提交 8eea7a8b 编写于 作者: DCloud-yinjiacheng's avatar DCloud-yinjiacheng

Android uni-usercapturescreen 语法调整

上级 bbccccd5
...@@ -28,8 +28,7 @@ ...@@ -28,8 +28,7 @@
title: 'Hello', title: 'Hello',
memListener:null, memListener:null,
setUserCaptureScreenFlag: false, setUserCaptureScreenFlag: false,
setUserCaptureScreenText: '禁止截屏', setUserCaptureScreenText: '禁止截屏'
permissionGranted: false
} }
}, },
onLoad() { onLoad() {
...@@ -139,30 +138,14 @@ ...@@ -139,30 +138,14 @@
var that = this; var that = this;
uni.onUserCaptureScreen(function(res) { uni.onUserCaptureScreen(function(res) {
console.log(res); console.log(res);
if (uni.getSystemInfoSync().platform == "android") {
// 除android 之外的平台,不需要判断返回状态码
if(res.errCode == -1){
// 启动失败
that.permissionGranted = true;
return ;
}else if(res.errCode == 0){
uni.showToast({ uni.showToast({
icon:"none", icon:"none",
title:'捕获截屏事件' title:'捕获截屏事件'
}) })
that.screenImage = res.image that.screenImage = res.path
}
}else{
// 除android 之外的平台,不需要判断返回状态码
uni.showToast({
icon:"none",
title:'捕获截屏事件'
})
}
}); });
if (uni.getSystemInfoSync().platform != "android" || that.permissionGranted) { if (uni.getSystemInfoSync().platform != "android") {
// 除android 之外的平台,直接提示监听已开启 // 除android 之外的平台,直接提示监听已开启
uni.showToast({ uni.showToast({
icon:"none", icon:"none",
......
...@@ -8,7 +8,7 @@ import File from "java.io.File"; ...@@ -8,7 +8,7 @@ import File from "java.io.File";
import Environment from "android.os.Environment"; import Environment from "android.os.Environment";
import System from 'java.lang.System'; import System from 'java.lang.System';
import WindowManager from 'android.view.WindowManager'; import WindowManager from 'android.view.WindowManager';
import { UserCaptureScreenResult, OnUserCaptureScreenResult, SetUserCaptureScreenOptions, UserCaptureScreenCallback, OnUserCaptureScreen, OffUserCaptureScreen, SetUserCaptureScreen } from "../interface.uts"; import { OnUserCaptureScreenCallbackResult, UserCaptureScreenCallback, OnUserCaptureScreen, OffUserCaptureScreen, SetUserCaptureScreenSuccess, SetUserCaptureScreenOptions, SetUserCaptureScreen } from "../interface.uts";
/** /**
* 文件监听器 * 文件监听器
...@@ -50,10 +50,7 @@ class ScreenFileObserver extends FileObserver { ...@@ -50,10 +50,7 @@ class ScreenFileObserver extends FileObserver {
lastObserverTime = currentTime; lastObserverTime = currentTime;
const screenShotPath = new File(this.screenFile, path).getPath(); const screenShotPath = new File(this.screenFile, path).getPath();
const res : OnUserCaptureScreenResult = { const res : OnUserCaptureScreenCallbackResult = {
errCode: 0,
errMsg: "onUserCaptureScreen:ok",
errSubject: "uni-onUserCaptureScreen",
path: screenShotPath path: screenShotPath
} }
listener?.(res); listener?.(res);
...@@ -68,23 +65,15 @@ class ScreenFileObserver extends FileObserver { ...@@ -68,23 +65,15 @@ class ScreenFileObserver extends FileObserver {
export const onUserCaptureScreen : OnUserCaptureScreen = function (callback : UserCaptureScreenCallback | null) { export const onUserCaptureScreen : OnUserCaptureScreen = function (callback : UserCaptureScreenCallback | null) {
// 检查相关权限是否已授予 // 检查相关权限是否已授予
if (ActivityCompat.checkSelfPermission(UTSAndroid.getAppContext()!, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.checkSelfPermission(UTSAndroid.getAppContext()!, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// 无权限,申请权限,并且告知用户监听失败 // 无权限,申请权限
ActivityCompat.requestPermissions(UTSAndroid.getUniActivity()!, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1001); ActivityCompat.requestPermissions(UTSAndroid.getUniActivity()!, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1001);
// 因权限缺失导致监听失败
const res : OnUserCaptureScreenResult = {
errCode: -1,
errMsg: "onUserCaptureScreen:permission denied",
errSubject: "uni-onUserCaptureScreen"
}
callback?.(res);
return; return;
} }
// 更新监听 // 更新监听
listener = callback; listener = callback;
let directory_screenshot : File; let directory_screenshot : File;
if (Build.MANUFACTURER.toLowerCase() === "xiaomi") { if (Build.MANUFACTURER.toLowerCase() == "xiaomi") {
// @Suppress("DEPRECATION") // @Suppress("DEPRECATION")
directory_screenshot = new File(new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DCIM), "Screenshots"); directory_screenshot = new File(new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DCIM), "Screenshots");
} else { } else {
...@@ -113,11 +102,7 @@ export const offUserCaptureScreen : OffUserCaptureScreen = function (_ : UserCap ...@@ -113,11 +102,7 @@ export const offUserCaptureScreen : OffUserCaptureScreen = function (_ : UserCap
export const setUserCaptureScreen : SetUserCaptureScreen = function (option : SetUserCaptureScreenOptions) { export const setUserCaptureScreen : SetUserCaptureScreen = function (option : SetUserCaptureScreenOptions) {
// 切换到UI线程 // 切换到UI线程
UTSAndroid.getUniActivity()?.runOnUiThread(new SetUserCaptureScreenRunnable(option.enable)); UTSAndroid.getUniActivity()?.runOnUiThread(new SetUserCaptureScreenRunnable(option.enable));
const res : UserCaptureScreenResult = { const res : SetUserCaptureScreenSuccess = {}
errCode: 0,
errMsg: "setUserCaptureScreen:ok",
errSubject: "uni-setUserCaptureScreen"
}
option.success?.(res); option.success?.(res);
option.complete?.(res); option.complete?.(res);
} }
......
/**
* uni.onUserCaptureScreen/uni.offUserCaptureScreen回调参数
*/
export type OnUserCaptureScreenCallbackResult = { export type OnUserCaptureScreenCallbackResult = {
/** /**
* 截屏文件路径(仅Android返回) * 截屏文件路径(仅Android返回)
...@@ -11,30 +13,28 @@ export type OnUserCaptureScreenCallbackResult = { ...@@ -11,30 +13,28 @@ export type OnUserCaptureScreenCallbackResult = {
*/ */
export type UserCaptureScreenCallback = (res : OnUserCaptureScreenCallbackResult) => void export type UserCaptureScreenCallback = (res : OnUserCaptureScreenCallbackResult) => void
/** /**
* uni.onUserCaptureScreen函数定义 * uni.onUserCaptureScreen函数定义
* 开启截屏监听 * 开启截屏监听
* *
* @param {UserCaptureScreenCallback} callback * @param {UserCaptureScreenCallback} callback
* @tutorial https://uniapp.dcloud.net.cn/api/system/capture-screen.html#onusercapturescreen * @tutorial https://uniapp.dcloud.net.cn/api/system/capture-screen.html#onusercapturescreen
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^22 * @platforms APP-IOS = ^9.0,APP-ANDROID = ^19
* @since 3.6.8 * @since 3.6.8
*/ */
export type OnUserCaptureScreen = (callback : UserCaptureScreenCallback | null) => void export type OnUserCaptureScreen = (callback : UserCaptureScreenCallback | null) => void
/** /**
* uni.offUserCaptureScreen函数定义
* 关闭截屏监听 * 关闭截屏监听
* *
* @param {UserCaptureScreenCallback} callback * @param {UserCaptureScreenCallback} callback
* @tutorial https://uniapp.dcloud.net.cn/api/system/capture-screen.html#offusercapturescreen * @tutorial https://uniapp.dcloud.net.cn/api/system/capture-screen.html#offusercapturescreen
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^22 * @platforms APP-IOS = ^9.0,APP-ANDROID = ^19
* @since 3.6.8 * @since 3.6.8
*/ */
export type OffUserCaptureScreen = (callback : UserCaptureScreenCallback | null) => void export type OffUserCaptureScreen = (callback : UserCaptureScreenCallback | null) => void
/** /**
* uni.setUserCaptureScreen成功回调参数 * uni.setUserCaptureScreen成功回调参数
*/ */
...@@ -48,7 +48,6 @@ export type SetUserCaptureScreenFail = { ...@@ -48,7 +48,6 @@ export type SetUserCaptureScreenFail = {
/** /**
* 错误码 * 错误码
* 0:成功 * 0:成功
* -1:permission denied
* 12001:system not support * 12001:system not support
* 12010:system internal error * 12010:system internal error
*/ */
...@@ -89,31 +88,31 @@ export type SetUserCaptureScreenOptions = { ...@@ -89,31 +88,31 @@ export type SetUserCaptureScreenOptions = {
/** /**
* 接口调用成功的回调函数 * 接口调用成功的回调函数
*/ */
//success : SetUserCaptureScreenSuccessCallback | null, // success : SetUserCaptureScreenSuccessCallback | null,
success ?: SetUserCaptureScreenSuccessCallback, success ?: SetUserCaptureScreenSuccessCallback,
/** /**
* 接口调用失败的回调函数 * 接口调用失败的回调函数
*/ */
//fail : SetUserCaptureScreenFailCallback | null, // fail : SetUserCaptureScreenFailCallback | null,
fail ?: SetUserCaptureScreenFailCallback, fail ?: SetUserCaptureScreenFailCallback,
/** /**
* 接口调用结束的回调函数(调用成功、失败都会执行) * 接口调用结束的回调函数(调用成功、失败都会执行)
*/ */
//complete : SetUserCaptureScreenSuccessCallback |SetUserCaptureScreenFailCallback | null // complete : SetUserCaptureScreenSuccessCallback | SetUserCaptureScreenFailCallback | null
complete ?: SetUserCaptureScreenCompleteCallback complete ?: SetUserCaptureScreenCompleteCallback
} }
/** /**
* uni.setUserCaptureScreen函数定义
* 设置防截屏 * 设置防截屏
* *
* @param {SetUserCaptureScreenOptions} options * @param {SetUserCaptureScreenOptions} options
* @tutorial https://uniapp.dcloud.net.cn/api/system/capture-screen.html#setusercapturescreen * @tutorial https://uniapp.dcloud.net.cn/api/system/capture-screen.html#setusercapturescreen
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^22 * @platforms APP-IOS = ^9.0,APP-ANDROID = ^19
* @since 3.7.3 * @since 3.7.3
*/ */
export type SetUserCaptureScreen = (options : SetUserCaptureScreenOptions) => void export type SetUserCaptureScreen = (options : SetUserCaptureScreenOptions) => void
interface uni { interface uni {
onUserCaptureScreen : OnUserCaptureScreen, onUserCaptureScreen : OnUserCaptureScreen,
offUserCaptureScreen : OffUserCaptureScreen, offUserCaptureScreen : OffUserCaptureScreen,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册