• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { AsyncCallback } from './basic';
17
18/**
19 * This module provides the capability to control motor vibration.
20 *
21 * @since 8
22 * @syscap SystemCapability.Sensors.MiscDevice
23 */
24declare namespace vibrator {
25    /**
26     * The trigger motor vibrates for a specified length of time.
27     * @param duration Indicate the duration of the motor vibration.
28     * @syscap SystemCapability.Sensors.MiscDevice
29     * @permission ohos.permission.VIBRATE
30     * @since 8
31     * @deprecated since 9
32     * @useinstead vibrator#startVibration
33     */
34    function vibrate(duration: number, callback?: AsyncCallback<void>): void;
35    function vibrate(duration: number): Promise<void>;
36
37    /**
38     * The trigger motor vibrates for the specified effect of the preset.
39     * @param effectId Indicate the specified effect of the preset, {@code EffectId}.
40     * @syscap SystemCapability.Sensors.MiscDevice
41     * @permission ohos.permission.VIBRATE
42     * @since 8
43     * @deprecated since 9
44     * @useinstead vibrator#startVibration
45     */
46    function vibrate(effectId: EffectId): Promise<void>;
47    function vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void;
48
49    /**
50     * Trigger vibrator vibration.
51     * @param { VibrateEffect } effect - Indicate vibrate effect, {@code VibrateEffect}.
52     * @param { VibrateAttribute } attribute - Indicate vibrate attribute, {@code VibrateAttribute}.
53     * @param { AsyncCallback<void> } callback - The callback of startVibration.
54     * @throws { BusinessError } 201 - Permission denied.
55     * @throws { BusinessError } 401 - Parameter error.
56     * @throws { BusinessError } 14600101 - Device operation failed.
57     * @permission ohos.permission.VIBRATE
58     * @syscap SystemCapability.Sensors.MiscDevice
59     * @since 9
60     */
61    function startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback<void>): void;
62
63    /**
64     * Trigger vibrator vibration.
65     * @param { VibrateEffect } effect - Indicate vibrate effect, {@code VibrateEffect}.
66     * @param { VibrateAttribute } attribute - Indicate vibrate attribute, {@code VibrateAttribute}.
67     * @returns { Promise<void>} Promise used to return the result.
68     * @throws { BusinessError } 201 - Permission denied.
69     * @throws { BusinessError } 401 - Parameter error.
70     * @throws { BusinessError } 14600101 - Device operation failed.
71     * @permission ohos.permission.VIBRATE
72     * @syscap SystemCapability.Sensors.MiscDevice
73     * @since 9
74     */
75    function startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<void>;
76
77    /**
78     * Stop the vibrator from vibrating.
79     * @param { VibratorStopMode } stopMode - Indicate the stop mode in which the motor vibrates, {@code VibratorStopMode}.
80     * @returns { Promise<void> } Promise used to return the result.
81     * @throws { BusinessError } 201 - Permission denied.
82     * @throws { BusinessError } 401 - Parameter error.
83     * @syscap SystemCapability.Sensors.MiscDevice
84     * @permission ohos.permission.VIBRATE
85     * @since 9
86     */
87    function stopVibration(stopMode: VibratorStopMode): Promise<void>;
88
89    /**
90     * Stop the vibrator from vibrating.
91     * @param { VibratorStopMode } stopMode - Indicate the stop mode in which the motor vibrates, {@code VibratorStopMode}.
92     * @param { AsyncCallback<void> } callback - The callback of stopVibration.
93     * @throws { BusinessError } 201 - Permission denied.
94     * @throws { BusinessError } 401 - Parameter error.
95     * @syscap SystemCapability.Sensors.MiscDevice
96     * @permission ohos.permission.VIBRATE
97     * @since 9
98     */
99    function stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback<void>): void;
100
101    /**
102     * Stop the motor from vibrating.
103     * @param stopMode Indicate the stop mode in which the motor vibrates, {@code VibratorStopMode}.
104     * @syscap SystemCapability.Sensors.MiscDevice
105     * @permission ohos.permission.VIBRATE
106     * @since 8
107     * @deprecated since 9
108     * @useinstead vibrator#stopVibration
109     */
110    function stop(stopMode: VibratorStopMode): Promise<void>;
111    function stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void;
112
113    /**
114     * Preset vibration effect string.
115     * @syscap SystemCapability.Sensors.MiscDevice
116     * @since 8
117     */
118    enum EffectId {
119        /* Describes the vibration effect of the vibrator when a user adjusts the timer.*/
120        EFFECT_CLOCK_TIMER = "haptic.clock.timer",
121    }
122
123    /**
124     * Vibrator vibration stop mode.
125     * @syscap SystemCapability.Sensors.MiscDevice
126     * @since 8
127     */
128    enum VibratorStopMode {
129        /* Indicates the mode of stopping a one-shot vibration effect.*/
130        VIBRATOR_STOP_MODE_TIME = "time",
131        /* Indicates the mode of stopping a preset vibration effect.*/
132        VIBRATOR_STOP_MODE_PRESET = "preset",
133    }
134
135    /**
136     * The use of vibration.
137     * @syscap SystemCapability.Sensors.MiscDevice
138     * @since 9
139     */
140    type Usage = "unknown" | "alarm" | "ring" | "notification" | "communication" |
141        "touch" | "media" | "physicalFeedback" | "simulateReality";
142
143    /**
144     * The attribute of vibration.
145     * @syscap SystemCapability.Sensors.MiscDevice
146     * @since 9
147     */
148    interface VibrateAttribute {
149        id?: number, /** Vibrator id, default is 0. */
150        usage: Usage, /** The use of vibration. */
151    }
152
153    /**
154     * Describes the effect of vibration.
155     * @syscap SystemCapability.Sensors.MiscDevice
156     * @since 9
157     */
158     type VibrateEffect = VibrateTime | VibratePreset;
159
160    /**
161     * Vibrate continuously for a period of time at the default intensity of the system.
162     * @syscap SystemCapability.Sensors.MiscDevice
163     * @since 9
164     */
165    interface VibrateTime {
166        type: "time";
167        duration: number; /** The duration of the vibration, in ms */
168    }
169
170    /**
171     * Preset vibration type vibration effect.
172     * @syscap SystemCapability.Sensors.MiscDevice
173     * @since 9
174     */
175    interface VibratePreset {
176        type: "preset";
177        effectId: string; /** Preset type vibration */
178        count: number; /** The number of vibration repetitions */
179    }
180}
181
182export default vibrator;