MediaSessionManager.java revision 07c7077c54717dbbf2c401ea32d00fa6df6d77c6
1/*
2 * Copyright (C) 2014 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.session;
18
19import android.content.Context;
20import android.media.session.ISessionManager;
21import android.os.IBinder;
22import android.os.RemoteException;
23import android.os.ServiceManager;
24import android.util.Log;
25
26import java.util.ArrayList;
27import java.util.List;
28
29/**
30 * MediaSessionManager allows the creation and control of MediaSessions in the
31 * system. A MediaSession enables publishing information about ongoing media and
32 * interacting with MediaControllers and MediaRoutes.
33 * <p>
34 * Use <code>Context.getSystemService(Context.MEDIA_SESSION_SERVICE)</code> to
35 * get an instance of this class.
36 * <p>
37 *
38 * @see Session
39 * @see SessionController
40 */
41public final class SessionManager {
42    private static final String TAG = "SessionManager";
43
44    private final ISessionManager mService;
45
46    private Context mContext;
47
48    /**
49     * @hide
50     */
51    public SessionManager(Context context) {
52        // Consider rewriting like DisplayManagerGlobal
53        // Decide if we need context
54        mContext = context;
55        IBinder b = ServiceManager.getService(Context.MEDIA_SESSION_SERVICE);
56        mService = ISessionManager.Stub.asInterface(b);
57    }
58
59    /**
60     * Creates a new session.
61     *
62     * @param tag A short name for debugging purposes
63     * @return a {@link Session} for the new session
64     */
65    public Session createSession(String tag) {
66        try {
67            Session.CallbackStub cbStub = new Session.CallbackStub();
68            Session session = new Session(mService
69                    .createSession(mContext.getPackageName(), cbStub, tag), cbStub);
70            cbStub.setMediaSession(session);
71
72            return session;
73        } catch (RemoteException e) {
74            Log.e(TAG, "Failed to create session: ", e);
75            return null;
76        }
77    }
78
79    /**
80     * Get a list of controllers for all ongoing sessions. This requires the
81     * android.Manifest.permission.MEDIA_CONTENT_CONTROL permission be held by
82     * the calling app.
83     *
84     * @return a list of controllers for ongoing sessions
85     */
86    public List<SessionController> getActiveSessions() {
87        // TODO
88        return new ArrayList<SessionController>();
89    }
90}
91