Debian Codegraph @

core/java/android/os/ExternalVibration.java

raw symbols SCIP
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 package android.os;
18
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.media.AudioAttributes;
22 import android.util.Slog;
23
24 import com.android.internal.util.Preconditions;
25
26 /**
27 * An ExternalVibration represents an on-going vibration being controlled by something other than
28 * the core vibrator service.
29 *
30 * @hide
31 */
32 public class ExternalVibration implements Parcelable {
33 private static final String TAG = "ExternalVibration";
34 private int mUid;
35 @NonNull
36 private String mPkg;
37 @NonNull
38 private AudioAttributes mAttrs;
39 @NonNull
40 private IExternalVibrationController mController;
41 // A token used to maintain equality comparisons when passing objects across process
42 // boundaries.
43 @NonNull
44 private IBinder mToken;
45 public ExternalVibration(int uid, @NonNull String pkg, @NonNull AudioAttributes attrs,
46 @NonNull IExternalVibrationController controller) {
47 this(uid, pkg, attrs, controller, new Binder());
48 }
49
50 /**
51 * Full constructor, but exposed to construct the ExternalVibration with an explicit binder
52 * token (for mocks).
53 *
54 * @hide
55 */
56 public ExternalVibration(int uid, @NonNull String pkg, @NonNull AudioAttributes attrs,
57 @NonNull IExternalVibrationController controller, @NonNull IBinder token) {
58 mUid = uid;
59 mPkg = Preconditions.checkNotNull(pkg);
60 mAttrs = Preconditions.checkNotNull(attrs);
61 mController = Preconditions.checkNotNull(controller);
62 mToken = Preconditions.checkNotNull(token);
63
64 // IExternalVibrationController is a hidden AIDL interface with implementation provided by
65 // the audio framework to allow mute/unmute control over the external vibration.
66 //
67 // Transactions are locked in audioflinger, and should be blocking to avoid racing
68 // conditions on multiple audio playback.
69 //
70 // They can also be triggered before starting a new external vibration in
71 // IExternalVibratorService, as the ongoing external vibration needs to be muted before the
72 // new one can start, which also requires blocking calls to mute.
73 Binder.allowBlocking(mController.asBinder());
74 }
75
76 private ExternalVibration(Parcel in) {
77 this(in.readInt(), in.readString(), readAudioAttributes(in),
78 IExternalVibrationController.Stub.asInterface(in.readStrongBinder()),
79 in.readStrongBinder());
80 }
81
82 private static AudioAttributes readAudioAttributes(Parcel in) {
83 int usage = in.readInt();
84 int contentType = in.readInt();
85 int capturePreset = in.readInt();
86 int flags = in.readInt();
87 AudioAttributes.Builder builder = new AudioAttributes.Builder();
88 return builder.setUsage(usage)
89 .setContentType(contentType)
90 .setCapturePreset(capturePreset)
91 .setFlags(flags)
92 .build();
93 }
94
95 public int getUid() {
96 return mUid;
97 }
98
99 public String getPackage() {
100 return mPkg;
101 }
102
103 public AudioAttributes getAudioAttributes() {
104 return mAttrs;
105 }
106
107 public VibrationAttributes getVibrationAttributes() {
108 return new VibrationAttributes.Builder(mAttrs).build();
109 }
110
111 /**
112 * Mutes the external vibration if it's playing and unmuted.
113 *
114 * @return whether the muting operation was successful
115 */
116 public boolean mute() {
117 try {
118 mController.mute();
119 } catch (RemoteException e) {
120 Slog.wtf(TAG, "Failed to mute vibration stream: " + this, e);
121 return false;
122 }
123 return true;
124 }
125
126 /**
127 * Unmutes the external vibration if it's playing and muted.
128 *
129 * @return whether the unmuting operation was successful
130 */
131 public boolean unmute() {
132 try {
133 mController.unmute();
134 } catch (RemoteException e) {
135 Slog.wtf(TAG, "Failed to unmute vibration stream: " + this, e);
136 return false;
137 }
138 return true;
139 }
140
141 /**
142 * Links a recipient to death against this external vibration token
143 */
144 public void linkToDeath(IBinder.DeathRecipient recipient) {
145 try {
146 mToken.linkToDeath(recipient, 0);
147 } catch (RemoteException e) {
148 return;
149 }
150 }
151
152 /**
153 * Unlinks a recipient to death against this external vibration token
154 */
155 public void unlinkToDeath(IBinder.DeathRecipient recipient) {
156 mToken.unlinkToDeath(recipient, 0);
157 }
158
159 @Override
160 public boolean equals(@Nullable Object o) {
161 if (o == null || !(o instanceof ExternalVibration)) {
162 return false;
163 }
164 ExternalVibration other = (ExternalVibration) o;
165 return mToken.equals(other.mToken);
166 }
167
168 @Override
169 public String toString() {
170 return "ExternalVibration{"
171 + "uid=" + mUid + ", "
172 + "pkg=" + mPkg + ", "
173 + "attrs=" + mAttrs + ", "
174 + "controller=" + mController
175 + "token=" + mToken
176 + "}";
177 }
178
179 @Override
180 public void writeToParcel(Parcel out, int flags) {
181 out.writeInt(mUid);
182 out.writeString(mPkg);
183 writeAudioAttributes(mAttrs, out, flags);
184 out.writeStrongBinder(mController.asBinder());
185 out.writeStrongBinder(mToken);
186 }
187
188 private static void writeAudioAttributes(AudioAttributes attrs, Parcel out, int flags) {
189 out.writeInt(attrs.getUsage());
190 out.writeInt(attrs.getContentType());
191 out.writeInt(attrs.getCapturePreset());
192 out.writeInt(attrs.getAllFlags());
193 }
194
195 @Override
196 public int describeContents() {
197 return 0;
198 }
199
200 public static final @android.annotation.NonNull Parcelable.Creator<ExternalVibration> CREATOR =
201 new Parcelable.Creator<ExternalVibration>() {
202 @Override
203 public ExternalVibration createFromParcel(Parcel in) {
204 return new ExternalVibration(in);
205 }
206
207 @Override
208 public ExternalVibration[] newArray(int size) {
209 return new ExternalVibration[size];
210 }
211 };
212 }