2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import android.annotation.NonNull;
20 import android.media.AudioAttributes;
21 import android.util.Slog;
23 import com.android.internal.util.Preconditions;
26 * An ExternalVibration represents an on-going vibration being controlled by something other than
27 * the core vibrator service.
31 public class ExternalVibration implements Parcelable {
32 private static final String TAG = "ExternalVibration";
37 private AudioAttributes mAttrs;
39 private IExternalVibrationController mController;
40 // A token used to maintain equality comparisons when passing objects across process
43 private IBinder mToken;
45 public ExternalVibration(int uid, @NonNull String pkg, @NonNull AudioAttributes attrs,
46 @NonNull IExternalVibrationController controller) {
48 mPkg = Preconditions.checkNotNull(pkg);
49 mAttrs = Preconditions.checkNotNull(attrs);
50 mController = Preconditions.checkNotNull(controller);
51 mToken = new Binder();
54 private ExternalVibration(Parcel in) {
56 mPkg = in.readString();
57 mAttrs = readAudioAttributes(in);
58 mController = IExternalVibrationController.Stub.asInterface(in.readStrongBinder());
59 mToken = in.readStrongBinder();
62 private AudioAttributes readAudioAttributes(Parcel in) {
63 int usage = in.readInt();
64 int contentType = in.readInt();
65 int capturePreset = in.readInt();
66 int flags = in.readInt();
67 AudioAttributes.Builder builder = new AudioAttributes.Builder();
68 return builder.setUsage(usage)
69 .setContentType(contentType)
70 .setCapturePreset(capturePreset)
79 public String getPackage() {
83 public AudioAttributes getAudioAttributes() {
88 * Mutes the external vibration if it's playing and unmuted.
90 * @return whether the muting operation was successful
92 public boolean mute() {
95 } catch (RemoteException e) {
96 Slog.wtf(TAG, "Failed to mute vibration stream: " + this, e);
103 * Unmutes the external vibration if it's playing and muted.
105 * @return whether the unmuting operation was successful
107 public boolean unmute() {
109 mController.unmute();
110 } catch (RemoteException e) {
111 Slog.wtf(TAG, "Failed to unmute vibration stream: " + this, e);
118 * Links a recipient to death against this external vibration token
120 public void linkToDeath(IBinder.DeathRecipient recipient) {
122 mToken.linkToDeath(recipient, 0);
123 } catch (RemoteException e) {
129 * Unlinks a recipient to death against this external vibration token
131 public void unlinkToDeath(IBinder.DeathRecipient recipient) {
132 mToken.unlinkToDeath(recipient, 0);
136 public boolean equals(Object o) {
137 if (o == null || !(o instanceof ExternalVibration)) {
140 ExternalVibration other = (ExternalVibration) o;
141 return mToken.equals(other.mToken);
145 public String toString() {
146 return "ExternalVibration{"
147 + "uid=" + mUid + ", "
148 + "pkg=" + mPkg + ", "
149 + "attrs=" + mAttrs + ", "
150 + "controller=" + mController
151 + "token=" + mController
156 public void writeToParcel(Parcel out, int flags) {
158 out.writeString(mPkg);
159 writeAudioAttributes(mAttrs, out, flags);
160 out.writeStrongBinder(mController.asBinder());
161 out.writeStrongBinder(mToken);
164 private static void writeAudioAttributes(AudioAttributes attrs, Parcel out, int flags) {
165 out.writeInt(attrs.getUsage());
166 out.writeInt(attrs.getContentType());
167 out.writeInt(attrs.getCapturePreset());
168 out.writeInt(attrs.getAllFlags());
172 public int describeContents() {
176 public static final @android.annotation.NonNull Parcelable.Creator<ExternalVibration> CREATOR =
177 new Parcelable.Creator<ExternalVibration>() {
179 public ExternalVibration createFromParcel(Parcel in) {
180 return new ExternalVibration(in);
184 public ExternalVibration[] newArray(int size) {
185 return new ExternalVibration[size];