• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.car;
18 
19 import android.annotation.SystemApi;
20 import android.content.Intent;
21 import android.os.Handler;
22 import android.os.IBinder;
23 import android.os.RemoteException;
24 
25 import java.lang.ref.WeakReference;
26 
27 /**
28  * CarProjectionManager allows applications implementing projection to register/unregister itself
29  * with projection manager, listen for voice notification.
30  * @hide
31  */
32 @SystemApi
33 public final class CarProjectionManager implements CarManagerBase {
34     /**
35      * Listener to get projected notifications.
36      *
37      * Currently only voice search request is supported.
38      */
39     public interface CarProjectionListener {
40         /**
41          * Voice search was requested by the user.
42          */
onVoiceAssistantRequest(boolean fromLongPress)43         void onVoiceAssistantRequest(boolean fromLongPress);
44     }
45 
46     /**
47      * Flag for voice search request.
48      */
49     public static final int PROJECTION_VOICE_SEARCH = 0x1;
50     /**
51      * Flag for long press voice search request.
52      */
53     public static final int PROJECTION_LONG_PRESS_VOICE_SEARCH = 0x2;
54 
55     private final ICarProjection mService;
56     private final Handler mHandler;
57     private final ICarProjectionCallbackImpl mBinderListener;
58 
59     private CarProjectionListener mListener;
60     private int mVoiceSearchFilter;
61 
62     /**
63      * @hide
64      */
CarProjectionManager(IBinder service, Handler handler)65     CarProjectionManager(IBinder service, Handler handler) {
66         mService = ICarProjection.Stub.asInterface(service);
67         mHandler = handler;
68         mBinderListener = new ICarProjectionCallbackImpl(this);
69     }
70 
71     /**
72      * Register listener to monitor projection. Only one listener can be registered and
73      * registering multiple times will lead into only the last listener to be active.
74      * @param listener
75      * @param voiceSearchFilter Flags of voice search requests to get notification.
76      * @throws CarNotConnectedException if the connection to the car service has been lost.
77      */
regsiterProjectionListener(CarProjectionListener listener, int voiceSearchFilter)78     public void regsiterProjectionListener(CarProjectionListener listener, int voiceSearchFilter)
79             throws CarNotConnectedException {
80         if (listener == null) {
81             throw new IllegalArgumentException("null listener");
82         }
83         synchronized (this) {
84             if (mListener == null || mVoiceSearchFilter != voiceSearchFilter) {
85                 try {
86                     mService.regsiterProjectionListener(mBinderListener, voiceSearchFilter);
87                 } catch (RemoteException e) {
88                     throw new CarNotConnectedException(e);
89                 }
90             }
91             mListener = listener;
92             mVoiceSearchFilter = voiceSearchFilter;
93         }
94     }
95 
96     /**
97      * Unregister listener and stop listening projection events.
98      * @throws CarNotConnectedException if the connection to the car service has been lost.
99      */
unregsiterProjectionListener()100     public void unregsiterProjectionListener() {
101         synchronized (this) {
102             try {
103                 mService.unregsiterProjectionListener(mBinderListener);
104             } catch (RemoteException e) {
105                 //ignore
106             }
107             mListener = null;
108             mVoiceSearchFilter = 0;
109         }
110     }
111 
112     /**
113      * Registers projection runner on projection start with projection service
114      * to create reverse binding.
115      * @param serviceIntent
116      * @throws CarNotConnectedException if the connection to the car service has been lost.
117      */
registerProjectionRunner(Intent serviceIntent)118     public void registerProjectionRunner(Intent serviceIntent) throws CarNotConnectedException {
119         if (serviceIntent == null) {
120             throw new IllegalArgumentException("null serviceIntent");
121         }
122         synchronized (this) {
123             try {
124                 mService.registerProjectionRunner(serviceIntent);
125             } catch (RemoteException e) {
126                 throw new CarNotConnectedException(e);
127             }
128         }
129     }
130 
131     /**
132      * Unregisters projection runner on projection stop with projection service to create
133      * reverse binding.
134      * @param serviceIntent
135      * @throws CarNotConnectedException if the connection to the car service has been lost.
136      */
unregisterProjectionRunner(Intent serviceIntent)137     public void unregisterProjectionRunner(Intent serviceIntent) {
138         if (serviceIntent == null) {
139             throw new IllegalArgumentException("null serviceIntent");
140         }
141         synchronized (this) {
142             try {
143                 mService.unregisterProjectionRunner(serviceIntent);
144             } catch (RemoteException e) {
145                 //ignore
146             }
147         }
148     }
149 
150     @Override
onCarDisconnected()151     public void onCarDisconnected() {
152         // nothing to do
153     }
154 
handleVoiceAssistantRequest(boolean fromLongPress)155     private void handleVoiceAssistantRequest(boolean fromLongPress) {
156         CarProjectionListener listener;
157         synchronized (this) {
158             if (mListener == null) {
159                 return;
160             }
161             listener = mListener;
162         }
163         listener.onVoiceAssistantRequest(fromLongPress);
164     }
165 
166     private static class ICarProjectionCallbackImpl extends ICarProjectionCallback.Stub {
167 
168         private final WeakReference<CarProjectionManager> mManager;
169 
ICarProjectionCallbackImpl(CarProjectionManager manager)170         private ICarProjectionCallbackImpl(CarProjectionManager manager) {
171             mManager = new WeakReference<>(manager);
172         }
173 
174         @Override
onVoiceAssistantRequest(final boolean fromLongPress)175         public void onVoiceAssistantRequest(final boolean fromLongPress) {
176             final CarProjectionManager manager = mManager.get();
177             if (manager == null) {
178                 return;
179             }
180             manager.mHandler.post(new Runnable() {
181                 @Override
182                 public void run() {
183                     manager.handleVoiceAssistantRequest(fromLongPress);
184                 }
185             });
186         }
187     }
188 }
189