• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         os::ExternalVibrationScale ret;
55         binder::Status status = evs->onExternalVibrationStart(*externalVibration, &ret);
56         if (status.isOk()) {
57             ALOGD("%s, start external vibration with intensity as %d", __func__, ret.scaleLevel);
58             return os::ExternalVibration::externalVibrationScaleToHapticScale(ret);
59         } else {
60             ALOGE("Start external vibration request failed: %s", status.toString8().c_str());
61         }
62     }
63     ALOGD("%s, start external vibration with intensity as MUTE due to %s",
64             __func__,
65             evs == nullptr ? "external vibration service not found"
66                            : "error when querying intensity");
67     return os::HapticScale::mute();
68 }
69 
onExternalVibrationStop(const sp<os::ExternalVibration> & externalVibration)70 void onExternalVibrationStop(const sp<os::ExternalVibration>& externalVibration) {
71     const sp<os::IExternalVibratorService> evs = getExternalVibratorService();
72     if (evs != nullptr) {
73         ALOGD("%s, stop external vibration", __func__);
74         evs->onExternalVibrationStop(*externalVibration);
75     }
76 }
77 
78 }  // namespace android::afutils
79