1 /*
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/ExternalVibrationScale.h>
24 #include <android/os/IExternalVibratorService.h>
25 #include <binder/IServiceManager.h>
26 #include <utils/Log.h>
27 
28 #include <mutex>
29 
30 namespace android::afutils {
31 
getExternalVibratorService()32 static sp<os::IExternalVibratorService> getExternalVibratorService() {
33     static std::mutex m;
34     static sp<os::IExternalVibratorService> sExternalVibratorService;
35 
36     std::lock_guard l(m);
37     if (sExternalVibratorService == nullptr) {
38         const sp<IBinder> binder = defaultServiceManager()->getService(
39                 String16("external_vibrator_service"));
40         if (binder != nullptr) {
41             sExternalVibratorService = interface_cast<os::IExternalVibratorService>(binder);
42         }
43     }
44     return sExternalVibratorService;
45 }
46 
onExternalVibrationStart(const sp<os::ExternalVibration> & externalVibration)47 os::HapticScale onExternalVibrationStart(const sp<os::ExternalVibration>& externalVibration) {
48     if (externalVibration->getAudioAttributes().flags & AUDIO_FLAG_MUTE_HAPTIC) {
49         ALOGD("%s, mute haptic according to audio attributes flag", __func__);
50         return os::HapticScale::mute();
51     }
52     const sp<os::IExternalVibratorService> evs = getExternalVibratorService();
53     if (evs != nullptr) {
54 
55         os::ExternalVibrationScale ret;
56         binder::Status status = evs->onExternalVibrationStart(*externalVibration, &ret);
57         if (status.isOk()) {
58             ALOGD("%s, start external vibration with intensity as %d", __func__, ret.scaleLevel);
59             return os::ExternalVibration::externalVibrationScaleToHapticScale(ret);
60         }
61     }
62     ALOGD("%s, start external vibration with intensity as MUTE due to %s",
63             __func__,
64             evs == nullptr ? "external vibration service not found"
65                            : "error when querying intensity");
66     return os::HapticScale::mute();
67 }
68 
onExternalVibrationStop(const sp<os::ExternalVibration> & externalVibration)69 void onExternalVibrationStop(const sp<os::ExternalVibration>& externalVibration) {
70     const sp<os::IExternalVibratorService> evs = getExternalVibratorService();
71     if (evs != nullptr) {
72         ALOGD("%s, stop external vibration", __func__);
73         evs->onExternalVibrationStop(*externalVibration);
74     }
75 }
76 
77 }  // namespace android::afutils
78