index.uts 4.1 KB
Newer Older
1
import { UTSAndroid } from "io.dcloud.uts";
2 3 4 5 6 7 8 9
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';
10
import WindowManager from 'android.view.WindowManager';
11
import { OnUserCaptureScreenCallbackResult, UserCaptureScreenCallback, OnUserCaptureScreen, OffUserCaptureScreen, SetUserCaptureScreenSuccess, SetUserCaptureScreenOptions, SetUserCaptureScreen } from "../interface.uts";
12 13 14


/**
15 16 17
	* 文件监听器
	*/
let observer : ScreenFileObserver | null = null;
18
/**
19 20 21
	* 记录文件监听器上次监听的时间戳,避免重复监听
	*/
let lastObserverTime : number = 0;
22
/**
23 24 25
	* 截屏回调
	*/
let listener : UserCaptureScreenCallback | null = null;
26 27

/**
28 29
	* android 文件监听实现
	*/
30 31 32
class ScreenFileObserver extends FileObserver {

	/**
33 34 35
		* 截屏文件目录
		*/
	private screenFile : File;
36

37 38 39
	constructor(screenFile : File) {
		super(screenFile);
		this.screenFile = screenFile;
40 41
	}

42
	override onEvent(event : Int, path : string | null) : void {
43 44
		// 只监听文件新增事件
		if (event == FileObserver.CREATE) {
45 46 47 48 49 50 51 52 53
			if (path != null) {
				const currentTime = System.currentTimeMillis();
				if ((currentTime - lastObserverTime) < 1000) {
					// 本地截屏行为比上一次超过1000ms, 才认为是一个有效的时间
					return;
				}
				lastObserverTime = currentTime;

				const screenShotPath = new File(this.screenFile, path).getPath();
54
				const res : OnUserCaptureScreenCallbackResult = {
55 56 57
					path: screenShotPath
				}
				listener?.(res);
58 59 60 61 62 63
			}
		}
	}
}

/**
64 65 66 67 68
	* 开启截图监听
	*/
export const onUserCaptureScreen : OnUserCaptureScreen = function (callback : UserCaptureScreenCallback | null) {
	// 检查相关权限是否已授予
	if (ActivityCompat.checkSelfPermission(UTSAndroid.getAppContext()!, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
69
		// 无权限,申请权限
70 71
		ActivityCompat.requestPermissions(UTSAndroid.getUniActivity()!, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1001);
		return;
72
	}
73 74 75 76
	// 更新监听
	listener = callback;

	let directory_screenshot : File;
77
	if (Build.MANUFACTURER.toLowerCase() == "xiaomi") {
78 79
		// @Suppress("DEPRECATION")
		directory_screenshot = new File(new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DCIM), "Screenshots");
80
	} else {
81 82
		// @Suppress("DEPRECATION")
		directory_screenshot = new File(new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_PICTURES), "Screenshots");
83
	}
84 85 86 87
	// 先结束监听 再开启监听
	observer?.stopWatching();
	observer = new ScreenFileObserver(directory_screenshot);
	observer?.startWatching();
88 89 90 91 92 93 94 95
	
	
	UTSAndroid.onAppActivityDestroy(function(){
		observer?.stopWatching()
		observer = null
	})
	
	
96 97 98
}

/**
99 100 101 102 103 104 105
	* 关闭截屏监听
	*/
export const offUserCaptureScreen : OffUserCaptureScreen = function (_ : UserCaptureScreenCallback | null) {
	// android10以上,关闭监听通过移除文件监听器实现
	observer?.stopWatching();
	observer = null;
	lastObserverTime = 0;
106 107
}

108
/**
109 110 111
	* 设置是否禁止截屏
	*/
export const setUserCaptureScreen : SetUserCaptureScreen = function (option : SetUserCaptureScreenOptions) {
112
	// 切换到UI线程
113
	UTSAndroid.getUniActivity()?.runOnUiThread(new SetUserCaptureScreenRunnable(option.enable));
114
	const res : SetUserCaptureScreenSuccess = {}
115 116 117 118 119
	option.success?.(res);
	option.complete?.(res);
}

class SetUserCaptureScreenRunnable extends Runnable {
120

121
	/**
122 123 124 125
		* ture: 允许用户截屏
		* false: 不允许用户截屏,防止用户截屏到应用页面内容
		*/
	private enable : boolean;
126

127
	constructor(enable : boolean) {
128
		super();
129
		this.enable = enable;
130 131
	}

132
	override run() : void {
133
		if (this.enable) {
134
			UTSAndroid.getUniActivity()?.getWindow()?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
135 136
		} else {
			UTSAndroid.getUniActivity()?.getWindow()?.addFlags(WindowManager.LayoutParams.FLAG_SECURE);
137 138 139
		}
	}
}