diff --git a/uni_modules/uni-usercapturescreen/changelog.md b/uni_modules/uni-usercapturescreen/changelog.md new file mode 100644 index 0000000000000000000000000000000000000000..22c87fea95bd77cc5e1aed88dfcbf3fef3235860 --- /dev/null +++ b/uni_modules/uni-usercapturescreen/changelog.md @@ -0,0 +1,4 @@ +## 1.0.1(2022-10-27) +修改插件描述 +## 1.0.0(2022-10-26) +支持安卓、iOS、微信小程序平台 diff --git a/uni_modules/uni-usercapturescreen/index.d.ts b/uni_modules/uni-usercapturescreen/index.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..05119569dcfd12fbe926eeeacd2795c395051646 --- /dev/null +++ b/uni_modules/uni-usercapturescreen/index.d.ts @@ -0,0 +1,16 @@ +type OnUserCaptureScreenCallback = (res?: { errMsg: string }) => void + +declare class Uni { + /** + * 监听用户主动截屏事件,用户使用系统截屏按键截屏时触发此事件。 + * + * 文档: [https://uniapp.dcloud.net.cn/api/system/capture-screen.html#onusercapturescreen](https://uniapp.dcloud.net.cn/api/system/capture-screen.html#onusercapturescreen) + */ + onUserCaptureScreen(callback: OnUserCaptureScreenCallback): void; + /** + * 用户主动截屏事件。取消事件监听。 + * + * 文档: [https://uniapp.dcloud.net.cn/api/system/capture-screen.html#offusercapturescreen](https://uniapp.dcloud.net.cn/api/system/capture-screen.html#offusercapturescreen) + */ + offUserCaptureScreen(callback: OnUserCaptureScreenCallback): void; +} diff --git a/uni_modules/uni-usercapturescreen/package.json b/uni_modules/uni-usercapturescreen/package.json new file mode 100644 index 0000000000000000000000000000000000000000..c2c1d59c0245d622b1533ff54f72037701c621a0 --- /dev/null +++ b/uni_modules/uni-usercapturescreen/package.json @@ -0,0 +1,87 @@ +{ + "id": "uni-usercapturescreen", + "displayName": "uni-usercapturescreen", + "version": "1.0.1", + "description": "用户主动截屏事件监听", + "keywords": [ + "截屏" + ], + "repository": "", + "engines": { + "HBuilderX": "^3.6.0" + }, + "dcloudext": { + "type": "uts", + "sale": { + "regular": { + "price": "0.00" + }, + "sourcecode": { + "price": "0.00" + } + }, + "contact": { + "qq": "" + }, + "declaration": { + "ads": "无", + "data": "插件不采集任何数据", + "permissions": "无" + }, + "npmurl": "" + }, + "uni_modules": { + "uni-ext-api":{ + "uni": { + "onUserCaptureScreen": "onUserCaptureScreen", + "offUserCaptureScreen": "offUserCaptureScreen" + } + }, + "dependencies": [], + "encrypt": [], + "platforms": { + "cloud": { + "tcb": "y", + "aliyun": "y" + }, + "client": { + "Vue": { + "vue2": "n", + "vue3": "y" + }, + "App": { + "app-android": "y", + "app-ios": "y" + }, + "H5-mobile": { + "Safari": "n", + "Android Browser": "n", + "微信浏览器(Android)": "n", + "QQ浏览器(Android)": "n" + }, + "H5-pc": { + "Chrome": "n", + "IE": "n", + "Edge": "n", + "Firefox": "n", + "Safari": "n" + }, + "小程序": { + "微信": "y", + "阿里": "n", + "百度": "n", + "字节跳动": "n", + "QQ": "n", + "钉钉": "n", + "快手": "n", + "飞书": "n", + "京东": "n" + }, + "快应用": { + "华为": "n", + "联盟": "n" + } + } + } + } +} diff --git a/uni_modules/uni-usercapturescreen/readme.md b/uni_modules/uni-usercapturescreen/readme.md new file mode 100644 index 0000000000000000000000000000000000000000..e5702ca80744a2a0ee0512a3882e4003165a7cd3 --- /dev/null +++ b/uni_modules/uni-usercapturescreen/readme.md @@ -0,0 +1,15 @@ +# uni-usercapturescreen + +用户主动截屏事件监听 + +### uni.onUserCaptureScreen + +监听用户主动截屏事件,用户使用系统截屏按键截屏时触发此事件。 + +> 使用文档:[https://uniapp.dcloud.net.cn/api/system/capture-screen.html#onusercapturescreen](https://uniapp.dcloud.net.cn/api/system/capture-screen.html#onusercapturescreen) + +### uni.offUserCaptureScreen + +用户主动截屏事件。取消事件监听。 + +> 使用文档:[https://uniapp.dcloud.net.cn/api/system/capture-screen.html#offusercapturescreen](https://uniapp.dcloud.net.cn/api/system/capture-screen.html#offusercapturescreen) diff --git a/uni_modules/uni-usercapturescreen/utssdk/app-android/index.uts b/uni_modules/uni-usercapturescreen/utssdk/app-android/index.uts new file mode 100644 index 0000000000000000000000000000000000000000..8a00a205fc51af4ff2e966c320afc79cd28c752a --- /dev/null +++ b/uni_modules/uni-usercapturescreen/utssdk/app-android/index.uts @@ -0,0 +1,148 @@ + + +import { + getUniActivity +} 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 Build from "android.os.Build"; +import FileObserver from "android.os.FileObserver"; +import File from "java.io.File"; +import Environment from "android.os.Environment"; +import System from 'java.lang.System'; + + + +/** + * 文件监听器 + */ +let screenOB: ScreenFileObserver | null = null; +/** + * 记录文件监听器上次监听的时间戳,避免重复监听 + */ +let lastFileObserverTime: number = 0; +/** + * 图片被捕获的实现 + */ +let imageChange: UTSCallback | null = null; + + + + +/** + * android 文件监听实现 + */ + +class ScreenFileObserver extends FileObserver { + + /** + * 所有截屏文件的存放目录 + */ + allScreen: File; + + + constructor(screenFile: string) { + super(screenFile) + this.allScreen = new File(screenFile); + } + + + override onEvent(event: Int, path?: string): 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 + } + + imageChange!(ret); + } + } +} + + + + + + + + +/** + * 开启截图监听 + */ +export function onUserCaptureScreen(callback: (res:UTSJSONObject) => void) { + + // 检查相关权限是否已经具备 + if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + // 不具备权限,申请权限,并且告知用户监听失败 + ActivityCompat.requestPermissions(getUniActivity()!, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1001) + + // 因权限缺失导致监听失败 + let ret = { + errCode:-1 + } + callback(ret); + 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); + + if (Build.MANUFACTURER.lowercase() === "xiaomi") { + directory_screenshot = new File(directory_dcim, "Screenshots"); + } else { + directory_screenshot = new File(directory_pictures, "Screenshots"); + } + + if (screenOB != null) { + screenOB!.stopWatching() + } + //开始监听 + screenOB = new ScreenFileObserver(directory_screenshot.path) + screenOB!.startWatching() + + // 监听成功 + let ret = { + errCode:0 + } + callback(ret); + +} + + +/** + * 关闭截屏监听 + */ +export function offUserCaptureScreen(success: (res: any) => void) { + + // android 10以上,关闭监听通过移除文件监听器实现 + if (screenOB != null) { + screenOB!.stopWatching() + screenOB = null + } + lastFileObserverTime = 0; + + success({}); +} + + + diff --git a/uni_modules/uni-usercapturescreen/utssdk/app-ios/index.uts b/uni_modules/uni-usercapturescreen/utssdk/app-ios/index.uts new file mode 100644 index 0000000000000000000000000000000000000000..1151d9e520c6da0284812eac14f2147aa243919f --- /dev/null +++ b/uni_modules/uni-usercapturescreen/utssdk/app-ios/index.uts @@ -0,0 +1,49 @@ +import { NotificationCenter} from 'Foundation'; +import { UIApplication } from "UIKit" + +/** + * 定义监听截屏事件工具类 + */ +class CaptureScreenTool { + static listener?: UTSCallback; + + // 监听截屏 + static listenCaptureScreen(callback?: UTSCallback) { + this.listener = callback + + // 注册监听截屏事件及回调方法 + // target-action 回调方法需要通过 Selector("方法名") 构建 + const method = Selector("userDidTakeScreenshot") + NotificationCenter.default.addObserver(this, selector = method, name = UIApplication.userDidTakeScreenshotNotification, object = null) + } + + // 捕获截屏回调的方法 + // target-action 的方法前需要添加 @objc 前缀 + @objc static userDidTakeScreenshot() { + const obj = new UTSJSONObject() + // 回调 + this.listener?.(obj) + } + + // 移除监听事件 + static removeListen(callback?: UTSCallback) { + this.listener = null + NotificationCenter.default.removeObserver(this) + const obj = new UTSJSONObject() + callback?.(obj) + } +} + +/** + * 开启截图监听 + */ +export function onUserCaptureScreen(callback?: UTSCallback) { + CaptureScreenTool.listenCaptureScreen(callback) +} + +/** + * 关闭截屏监听 + */ +export function offUserCaptureScreen(callback?: UTSCallback) { + CaptureScreenTool.removeListen(callback) +} diff --git a/uni_modules/uni-usercapturescreen/utssdk/mp-weixin/index.js b/uni_modules/uni-usercapturescreen/utssdk/mp-weixin/index.js new file mode 100644 index 0000000000000000000000000000000000000000..6aa57b32297e34124524f4ce4cb3b2b36b4ed5a5 --- /dev/null +++ b/uni_modules/uni-usercapturescreen/utssdk/mp-weixin/index.js @@ -0,0 +1,7 @@ +export function onUserCaptureScreen (callback) { + return wx.onUserCaptureScreen(callback) +} + +export function offUserCaptureScreen (callback) { + return wx.offUserCaptureScreen(callback) +} diff --git a/uni_modules/uts-nativepage/utssdk/app-android/AndroidManifest.xml b/uni_modules/uts-nativepage/utssdk/app-android/AndroidManifest.xml index 08d6f6c929ad9cb3110fb521101294a2083da39e..ef2453919e71ea4687b0a73257366c9cac1a1cf0 100644 --- a/uni_modules/uts-nativepage/utssdk/app-android/AndroidManifest.xml +++ b/uni_modules/uts-nativepage/utssdk/app-android/AndroidManifest.xml @@ -7,5 +7,17 @@ + + + + + + + + diff --git a/uni_modules/uts-nativepage/utssdk/app-android/DoAppWidget.uts b/uni_modules/uts-nativepage/utssdk/app-android/DoAppWidget.uts new file mode 100644 index 0000000000000000000000000000000000000000..bf0495f2ad90991d07f4c653c5bc48b2f2385ae6 --- /dev/null +++ b/uni_modules/uts-nativepage/utssdk/app-android/DoAppWidget.uts @@ -0,0 +1,92 @@ +import AppWidgetProvider from 'android.appwidget.AppWidgetProvider'; +import Context from 'android.content.Context'; +import AppWidgetManager from 'android.appwidget.AppWidgetManager'; +import RemoteViews from 'android.widget.RemoteViews'; +import Handler from 'android.os.Handler'; +import UTSAndroid from 'io.dcloud.uts.UTSAndroid'; +import R from 'io.dcloud.uni_modules.uts_nativepage.R'; + +export class DoAppWidget extends AppWidgetProvider { + + constructor(){ + super() + console.log("DoAppWidget") + } + + + override onUpdate( + context: Context, + appWidgetManager: AppWidgetManager, + appWidgetIds: IntArray + ):void{ + console.log("dqqdo", "周期更新函数被触发") + // There may be multiple widgets active, so update all of them + for (appWidgetId in appWidgetIds) { + updateAppWidget(context, appWidgetManager, appWidgetId) + } + } + + override onEnabled(_context: Context):void{ + // Enter relevant functionality for when the first widget is created + console.log("dqqdo", "桌面组件被添加") + } + + override onDisabled(_context: Context):void{ + // Enter relevant functionality for when the last widget is disabled + console.log("dqqdo", "桌面组件被移除") + } + +} + + + + +/** + * Dialog ui任务封装 + */ +class UIRunnable extends Runnable { + + context: Context; + appWidgetManager: AppWidgetManager; + appWidgetId: Int; + widgetText:string; + constructor(contextP:Context,appWidgetManagerP: AppWidgetManager,appWidgetIdP: Int,widgetTextP:string){ + super(); + this.context = contextP + this.appWidgetManager = appWidgetManagerP + this.appWidgetId = appWidgetIdP + this.widgetText = widgetTextP; + } + + + override run() { + let views = new RemoteViews(this.context.packageName, R.layout.do_app_widget) + views.setTextViewText(R.id.appwidget_text_sub, this.widgetText) + // Instruct the widget manager to update the widget + this.appWidgetManager.updateAppWidget(this.appWidgetId, views) + + } +}; + + + + function updateAppWidget( + context: Context, + appWidgetManager: AppWidgetManager, + appWidgetId: Int +) { + + let widgetText = "当前股票价格为:0.0元" + + setTimeout(function(){ + // 模拟请求网络,这里需要替换成真正的网络请求 + Thread.sleep(1000) + widgetText = "当前股票价格为:13.25元" + let uiRunnable = new UIRunnable(context,appWidgetManager,appWidgetId,widgetText) + // 更新UI + UTSAndroid.getUniActivity()!.runOnUiThread(uiRunnable) + + },500); + + +} \ No newline at end of file diff --git a/uni_modules/uts-nativepage/utssdk/app-android/index.uts b/uni_modules/uts-nativepage/utssdk/app-android/index.uts index d6faac1459d1c49aa3907c9ab13f0bdfa9a212d0..23ac518734cd9040e1757bcbe2441eca70e8a685 100644 --- a/uni_modules/uts-nativepage/utssdk/app-android/index.uts +++ b/uni_modules/uts-nativepage/utssdk/app-android/index.uts @@ -33,13 +33,10 @@ import TextView from 'android.widget.TextView'; import ViewGroup from 'android.view.ViewGroup'; import LayoutInflater from 'android.view.LayoutInflater'; import LinearLayoutManager from 'androidx.recyclerview.widget.LinearLayoutManager'; - - +export {DoAppWidget} from "./DoAppWidget.uts" class ForeService extends Service { - - constructor (){ super(); } diff --git a/uni_modules/uts-nativepage/utssdk/app-android/res/drawable-nodpi/example_appwidget_preview.png b/uni_modules/uts-nativepage/utssdk/app-android/res/drawable-nodpi/example_appwidget_preview.png new file mode 100644 index 0000000000000000000000000000000000000000..894b069a4907d258f60b1b2406b90f5a0fe1c35b Binary files /dev/null and b/uni_modules/uts-nativepage/utssdk/app-android/res/drawable-nodpi/example_appwidget_preview.png differ diff --git a/uni_modules/uts-nativepage/utssdk/app-android/res/layout/do_app_widget.xml b/uni_modules/uts-nativepage/utssdk/app-android/res/layout/do_app_widget.xml new file mode 100644 index 0000000000000000000000000000000000000000..75dc79aebb96750a9b8840bafb1e0ff41e87bc26 --- /dev/null +++ b/uni_modules/uts-nativepage/utssdk/app-android/res/layout/do_app_widget.xml @@ -0,0 +1,30 @@ + + + + + + + + + \ No newline at end of file diff --git a/uni_modules/uts-nativepage/utssdk/app-android/res/values/attrs.xml b/uni_modules/uts-nativepage/utssdk/app-android/res/values/attrs.xml new file mode 100644 index 0000000000000000000000000000000000000000..7781ac86bc3428e4044ae9cd3a0227874c84a64d --- /dev/null +++ b/uni_modules/uts-nativepage/utssdk/app-android/res/values/attrs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/uni_modules/uts-nativepage/utssdk/app-android/res/values/strings.xml b/uni_modules/uts-nativepage/utssdk/app-android/res/values/strings.xml index 3d3ae22f078810276ce2fd2dec286a576284f8d0..c18dd751db0a9413d4d4d047dc830dd7d77488fd 100644 --- a/uni_modules/uts-nativepage/utssdk/app-android/res/values/strings.xml +++ b/uni_modules/uts-nativepage/utssdk/app-android/res/values/strings.xml @@ -11,4 +11,5 @@ Pause Delete Ads + This is an app widget description \ No newline at end of file diff --git a/uni_modules/uts-nativepage/utssdk/app-android/res/values/styles.xml b/uni_modules/uts-nativepage/utssdk/app-android/res/values/styles.xml new file mode 100644 index 0000000000000000000000000000000000000000..ad0f66b7ee1d7fdcf57419d923f5838e8a5d2e2b --- /dev/null +++ b/uni_modules/uts-nativepage/utssdk/app-android/res/values/styles.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/uni_modules/uts-nativepage/utssdk/app-android/res/values/themes.xml b/uni_modules/uts-nativepage/utssdk/app-android/res/values/themes.xml new file mode 100644 index 0000000000000000000000000000000000000000..e4bc48595ffa5b54cc8cf4f5adcd19cd6596b45d --- /dev/null +++ b/uni_modules/uts-nativepage/utssdk/app-android/res/values/themes.xml @@ -0,0 +1,18 @@ + + + + + + + \ No newline at end of file diff --git a/uni_modules/uts-nativepage/utssdk/app-android/res/xml/do_app_widget_info.xml b/uni_modules/uts-nativepage/utssdk/app-android/res/xml/do_app_widget_info.xml new file mode 100644 index 0000000000000000000000000000000000000000..f4638c0546cdc3bab0edd8fc2df72fa874c35918 --- /dev/null +++ b/uni_modules/uts-nativepage/utssdk/app-android/res/xml/do_app_widget_info.xml @@ -0,0 +1,15 @@ + + + \ No newline at end of file