1 | import {
|
2 | createPermissionHook,
|
3 | PermissionExpiration,
|
4 | PermissionHookOptions,
|
5 | PermissionResponse,
|
6 | PermissionStatus,
|
7 | UnavailabilityError,
|
8 | Subscription,
|
9 | EventEmitter,
|
10 | } from 'expo-modules-core';
|
11 | import { Platform } from 'react-native';
|
12 |
|
13 | import ExpoBrightness from './ExpoBrightness';
|
14 |
|
15 | const BrightnessEventEmitter = new EventEmitter(ExpoBrightness);
|
16 |
|
17 | // @needsAudit
|
18 | export enum BrightnessMode {
|
19 | /**
|
20 | * Means that the current brightness mode cannot be determined.
|
21 | */
|
22 | UNKNOWN = 0,
|
23 | /**
|
24 | * Mode in which the device OS will automatically adjust the screen brightness depending on the
|
25 | * ambient light.
|
26 | */
|
27 | AUTOMATIC = 1,
|
28 | /**
|
29 | * Mode in which the screen brightness will remain constant and will not be adjusted by the OS.
|
30 | */
|
31 | MANUAL = 2,
|
32 | }
|
33 |
|
34 | // @needsAudit
|
35 | export type BrightnessEvent = {
|
36 | /**
|
37 | * A number between `0` and `1`, inclusive, representing the current screen brightness.
|
38 | */
|
39 | brightness: number;
|
40 | };
|
41 |
|
42 | export { PermissionExpiration, PermissionHookOptions, PermissionResponse, PermissionStatus };
|
43 |
|
44 | /**
|
45 | * Returns whether the Brightness API is enabled on the current device. This does not check the app
|
46 | * permissions.
|
47 | * @return Async `boolean`, indicating whether the Brightness API is available on the current device.
|
48 | * Currently this resolves `true` on iOS and Android only.
|
49 | */
|
50 | export async function isAvailableAsync(): Promise<boolean> {
|
51 | return !!ExpoBrightness.getBrightnessAsync;
|
52 | }
|
53 |
|
54 | // @needsAudit
|
55 | /**
|
56 | * Gets the current brightness level of the device's main screen.
|
57 | * @return A `Promise` that fulfils with a number between `0` and `1`, inclusive, representing the
|
58 | * current screen brightness.
|
59 | */
|
60 | export async function getBrightnessAsync(): Promise<number> {
|
61 | if (!ExpoBrightness.getBrightnessAsync) {
|
62 | throw new UnavailabilityError('expo-brightness', 'getBrightnessAsync');
|
63 | }
|
64 | return await ExpoBrightness.getBrightnessAsync();
|
65 | }
|
66 |
|
67 | // @needsAudit
|
68 | /**
|
69 | * Sets the current screen brightness. On iOS, this setting will persist until the device is locked,
|
70 | * after which the screen brightness will revert to the user's default setting. On Android, this
|
71 | * setting only applies to the current activity; it will override the system brightness value
|
72 | * whenever your app is in the foreground.
|
73 | * @param brightnessValue A number between `0` and `1`, inclusive, representing the desired screen
|
74 | * brightness.
|
75 | * @return A `Promise` that fulfils when the brightness has been successfully set.
|
76 | */
|
77 | export async function setBrightnessAsync(brightnessValue: number): Promise<void> {
|
78 | if (!ExpoBrightness.setBrightnessAsync) {
|
79 | throw new UnavailabilityError('expo-brightness', 'setBrightnessAsync');
|
80 | }
|
81 | const clampedBrightnessValue = Math.max(0, Math.min(brightnessValue, 1));
|
82 | if (isNaN(clampedBrightnessValue)) {
|
83 | throw new TypeError(`setBrightnessAsync cannot be called with ${brightnessValue}`);
|
84 | }
|
85 | return await ExpoBrightness.setBrightnessAsync(clampedBrightnessValue);
|
86 | }
|
87 |
|
88 | // @needsAudit
|
89 | /**
|
90 | * Gets the global system screen brightness.
|
91 | * @return A `Promise` that is resolved with a number between `0` and `1`, inclusive, representing
|
92 | * the current system screen brightness.
|
93 | * @platform android
|
94 | */
|
95 | export async function getSystemBrightnessAsync(): Promise<number> {
|
96 | if (Platform.OS !== 'android') {
|
97 | return await getBrightnessAsync();
|
98 | }
|
99 | return await ExpoBrightness.getSystemBrightnessAsync();
|
100 | }
|
101 |
|
102 | // @needsAudit
|
103 | /**
|
104 | * > __WARNING:__ This method is experimental.
|
105 | *
|
106 | * Sets the global system screen brightness and changes the brightness mode to
|
107 | * `MANUAL`. Requires `SYSTEM_BRIGHTNESS` permissions.
|
108 | * @param brightnessValue A number between `0` and `1`, inclusive, representing the desired screen
|
109 | * brightness.
|
110 | * @return A `Promise` that fulfils when the brightness has been successfully set.
|
111 | * @platform android
|
112 | */
|
113 | export async function setSystemBrightnessAsync(brightnessValue: number): Promise<void> {
|
114 | const clampedBrightnessValue = Math.max(0, Math.min(brightnessValue, 1));
|
115 | if (isNaN(clampedBrightnessValue)) {
|
116 | throw new TypeError(`setSystemBrightnessAsync cannot be called with ${brightnessValue}`);
|
117 | }
|
118 | if (Platform.OS !== 'android') {
|
119 | return await setBrightnessAsync(clampedBrightnessValue);
|
120 | }
|
121 | return await ExpoBrightness.setSystemBrightnessAsync(clampedBrightnessValue);
|
122 | }
|
123 |
|
124 | /**
|
125 | * @deprecated Use [`restoreSystemBrightnessAsync`](#brightnessrestoresystembrightnessasync) method instead.
|
126 | * @platform android
|
127 | */
|
128 | export async function useSystemBrightnessAsync(): Promise<void> {
|
129 | return restoreSystemBrightnessAsync();
|
130 | }
|
131 |
|
132 | // @needsAudit
|
133 | /**
|
134 | * Resets the brightness setting of the current activity to use the system-wide
|
135 | * brightness value rather than overriding it.
|
136 | * @return A `Promise` that fulfils when the setting has been successfully changed.
|
137 | * @platform android
|
138 | */
|
139 | export async function restoreSystemBrightnessAsync(): Promise<void> {
|
140 | if (Platform.OS !== 'android') {
|
141 | return;
|
142 | }
|
143 | return await ExpoBrightness.restoreSystemBrightnessAsync();
|
144 | }
|
145 |
|
146 | // @needsAudit
|
147 | /**
|
148 | * Returns a boolean specifying whether or not the current activity is using the
|
149 | * system-wide brightness value.
|
150 | * @return A `Promise` that fulfils with `true` when the current activity is using the system-wide
|
151 | * brightness value, and `false` otherwise.
|
152 | * @platform android
|
153 | */
|
154 | export async function isUsingSystemBrightnessAsync(): Promise<boolean> {
|
155 | if (Platform.OS !== 'android') {
|
156 | return false;
|
157 | }
|
158 | return await ExpoBrightness.isUsingSystemBrightnessAsync();
|
159 | }
|
160 |
|
161 | // @needsAudit
|
162 | /**
|
163 | * Gets the system brightness mode (e.g. whether or not the OS will automatically
|
164 | * adjust the screen brightness depending on ambient light).
|
165 | * @return A `Promise` that fulfils with a [`BrightnessMode`](#brightnessmode). Requires
|
166 | * `SYSTEM_BRIGHTNESS` permissions.
|
167 | * @platform android
|
168 | */
|
169 | export async function getSystemBrightnessModeAsync(): Promise<BrightnessMode> {
|
170 | if (Platform.OS !== 'android') {
|
171 | return BrightnessMode.UNKNOWN;
|
172 | }
|
173 | return await ExpoBrightness.getSystemBrightnessModeAsync();
|
174 | }
|
175 |
|
176 | // @needsAudit
|
177 | /**
|
178 | * Sets the system brightness mode.
|
179 | * @param brightnessMode One of `BrightnessMode.MANUAL` or `BrightnessMode.AUTOMATIC`. The system
|
180 | * brightness mode cannot be set to `BrightnessMode.UNKNOWN`.
|
181 | * @platform android
|
182 | */
|
183 | export async function setSystemBrightnessModeAsync(brightnessMode: BrightnessMode): Promise<void> {
|
184 | if (Platform.OS !== 'android' || brightnessMode === BrightnessMode.UNKNOWN) {
|
185 | return;
|
186 | }
|
187 | return await ExpoBrightness.setSystemBrightnessModeAsync(brightnessMode);
|
188 | }
|
189 |
|
190 | // @needsAudit
|
191 | /**
|
192 | * Checks user's permissions for accessing system brightness.
|
193 | * @return A promise that fulfils with an object of type [PermissionResponse](#permissionrespons).
|
194 | */
|
195 | export async function getPermissionsAsync(): Promise<PermissionResponse> {
|
196 | return ExpoBrightness.getPermissionsAsync();
|
197 | }
|
198 |
|
199 | // @needsAudit
|
200 | /**
|
201 | * Asks the user to grant permissions for accessing system brightness.
|
202 | * @return A promise that fulfils with an object of type [PermissionResponse](#permissionrespons).
|
203 | */
|
204 | export async function requestPermissionsAsync(): Promise<PermissionResponse> {
|
205 | return ExpoBrightness.requestPermissionsAsync();
|
206 | }
|
207 |
|
208 | // @needsAudit
|
209 | /**
|
210 | * Check or request permissions to modify the system brightness.
|
211 | * This uses both `requestPermissionAsync` and `getPermissionsAsync` to interact with the permissions.
|
212 | *
|
213 | * @example
|
214 | * ```ts
|
215 | * const [permissionResponse, requestPermission] = Brightness.usePermissions();
|
216 | * ```
|
217 | */
|
218 | export const usePermissions = createPermissionHook({
|
219 | getMethod: getPermissionsAsync,
|
220 | requestMethod: requestPermissionsAsync,
|
221 | });
|
222 |
|
223 | // @needsAudit
|
224 | /**
|
225 | * Subscribe to brightness (iOS) updates. The event fires whenever
|
226 | * the power mode is toggled.
|
227 | *
|
228 | * On web and android the event never fires.
|
229 | * @param listener A callback that is invoked when brightness (iOS) changes.
|
230 | * The callback is provided a single argument that is an object with a `brightness` key.
|
231 | * @return A `Subscription` object on which you can call `remove()` to unsubscribe from the listener.
|
232 | * @platform ios
|
233 | */
|
234 | export function addBrightnessListener(listener: (event: BrightnessEvent) => void): Subscription {
|
235 | return BrightnessEventEmitter.addListener('Expo.brightnessDidChange', listener);
|
236 | }
|