-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
screen.ts
105 lines (92 loc) · 3.03 KB
/
screen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import * as Brightness from 'expo-brightness'
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake'
import { Permissions } from 'react-native-unimodules'
import { errorHandler, shouleBeObject, successHandler } from '../utils'
/**
* 设置屏幕亮度
* @param opts
* @param {number} opts.value - 屏幕亮度值,范围 0 ~ 1。0 最暗,1 最亮
*/
export async function setScreenBrightness (opts = {}) {
let res = {errMsg: 'setScreenBrightness:ok'}
const isObject = shouleBeObject(opts)
if (!isObject.res) {
res = {errMsg: `setScreenBrightness${isObject.msg}`}
console.error(res.errMsg)
return Promise.reject(res)
}
const {value, success, fail, complete}: any = opts
await Permissions.askAsync(Permissions.SYSTEM_BRIGHTNESS)
const {status} = await Permissions.getAsync(Permissions.SYSTEM_BRIGHTNESS)
if (status === 'granted') {
try {
await (Brightness as any).setSystemBrightnessAsync(value)
return successHandler(success, complete)(res)
} catch (e) {
res.errMsg = `setScreenBrightness:fail invalid ${e}`
return errorHandler(fail, complete)(res)
}
}
}
/**
* 获取屏幕亮度
* @param opts
*/
export async function getScreenBrightness (opts = {}) {
let res = {errMsg: 'getScreenBrightness:ok'} as any
const isObject = shouleBeObject(opts)
if (!isObject.res) {
res = {errMsg: `getScreenBrightness${isObject.msg}`}
console.error(res.errMsg)
return Promise.reject(res)
}
const {success, fail, complete}: any = opts
await Permissions.askAsync(Permissions.SYSTEM_BRIGHTNESS)
const {status} = await Permissions.getAsync(Permissions.SYSTEM_BRIGHTNESS)
if (status === 'granted') {
try {
res.num = await (Brightness as any).getBrightnessAsync()
return successHandler(success, complete)(res)
} catch (e) {
res.errMsg = `getScreenBrightness:fail invalid ${e}`
return errorHandler(fail, complete)(res)
}
}
}
/**
* keepScreenOn
* @param {{}} opts
* @param {boolean} opts.keepScreenOn - 是否保持屏幕常亮
*/
export async function setKeepScreenOn (opts = {}) {
let res = {errMsg: 'setKeepScreenOn:ok'} as any
const isObject = shouleBeObject(opts)
if (!isObject.res) {
res = {errMsg: `setKeepScreenOn${isObject.msg}`}
console.error(res.errMsg)
return Promise.reject(res)
}
const {keepScreenOn, success, fail, complete}: any = opts
await Permissions.askAsync(Permissions.SYSTEM_BRIGHTNESS)
const {status} = await Permissions.getAsync(Permissions.SYSTEM_BRIGHTNESS)
if (status === 'granted') {
try {
if (keepScreenOn) {
// activateKeepAwake()
} else {
// deactivateKeepAwake()
}
return successHandler(success, complete)(res)
} catch (e) {
res.errMsg = `setKeepScreenOn:fail invalid ${e}`
return errorHandler(fail, complete)(res)
}
}
}
/**
* @todo
* 监听用户主动截屏事件。用户使用系统截屏按键截屏时触发
*/
export function onUserCaptureScreen (callback) {
console.log('onUserCaptureScreen has not finished')
}