blob: 57c58dc3b2ed57238d5cca1ae4a42071ca3eb3f7 [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;
20import android.os.Bundle;
21import android.os.IBinder;
22import android.os.Parcel;
23import android.os.Parcelable;
24import android.os.RemoteException;
Sungsoo Limaea24382018-12-31 16:54:30 +090025import android.os.ResultReceiver;
Sungsoo Limf894f772018-12-28 13:47:08 +090026import android.util.Log;
27
28import java.util.Objects;
29
30/**
31 * Handles incoming commands from {@link MediaController2} and {@link MediaBrowser2}
32 * to both {@link MediaSession2} and {@link MediaLibrarySession}.
33 * @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
40 public static final Parcelable.Creator<Session2Link> CREATOR =
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) {
116 throw e.rethrowFromSystemServer();
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) {
125 throw e.rethrowFromSystemServer();
126 }
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) {
135 throw e.rethrowFromSystemServer();
136 }
137 }
138
139 /** Stub implementation for IMediaSession2.connect */
140 public void onConnect(final Controller2Link caller, int seq, Bundle connectionRequest) {
141 mSession.onConnect(caller, seq, connectionRequest);
142 }
143
144 /** Stub implementation for IMediaSession2.disconnect */
145 public void onDisconnect(final Controller2Link caller, int seq) {
146 mSession.onDisconnect(caller, seq);
147 }
148
149 /** Stub implementation for IMediaSession2.sendSessionCommand */
150 public void onSessionCommand(final Controller2Link caller, final int seq,
Sungsoo Limaea24382018-12-31 16:54:30 +0900151 final Session2Command command, final Bundle args, ResultReceiver resultReceiver) {
152 mSession.onSessionCommand(caller, seq, command, args, resultReceiver);
Sungsoo Limf894f772018-12-28 13:47:08 +0900153 }
154
155 private class Session2Stub extends IMediaSession2.Stub {
156 @Override
157 public void connect(final Controller2Link caller, int seq, Bundle connectionRequest) {
158 Session2Link.this.onConnect(caller, seq, connectionRequest);
159 }
160
161 @Override
162 public void disconnect(final Controller2Link caller, int seq) {
163 Session2Link.this.onDisconnect(caller, seq);
164 }
165
166 @Override
167 public void sendSessionCommand(final Controller2Link caller, final int seq,
Sungsoo Limaea24382018-12-31 16:54:30 +0900168 final Session2Command command, final Bundle args, ResultReceiver resultReceiver) {
169 Session2Link.this.onSessionCommand(caller, seq, command, args, resultReceiver);
Sungsoo Limf894f772018-12-28 13:47:08 +0900170 }
171 }
172}