blob: ec906458a386817b36b61e5360cc264104456430 [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
Jeongik Chae214e642022-09-26 23:08:56 +090025
26// To guarantee if HapticScale enum has the same value as IExternalVibratorService
27static_assert(static_cast<int>(android::os::HapticScale::MUTE) == static_cast<int>(android::os::IExternalVibratorService::SCALE_MUTE));
28static_assert(static_cast<int>(android::os::HapticScale::VERY_LOW) == static_cast<int>(android::os::IExternalVibratorService::SCALE_VERY_LOW));
29static_assert(static_cast<int>(android::os::HapticScale::LOW) == static_cast<int>(android::os::IExternalVibratorService::SCALE_LOW));
30static_assert(static_cast<int>(android::os::HapticScale::NONE) == static_cast<int>(android::os::IExternalVibratorService::SCALE_NONE));
31static_assert(static_cast<int>(android::os::HapticScale::HIGH) == static_cast<int>(android::os::IExternalVibratorService::SCALE_HIGH));
32static_assert(static_cast<int>(android::os::HapticScale::VERY_HIGH) == static_cast<int>(android::os::IExternalVibratorService::SCALE_VERY_HIGH));
33
Michael Wright3fee95e2018-12-12 19:51:26 +000034void writeAudioAttributes(const audio_attributes_t& attrs, android::Parcel* out) {
35 out->writeInt32(attrs.usage);
36 out->writeInt32(attrs.content_type);
37 out->writeInt32(attrs.source);
38 out->writeInt32(attrs.flags);
39}
40
41void readAudioAttributes(audio_attributes_t* attrs, const android::Parcel* in) {
42 attrs->usage = static_cast<audio_usage_t>(in->readInt32());
43 attrs->content_type = static_cast<audio_content_type_t>(in->readInt32());
44 attrs->source = static_cast<audio_source_t>(in->readInt32());
45 attrs->flags = static_cast<audio_flags_mask_t>(in->readInt32());
46}
47
48namespace android {
49namespace os {
50
51ExternalVibration::ExternalVibration(int32_t uid, std::string pkg, const audio_attributes_t& attrs,
52 sp<IExternalVibrationController> controller) :
53 mUid(uid), mPkg(pkg), mAttrs(attrs), mController(controller) { }
54
55status_t ExternalVibration::writeToParcel(Parcel* parcel) const {
56 parcel->writeInt32(mUid);
57 parcel->writeString16(String16(mPkg.c_str()));
58 writeAudioAttributes(mAttrs, parcel);
59 parcel->writeStrongBinder(IInterface::asBinder(mController));
60 parcel->writeStrongBinder(mToken);
61 return OK;
62}
63status_t ExternalVibration::readFromParcel(const Parcel* parcel) {
64 mUid = parcel->readInt32();
65 String8 pkgStr8 = String8(parcel->readString16());
66 mPkg = pkgStr8.c_str();
67 readAudioAttributes(&mAttrs, parcel);
68 mController = IExternalVibrationController::asInterface(parcel->readStrongBinder());
69 mToken = parcel->readStrongBinder();
70 return OK;
71}
72
73inline bool ExternalVibration::operator==(const ExternalVibration& rhs) const {
74 return mToken == rhs.mToken;
75}
76
77} // namespace os
78} // namespace android