1 /*
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>
18 #include <vibrator/ExternalVibrationUtils.h>
19 
20 #include <android/os/ExternalVibrationScale.h>
21 #include <binder/Parcel.h>
22 #include <log/log.h>
23 #include <utils/Errors.h>
24 
writeAudioAttributes(const audio_attributes_t & attrs,android::Parcel * out)25 void 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 
readAudioAttributes(audio_attributes_t * attrs,const android::Parcel * in)32 void 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 
39 namespace android {
40 namespace os {
41 
ExternalVibration(int32_t uid,std::string pkg,const audio_attributes_t & attrs,sp<IExternalVibrationController> controller)42 ExternalVibration::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 
writeToParcel(Parcel * parcel) const46 status_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 }
readFromParcel(const Parcel * parcel)54 status_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 
operator ==(const ExternalVibration & rhs) const64 inline bool ExternalVibration::operator==(const ExternalVibration& rhs) const {
65     return mToken == rhs.mToken;
66 }
67 
externalVibrationScaleToHapticScale(os::ExternalVibrationScale externalVibrationScale)68 os::HapticScale ExternalVibration::externalVibrationScaleToHapticScale(
69         os::ExternalVibrationScale externalVibrationScale) {
70     os::HapticLevel scaleLevel = os::HapticLevel::NONE;
71 
72     switch (externalVibrationScale.scaleLevel) {
73         case os::ExternalVibrationScale::ScaleLevel::SCALE_MUTE:
74             scaleLevel = os::HapticLevel::MUTE;
75             break;
76         case os::ExternalVibrationScale::ScaleLevel::SCALE_VERY_LOW:
77             scaleLevel = os::HapticLevel::VERY_LOW;
78             break;
79         case os::ExternalVibrationScale::ScaleLevel::SCALE_LOW:
80             scaleLevel = os::HapticLevel::LOW;
81             break;
82         case os::ExternalVibrationScale::ScaleLevel::SCALE_NONE:
83             scaleLevel = os::HapticLevel::NONE;
84             break;
85         case os::ExternalVibrationScale::ScaleLevel::SCALE_HIGH:
86             scaleLevel = os::HapticLevel::HIGH;
87             break;
88         case os::ExternalVibrationScale::ScaleLevel::SCALE_VERY_HIGH:
89             scaleLevel = os::HapticLevel::VERY_HIGH;
90             break;
91         default:
92             ALOGE("Unknown ExternalVibrationScale %d, not applying scaling",
93                   externalVibrationScale.scaleLevel);
94     }
95 
96     return {/*level=*/scaleLevel, /*adaptiveScaleFactor=*/
97                       externalVibrationScale.adaptiveHapticsScale};
98 }
99 
100 } // namespace os
101 } // namespace android
102