blob: ab15a09c6cdd7b91a843b7d04c5897db00edd96d [file] [log] [blame]
Andy Hung7fb97e12023-07-20 21:23:42 -07001/*
2 *
3 * Copyright 2023, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#define LOG_TAG "AudioFlinger::Vibrator"
19//#define LOG_NDEBUG 0
20
21#include "Vibrator.h"
22
23#include <android/os/IExternalVibratorService.h>
24#include <binder/IServiceManager.h>
25#include <utils/Log.h>
26
27#include <mutex>
28
29namespace android::afutils {
30
31static sp<os::IExternalVibratorService> getExternalVibratorService() {
32 static std::mutex m;
33 static sp<os::IExternalVibratorService> sExternalVibratorService;
34
35 std::lock_guard l(m);
36 if (sExternalVibratorService == nullptr) {
37 const sp<IBinder> binder = defaultServiceManager()->getService(
38 String16("external_vibrator_service"));
39 if (binder != nullptr) {
40 sExternalVibratorService = interface_cast<os::IExternalVibratorService>(binder);
41 }
42 }
43 return sExternalVibratorService;
44}
45
46os::HapticScale onExternalVibrationStart(const sp<os::ExternalVibration>& externalVibration) {
jiabin60cbcf32024-01-16 21:32:09 +000047 if (externalVibration->getAudioAttributes().flags & AUDIO_FLAG_MUTE_HAPTIC) {
48 ALOGD("%s, mute haptic according to audio attributes flag", __func__);
49 return os::HapticScale::MUTE;
50 }
Andy Hung7fb97e12023-07-20 21:23:42 -070051 const sp<os::IExternalVibratorService> evs = getExternalVibratorService();
52 if (evs != nullptr) {
53 int32_t ret;
54 binder::Status status = evs->onExternalVibrationStart(*externalVibration, &ret);
55 if (status.isOk()) {
56 ALOGD("%s, start external vibration with intensity as %d", __func__, ret);
57 return os::ExternalVibration::externalVibrationScaleToHapticScale(ret);
58 }
59 }
60 ALOGD("%s, start external vibration with intensity as MUTE due to %s",
61 __func__,
62 evs == nullptr ? "external vibration service not found"
63 : "error when querying intensity");
64 return os::HapticScale::MUTE;
65}
66
67void onExternalVibrationStop(const sp<os::ExternalVibration>& externalVibration) {
68 const sp<os::IExternalVibratorService> evs = getExternalVibratorService();
69 if (evs != nullptr) {
70 ALOGD("%s, stop external vibration", __func__);
71 evs->onExternalVibrationStop(*externalVibration);
72 }
73}
74
75} // namespace android::afutils