blob: 6e550e86a9fe170f8dd38176bc2d7b399688aadd [file] [log] [blame]
Sungsoo Limf894f772018-12-28 13:47:08 +09001/*
2 * Copyright 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
17package android.media;
18
19import android.annotation.NonNull;
Jaewan Kim45d94a42019-01-09 17:12:46 +090020import android.os.Binder;
Sungsoo Limf894f772018-12-28 13:47:08 +090021import android.os.Bundle;
22import android.os.IBinder;
23import android.os.Parcel;
24import android.os.Parcelable;
25import android.os.RemoteException;
Sungsoo Limaea24382018-12-31 16:54:30 +090026import android.os.ResultReceiver;
Sungsoo Limf894f772018-12-28 13:47:08 +090027import android.util.Log;
28
29import java.util.Objects;
30
31/**
Sungsoo Limc6cfa0f2018-12-31 19:21:02 +090032 * Handles incoming commands from {@link MediaController2} to {@link MediaSession2}.
Sungsoo Limf894f772018-12-28 13:47:08 +090033 * @hide
34 */
35// @SystemApi
36public final class Session2Link implements Parcelable {
37 private static final String TAG = "Session2Link";
38 private static final boolean DEBUG = MediaSession2.DEBUG;
39
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -070040 public static final @android.annotation.NonNull Parcelable.Creator<Session2Link> CREATOR =
Sungsoo Limf894f772018-12-28 13:47:08 +090041 new Parcelable.Creator<Session2Link>() {
42 @Override
43 public Session2Link createFromParcel(Parcel in) {
44 return new Session2Link(in);
45 }
46
47 @Override
48 public Session2Link[] newArray(int size) {
49 return new Session2Link[size];
50 }
51 };
52
53 private final MediaSession2 mSession;
54 private final IMediaSession2 mISession;
55
56 public Session2Link(MediaSession2 session) {
57 mSession = session;
58 mISession = new Session2Stub();
59 }
60
61 Session2Link(Parcel in) {
62 mSession = null;
63 mISession = IMediaSession2.Stub.asInterface(in.readStrongBinder());
64 }
65
66 @Override
67 public int describeContents() {
68 return 0;
69 }
70
71 @Override
72 public void writeToParcel(Parcel dest, int flags) {
73 dest.writeStrongBinder(mISession.asBinder());
74 }
75
76 @Override
77 public int hashCode() {
78 return mISession.asBinder().hashCode();
79 }
80
81 @Override
82 public boolean equals(Object obj) {
83 if (!(obj instanceof Session2Link)) {
84 return false;
85 }
86 Session2Link other = (Session2Link) obj;
87 return Objects.equals(mISession.asBinder(), other.mISession.asBinder());
88 }
89
90 /** Link to death with mISession */
91 public void linkToDeath(@NonNull IBinder.DeathRecipient recipient, int flags) {
92 if (mISession != null) {
93 try {
94 mISession.asBinder().linkToDeath(recipient, flags);
95 } catch (RemoteException e) {
96 if (DEBUG) {
97 Log.d(TAG, "Session died too early.", e);
98 }
99 }
100 }
101 }
102
103 /** Unlink to death with mISession */
104 public boolean unlinkToDeath(@NonNull IBinder.DeathRecipient recipient, int flags) {
105 if (mISession != null) {
106 return mISession.asBinder().unlinkToDeath(recipient, flags);
107 }
108 return true;
109 }
110
111 /** Interface method for IMediaSession2.connect */
112 public void connect(final Controller2Link caller, int seq, Bundle connectionRequest) {
113 try {
114 mISession.connect(caller, seq, connectionRequest);
115 } catch (RemoteException e) {
Sungsoo Limc6cfa0f2018-12-31 19:21:02 +0900116 throw new RuntimeException(e);
Sungsoo Limf894f772018-12-28 13:47:08 +0900117 }
118 }
119
120 /** Interface method for IMediaSession2.disconnect */
121 public void disconnect(final Controller2Link caller, int seq) {
122 try {
123 mISession.disconnect(caller, seq);
124 } catch (RemoteException e) {
Sungsoo Limc6cfa0f2018-12-31 19:21:02 +0900125 throw new RuntimeException(e);
Sungsoo Limf894f772018-12-28 13:47:08 +0900126 }
127 }
128
129 /** Interface method for IMediaSession2.sendSessionCommand */
130 public void sendSessionCommand(final Controller2Link caller, final int seq,
Sungsoo Limaea24382018-12-31 16:54:30 +0900131 final Session2Command command, final Bundle args, ResultReceiver resultReceiver) {
Sungsoo Limf894f772018-12-28 13:47:08 +0900132 try {
Sungsoo Limaea24382018-12-31 16:54:30 +0900133 mISession.sendSessionCommand(caller, seq, command, args, resultReceiver);
Sungsoo Limf894f772018-12-28 13:47:08 +0900134 } catch (RemoteException e) {
Sungsoo Limc6cfa0f2018-12-31 19:21:02 +0900135 throw new RuntimeException(e);
136 }
137 }
138
139 /** Interface method for IMediaSession2.sendSessionCommand */
140 public void cancelSessionCommand(final Controller2Link caller, final int seq) {
141 try {
142 mISession.cancelSessionCommand(caller, seq);
143 } catch (RemoteException e) {
144 throw new RuntimeException(e);
Sungsoo Limf894f772018-12-28 13:47:08 +0900145 }
146 }
147
148 /** Stub implementation for IMediaSession2.connect */
Jaewan Kim45d94a42019-01-09 17:12:46 +0900149 public void onConnect(final Controller2Link caller, int pid, int uid, int seq,
150 Bundle connectionRequest) {
151 mSession.onConnect(caller, pid, uid, seq, connectionRequest);
Sungsoo Limf894f772018-12-28 13:47:08 +0900152 }
153
154 /** Stub implementation for IMediaSession2.disconnect */
155 public void onDisconnect(final Controller2Link caller, int seq) {
156 mSession.onDisconnect(caller, seq);
157 }
158
159 /** Stub implementation for IMediaSession2.sendSessionCommand */
160 public void onSessionCommand(final Controller2Link caller, final int seq,
Sungsoo Limaea24382018-12-31 16:54:30 +0900161 final Session2Command command, final Bundle args, ResultReceiver resultReceiver) {
162 mSession.onSessionCommand(caller, seq, command, args, resultReceiver);
Sungsoo Limf894f772018-12-28 13:47:08 +0900163 }
164
Sungsoo Limc6cfa0f2018-12-31 19:21:02 +0900165 /** Stub implementation for IMediaSession2.cancelSessionCommand */
166 public void onCancelCommand(final Controller2Link caller, final int seq) {
167 mSession.onCancelCommand(caller, seq);
168 }
169
Sungsoo Limf894f772018-12-28 13:47:08 +0900170 private class Session2Stub extends IMediaSession2.Stub {
171 @Override
172 public void connect(final Controller2Link caller, int seq, Bundle connectionRequest) {
Jaewan Kim45d94a42019-01-09 17:12:46 +0900173 if (caller == null || connectionRequest == null) {
174 return;
175 }
176 final int pid = Binder.getCallingPid();
177 final int uid = Binder.getCallingUid();
178 final long token = Binder.clearCallingIdentity();
179 try {
180 Session2Link.this.onConnect(caller, pid, uid, seq, connectionRequest);
181 } finally {
182 Binder.restoreCallingIdentity(token);
183 }
Sungsoo Limf894f772018-12-28 13:47:08 +0900184 }
185
186 @Override
187 public void disconnect(final Controller2Link caller, int seq) {
Jaewan Kim45d94a42019-01-09 17:12:46 +0900188 if (caller == null) {
189 return;
190 }
191 final long token = Binder.clearCallingIdentity();
192 try {
193 Session2Link.this.onDisconnect(caller, seq);
194 } finally {
195 Binder.restoreCallingIdentity(token);
196 }
Sungsoo Limf894f772018-12-28 13:47:08 +0900197 }
198
199 @Override
200 public void sendSessionCommand(final Controller2Link caller, final int seq,
Sungsoo Limaea24382018-12-31 16:54:30 +0900201 final Session2Command command, final Bundle args, ResultReceiver resultReceiver) {
Jaewan Kim45d94a42019-01-09 17:12:46 +0900202 if (caller == null) {
203 return;
204 }
205 final long token = Binder.clearCallingIdentity();
206 try {
207 Session2Link.this.onSessionCommand(caller, seq, command, args, resultReceiver);
208 } finally {
209 Binder.restoreCallingIdentity(token);
210 }
Sungsoo Limf894f772018-12-28 13:47:08 +0900211 }
Sungsoo Limc6cfa0f2018-12-31 19:21:02 +0900212
213 @Override
214 public void cancelSessionCommand(final Controller2Link caller, final int seq) {
Jaewan Kim45d94a42019-01-09 17:12:46 +0900215 if (caller == null) {
216 return;
217 }
218 final long token = Binder.clearCallingIdentity();
219 try {
220 Session2Link.this.onCancelCommand(caller, seq);
221 } finally {
222 Binder.restoreCallingIdentity(token);
223 }
Sungsoo Limc6cfa0f2018-12-31 19:21:02 +0900224 }
Sungsoo Limf894f772018-12-28 13:47:08 +0900225 }
226}