Home | History | Annotate | Download | only in media
      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.content.Context;
     22 import android.media.session.MediaSessionManager;
     23 import android.media.update.ApiLoader;
     24 import android.media.update.SessionToken2Provider;
     25 import android.os.Bundle;
     26 
     27 import java.lang.annotation.Retention;
     28 import java.lang.annotation.RetentionPolicy;
     29 
     30 /**
     31  * @hide
     32  * Represents an ongoing {@link MediaSession2} or a {@link MediaSessionService2}.
     33  * If it's representing a session service, it may not be ongoing.
     34  * <p>
     35  * This may be passed to apps by the session owner to allow them to create a
     36  * {@link MediaController2} to communicate with the session.
     37  * <p>
     38  * It can be also obtained by {@link MediaSessionManager}.
     39  */
     40 // New version of MediaSession.Token for following reasons
     41 //   - Stop implementing Parcelable for updatable support
     42 //   - Represent session and library service (formerly browser service) in one class.
     43 //     Previously MediaSession.Token was for session and ComponentName was for service.
     44 public final class SessionToken2 {
     45     @Retention(RetentionPolicy.SOURCE)
     46     @IntDef(value = {TYPE_SESSION, TYPE_SESSION_SERVICE, TYPE_LIBRARY_SERVICE})
     47     public @interface TokenType {
     48     }
     49 
     50     public static final int TYPE_SESSION = 0;
     51     public static final int TYPE_SESSION_SERVICE = 1;
     52     public static final int TYPE_LIBRARY_SERVICE = 2;
     53 
     54     private final SessionToken2Provider mProvider;
     55 
     56     // From the return value of android.os.Process.getUidForName(String) when error
     57     private static final int UID_UNKNOWN = -1;
     58 
     59     /**
     60      * Constructor for the token. You can only create token for session service or library service
     61      * to use by {@link MediaController2} or {@link MediaBrowser2}.
     62      *
     63      * @param context context
     64      * @param packageName package name
     65      * @param serviceName name of service. Can be {@code null} if it's not an service.
     66      */
     67     public SessionToken2(@NonNull Context context, @NonNull String packageName,
     68             @NonNull String serviceName) {
     69         this(context, packageName, serviceName, UID_UNKNOWN);
     70     }
     71 
     72     /**
     73      * Constructor for the token. You can only create token for session service or library service
     74      * to use by {@link MediaController2} or {@link MediaBrowser2}.
     75      *
     76      * @param context context
     77      * @param packageName package name
     78      * @param serviceName name of service. Can be {@code null} if it's not an service.
     79      * @param uid uid of the app.
     80      * @hide
     81      */
     82     public SessionToken2(@NonNull Context context, @NonNull String packageName,
     83             @NonNull String serviceName, int uid) {
     84         mProvider = ApiLoader.getProvider().createSessionToken2(
     85                 context, this, packageName, serviceName, uid);
     86     }
     87 
     88     /**
     89      * Constructor for the token.
     90      * @hide
     91      */
     92     public SessionToken2(@NonNull SessionToken2Provider provider) {
     93         mProvider = provider;
     94     }
     95 
     96     @Override
     97     public int hashCode() {
     98         return mProvider.hashCode_impl();
     99     }
    100 
    101     @Override
    102     public boolean equals(Object obj) {
    103         return mProvider.equals_impl(obj);
    104     }
    105 
    106     @Override
    107     public String toString() {
    108         return mProvider.toString_impl();
    109     }
    110 
    111     /**
    112      * @hide
    113      */
    114     public SessionToken2Provider getProvider() {
    115         return mProvider;
    116     }
    117 
    118     /**
    119      * @return uid of the session
    120      */
    121     public int getUid() {
    122         return mProvider.getUid_impl();
    123     }
    124 
    125     /**
    126      * @return package name
    127      */
    128     public String getPackageName() {
    129         return mProvider.getPackageName_impl();
    130     }
    131 
    132     /**
    133      * @return id
    134      */
    135     public String getId() {
    136         return mProvider.getId_imp();
    137     }
    138 
    139     /**
    140      * @return type of the token
    141      * @see #TYPE_SESSION
    142      * @see #TYPE_SESSION_SERVICE
    143      */
    144     public @TokenType int getType() {
    145         return mProvider.getType_impl();
    146     }
    147 
    148     /**
    149      * Create a token from the bundle, exported by {@link #toBundle()}.
    150      * @param bundle
    151      * @return
    152      */
    153     public static SessionToken2 fromBundle(@NonNull Bundle bundle) {
    154         return ApiLoader.getProvider().fromBundle_SessionToken2(bundle);
    155     }
    156 
    157     /**
    158      * Create a {@link Bundle} from this token to share it across processes.
    159      * @return Bundle
    160      */
    161     public Bundle toBundle() {
    162         return mProvider.toBundle_impl();
    163     }
    164 }
    165