blob: 80e911c65d5d11495ffbe88b5490152e8aaa8fc1 [file] [log] [blame]
Michael Wright3fee95e2018-12-12 19:51:26 +00001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <vibrator/ExternalVibration.h>
Jeongik Chae214e642022-09-26 23:08:56 +090018#include <vibrator/ExternalVibrationUtils.h>
Michael Wright3fee95e2018-12-12 19:51:26 +000019
Jeongik Chae214e642022-09-26 23:08:56 +090020#include <android/os/IExternalVibratorService.h>
Michael Wright3fee95e2018-12-12 19:51:26 +000021#include <binder/Parcel.h>
22#include <log/log.h>
23#include <utils/Errors.h>
24
Michael Wright3fee95e2018-12-12 19:51:26 +000025void writeAudioAttributes(const audio_attributes_t& attrs, android::Parcel* out) {
26 out->writeInt32(attrs.usage);
27 out->writeInt32(attrs.content_type);
28 out->writeInt32(attrs.source);
29 out->writeInt32(attrs.flags);
30}
31
32void readAudioAttributes(audio_attributes_t* attrs, const android::Parcel* in) {
33 attrs->usage = static_cast<audio_usage_t>(in->readInt32());
34 attrs->content_type = static_cast<audio_content_type_t>(in->readInt32());
35 attrs->source = static_cast<audio_source_t>(in->readInt32());
36 attrs->flags = static_cast<audio_flags_mask_t>(in->readInt32());
37}
38
39namespace android {
40namespace os {
41
42ExternalVibration::ExternalVibration(int32_t uid, std::string pkg, const audio_attributes_t& attrs,
43 sp<IExternalVibrationController> controller) :
44 mUid(uid), mPkg(pkg), mAttrs(attrs), mController(controller) { }
45
46status_t ExternalVibration::writeToParcel(Parcel* parcel) const {
47 parcel->writeInt32(mUid);
48 parcel->writeString16(String16(mPkg.c_str()));
49 writeAudioAttributes(mAttrs, parcel);
50 parcel->writeStrongBinder(IInterface::asBinder(mController));
51 parcel->writeStrongBinder(mToken);
52 return OK;
53}
54status_t ExternalVibration::readFromParcel(const Parcel* parcel) {
55 mUid = parcel->readInt32();
56 String8 pkgStr8 = String8(parcel->readString16());
57 mPkg = pkgStr8.c_str();
58 readAudioAttributes(&mAttrs, parcel);
59 mController = IExternalVibrationController::asInterface(parcel->readStrongBinder());
60 mToken = parcel->readStrongBinder();
61 return OK;
62}
63
64inline bool ExternalVibration::operator==(const ExternalVibration& rhs) const {
65 return mToken == rhs.mToken;
66}
67
Simon Bowdenbe9d0e12022-10-17 14:49:58 +000068os::HapticScale ExternalVibration::externalVibrationScaleToHapticScale(int externalVibrationScale) {
69 switch (externalVibrationScale) {
70 case IExternalVibratorService::SCALE_MUTE:
71 return os::HapticScale::MUTE;
72 case IExternalVibratorService::SCALE_VERY_LOW:
73 return os::HapticScale::VERY_LOW;
74 case IExternalVibratorService::SCALE_LOW:
75 return os::HapticScale::LOW;
76 case IExternalVibratorService::SCALE_NONE:
77 return os::HapticScale::NONE;
78 case IExternalVibratorService::SCALE_HIGH:
79 return os::HapticScale::HIGH;
80 case IExternalVibratorService::SCALE_VERY_HIGH:
81 return os::HapticScale::VERY_HIGH;
82 default:
83 ALOGE("Unknown ExternalVibrationScale %d, not applying scaling", externalVibrationScale);
84 return os::HapticScale::NONE;
85 }
86}
87
Michael Wright3fee95e2018-12-12 19:51:26 +000088} // namespace os
89} // namespace android