1import { PermissionStatus, EventEmitter, Platform, } from 'expo-modules-core';
2/**
3 * A base class for subscribable sensors. The events emitted by this class are measurements
4 * specified by the parameter type `Measurement`.
5 */
6export default class DeviceSensor {
7    _nativeModule;
8    _nativeEmitter;
9    _nativeEventName;
10    _listenerCount;
11    constructor(nativeSensorModule, nativeEventName) {
12        this._nativeModule = nativeSensorModule;
13        this._nativeEmitter = new EventEmitter(nativeSensorModule);
14        this._nativeEventName = nativeEventName;
15        this._listenerCount = 0;
16    }
17    addListener(listener) {
18        const subscription = this._nativeEmitter.addListener(this._nativeEventName, listener);
19        subscription.remove = () => this.removeSubscription(subscription);
20        this._listenerCount++;
21        return subscription;
22    }
23    /**
24     * Returns boolean which signifies if sensor has any listeners registered.
25     */
26    hasListeners() {
27        return this._listenerCount > 0;
28    }
29    /**
30     * Returns the registered listeners count.
31     */
32    getListenerCount() {
33        return this._listenerCount;
34    }
35    /**
36     * Removes all registered listeners.
37     */
38    removeAllListeners() {
39        this._listenerCount = 0;
40        this._nativeEmitter.removeAllListeners(this._nativeEventName);
41    }
42    /**
43     * Removes the given subscription.
44     * @param subscription A subscription to remove.
45     */
46    removeSubscription(subscription) {
47        this._listenerCount--;
48        this._nativeEmitter.removeSubscription(subscription);
49    }
50    /**
51     * Set the sensor update interval.
52     *
53     * @param intervalMs Desired interval in milliseconds between sensor updates.
54     * > Starting from Android 12 (API level 31), the system has a 200ms limit for each sensor updates.
55     * >
56     * > If you need an update interval less than 200ms, you should:
57     * > * add `android.permission.HIGH_SAMPLING_RATE_SENSORS` to [**app.json** `permissions` field](/versions/latest/config/app/#permissions)
58     * > * or if you are using bare workflow, add `<uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS"/>` to **AndroidManifest.xml**.
59     */
60    setUpdateInterval(intervalMs) {
61        if (!this._nativeModule.setUpdateInterval) {
62            console.warn(`expo-sensors: setUpdateInterval() is not supported on ${Platform.OS}`);
63        }
64        else {
65            this._nativeModule.setUpdateInterval(intervalMs);
66        }
67    }
68    /**
69     * > **info** You should always check the sensor availability before attempting to use it.
70     * @return A promise that resolves to a `boolean` denoting the availability of the sensor.
71     */
72    async isAvailableAsync() {
73        if (!this._nativeModule.isAvailableAsync) {
74            return false;
75        }
76        else {
77            return await this._nativeModule.isAvailableAsync();
78        }
79    }
80    /**
81     * Checks user's permissions for accessing sensor.
82     */
83    async getPermissionsAsync() {
84        if (!this._nativeModule.getPermissionsAsync) {
85            return defaultPermissionsResponse;
86        }
87        else {
88            return await this._nativeModule.getPermissionsAsync();
89        }
90    }
91    /**
92     * Asks the user to grant permissions for accessing sensor.
93     */
94    async requestPermissionsAsync() {
95        if (!this._nativeModule.requestPermissionsAsync) {
96            return defaultPermissionsResponse;
97        }
98        else {
99            return await this._nativeModule.requestPermissionsAsync();
100        }
101    }
102}
103const defaultPermissionsResponse = {
104    granted: true,
105    expires: 'never',
106    canAskAgain: true,
107    status: PermissionStatus.GRANTED,
108};
109export { PermissionStatus };
110//# sourceMappingURL=DeviceSensor.js.map