import android.annotation.IntDef;
import android.annotation.NonNull;
-import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.content.Context;
import android.media.session.MediaSessionManager;
import android.media.update.ApiLoader;
import android.media.update.SessionToken2Provider;
import android.os.Bundle;
-import android.os.IInterface;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
private final SessionToken2Provider mProvider;
+ // From the return value of android.os.Process.getUidForName(String) when error
+ private static final int UID_UNKNOWN = -1;
+
/**
* Constructor for the token. You can only create token for session service or library service
* to use by {@link MediaController2} or {@link MediaBrowser2}.
*
* @param context context
- * @param type type
* @param packageName package name
* @param serviceName name of service. Can be {@code null} if it's not an service.
*/
- public SessionToken2(@NonNull Context context, @TokenType int type, @NonNull String packageName,
+ public SessionToken2(@NonNull Context context, @NonNull String packageName,
@NonNull String serviceName) {
- this(context, -1, type, packageName, serviceName, null, null);
+ this(context, packageName, serviceName, UID_UNKNOWN);
}
/**
- * Constructor for the token.
+ * Constructor for the token. You can only create token for session service or library service
+ * to use by {@link MediaController2} or {@link MediaBrowser2}.
*
* @param context context
- * @param uid uid
- * @param type type
* @param packageName package name
* @param serviceName name of service. Can be {@code null} if it's not an service.
- * @param id id. Can be {@code null} if serviceName is specified.
- * @param sessionBinderInterface sessionBinder. Required for the session.
+ * @param uid uid of the app.
+ * @hide
+ */
+ public SessionToken2(@NonNull Context context, @NonNull String packageName,
+ @NonNull String serviceName, int uid) {
+ mProvider = ApiLoader.getProvider(context).createSessionToken2(
+ context, this, packageName, serviceName, uid);
+ }
+
+ /**
+ * Constructor for the token.
+ * @hide
*/
@SystemApi
- public SessionToken2(@NonNull Context context, int uid, @TokenType int type,
- @NonNull String packageName, @Nullable String serviceName, @Nullable String id,
- @Nullable IInterface sessionBinderInterface) {
- mProvider = ApiLoader.getProvider(context)
- .createSessionToken2(context, this, uid, type, packageName,
- serviceName, id, sessionBinderInterface);
+ public SessionToken2(@NonNull SessionToken2Provider provider) {
+ mProvider = provider;
}
@Override
VolumeProvider volumeProvider, int ratingType, PendingIntent sessionActivity,
Executor executor, MediaLibrarySessionCallback callback);
SessionToken2Provider createSessionToken2(Context context, SessionToken2 instance,
- int uid, int type, String packageName, String serviceName, String id,
- IInterface sessionBinderInterface);
+ String packageName, String serviceName, int uid);
SessionToken2 SessionToken2_fromBundle(Context context, Bundle bundle);
SessionPlayer2Provider createSessionPlayer2(Context context, SessionPlayer2 instance);
ServiceInfo serviceInfo = services.get(i).serviceInfo;
int uid;
try {
+ // TODO(jaewan): Do this per user.
uid = manager.getPackageUid(serviceInfo.packageName,
PackageManager.GET_META_DATA);
} catch (NameNotFoundException e) {
continue;
}
- String id = (serviceInfo.metaData != null) ? serviceInfo.metaData.getString(
- MediaSessionService2.SERVICE_META_DATA) : null;
- // Do basic sanity check
- // TODO(jaewan): also santity check if it's protected with the system|privileged
- // permission
- boolean conflict = (getSessionRecordLocked(uid, serviceInfo.name, id) != null);
- if (conflict) {
- Log.w(TAG, serviceInfo.packageName + " contains multiple"
- + " MediaSessionService2s declared in the manifest with"
- + " the same ID=" + id + ". Ignoring "
- + serviceInfo.packageName + "/" + serviceInfo.name);
- } else {
- int type = (libraryServices.contains(services.get(i)))
- ? SessionToken2.TYPE_LIBRARY_SERVICE
- : SessionToken2.TYPE_SESSION_SERVICE;
- SessionToken2 token = new SessionToken2(getContext(), uid, type,
- serviceInfo.packageName, serviceInfo.name, id, null);
+
+ try {
+ SessionToken2 token = new SessionToken2(getContext(),
+ serviceInfo.packageName, serviceInfo.name, uid);
MediaSession2Record record = new MediaSession2Record(getContext(),
token, mSessionDestroyedListener);
mSessions.add(record);
+ } catch (IllegalArgumentException e) {
+ Log.d(TAG, "Invalid session service", e);
}
}
}