Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
Hello UTS
提交
5224c3f0
H
Hello UTS
项目概览
DCloud
/
Hello UTS
通知
1598
Star
27
Fork
9
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
2
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
Hello UTS
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
2
Issue
2
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
5224c3f0
编写于
9月 23, 2022
作者:
杜庆泉
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
基本实现截屏监听,还需要处理细节
上级
6d4de4d4
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
381 addition
and
341 deletion
+381
-341
pages/SystemAPI/SystemAPI.vue
pages/SystemAPI/SystemAPI.vue
+8
-0
uni_modules/uts-nativepage/utssdk/app-android/AndroidManifest.xml
...les/uts-nativepage/utssdk/app-android/AndroidManifest.xml
+1
-1
uni_modules/uts-nativepage/utssdk/app-android/index.uts
uni_modules/uts-nativepage/utssdk/app-android/index.uts
+1
-1
uni_modules/uts-screenshot-listener/utssdk/app-android/index.uts
...ules/uts-screenshot-listener/utssdk/app-android/index.uts
+371
-339
uni_modules/uts-screenshot-listener/utssdk/app-android/libs/androix-core-1.0.0-自定义基座时移除.jar
...r/utssdk/app-android/libs/androix-core-1.0.0-自定义基座时移除.jar
+0
-0
未找到文件。
pages/SystemAPI/SystemAPI.vue
浏览文件 @
5224c3f0
...
...
@@ -4,12 +4,14 @@
<view
class=
"uni-btn-v uni-common-mt"
>
<button
type=
"primary"
@
tap=
"testGetBatteryCapacity"
>
获取电池电量
</button>
<button
type=
"primary"
@
tap=
"testGotoDemoActivity"
>
跳转至新的原生页面
</button>
<button
type=
"primary"
@
tap=
"testScreenShotListen"
>
监听截图事件
</button>
</view>
</view>
</
template
>
<
script
>
import
getBatteryInfo
from
"
@/uni_modules/uni-getbatteryinfo
"
;
import
gotoDemoActivity
from
"
@/uni_modules/uts-nativepage
"
;
import
{
requestPremission
,
initAppLifecycle
}
from
"
@/uni_modules/uts-screenshot-listener
"
;
export
default
{
data
()
{
...
...
@@ -30,6 +32,12 @@
}
})
},
testScreenShotListen
()
{
// 请求写入储存的权限
console
.
log
(
"
111
"
);
requestPremission
();
initAppLifecycle
();
},
testGotoDemoActivity
()
{
gotoDemoActivity
();
...
...
uni_modules/uts-nativepage/utssdk/app-android/AndroidManifest.xml
浏览文件 @
5224c3f0
uni_modules/uts-nativepage/utssdk/app-android/index.uts
浏览文件 @
5224c3f0
...
...
@@ -24,7 +24,7 @@ import {
import Service from 'android.app.Service';
import System from 'java.lang.System';
import WindowManager from 'android.view.WindowManager';
class ForeService extends Service {
...
...
uni_modules/uts-screenshot-listener/utssdk/app-android/index.uts
浏览文件 @
5224c3f0
class ScreenShotHelper {
companion object {
const val TAG = "ScreenShotLog"
import {
onAppActivityDestroy,
onAppActivityPause,
onAppActivityResume,
onAppActivityBack,
getUniActivity,
getAppContext
} from "io.dcloud.uts.android";
import ActivityCompat from "androidx.core.app.ActivityCompat";
import Manifest from "android.Manifest";
import PackageManager from "android.content.pm.PackageManager";
import Context from "android.content.Context";
import Point from "android.graphics.Point";
import Build from "android.os.Build";
import Handler from "android.os.Handler";
import MediaStore from "android.provider.MediaStore";
import Looper from "android.os.Looper";
import Cursor from "android.database.Cursor";
import ContentObserver from "android.database.ContentObserver";
import Uri from "android.net.Uri";
import BitmapFactory from "android.graphics.BitmapFactory";
import Display from "android.view.Display";
import Locale from "java.util.Locale";
import WindowManager from "android.view.WindowManager";
/**
* 读取媒体数据库时需要读取的列
*/
val MEDIA_PROJECTIONS = arrayOf(
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.DATE_TAKEN
)
/**
/**
* 读取媒体数据库时需要读取的列,其中 width、height 字段在 API 16 之后才有
*/
val
MEDIA_PROJECTIONS_API_16 = arrayOf(
const
MEDIA_PROJECTIONS_API_16 = arrayOf(
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.WIDTH,
MediaStore.Images.ImageColumns.HEIGHT
)
)
/**
/**
* 截屏路径判断的关键字
*/
val
KEYWORDS = arrayOf(
const
KEYWORDS = arrayOf(
"screenshot", "screen_shot", "screen-shot", "screen shot",
"screencapture", "screen_capture", "screen-capture", "screen capture",
"screencap", "screen_cap", "screen-cap", "screen cap"
)
)
fun showLog(msg: String) {
Log.d(TAG, msg)
}
}
}
/**
* @author Alex.yelj
* 2020-01-15 17:28 Wednesday
* Description: 截屏监听
*/
class ScreenShotListener constructor(context: Context?) {
private var mContext: Context
private var mScreenRealSize: Point? = null
private val mHasCallbackPaths: ArrayList<String> = ArrayList()
private var mListener: OnScreenShotListener? = null
private var mStartListenTime: Long = 0
/**
* 内部存储器内容观察者
/**
* 屏幕尺寸
*/
private var mInternalObserver: MediaContentObserver? = null
/**
* 外部存储器内容观察者
let mScreenRealSize: Point|null = getRealScreenSize();
let mHasCallbackPaths: ArrayList<string> = new ArrayList()
let mListener: OnScreenShotListener|null = null;
let mStartListenTime: number = 0;
let mUiHandler: Handler = Handler(Looper.getMainLooper())
/**
* 内部文件监听器
*/
private var mExternalObserver: MediaContentObserver? = null
/**
* 运行在 UI 线程的 Handler, 用于运行监听器回调
let mInternalObserver: MediaContentObserver|null = null;
/**
* 外部文件监听器
*/
private var mUiHandler = Handler(Looper.getMainLooper())
init {
ScreenShotHelper.showLog("init")
assertInMainThread()
requireNotNull(context) { "The context must not be null." }
mContext = context
if (mScreenRealSize == null) {
mScreenRealSize = getRealScreenSize()
if (mScreenRealSize != null) {
ScreenShotHelper.showLog("Screen Real Size: " + mScreenRealSize!!.x + " * " + mScreenRealSize!!.y)
} else {
ScreenShotHelper.showLog("Get screen real size failed.")
}
}
}
let mExternalObserver: MediaContentObserver|null = null;
/**
* 单例
*/
companion object : SingletonHolder<ScreenShotListener, Context>(::ScreenShotListener)
/**
/**
* 开启监听
*/
fun startListener() {
assertInMainThread()
function startListener(): void {
// 记录开始监听的时间戳
mStartListenTime = System.currentTimeMillis()
...
...
@@ -100,34 +83,36 @@ class ScreenShotListener constructor(context: Context?) {
MediaContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mUiHandler)
// 注册内容观察者
mContext
.contentResolver.registerContentObserver(
getUniActivity()!
.contentResolver.registerContentObserver(
MediaStore.Images.Media.INTERNAL_CONTENT_URI,
false,
mInternalObserver
mInternalObserver!
)
mContext
.contentResolver.registerContentObserver(
getUniActivity()!
.contentResolver.registerContentObserver(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
false,
mExternalObserver
mExternalObserver!
)
}
fun stopListener() {
assertInMainThread()
/**
* 停止监听
*/
function stopListener() {
// 注销内容观察者
if (mInternalObserver != null) {
try {
mContext.contentResolver.unregisterContentObserver(mInternalObserver!
!)
} catch (e: Exception
) {
getUniActivity()!.contentResolver.unregisterContentObserver(mInternalObserver
!)
} catch (e
) {
e.printStackTrace()
}
mInternalObserver = null
}
if (mExternalObserver != null) {
try {
mContext.contentResolver.unregisterContentObserver(mExternalObserver!
!)
} catch (e: Exception
) {
getUniActivity()!.contentResolver.unregisterContentObserver(mExternalObserver
!)
} catch (e
) {
e.printStackTrace()
}
mExternalObserver = null
...
...
@@ -141,29 +126,31 @@ class ScreenShotListener constructor(context: Context?) {
/**
* 处理媒体数据库的内容改变
*/
fu
n handleMediaContentChange(contentUri: Uri) {
var cursor: Cursor? = null
functio
n handleMediaContentChange(contentUri: Uri) {
let cursor: Cursor|null = null;
try {
cursor = mContext.contentResolver.query(
console.log(getUniActivity());
console.log(getUniActivity()!.contentResolver);
cursor = getUniActivity()!.contentResolver.query(
contentUri,
if (Build.VERSION.SDK_INT < 16) ScreenShotHelper.MEDIA_PROJECTIONS else ScreenShotHelper.
MEDIA_PROJECTIONS_API_16,
MEDIA_PROJECTIONS_API_16,
null, null,
"${MediaStore.Images.ImageColumns.DATE_ADDED} desc limit 1"
)
MediaStore.Images.ImageColumns.DATE_ADDED + " desc limit 1"
)!
console.log(cursor);
if (cursor == null) {
ScreenShotHelper.showLog("Deviant logic.")
return
}
if (!cursor.moveToFirst()) {
ScreenShotHelper.showLog("Cursor no data.")
return
}
// 获取各列的索引
val
dataIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
val
dateTakenIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATE_TAKEN)
let
dataIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
let
dateTakenIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATE_TAKEN)
var widthIndex = -1
var heightIndex = -1
if (Build.VERSION.SDK_INT >= 16) {
...
...
@@ -172,23 +159,23 @@ class ScreenShotListener constructor(context: Context?) {
}
// 获取行数据
val
data = cursor.getString(dataIndex)
val
dateTaken = cursor.getLong(dateTakenIndex)
let
data = cursor.getString(dataIndex)
let
dateTaken = cursor.getLong(dateTakenIndex)
var width = 0
var height = 0
if (widthIndex >= 0 && heightIndex >= 0) {
width = cursor.getInt(widthIndex)
height = cursor.getInt(heightIndex)
} else {
val
size = getImageSize(data)
let
size = getImageSize(data)
width = size.x
height = size.y
}
// 处理获取到的第一行数据
handleMediaRowData(data, dateTaken, width, height)
} catch (e: Exception
) {
ScreenShotHelper.showLog("Exception: ${e.message}")
} catch (e
) {
console.log(e);
e.printStackTrace()
} finally {
if (cursor != null && !cursor.isClosed) {
...
...
@@ -197,8 +184,8 @@ class ScreenShotListener constructor(context: Context?) {
}
}
private fun getImageSize(imagePath: S
tring): Point {
val
options = BitmapFactory.Options()
function getImageSize(imagePath: s
tring): Point {
let
options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFile(imagePath, options)
return Point(options.outWidth, options.outHeight)
...
...
@@ -207,22 +194,20 @@ class ScreenShotListener constructor(context: Context?) {
/**
* 处理获取到的一行数据
*/
private fu
n handleMediaRowData(data: String, dateTaken: Long, width: Int, height: Int) {
functio
n handleMediaRowData(data: String, dateTaken: Long, width: Int, height: Int) {
if (checkScreenShot(data, dateTaken, width, height)) {
ScreenShotHelper.showLog("ScreenShot: path = $data; size = $width * $height; date = $dateTaken")
if (mListener != null && !checkCallback(data)) {
mListener!!.onScreenShot(data)
if (!checkCallback(data)) {
console.log(data);
}
} else {
// 如果在观察区间媒体数据库有数据改变,又不符合截屏规则,则输出到 log 待分析
ScreenShotHelper.showLog("Media content changed, but not screenshot: path = $data; size = $width * $height; date = $dateTaken")
}
}
/**
* 判断指定的数据行是否符合截屏条件
*/
private fun checkScreenShot(data: String?, dateTaken: Long, width: Int, height: Int): B
oolean {
function checkScreenShot(data?: string, dateTaken: Long, width: Int, height: Int): b
oolean {
// 判断依据一: 时间判断
// 如果加入数据库的时间在开始监听之前, 或者与当前时间相差大于10秒, 则认为当前没有截屏
if (dateTaken < mStartListenTime || System.currentTimeMillis() - dateTaken > 10 * 1000) {
...
...
@@ -232,21 +217,21 @@ class ScreenShotListener constructor(context: Context?) {
// 判断依据二: 尺寸判断
if (mScreenRealSize != null) {
// 如果图片尺寸超出屏幕, 则认为当前没有截屏
if (!(width <= mScreenRealSize!!.x && height <= mScreenRealSize!
!.y)
|| (height <= mScreenRealSize!!.x && width <= mScreenRealSize!
!.y)
if (!(width <= mScreenRealSize!.x && height <= mScreenRealSize
!.y)
|| (height <= mScreenRealSize!.x && width <= mScreenRealSize
!.y)
) {
return false
}
}
// 判断依据三: 路径判断
if (data.isNullOrEmpty()
) {
if (data == null
) {
return false
}
val lowerData = data.toLowerC
ase(Locale.getDefault())
let lowerData = data.lowerc
ase(Locale.getDefault())
// 判断图片路径是否含有指定的关键字之一, 如果有, 则认为当前截屏了
for (keyWork in ScreenShotHelper.
KEYWORDS) {
for (keyWork in
KEYWORDS) {
if (lowerData.contains(keyWork)) {
return true
}
...
...
@@ -258,16 +243,16 @@ class ScreenShotListener constructor(context: Context?) {
* 判断是否已回调过, 某些手机ROM截屏一次会发出多次内容改变的通知; <br></br>
* 删除一个图片也会发通知, 同时防止删除图片时误将上一张符合截屏规则的图片当做是当前截屏.
*/
private fun checkCallback(imagePath: String): Boolean {
function checkCallback(imagePath: String): boolean {
if (mHasCallbackPaths.contains(imagePath)) {
ScreenShotHelper.showLog("ScreenShot: imgPath has done; imagePath = $imagePath")
return true
}
// 大概缓存15~20条记录便可
if (mHasCallbackPaths.size >= 20) {
for (i in 0..
4) {
mHasCallbackPaths.removeAt(0)
}
// for (i of
4) {
//
mHasCallbackPaths.removeAt(0)
//
}
}
mHasCallbackPaths.add(imagePath)
return false
...
...
@@ -276,69 +261,116 @@ class ScreenShotListener constructor(context: Context?) {
/**
* 获取屏幕分辨率
*/
private fun getRealScreenSize(): Point?
{
var screenSize: Point? = null
function getRealScreenSize(): Point | null
{
let screenSize:Point = Point();
try {
screenSize = Point()
val windowManager = mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val defaultDisplay = windowManager.defaultDisplay
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
let windowManager = getUniActivity()!.getSystemService(Context.WINDOW_SERVICE) as WindowManager
let defaultDisplay = windowManager.defaultDisplay
defaultDisplay.getRealSize(screenSize)
} else {
try {
val mGetRawW = Display::class.java.getMethod("getRawWidth")
val mGetRawH = Display::class.java.getMethod("getRawHeight")
screenSize.set(
mGetRawW.invoke(defaultDisplay) as Int,
mGetRawH.invoke(defaultDisplay) as Int
)
} catch (e: Exception) {
screenSize.set(defaultDisplay.width, defaultDisplay.height)
e.printStackTrace()
}
}
} catch (e: Exception) {
} catch (e) {
e.printStackTrace()
}
return screenSize
}
private fun assertInMainThread() {
if (Looper.myLooper() != Looper.getMainLooper()) {
val stackTrace = Thread.currentThread().stackTrace
var methodMsg: String? = null
if (stackTrace != null && stackTrace.size >= 4) {
methodMsg = stackTrace[3].toString()
}
ScreenShotHelper.showLog("Call the method must be in main thread: $methodMsg")
}
}
/**
/**
* 媒体内容观察者
*/
private inner class MediaContentObserver(var contentUri: Uri, handler: Handler) :
ContentObserver(handler) {
class MediaContentObserver extends ContentObserver {
contentUri: Uri;
handler: Handler;
override fun onChange(selfChange: Boolean) {
constructor(contentUri: Uri, handler: Handler){
super(handler)
this.contentUri = contentUri
this.handler = handler
}
override onChange(selfChange: Boolean) {
super.onChange(selfChange)
console.log("111222");
handleMediaContentChange(contentUri)
}
}
/**
* 截屏监听接口
*/
type OnScreenShotListener = {
onScreenShot: (picPath: string) => void;
}
class OnScreenShotListenerImpl extends OnScreenShotListener(){
onScreenShot(picPath: string){
console.log(picPath);
}
}
/**
* 设置截屏监听器回调
/**
* 请求权限
*/
fun setListener(listener: OnScreenShotListener) {
this.mListener = listener
export function requestPremission() {
// 注册一个请求回调
// 发起权限申请
if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getUniActivity()!, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1001)
}
// 请求权限
return { name: "requestPremission" };
}
/**
* 初始化生命周期监听
*/
export function initAppLifecycle() {
onAppActivityPause(() => {
stopScreenShotListen()
});
/**
* 截屏监听接口
* activity 得到焦点的周期回调
* 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityresume
*/
interface OnScreenShotListener {
fun onScreenShot(picPath: String)
}
onAppActivityResume(() => {
startScreenShotListen()
});
}
/**
* 开启屏幕监听
*/
function startScreenShotListen() {
mListener = new OnScreenShotListenerImpl();
startListener()
}
/**
* 关闭
*/
function stopScreenShotListen() {
stopListener()
}
uni_modules/uts-screenshot-listener/utssdk/app-android/libs/androix-core-1.0.0-自定义基座时移除.jar
0 → 100644
浏览文件 @
5224c3f0
文件已添加
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录