1/**
2 * Copyright (c) 2023 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
16/**
17 * Vibration And Proximity Util
18 *
19 * standard:
20 * 1. define TAG, recommend class name.
21 * 2. switch IS_DEBUG_ON as true, when debugging.
22 * 3. msg should be short and valuable.
23 * 4. choose appropriate function.
24 * 5. the function execute many times can not print.
25 * 6. uniqueness.
26 */
27import audio from '@ohos.multimedia.audio';
28import CallServiceProxy from '../../model/CallServiceProxy';
29import LogUtils from './LogUtils';
30import runningLock from '@ohos.runningLock';
31import vibrator from '@ohos.vibrator';
32
33const TAG = 'VibrationAndProximityUtils';
34const VIBRATION_COUNT = 20;
35
36/**
37 *  Vibration And Proximity tool class
38 */
39export class VibrationAndProximityUtils {
40  private static startVibrationFlag = false;
41  private static recordLock = null;
42
43  /**
44   * suspend Screen
45   */
46  public suspendScreen(): void {
47    if (VibrationAndProximityUtils.recordLock) {
48      VibrationAndProximityUtils.recordLock.hold(Number.MAX_VALUE);
49      LogUtils.i(TAG, 'suspendScreen hold');
50    } else {
51      runningLock.create('call_lock', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL).then(lock => {
52        LogUtils.i(TAG, 'suspendScreen create running lock success');
53        try {
54          VibrationAndProximityUtils.recordLock = lock;
55          lock.hold(Number.MAX_VALUE);
56          LogUtils.i(TAG, 'suspendScreen hold');
57        } catch (err) {
58          LogUtils.e(TAG, 'suspendScreen hold running lock failed, err: ' + err);
59        }
60      }).catch(err => {
61        LogUtils.e(TAG, 'suspendScreen create running lock failed, err: ' + err);
62      });
63    }
64  }
65
66  /**
67   * wakeup Screen
68   */
69  public wakeupScreen(): void {
70    if (VibrationAndProximityUtils.recordLock) {
71      VibrationAndProximityUtils.recordLock.unhold();
72      LogUtils.i(TAG, 'wakeupScreen unhold');
73    } else {
74      runningLock.create('call_lock', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL).then(lock => {
75        LogUtils.i(TAG, 'wakeupScreen create running lock success');
76        try {
77          VibrationAndProximityUtils.recordLock = lock;
78          lock.unhold();
79          LogUtils.i(TAG, 'wakeupScreen unhold');
80        } catch (err) {
81          LogUtils.e(TAG, 'wakeupScreen unhold running lock failed, err: ' + err);
82        }
83      }).catch(err => {
84        LogUtils.e(TAG, 'wakeupScreen create running lock failed, err: ' + err);
85      });
86    }
87  }
88
89  /**
90   * start Vibration
91   */
92  public startVibration(): void {
93    try {
94      if (VibrationAndProximityUtils.startVibrationFlag) {
95        LogUtils.e(TAG, 'startVibration return');
96        return;
97      }
98      vibrator.startVibration({
99        type: 'preset',
100        effectId: 'haptic.ringtone.T-Mobile_Ring',
101        count: VIBRATION_COUNT
102      }, {
103        usage: 'ring'
104      }, (error) => {
105        if (error) {
106          LogUtils.e(TAG, 'startVibration fail, error.code: ' + error.code + ', error.message: ' + error.message);
107        } else {
108          VibrationAndProximityUtils.startVibrationFlag = true;
109          LogUtils.i(TAG, 'startVibration Callback returned to indicate a successful vibration.');
110        }
111      });
112    } catch (err) {
113      LogUtils.e(TAG, 'startVibration errCode: ' + err.code + ' ,msg: ' + err.message);
114    }
115  }
116
117  /**
118   * stop Vibration
119   */
120  public stopVibration(): void {
121    if (!VibrationAndProximityUtils.startVibrationFlag) {
122      LogUtils.e(TAG, 'stopVibration return');
123      return;
124    }
125    try {
126      vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function (error) {
127        if (error) {
128          LogUtils.e(TAG, 'stopVibration error.code: ' + error.code + ', error.message: ' + error.message);
129          return;
130        }
131        VibrationAndProximityUtils.startVibrationFlag = false;
132        LogUtils.i(TAG, 'stopVibration Callback returned to indicate successful.');
133      });
134    } catch (err) {
135      LogUtils.e(TAG, 'stopVibration errCode: ' + err.code + ' ,msg: ' + err.message);
136    }
137  }
138
139  /**
140   * add Voice Observe
141   */
142  addVoiceObserver(): void {
143    audio.getAudioManager().on('volumeChange', () => {
144      LogUtils.i(TAG, 'addVoiceObserver volumeChange');
145      this.stopVibration();
146      CallServiceProxy.getInstance().muteRinger();
147    });
148  }
149}
150
151let mVibrationAndProximityUtils = new VibrationAndProximityUtils();
152
153export default mVibrationAndProximityUtils as VibrationAndProximityUtils;
154
155