• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 package android.car.media;
17 
18 import android.annotation.RequiresPermission;
19 import android.car.Car;
20 import android.car.CarManagerBase;
21 import android.os.IBinder;
22 import android.os.RemoteException;
23 
24 import java.util.HashMap;
25 import java.util.Map;
26 
27 /**
28  * API for updating and receiving updates to the primary media source in the car.
29  * @hide
30  */
31 public final class CarMediaManager implements CarManagerBase {
32 
33     private final ICarMedia mService;
34     private Map<MediaSourceChangedListener, ICarMediaSourceListener> mCallbackMap = new HashMap();
35 
36     /**
37      * Get an instance of the CarPowerManager.
38      *
39      * Should not be obtained directly by clients, use {@link Car#getCarManager(String)} instead.
40      * @hide
41      */
CarMediaManager(IBinder service)42     public CarMediaManager(IBinder service) {
43         mService = ICarMedia.Stub.asInterface(service);
44     }
45 
46     /**
47      * Listener for updates to the primary media source
48      * @hide
49      */
50     public interface MediaSourceChangedListener {
51 
52         /**
53          * Called when the primary media source is changed
54          * @hide
55          */
onMediaSourceChanged(String packageName)56         void onMediaSourceChanged(String packageName);
57     }
58 
59     /**
60      * Gets the currently active media source, or null if none exists
61      * Requires android.Manifest.permission.MEDIA_CONTENT_CONTROL permission
62      * @hide
63      */
64     @RequiresPermission(value = android.Manifest.permission.MEDIA_CONTENT_CONTROL)
getMediaSource()65     public synchronized String getMediaSource() {
66         try {
67             return mService.getMediaSource();
68         } catch (RemoteException e) {
69             throw e.rethrowFromSystemServer();
70         }
71     }
72 
73     /**
74      * Sets the currently active media source
75      * Requires android.Manifest.permission.MEDIA_CONTENT_CONTROL permission
76      * @hide
77      */
78     @RequiresPermission(value = android.Manifest.permission.MEDIA_CONTENT_CONTROL)
setMediaSource(String packageName)79     public synchronized void setMediaSource(String packageName) {
80         try {
81             mService.setMediaSource(packageName);
82         } catch (RemoteException e) {
83             throw e.rethrowFromSystemServer();
84         }
85     }
86 
87     /**
88      * Register a callback that receives updates to the active media source.
89      * Requires android.Manifest.permission.MEDIA_CONTENT_CONTROL permission
90      * @hide
91      */
92     @RequiresPermission(value = android.Manifest.permission.MEDIA_CONTENT_CONTROL)
registerMediaSourceListener(MediaSourceChangedListener callback)93     public synchronized void registerMediaSourceListener(MediaSourceChangedListener callback) {
94         try {
95             ICarMediaSourceListener binderCallback = new ICarMediaSourceListener.Stub() {
96                 @Override
97                 public void onMediaSourceChanged(String packageName) {
98                     callback.onMediaSourceChanged(packageName);
99                 }
100             };
101             mCallbackMap.put(callback, binderCallback);
102             mService.registerMediaSourceListener(binderCallback);
103         } catch (RemoteException e) {
104             throw e.rethrowFromSystemServer();
105         }
106     }
107 
108     /**
109      * Unregister a callback that receives updates to the active media source.
110      * Requires android.Manifest.permission.MEDIA_CONTENT_CONTROL permission
111      * @hide
112      */
113     @RequiresPermission(value = android.Manifest.permission.MEDIA_CONTENT_CONTROL)
unregisterMediaSourceListener(MediaSourceChangedListener callback)114     public synchronized void unregisterMediaSourceListener(MediaSourceChangedListener callback) {
115         try {
116             ICarMediaSourceListener binderCallback = mCallbackMap.remove(callback);
117             mService.unregisterMediaSourceListener(binderCallback);
118         } catch (RemoteException e) {
119             throw e.rethrowFromSystemServer();
120         }
121     }
122 
123     /** @hide */
124     @Override
onCarDisconnected()125     public synchronized void onCarDisconnected() {
126     }
127 }
128