| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 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 |
|
| 42 |
|
| 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 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 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 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 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 |
|
| 113 |
|
| 114 |
|
| 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 |
|
| 128 |
|
| 129 |
|
| 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 |
|
| 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 |
|
| 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 |
} |