199da06d0Sopenharmony_ci/**
299da06d0Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
399da06d0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
499da06d0Sopenharmony_ci * you may not use this file except in compliance with the License.
599da06d0Sopenharmony_ci * You may obtain a copy of the License at
699da06d0Sopenharmony_ci *
799da06d0Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
899da06d0Sopenharmony_ci *
999da06d0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1099da06d0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1199da06d0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1299da06d0Sopenharmony_ci * See the License for the specific language governing permissions and
1399da06d0Sopenharmony_ci * limitations under the License.
1499da06d0Sopenharmony_ci */
1599da06d0Sopenharmony_ci
1699da06d0Sopenharmony_ci/**
1799da06d0Sopenharmony_ci * Vibration And Proximity Util
1899da06d0Sopenharmony_ci *
1999da06d0Sopenharmony_ci * standard:
2099da06d0Sopenharmony_ci * 1. define TAG, recommend class name.
2199da06d0Sopenharmony_ci * 2. switch IS_DEBUG_ON as true, when debugging.
2299da06d0Sopenharmony_ci * 3. msg should be short and valuable.
2399da06d0Sopenharmony_ci * 4. choose appropriate function.
2499da06d0Sopenharmony_ci * 5. the function execute many times can not print.
2599da06d0Sopenharmony_ci * 6. uniqueness.
2699da06d0Sopenharmony_ci */
2799da06d0Sopenharmony_ciimport audio from '@ohos.multimedia.audio';
2899da06d0Sopenharmony_ciimport CallServiceProxy from '../../model/CallServiceProxy';
2999da06d0Sopenharmony_ciimport LogUtils from './LogUtils';
3099da06d0Sopenharmony_ciimport runningLock from '@ohos.runningLock';
3199da06d0Sopenharmony_ciimport vibrator from '@ohos.vibrator';
3299da06d0Sopenharmony_ci
3399da06d0Sopenharmony_ciconst TAG = 'VibrationAndProximityUtils';
3499da06d0Sopenharmony_ciconst VIBRATION_COUNT = 20;
3599da06d0Sopenharmony_ci
3699da06d0Sopenharmony_ci/**
3799da06d0Sopenharmony_ci *  Vibration And Proximity tool class
3899da06d0Sopenharmony_ci */
3999da06d0Sopenharmony_ciexport class VibrationAndProximityUtils {
4099da06d0Sopenharmony_ci  private static startVibrationFlag = false;
4199da06d0Sopenharmony_ci  private static recordLock = null;
4299da06d0Sopenharmony_ci
4399da06d0Sopenharmony_ci  /**
4499da06d0Sopenharmony_ci   * suspend Screen
4599da06d0Sopenharmony_ci   */
4699da06d0Sopenharmony_ci  public suspendScreen(): void {
4799da06d0Sopenharmony_ci    if (VibrationAndProximityUtils.recordLock) {
4899da06d0Sopenharmony_ci      VibrationAndProximityUtils.recordLock.hold(Number.MAX_VALUE);
4999da06d0Sopenharmony_ci      LogUtils.i(TAG, 'suspendScreen hold');
5099da06d0Sopenharmony_ci    } else {
5199da06d0Sopenharmony_ci      runningLock.create('call_lock', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL).then(lock => {
5299da06d0Sopenharmony_ci        LogUtils.i(TAG, 'suspendScreen create running lock success');
5399da06d0Sopenharmony_ci        try {
5499da06d0Sopenharmony_ci          VibrationAndProximityUtils.recordLock = lock;
5599da06d0Sopenharmony_ci          lock.hold(Number.MAX_VALUE);
5699da06d0Sopenharmony_ci          LogUtils.i(TAG, 'suspendScreen hold');
5799da06d0Sopenharmony_ci        } catch (err) {
5899da06d0Sopenharmony_ci          LogUtils.e(TAG, 'suspendScreen hold running lock failed, err: ' + err);
5999da06d0Sopenharmony_ci        }
6099da06d0Sopenharmony_ci      }).catch(err => {
6199da06d0Sopenharmony_ci        LogUtils.e(TAG, 'suspendScreen create running lock failed, err: ' + err);
6299da06d0Sopenharmony_ci      });
6399da06d0Sopenharmony_ci    }
6499da06d0Sopenharmony_ci  }
6599da06d0Sopenharmony_ci
6699da06d0Sopenharmony_ci  /**
6799da06d0Sopenharmony_ci   * wakeup Screen
6899da06d0Sopenharmony_ci   */
6999da06d0Sopenharmony_ci  public wakeupScreen(): void {
7099da06d0Sopenharmony_ci    if (VibrationAndProximityUtils.recordLock) {
7199da06d0Sopenharmony_ci      VibrationAndProximityUtils.recordLock.unhold();
7299da06d0Sopenharmony_ci      LogUtils.i(TAG, 'wakeupScreen unhold');
7399da06d0Sopenharmony_ci    } else {
7499da06d0Sopenharmony_ci      runningLock.create('call_lock', runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL).then(lock => {
7599da06d0Sopenharmony_ci        LogUtils.i(TAG, 'wakeupScreen create running lock success');
7699da06d0Sopenharmony_ci        try {
7799da06d0Sopenharmony_ci          VibrationAndProximityUtils.recordLock = lock;
7899da06d0Sopenharmony_ci          lock.unhold();
7999da06d0Sopenharmony_ci          LogUtils.i(TAG, 'wakeupScreen unhold');
8099da06d0Sopenharmony_ci        } catch (err) {
8199da06d0Sopenharmony_ci          LogUtils.e(TAG, 'wakeupScreen unhold running lock failed, err: ' + err);
8299da06d0Sopenharmony_ci        }
8399da06d0Sopenharmony_ci      }).catch(err => {
8499da06d0Sopenharmony_ci        LogUtils.e(TAG, 'wakeupScreen create running lock failed, err: ' + err);
8599da06d0Sopenharmony_ci      });
8699da06d0Sopenharmony_ci    }
8799da06d0Sopenharmony_ci  }
8899da06d0Sopenharmony_ci
8999da06d0Sopenharmony_ci  /**
9099da06d0Sopenharmony_ci   * start Vibration
9199da06d0Sopenharmony_ci   */
9299da06d0Sopenharmony_ci  public startVibration(): void {
9399da06d0Sopenharmony_ci    try {
9499da06d0Sopenharmony_ci      if (VibrationAndProximityUtils.startVibrationFlag) {
9599da06d0Sopenharmony_ci        LogUtils.e(TAG, 'startVibration return');
9699da06d0Sopenharmony_ci        return;
9799da06d0Sopenharmony_ci      }
9899da06d0Sopenharmony_ci      vibrator.startVibration({
9999da06d0Sopenharmony_ci        type: 'preset',
10099da06d0Sopenharmony_ci        effectId: 'haptic.ringtone.T-Mobile_Ring',
10199da06d0Sopenharmony_ci        count: VIBRATION_COUNT
10299da06d0Sopenharmony_ci      }, {
10399da06d0Sopenharmony_ci        usage: 'ring'
10499da06d0Sopenharmony_ci      }, (error) => {
10599da06d0Sopenharmony_ci        if (error) {
10699da06d0Sopenharmony_ci          LogUtils.e(TAG, 'startVibration fail, error.code: ' + error.code + ', error.message: ' + error.message);
10799da06d0Sopenharmony_ci        } else {
10899da06d0Sopenharmony_ci          VibrationAndProximityUtils.startVibrationFlag = true;
10999da06d0Sopenharmony_ci          LogUtils.i(TAG, 'startVibration Callback returned to indicate a successful vibration.');
11099da06d0Sopenharmony_ci        }
11199da06d0Sopenharmony_ci      });
11299da06d0Sopenharmony_ci    } catch (err) {
11399da06d0Sopenharmony_ci      LogUtils.e(TAG, 'startVibration errCode: ' + err.code + ' ,msg: ' + err.message);
11499da06d0Sopenharmony_ci    }
11599da06d0Sopenharmony_ci  }
11699da06d0Sopenharmony_ci
11799da06d0Sopenharmony_ci  /**
11899da06d0Sopenharmony_ci   * stop Vibration
11999da06d0Sopenharmony_ci   */
12099da06d0Sopenharmony_ci  public stopVibration(): void {
12199da06d0Sopenharmony_ci    if (!VibrationAndProximityUtils.startVibrationFlag) {
12299da06d0Sopenharmony_ci      LogUtils.e(TAG, 'stopVibration return');
12399da06d0Sopenharmony_ci      return;
12499da06d0Sopenharmony_ci    }
12599da06d0Sopenharmony_ci    try {
12699da06d0Sopenharmony_ci      vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function (error) {
12799da06d0Sopenharmony_ci        if (error) {
12899da06d0Sopenharmony_ci          LogUtils.e(TAG, 'stopVibration error.code: ' + error.code + ', error.message: ' + error.message);
12999da06d0Sopenharmony_ci          return;
13099da06d0Sopenharmony_ci        }
13199da06d0Sopenharmony_ci        VibrationAndProximityUtils.startVibrationFlag = false;
13299da06d0Sopenharmony_ci        LogUtils.i(TAG, 'stopVibration Callback returned to indicate successful.');
13399da06d0Sopenharmony_ci      });
13499da06d0Sopenharmony_ci    } catch (err) {
13599da06d0Sopenharmony_ci      LogUtils.e(TAG, 'stopVibration errCode: ' + err.code + ' ,msg: ' + err.message);
13699da06d0Sopenharmony_ci    }
13799da06d0Sopenharmony_ci  }
13899da06d0Sopenharmony_ci
13999da06d0Sopenharmony_ci  /**
14099da06d0Sopenharmony_ci   * add Voice Observe
14199da06d0Sopenharmony_ci   */
14299da06d0Sopenharmony_ci  addVoiceObserver(): void {
14399da06d0Sopenharmony_ci    audio.getAudioManager().on('volumeChange', () => {
14499da06d0Sopenharmony_ci      LogUtils.i(TAG, 'addVoiceObserver volumeChange');
14599da06d0Sopenharmony_ci      this.stopVibration();
14699da06d0Sopenharmony_ci      CallServiceProxy.getInstance().muteRinger();
14799da06d0Sopenharmony_ci    });
14899da06d0Sopenharmony_ci  }
14999da06d0Sopenharmony_ci}
15099da06d0Sopenharmony_ci
15199da06d0Sopenharmony_cilet mVibrationAndProximityUtils = new VibrationAndProximityUtils();
15299da06d0Sopenharmony_ci
15399da06d0Sopenharmony_ciexport default mVibrationAndProximityUtils as VibrationAndProximityUtils;
15499da06d0Sopenharmony_ci
155