Andy Hung | 7fb97e1 | 2023-07-20 21:23:42 -0700 | [diff] [blame] | 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/IExternalVibratorService.h> |
| 24 | #include <binder/IServiceManager.h> |
| 25 | #include <utils/Log.h> |
| 26 | |
| 27 | #include <mutex> |
| 28 | |
| 29 | namespace android::afutils { |
| 30 | |
| 31 | static 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 | |
| 46 | os::HapticScale onExternalVibrationStart(const sp<os::ExternalVibration>& externalVibration) { |
| 47 | const sp<os::IExternalVibratorService> evs = getExternalVibratorService(); |
| 48 | if (evs != nullptr) { |
| 49 | int32_t ret; |
| 50 | binder::Status status = evs->onExternalVibrationStart(*externalVibration, &ret); |
| 51 | if (status.isOk()) { |
| 52 | ALOGD("%s, start external vibration with intensity as %d", __func__, ret); |
| 53 | return os::ExternalVibration::externalVibrationScaleToHapticScale(ret); |
| 54 | } |
| 55 | } |
| 56 | ALOGD("%s, start external vibration with intensity as MUTE due to %s", |
| 57 | __func__, |
| 58 | evs == nullptr ? "external vibration service not found" |
| 59 | : "error when querying intensity"); |
| 60 | return os::HapticScale::MUTE; |
| 61 | } |
| 62 | |
| 63 | void onExternalVibrationStop(const sp<os::ExternalVibration>& externalVibration) { |
| 64 | const sp<os::IExternalVibratorService> evs = getExternalVibratorService(); |
| 65 | if (evs != nullptr) { |
| 66 | ALOGD("%s, stop external vibration", __func__); |
| 67 | evs->onExternalVibrationStop(*externalVibration); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | } // namespace android::afutils |