Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
qq_65376846
uni-api
提交
a2acc722
U
uni-api
项目概览
qq_65376846
/
uni-api
与 Fork 源项目一致
Fork自
DCloud / uni-api
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-api
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a2acc722
编写于
3月 15, 2023
作者:
DCloud-yinjiacheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Android uni-usercapturescreen 语法调整
上级
3c5f0a7f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
120 addition
and
143 deletion
+120
-143
pages/index/index.vue
pages/index/index.vue
+7
-10
uni_modules/uni-usercapturescreen/utssdk/app-android/index.uts
...odules/uni-usercapturescreen/utssdk/app-android/index.uts
+83
-125
uni_modules/uni-usercapturescreen/utssdk/interface.uts
uni_modules/uni-usercapturescreen/utssdk/interface.uts
+30
-8
未找到文件。
pages/index/index.vue
浏览文件 @
a2acc722
...
...
@@ -28,7 +28,8 @@
title
:
'
Hello
'
,
memListener
:
null
,
setUserCaptureScreenFlag
:
false
,
setUserCaptureScreenText
:
'
禁止截屏
'
setUserCaptureScreenText
:
'
禁止截屏
'
,
permissionGranted
:
false
}
},
onLoad
()
{
...
...
@@ -142,13 +143,9 @@
// 除android 之外的平台,不需要判断返回状态码
if
(
res
.
errCode
==
-
1
){
// 启动失败
that
.
permissionGranted
=
true
;
return
;
}
else
if
(
res
.
errCode
==
0
){
uni
.
showToast
({
icon
:
"
none
"
,
title
:
'
截屏监听已开启
'
})
}
else
{
uni
.
showToast
({
icon
:
"
none
"
,
title
:
'
捕获截屏事件
'
...
...
@@ -164,7 +161,7 @@
}
});
if
(
uni
.
getSystemInfoSync
().
platform
!=
"
android
"
)
{
if
(
uni
.
getSystemInfoSync
().
platform
!=
"
android
"
||
that
.
permissionGranted
)
{
// 除android 之外的平台,直接提示监听已开启
uni
.
showToast
({
icon
:
"
none
"
,
...
...
@@ -198,13 +195,13 @@
uni
.
setUserCaptureScreen
({
enable
:
flag
,
success
:
(
res
)
=>
{
console
.
log
(
"
setUserCaptureScreen
open
:
"
+
flag
+
"
success:
"
+
JSON
.
stringify
(
res
));
console
.
log
(
"
setUserCaptureScreen
enable
:
"
+
flag
+
"
success:
"
+
JSON
.
stringify
(
res
));
},
fail
:
(
res
)
=>
{
console
.
log
(
"
setUserCaptureScreen
open
:
"
+
flag
+
"
fail:
"
+
JSON
.
stringify
(
res
));
console
.
log
(
"
setUserCaptureScreen
enable
:
"
+
flag
+
"
fail:
"
+
JSON
.
stringify
(
res
));
},
complete
:
(
res
)
=>
{
console
.
log
(
"
setUserCaptureScreen
open
:
"
+
flag
+
"
complete:
"
+
JSON
.
stringify
(
res
));
console
.
log
(
"
setUserCaptureScreen
enable
:
"
+
flag
+
"
complete:
"
+
JSON
.
stringify
(
res
));
}
});
uni
.
showToast
({
...
...
uni_modules/uni-usercapturescreen/utssdk/app-android/index.uts
浏览文件 @
a2acc722
import {
UTSAndroid
} from "io.dcloud.uts";
import { UTSAndroid } from "io.dcloud.uts";
import ActivityCompat from "androidx.core.app.ActivityCompat";
import Manifest from "android.Manifest";
import PackageManager from "android.content.pm.PackageManager";
...
...
@@ -14,167 +8,134 @@ import File from "java.io.File";
import Environment from "android.os.Environment";
import System from 'java.lang.System';
import WindowManager from 'android.view.WindowManager';
import { SetUserCaptureScreenOption, UserCaptureScreenResult, OnUserCaptureScreen, OffUserCaptureScreen, SetUserCaptureScreen } from "../interface.uts";
import { UserCaptureScreenResult, OnUserCaptureScreenResult, SetUserCaptureScreenOptions, UserCaptureScreenCallback, OnUserCaptureScreen, OffUserCaptureScreen, SetUserCaptureScreen } from "../interface.uts";
/**
* 文件监听器
*/
let
screenOB
: ScreenFileObserver | null = null;
* 文件监听器
*/
let
observer
: ScreenFileObserver | null = null;
/**
* 记录文件监听器上次监听的时间戳,避免重复监听
*/
let last
FileObserverTime
: number = 0;
* 记录文件监听器上次监听的时间戳,避免重复监听
*/
let last
ObserverTime
: number = 0;
/**
* 图片被捕获的实现
*/
let imageChange: UTSCallback | null = null;
* 截屏回调
*/
let listener : UserCaptureScreenCallback | null = null;
/**
* android 文件监听实现
*/
@Suppress("DEPRECATION")
* android 文件监听实现
*/
class ScreenFileObserver extends FileObserver {
/**
* 所有截屏文件的存放
目录
*/
allScreen
: File;
* 截屏文件
目录
*/
private screenFile
: File;
constructor(screenFile: string) {
super(new File(screenFile))
this.allScreen = new File(screenFile);
constructor(screenFile : File) {
super(screenFile);
this.screenFile = screenFile;
}
override onEvent(event: Int, path?: string): void {
override onEvent(event : Int, path : string | null) : void {
// 只监听文件新增事件
if (event == FileObserver.CREATE) {
let newPath: string = new File(this.allScreen, path!).getPath();
let currentTime = System.currentTimeMillis();
if ((currentTime - lastFileObserverTime) < 1000) {
// 本地截屏行为比上一次超过1000ms,才认为是一个有效的时间
return;
}
lastFileObserverTime = System.currentTimeMillis()
let ret = {
errCode:1,
image:newPath
if (path != null) {
const currentTime = System.currentTimeMillis();
if ((currentTime - lastObserverTime) < 1000) {
// 本地截屏行为比上一次超过1000ms, 才认为是一个有效的时间
return;
}
lastObserverTime = currentTime;
const screenShotPath = new File(this.screenFile, path).getPath();
const res : OnUserCaptureScreenResult = {
errCode: 0,
errMsg: "onUserCaptureScreen:ok",
errSubject: "uni-onUserCaptureScreen",
path: screenShotPath
}
listener?.(res);
}
imageChange!(ret);
}
}
}
/**
* 开启截图监听
*/
@Suppress("DEPRECATION")
export const onUserCaptureScreen : OnUserCaptureScreen = function (callback : UTSCallback) {
// 检查相关权限是否已经具备
if (ActivityCompat.checkSelfPermission(UTSAndroid.getUniActivity()!, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// 不具备权限,申请权限,并且告知用户监听失败
ActivityCompat.requestPermissions(UTSAndroid.getUniActivity()!, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1001)
* 开启截图监听
*/
export const onUserCaptureScreen : OnUserCaptureScreen = function (callback : UserCaptureScreenCallback | null) {
// 检查相关权限是否已授予
if (ActivityCompat.checkSelfPermission(UTSAndroid.getAppContext()!, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// 无权限,申请权限,并且告知用户监听失败
ActivityCompat.requestPermissions(UTSAndroid.getUniActivity()!, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1001);
// 因权限缺失导致监听失败
let ret = {
errCode:-1
const res : OnUserCaptureScreenResult = {
errCode: -1,
errMsg: "onUserCaptureScreen:permission denied",
errSubject: "uni-onUserCaptureScreen"
}
callback
(ret
);
return
;
callback
?.(res
);
return;
}
imageChange = callback;
let directory_screenshot: File;
// 找到设备存放截屏文件的目录
let directory_pictures = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_PICTURES);
let directory_dcim = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DCIM);
// 更新监听
listener = callback;
let directory_screenshot : File;
if (Build.MANUFACTURER.toLowerCase() === "xiaomi") {
directory_screenshot = new File(directory_dcim, "Screenshots");
// @Suppress("DEPRECATION")
directory_screenshot = new File(new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DCIM), "Screenshots");
} else {
directory_screenshot = new File(directory_pictures, "Screenshots");
// @Suppress("DEPRECATION")
directory_screenshot = new File(new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_PICTURES), "Screenshots");
}
if (screenOB != null) {
screenOB!.stopWatching()
}
//开始监听
screenOB = new ScreenFileObserver(directory_screenshot.path)
screenOB!.startWatching()
// 监听成功
let ret = {
errCode:0
}
callback(ret);
// 先结束监听 再开启监听
observer?.stopWatching();
observer = new ScreenFileObserver(directory_screenshot);
observer?.startWatching();
}
/**
* 关闭截屏监听
*/
export const offUserCaptureScreen : OffUserCaptureScreen = function (callback : UTSCallback) {
// android 10以上,关闭监听通过移除文件监听器实现
if (screenOB != null) {
screenOB!.stopWatching()
screenOB = null
}
lastFileObserverTime = 0;
callback({});
* 关闭截屏监听
*/
export const offUserCaptureScreen : OffUserCaptureScreen = function (_ : UserCaptureScreenCallback | null) {
// android10以上,关闭监听通过移除文件监听器实现
observer?.stopWatching();
observer = null;
lastObserverTime = 0;
}
/**
* 设置是否禁止截屏
*/
export const setUserCaptureScreen : SetUserCaptureScreen = function (option
: SetUserCaptureScreenOption
) {
* 设置是否禁止截屏
*/
export const setUserCaptureScreen : SetUserCaptureScreen = function (option
: SetUserCaptureScreenOptions
) {
// 切换到UI线程
UTSAndroid.getUniActivity()?.runOnUiThread(new SetUserCaptureScreenRunnable(option.enable));
const res = new UserCaptureScreenResult();
res.errCode = 0;
res.errMsg = "setUserCaptureScreen:ok";
res.errSubject = "uni-setUserCaptureScreen";
const res : UserCaptureScreenResult = {
errCode: 0,
errMsg: "setUserCaptureScreen:ok",
errSubject: "uni-setUserCaptureScreen"
}
option.success?.(res);
option.complete?.(res);
}
class SetUserCaptureScreenRunnable extends Runnable {
/**
* ture: 允许用户截屏
* false: 不允许用户截屏,防止用户截屏到应用页面内容
*/
private enable: boolean;
* ture: 允许用户截屏
* false: 不允许用户截屏,防止用户截屏到应用页面内容
*/
private enable
: boolean;
constructor(enable: boolean) {
constructor(enable
: boolean) {
super();
this.enable = enable;
}
override run(): void {
override run()
: void {
if (this.enable) {
UTSAndroid.getUniActivity()?.getWindow()?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
} else {
...
...
@@ -182,6 +143,3 @@ class SetUserCaptureScreenRunnable extends Runnable {
}
}
}
uni_modules/uni-usercapturescreen/utssdk/interface.uts
浏览文件 @
a2acc722
...
...
@@ -16,7 +16,28 @@ export type UserCaptureScreenResult = {
errMsg : string,
}
export type SetUserCaptureScreenOption = {
export type OnUserCaptureScreenResult = {
/**
* 错误码
* 0:成功
* -1:permission denied(仅Android返回)
*/
errCode : number,
/**
* 调用API的名称
*/
errSubject : string,
/**
* 错误的详细信息
*/
errMsg : string,
/**
* 截屏文件路径(仅Android返回)
*/
path : string | null
}
export type SetUserCaptureScreenOptions = {
/**
* true: 允许用户截屏 false: 不允许用户截屏,防止用户截屏到应用页面内容
*/
...
...
@@ -35,25 +56,27 @@ export type SetUserCaptureScreenOption = {
complete ?: (res : UserCaptureScreenResult) => void
}
export type UserCaptureScreenCallback = (res : OnUserCaptureScreenResult) => void
/**
* 开启截屏监听
*
* @param {U
TS
Callback} callback
* @param {U
serCaptureScreen
Callback} callback
* @tutorial https://uniapp.dcloud.net.cn/api/system/capture-screen.html#onusercapturescreen
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^22
* @since 3.6.8
*/
export type OnUserCaptureScreen = (callback
?: UTSCallback
) => void
export type OnUserCaptureScreen = (callback
: UserCaptureScreenCallback | null
) => void
/**
* 关闭截屏监听
*
* @param {U
TS
Callback} callback
* @param {U
serCaptureScreen
Callback} callback
* @tutorial https://uniapp.dcloud.net.cn/api/system/capture-screen.html#offusercapturescreen
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^22
* @since 3.6.8
*/
export type OffUserCaptureScreen = (callback
?: UTSCallback
) => void
export type OffUserCaptureScreen = (callback
: UserCaptureScreenCallback | null
) => void
/**
* 设置防截屏
...
...
@@ -63,11 +86,10 @@ export type OffUserCaptureScreen = (callback ?: UTSCallback) => void
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^22
* @since 3.7.3
*/
export type SetUserCaptureScreen = (options : SetUserCaptureScreenOption) => void
export type SetUserCaptureScreen = (options : SetUserCaptureScreenOption
s
) => void
interface uni {
onUserCaptureScreen : OnUserCaptureScreen,
offUserCaptureScreen : OffUserCaptureScreen,
setUserCaptureScreen : SetUserCaptureScreen
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录