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.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ResolveInfo; 27 import android.os.Bundle; 28 import android.os.Parcel; 29 import android.os.Parcelable; 30 import android.text.TextUtils; 31 import android.util.Log; 32 33 import java.lang.annotation.Retention; 34 import java.lang.annotation.RetentionPolicy; 35 import java.util.List; 36 import java.util.Objects; 37 38 /** 39 * This API is not generally intended for third party application developers. 40 * Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a> 41 * <a href="{@docRoot}reference/androidx/media2/session/package-summary.html">Media2 session 42 * Library</a> for consistent behavior across all devices. 43 * <p> 44 * Represents an ongoing {@link MediaSession2} or a {@link MediaSession2Service}. 45 * If it's representing a session service, it may not be ongoing. 46 * <p> 47 * This may be passed to apps by the session owner to allow them to create a 48 * {@link MediaController2} to communicate with the session. 49 * <p> 50 * It can be also obtained by {@link android.media.session.MediaSessionManager}. 51 */ 52 public final class Session2Token implements Parcelable { 53 private static final String TAG = "Session2Token"; 54 55 public static final @android.annotation.NonNull Creator<Session2Token> CREATOR = 56 new Creator<Session2Token>() { 57 @Override 58 public Session2Token createFromParcel(Parcel p) { 59 return new Session2Token(p); 60 } 61 62 @Override 63 public Session2Token[] newArray(int size) { 64 return new Session2Token[size]; 65 } 66 }; 67 68 /** 69 * @hide 70 */ 71 @Retention(RetentionPolicy.SOURCE) 72 @IntDef(prefix = "TYPE_", value = {TYPE_SESSION, TYPE_SESSION_SERVICE}) 73 public @interface TokenType { 74 } 75 76 /** 77 * Type for {@link MediaSession2}. 78 */ 79 public static final int TYPE_SESSION = 0; 80 81 /** 82 * Type for {@link MediaSession2Service}. 83 */ 84 public static final int TYPE_SESSION_SERVICE = 1; 85 86 private final int mUid; 87 @TokenType 88 private final int mType; 89 private final String mPackageName; 90 private final String mServiceName; 91 private final Session2Link mSessionLink; 92 private final ComponentName mComponentName; 93 private final Bundle mExtras; 94 95 /** 96 * Constructor for the token with type {@link #TYPE_SESSION_SERVICE}. 97 * 98 * @param context The context. 99 * @param serviceComponent The component name of the service. 100 */ Session2Token(@onNull Context context, @NonNull ComponentName serviceComponent)101 public Session2Token(@NonNull Context context, @NonNull ComponentName serviceComponent) { 102 if (context == null) { 103 throw new IllegalArgumentException("context shouldn't be null"); 104 } 105 if (serviceComponent == null) { 106 throw new IllegalArgumentException("serviceComponent shouldn't be null"); 107 } 108 109 final PackageManager manager = context.getPackageManager(); 110 final int uid = getUid(manager, serviceComponent.getPackageName()); 111 112 if (!isInterfaceDeclared(manager, MediaSession2Service.SERVICE_INTERFACE, 113 serviceComponent)) { 114 Log.w(TAG, serviceComponent + " doesn't implement MediaSession2Service."); 115 } 116 mComponentName = serviceComponent; 117 mPackageName = serviceComponent.getPackageName(); 118 mServiceName = serviceComponent.getClassName(); 119 mUid = uid; 120 mType = TYPE_SESSION_SERVICE; 121 mSessionLink = null; 122 mExtras = Bundle.EMPTY; 123 } 124 Session2Token(int uid, int type, String packageName, Session2Link sessionLink, @NonNull Bundle tokenExtras)125 Session2Token(int uid, int type, String packageName, Session2Link sessionLink, 126 @NonNull Bundle tokenExtras) { 127 mUid = uid; 128 mType = type; 129 mPackageName = packageName; 130 mServiceName = null; 131 mComponentName = null; 132 mSessionLink = sessionLink; 133 mExtras = tokenExtras; 134 } 135 Session2Token(Parcel in)136 Session2Token(Parcel in) { 137 mUid = in.readInt(); 138 mType = in.readInt(); 139 mPackageName = in.readString(); 140 mServiceName = in.readString(); 141 mSessionLink = in.readParcelable(null); 142 mComponentName = ComponentName.unflattenFromString(in.readString()); 143 144 Bundle extras = in.readBundle(); 145 if (extras == null) { 146 Log.w(TAG, "extras shouldn't be null."); 147 extras = Bundle.EMPTY; 148 } else if (MediaSession2.hasCustomParcelable(extras)) { 149 Log.w(TAG, "extras contain custom parcelable. Ignoring."); 150 extras = Bundle.EMPTY; 151 } 152 mExtras = extras; 153 } 154 155 @Override writeToParcel(Parcel dest, int flags)156 public void writeToParcel(Parcel dest, int flags) { 157 dest.writeInt(mUid); 158 dest.writeInt(mType); 159 dest.writeString(mPackageName); 160 dest.writeString(mServiceName); 161 dest.writeParcelable(mSessionLink, flags); 162 dest.writeString(mComponentName == null ? "" : mComponentName.flattenToString()); 163 dest.writeBundle(mExtras); 164 } 165 166 @Override describeContents()167 public int describeContents() { 168 return 0; 169 } 170 171 @Override hashCode()172 public int hashCode() { 173 return Objects.hash(mType, mUid, mPackageName, mServiceName, mSessionLink); 174 } 175 176 @Override equals(Object obj)177 public boolean equals(Object obj) { 178 if (!(obj instanceof Session2Token)) { 179 return false; 180 } 181 Session2Token other = (Session2Token) obj; 182 return mUid == other.mUid 183 && TextUtils.equals(mPackageName, other.mPackageName) 184 && TextUtils.equals(mServiceName, other.mServiceName) 185 && mType == other.mType 186 && Objects.equals(mSessionLink, other.mSessionLink); 187 } 188 189 @Override toString()190 public String toString() { 191 return "Session2Token {pkg=" + mPackageName + " type=" + mType 192 + " service=" + mServiceName + " Session2Link=" + mSessionLink + "}"; 193 } 194 195 /** 196 * @return uid of the session 197 */ getUid()198 public int getUid() { 199 return mUid; 200 } 201 202 /** 203 * @return package name of the session 204 */ 205 @NonNull getPackageName()206 public String getPackageName() { 207 return mPackageName; 208 } 209 210 /** 211 * @return service name of the session. Can be {@code null} for {@link #TYPE_SESSION}. 212 */ 213 @Nullable getServiceName()214 public String getServiceName() { 215 return mServiceName; 216 } 217 218 /** 219 * @return type of the token 220 * @see #TYPE_SESSION 221 * @see #TYPE_SESSION_SERVICE 222 */ getType()223 public @TokenType int getType() { 224 return mType; 225 } 226 227 /** 228 * @return extras of the token 229 * @see MediaSession2.Builder#setExtras(Bundle) 230 */ 231 @NonNull getExtras()232 public Bundle getExtras() { 233 return new Bundle(mExtras); 234 } 235 getSessionLink()236 Session2Link getSessionLink() { 237 return mSessionLink; 238 } 239 isInterfaceDeclared(PackageManager manager, String serviceInterface, ComponentName serviceComponent)240 private static boolean isInterfaceDeclared(PackageManager manager, String serviceInterface, 241 ComponentName serviceComponent) { 242 Intent serviceIntent = new Intent(serviceInterface); 243 // Use queryIntentServices to find services with MediaSession2Service.SERVICE_INTERFACE. 244 // We cannot use resolveService with intent specified class name, because resolveService 245 // ignores actions if Intent.setClassName() is specified. 246 serviceIntent.setPackage(serviceComponent.getPackageName()); 247 248 List<ResolveInfo> list = manager.queryIntentServices( 249 serviceIntent, PackageManager.GET_META_DATA); 250 if (list != null) { 251 for (int i = 0; i < list.size(); i++) { 252 ResolveInfo resolveInfo = list.get(i); 253 if (resolveInfo == null || resolveInfo.serviceInfo == null) { 254 continue; 255 } 256 if (TextUtils.equals( 257 resolveInfo.serviceInfo.name, serviceComponent.getClassName())) { 258 return true; 259 } 260 } 261 } 262 return false; 263 } 264 getUid(PackageManager manager, String packageName)265 private static int getUid(PackageManager manager, String packageName) { 266 try { 267 return manager.getApplicationInfo(packageName, 0).uid; 268 } catch (PackageManager.NameNotFoundException e) { 269 throw new IllegalArgumentException("Cannot find package " + packageName); 270 } 271 } 272 } 273