OSDN Git Service

MediaSession2: Simplify constructor of SessionToken2
authorJaewan Kim <jaewan@google.com>
Mon, 29 Jan 2018 12:49:41 +0000 (21:49 +0900)
committerJaewan Kim <jaewan@google.com>
Mon, 29 Jan 2018 13:43:13 +0000 (22:43 +0900)
Test: Run all MediaComponents tests once
Change-Id: Ic99d5916d354815a57fe37d04dfeb625c493076d

media/java/android/media/SessionToken2.java
media/java/android/media/update/StaticProvider.java
services/core/java/com/android/server/media/MediaSessionService.java

index 7591eb3..2c2090c 100644 (file)
@@ -18,14 +18,12 @@ package android.media;
 
 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;
@@ -52,38 +50,45 @@ public final class SessionToken2 {
 
     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
index 2176795..14b23c8 100644 (file)
@@ -77,8 +77,7 @@ public interface StaticProvider {
             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);
index 9e29e84..b877184 100644 (file)
@@ -469,31 +469,21 @@ public class MediaSessionService extends SystemService implements Monitor {
                 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);
                 }
             }
         }