Sungsoo Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.media; |
| 18 | |
| 19 | import android.annotation.NonNull; |
Jaewan Kim | 45d94a4 | 2019-01-09 17:12:46 +0900 | [diff] [blame] | 20 | import android.os.Binder; |
Sungsoo Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 21 | import android.os.Bundle; |
| 22 | import android.os.IBinder; |
| 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
| 25 | import android.os.RemoteException; |
Sungsoo Lim | aea2438 | 2018-12-31 16:54:30 +0900 | [diff] [blame] | 26 | import android.os.ResultReceiver; |
Sungsoo Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | |
| 29 | import java.util.Objects; |
| 30 | |
| 31 | /** |
Sungsoo Lim | c6cfa0f | 2018-12-31 19:21:02 +0900 | [diff] [blame] | 32 | * Handles incoming commands from {@link MediaController2} to {@link MediaSession2}. |
Sungsoo Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 33 | * @hide |
| 34 | */ |
| 35 | // @SystemApi |
| 36 | public final class Session2Link implements Parcelable { |
| 37 | private static final String TAG = "Session2Link"; |
| 38 | private static final boolean DEBUG = MediaSession2.DEBUG; |
| 39 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 40 | public static final @android.annotation.NonNull Parcelable.Creator<Session2Link> CREATOR = |
Sungsoo Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 41 | 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 Lim | c6cfa0f | 2018-12-31 19:21:02 +0900 | [diff] [blame] | 116 | throw new RuntimeException(e); |
Sungsoo Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 117 | } |
| 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 Lim | c6cfa0f | 2018-12-31 19:21:02 +0900 | [diff] [blame] | 125 | throw new RuntimeException(e); |
Sungsoo Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | /** Interface method for IMediaSession2.sendSessionCommand */ |
| 130 | public void sendSessionCommand(final Controller2Link caller, final int seq, |
Sungsoo Lim | aea2438 | 2018-12-31 16:54:30 +0900 | [diff] [blame] | 131 | final Session2Command command, final Bundle args, ResultReceiver resultReceiver) { |
Sungsoo Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 132 | try { |
Sungsoo Lim | aea2438 | 2018-12-31 16:54:30 +0900 | [diff] [blame] | 133 | mISession.sendSessionCommand(caller, seq, command, args, resultReceiver); |
Sungsoo Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 134 | } catch (RemoteException e) { |
Sungsoo Lim | c6cfa0f | 2018-12-31 19:21:02 +0900 | [diff] [blame] | 135 | 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 Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | /** Stub implementation for IMediaSession2.connect */ |
Jaewan Kim | 45d94a4 | 2019-01-09 17:12:46 +0900 | [diff] [blame] | 149 | public void onConnect(final Controller2Link caller, int pid, int uid, int seq, |
| 150 | Bundle connectionRequest) { |
| 151 | mSession.onConnect(caller, pid, uid, seq, connectionRequest); |
Sungsoo Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 152 | } |
| 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 Lim | aea2438 | 2018-12-31 16:54:30 +0900 | [diff] [blame] | 161 | final Session2Command command, final Bundle args, ResultReceiver resultReceiver) { |
| 162 | mSession.onSessionCommand(caller, seq, command, args, resultReceiver); |
Sungsoo Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 163 | } |
| 164 | |
Sungsoo Lim | c6cfa0f | 2018-12-31 19:21:02 +0900 | [diff] [blame] | 165 | /** Stub implementation for IMediaSession2.cancelSessionCommand */ |
| 166 | public void onCancelCommand(final Controller2Link caller, final int seq) { |
| 167 | mSession.onCancelCommand(caller, seq); |
| 168 | } |
| 169 | |
Sungsoo Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 170 | private class Session2Stub extends IMediaSession2.Stub { |
| 171 | @Override |
| 172 | public void connect(final Controller2Link caller, int seq, Bundle connectionRequest) { |
Jaewan Kim | 45d94a4 | 2019-01-09 17:12:46 +0900 | [diff] [blame] | 173 | 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 Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void disconnect(final Controller2Link caller, int seq) { |
Jaewan Kim | 45d94a4 | 2019-01-09 17:12:46 +0900 | [diff] [blame] | 188 | 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 Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void sendSessionCommand(final Controller2Link caller, final int seq, |
Sungsoo Lim | aea2438 | 2018-12-31 16:54:30 +0900 | [diff] [blame] | 201 | final Session2Command command, final Bundle args, ResultReceiver resultReceiver) { |
Jaewan Kim | 45d94a4 | 2019-01-09 17:12:46 +0900 | [diff] [blame] | 202 | 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 Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 211 | } |
Sungsoo Lim | c6cfa0f | 2018-12-31 19:21:02 +0900 | [diff] [blame] | 212 | |
| 213 | @Override |
| 214 | public void cancelSessionCommand(final Controller2Link caller, final int seq) { |
Jaewan Kim | 45d94a4 | 2019-01-09 17:12:46 +0900 | [diff] [blame] | 215 | 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 Lim | c6cfa0f | 2018-12-31 19:21:02 +0900 | [diff] [blame] | 224 | } |
Sungsoo Lim | f894f77 | 2018-12-28 13:47:08 +0900 | [diff] [blame] | 225 | } |
| 226 | } |