UNPKG

7.07 kBJavaScriptView Raw
1import { createPermissionHook, PermissionStatus, UnavailabilityError, } from 'expo-modules-core';
2import { Platform } from 'react-native';
3import ExpoBrightness from './ExpoBrightness';
4// @needsAudit
5export var BrightnessMode;
6(function (BrightnessMode) {
7 /**
8 * Means that the current brightness mode cannot be determined.
9 */
10 BrightnessMode[BrightnessMode["UNKNOWN"] = 0] = "UNKNOWN";
11 /**
12 * Mode in which the device OS will automatically adjust the screen brightness depending on the
13 * ambient light.
14 */
15 BrightnessMode[BrightnessMode["AUTOMATIC"] = 1] = "AUTOMATIC";
16 /**
17 * Mode in which the screen brightness will remain constant and will not be adjusted by the OS.
18 */
19 BrightnessMode[BrightnessMode["MANUAL"] = 2] = "MANUAL";
20})(BrightnessMode || (BrightnessMode = {}));
21export { PermissionStatus };
22/**
23 * Returns whether the Brightness API is enabled on the current device. This does not check the app
24 * permissions.
25 * @return Async `boolean`, indicating whether the Brightness API is available on the current device.
26 * Currently this resolves `true` on iOS and Android only.
27 */
28export async function isAvailableAsync() {
29 return !!ExpoBrightness.getBrightnessAsync;
30}
31// @needsAudit
32/**
33 * Gets the current brightness level of the device's main screen.
34 * @return A `Promise` that fulfils with a number between `0` and `1`, inclusive, representing the
35 * current screen brightness.
36 */
37export async function getBrightnessAsync() {
38 if (!ExpoBrightness.getBrightnessAsync) {
39 throw new UnavailabilityError('expo-brightness', 'getBrightnessAsync');
40 }
41 return await ExpoBrightness.getBrightnessAsync();
42}
43// @needsAudit
44/**
45 * Sets the current screen brightness. On iOS, this setting will persist until the device is locked,
46 * after which the screen brightness will revert to the user's default setting. On Android, this
47 * setting only applies to the current activity; it will override the system brightness value
48 * whenever your app is in the foreground.
49 * @param brightnessValue A number between `0` and `1`, inclusive, representing the desired screen
50 * brightness.
51 * @return A `Promise` that fulfils when the brightness has been successfully set.
52 */
53export async function setBrightnessAsync(brightnessValue) {
54 if (!ExpoBrightness.setBrightnessAsync) {
55 throw new UnavailabilityError('expo-brightness', 'setBrightnessAsync');
56 }
57 const clampedBrightnessValue = Math.max(0, Math.min(brightnessValue, 1));
58 if (isNaN(clampedBrightnessValue)) {
59 throw new TypeError(`setBrightnessAsync cannot be called with ${brightnessValue}`);
60 }
61 return await ExpoBrightness.setBrightnessAsync(clampedBrightnessValue);
62}
63// @needsAudit
64/**
65 * __Android only.__ Gets the global system screen brightness.
66 * @return A `Promise` that is resolved with a number between `0` and `1`, inclusive, representing
67 * the current system screen brightness.
68 */
69export async function getSystemBrightnessAsync() {
70 if (Platform.OS !== 'android') {
71 return await getBrightnessAsync();
72 }
73 return await ExpoBrightness.getSystemBrightnessAsync();
74}
75// @needsAudit
76/**
77 * > __WARNING:__ This method is experimental.
78 *
79 * __Android only.__ Sets the global system screen brightness and changes the brightness mode to
80 * `MANUAL`. Requires `SYSTEM_BRIGHTNESS` permissions.
81 * @param brightnessValue A number between `0` and `1`, inclusive, representing the desired screen
82 * brightness.
83 * @return A `Promise` that fulfils when the brightness has been successfully set.
84 */
85export async function setSystemBrightnessAsync(brightnessValue) {
86 const clampedBrightnessValue = Math.max(0, Math.min(brightnessValue, 1));
87 if (isNaN(clampedBrightnessValue)) {
88 throw new TypeError(`setSystemBrightnessAsync cannot be called with ${brightnessValue}`);
89 }
90 if (Platform.OS !== 'android') {
91 return await setBrightnessAsync(clampedBrightnessValue);
92 }
93 return await ExpoBrightness.setSystemBrightnessAsync(clampedBrightnessValue);
94}
95// @needsAudit
96/**
97 * __Android only.__ Resets the brightness setting of the current activity to use the system-wide
98 * brightness value rather than overriding it.
99 * @return A `Promise` that fulfils when the setting has been successfully changed.
100 */
101export async function useSystemBrightnessAsync() {
102 if (Platform.OS !== 'android') {
103 return;
104 }
105 // eslint-disable-next-line react-hooks/rules-of-hooks
106 return await ExpoBrightness.useSystemBrightnessAsync();
107}
108// @needsAudit
109/**
110 * __Android only.__ Returns a boolean specifying whether or not the current activity is using the
111 * system-wide brightness value.
112 * @return A `Promise` that fulfils with `true` when the current activity is using the system-wide
113 * brightness value, and `false` otherwise.
114 */
115export async function isUsingSystemBrightnessAsync() {
116 if (Platform.OS !== 'android') {
117 return false;
118 }
119 return await ExpoBrightness.isUsingSystemBrightnessAsync();
120}
121// @needsAudit
122/**
123 * __Android only.__ Gets the system brightness mode (e.g. whether or not the OS will automatically
124 * adjust the screen brightness depending on ambient light).
125 * @return A `Promise` that fulfils with a [`BrightnessMode`](#brightnessmode). Requires
126 * `SYSTEM_BRIGHTNESS` permissions.
127 */
128export async function getSystemBrightnessModeAsync() {
129 if (Platform.OS !== 'android') {
130 return BrightnessMode.UNKNOWN;
131 }
132 return await ExpoBrightness.getSystemBrightnessModeAsync();
133}
134// @needsAudit
135/**
136 * __Android only.__ Sets the system brightness mode.
137 * @param brightnessMode One of `BrightnessMode.MANUAL` or `BrightnessMode.AUTOMATIC`. The system
138 * brightness mode cannot be set to `BrightnessMode.UNKNOWN`.
139 */
140export async function setSystemBrightnessModeAsync(brightnessMode) {
141 if (Platform.OS !== 'android' || brightnessMode === BrightnessMode.UNKNOWN) {
142 return;
143 }
144 return await ExpoBrightness.setSystemBrightnessModeAsync(brightnessMode);
145}
146// @needsAudit
147/**
148 * Checks user's permissions for accessing system brightness.
149 * @return A promise that fulfils with an object of type [PermissionResponse](#permissionrespons).
150 */
151export async function getPermissionsAsync() {
152 return ExpoBrightness.getPermissionsAsync();
153}
154// @needsAudit
155/**
156 * Asks the user to grant permissions for accessing system brightness.
157 * @return A promise that fulfils with an object of type [PermissionResponse](#permissionrespons).
158 */
159export async function requestPermissionsAsync() {
160 return ExpoBrightness.requestPermissionsAsync();
161}
162// @needsAudit
163/**
164 * Check or request permissions to modify the system brightness.
165 * This uses both `requestPermissionAsync` and `getPermissionsAsync` to interact with the permissions.
166 *
167 * @example
168 * ```ts
169 * const [status, requestPermission] = Brightness.usePermissions();
170 * ```
171 */
172export const usePermissions = createPermissionHook({
173 getMethod: getPermissionsAsync,
174 requestMethod: requestPermissionsAsync,
175});
176//# sourceMappingURL=Brightness.js.map
\No newline at end of file