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 */ 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 */ 65 CarProjectionManager(IBinder service, Handler handler) { 66 mService = ICarProjection.Stub.asInterface(service); 67 mHandler = handler; 68 mBinderListener = new ICarProjectionCallbackImpl(this); 69 } 70 71 /** 72 * Compatibility with previous APIs due to typo 73 * @throws CarNotConnectedException if the connection to the car service has been lost. 74 * @hide 75 */ 76 public void regsiterProjectionListener(CarProjectionListener listener, int voiceSearchFilter) 77 throws CarNotConnectedException { 78 registerProjectionListener(listener, voiceSearchFilter); 79 } 80 81 /** 82 * Register listener to monitor projection. Only one listener can be registered and 83 * registering multiple times will lead into only the last listener to be active. 84 * @param listener 85 * @param voiceSearchFilter Flags of voice search requests to get notification. 86 * @throws CarNotConnectedException if the connection to the car service has been lost. 87 */ 88 public void registerProjectionListener(CarProjectionListener listener, int voiceSearchFilter) 89 throws CarNotConnectedException { 90 if (listener == null) { 91 throw new IllegalArgumentException("null listener"); 92 } 93 synchronized (this) { 94 if (mListener == null || mVoiceSearchFilter != voiceSearchFilter) { 95 try { 96 mService.registerProjectionListener(mBinderListener, voiceSearchFilter); 97 } catch (RemoteException e) { 98 throw new CarNotConnectedException(e); 99 } 100 } 101 mListener = listener; 102 mVoiceSearchFilter = voiceSearchFilter; 103 } 104 } 105 106 /** 107 * Compatibility with previous APIs due to typo 108 * @throws CarNotConnectedException if the connection to the car service has been lost. 109 * @hide 110 */ 111 public void unregsiterProjectionListener() { 112 unregisterProjectionListener(); 113 } 114 115 /** 116 * Unregister listener and stop listening projection events. 117 * @throws CarNotConnectedException if the connection to the car service has been lost. 118 */ 119 public void unregisterProjectionListener() { 120 synchronized (this) { 121 try { 122 mService.unregisterProjectionListener(mBinderListener); 123 } catch (RemoteException e) { 124 //ignore 125 } 126 mListener = null; 127 mVoiceSearchFilter = 0; 128 } 129 } 130 131 /** 132 * Registers projection runner on projection start with projection service 133 * to create reverse binding. 134 * @param serviceIntent 135 * @throws CarNotConnectedException if the connection to the car service has been lost. 136 */ 137 public void registerProjectionRunner(Intent serviceIntent) throws CarNotConnectedException { 138 if (serviceIntent == null) { 139 throw new IllegalArgumentException("null serviceIntent"); 140 } 141 synchronized (this) { 142 try { 143 mService.registerProjectionRunner(serviceIntent); 144 } catch (RemoteException e) { 145 throw new CarNotConnectedException(e); 146 } 147 } 148 } 149 150 /** 151 * Unregisters projection runner on projection stop with projection service to create 152 * reverse binding. 153 * @param serviceIntent 154 * @throws CarNotConnectedException if the connection to the car service has been lost. 155 */ 156 public void unregisterProjectionRunner(Intent serviceIntent) { 157 if (serviceIntent == null) { 158 throw new IllegalArgumentException("null serviceIntent"); 159 } 160 synchronized (this) { 161 try { 162 mService.unregisterProjectionRunner(serviceIntent); 163 } catch (RemoteException e) { 164 //ignore 165 } 166 } 167 } 168 169 @Override 170 public void onCarDisconnected() { 171 // nothing to do 172 } 173 174 private void handleVoiceAssistantRequest(boolean fromLongPress) { 175 CarProjectionListener listener; 176 synchronized (this) { 177 if (mListener == null) { 178 return; 179 } 180 listener = mListener; 181 } 182 listener.onVoiceAssistantRequest(fromLongPress); 183 } 184 185 private static class ICarProjectionCallbackImpl extends ICarProjectionCallback.Stub { 186 187 private final WeakReference<CarProjectionManager> mManager; 188 189 private ICarProjectionCallbackImpl(CarProjectionManager manager) { 190 mManager = new WeakReference<>(manager); 191 } 192 193 @Override 194 public void onVoiceAssistantRequest(final boolean fromLongPress) { 195 final CarProjectionManager manager = mManager.get(); 196 if (manager == null) { 197 return; 198 } 199 manager.mHandler.post(new Runnable() { 200 @Override 201 public void run() { 202 manager.handleVoiceAssistantRequest(fromLongPress); 203 } 204 }); 205 } 206 } 207 } 208