forked from halilb/react-native-system-setting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemSetting.js
258 lines (217 loc) · 7.36 KB
/
SystemSetting.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { NativeModules, NativeEventEmitter, Linking, Platform } from 'react-native'
import Utils from './Utils'
const SystemSettingNative = NativeModules.SystemSetting
const SCREEN_BRIGHTNESS_MODE_UNKNOW = -1
const SCREEN_BRIGHTNESS_MODE_MANUAL = 0
const SCREEN_BRIGHTNESS_MODE_AUTOMATIC = 1
const eventEmitter = new NativeEventEmitter(SystemSettingNative)
export default class SystemSetting {
static saveBrightnessVal = -1
static saveScreenModeVal = SCREEN_BRIGHTNESS_MODE_AUTOMATIC
/**
* @deprecated
*/
static setAppStore() {
console.warn("You don't need call setAppStore() anymore since V1.7.0")
}
static async getBrightness() {
return await SystemSettingNative.getBrightness()
}
static async setBrightness(val) {
try {
await SystemSettingNative.setBrightness(val)
return true
} catch (e) {
return false
}
}
static async setBrightnessForce(val) {
if (Utils.isAndroid) {
const success = await SystemSetting.setScreenMode(SCREEN_BRIGHTNESS_MODE_MANUAL)
if (!success) {
return false
}
}
return await SystemSetting.setBrightness(val)
}
static setAppBrightness(val) {
if (Utils.isAndroid) {
SystemSettingNative.setAppBrightness(val)
} else {
SystemSetting.setBrightness(val)
}
return true
}
static async getAppBrightness() {
if (Utils.isAndroid) {
return SystemSettingNative.getAppBrightness()
} else {
return SystemSetting.getBrightness()
}
}
static grantWriteSettingPremission() {
if (Utils.isAndroid) {
SystemSettingNative.openWriteSetting()
}
}
static async getScreenMode() {
if (Utils.isAndroid) {
return await SystemSettingNative.getScreenMode()
}
return -1 // cannot get iOS screen mode
}
static async setScreenMode(val) {
if (Utils.isAndroid) {
try {
await SystemSettingNative.setScreenMode(val)
} catch (e) {
return false
}
}
return true
}
static async saveBrightness() {
SystemSetting.saveBrightnessVal = await SystemSetting.getBrightness()
SystemSetting.saveScreenModeVal = await SystemSetting.getScreenMode()
}
static restoreBrightness() {
if (SystemSetting.saveBrightnessVal == -1) {
console.warn('you should call saveBrightness() at least once')
} else {
SystemSetting.setBrightness(SystemSetting.saveBrightnessVal)
SystemSetting.setScreenMode(SystemSetting.saveScreenModeVal)
}
return SystemSetting.saveBrightnessVal
}
static async getVolume(type = 'music') {
return await SystemSettingNative.getVolume(type)
}
static setVolume(val, config = {}) {
if (typeof (config) === 'string') {
console.log('setVolume(val, type) is deprecated since 1.2.2, use setVolume(val, config) instead')
config = { type: config }
}
config = Object.assign({
playSound: false,
type: 'music',
showUI: false
}, config)
SystemSettingNative.setVolume(val, config)
}
static addVolumeListener(callback) {
return eventEmitter.addListener('EventVolume', callback)
}
static removeVolumeListener(listener) {
listener && listener.remove()
}
static async isWifiEnabled() {
const result = await SystemSettingNative.isWifiEnabled()
return (result) > 0
}
static switchWifiSilence(complete) {
if (Utils.isAndroid) {
SystemSetting.listenEvent(complete)
SystemSettingNative.switchWifiSilence()
} else {
SystemSetting.switchWifi(complete)
}
}
static switchWifi(complete) {
SystemSetting.listenEvent(complete)
SystemSettingNative.switchWifi()
}
static async isLocationEnabled() {
return await SystemSettingNative.isLocationEnabled()
}
static async getLocationMode() {
if (Utils.isAndroid) {
return await SystemSettingNative.getLocationMode()
} else {
return await SystemSetting.isLocationEnabled() ? 1 : 0
}
}
static switchLocation(complete) {
SystemSetting.listenEvent(complete)
SystemSettingNative.switchLocation()
}
static async isBluetoothEnabled() {
return await SystemSettingNative.isBluetoothEnabled()
}
static switchBluetooth(complete) {
SystemSetting.listenEvent(complete)
SystemSettingNative.switchBluetooth()
}
static switchBluetoothSilence(complete) {
if (Utils.isAndroid) {
SystemSetting.listenEvent(complete)
SystemSettingNative.switchBluetoothSilence()
} else {
SystemSettingNative.switchBluetooth(complete)
}
}
static async isAirplaneEnabled() {
return await SystemSettingNative.isAirplaneEnabled()
}
static switchAirplane(complete) {
SystemSetting.listenEvent(complete)
SystemSettingNative.switchAirplane()
}
static async openAppSystemSettings() {
switch(Platform.OS) {
case 'ios': {
const settingsLink = 'app-settings:';
const supported = await Linking.canOpenURL(settingsLink)
if (supported) await Linking.openURL(settingsLink);
break;
}
case 'android':
await SystemSettingNative.openAppSystemSettings()
break;
default:
throw new Error('unknown platform')
break;
}
}
static async addBluetoothListener(callback) {
return await SystemSetting._addListener(false, 'bluetooth', 'EventBluetoothChange', callback)
}
static async addWifiListener(callback) {
return await SystemSetting._addListener(true, 'wifi', 'EventWifiChange', callback)
}
static async addLocationListener(callback) {
return await SystemSetting._addListener(true, 'location', 'EventLocationChange', callback)
}
static async addLocationModeListener(callback) {
return await SystemSetting._addListener(true, 'locationMode', 'EventLocationModeChange', callback)
}
static async addAirplaneListener(callback) {
return await SystemSetting._addListener(true, 'airplane', 'EventAirplaneChange', callback)
}
static async _addListener(androidOnly, type, eventName, callback) {
if (!androidOnly || Utils.isAndroid) {
const result = await SystemSetting._activeListener(type)
if (result) {
return eventEmitter.addListener(eventName, callback)
}
}
return null
}
static async _activeListener(name) {
try {
await SystemSettingNative.activeListener(name)
} catch (e) {
console.warn(e.message)
return false;
}
return true;
}
static removeListener(listener) {
listener && listener.remove()
}
static listenEvent(complete) {
const listener = eventEmitter.addListener('EventEnterForeground', () => {
listener.remove()
complete()
})
}
}