blob: fe390909732661cb82a964c3fbb4544b19177c16 [file] [log] [blame]
Santos Cordon89647a62013-07-16 13:38:09 -07001/*
2 * Copyright (C) 2013 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 com.android.phone;
18
Yorke Lee79840002013-09-16 14:30:56 -070019import android.Manifest;
Santos Cordon89647a62013-07-16 13:38:09 -070020import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.ServiceConnection;
Chiao Cheng11a4b652013-09-02 01:08:19 -070024import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
Yorke Lee79840002013-09-16 14:30:56 -070026import android.content.pm.ServiceInfo;
Santos Cordon89647a62013-07-16 13:38:09 -070027import android.os.Handler;
28import android.os.IBinder;
29import android.os.Message;
Chiao Cheng26c6e922013-09-14 16:45:38 -070030import android.os.PowerManager;
Santos Cordon89647a62013-07-16 13:38:09 -070031import android.os.RemoteException;
Chiao Cheng26c6e922013-09-14 16:45:38 -070032import android.os.SystemClock;
Santos Cordon89647a62013-07-16 13:38:09 -070033import android.os.SystemProperties;
Yorke Leede41f672013-09-19 13:46:55 -070034import android.text.TextUtils;
Santos Cordon89647a62013-07-16 13:38:09 -070035import android.util.Log;
36
Yorke Leede41f672013-09-19 13:46:55 -070037import com.android.internal.telephony.Connection;
38import com.android.internal.telephony.Connection.PostDialState;
Santos Cordon9b7bac72013-08-06 08:04:52 -070039import com.android.phone.AudioRouter.AudioModeListener;
Yorke Lee362cec22013-09-18 15:20:26 -070040import com.android.phone.NotificationMgr.StatusBarHelper;
Santos Cordon9b7bac72013-08-06 08:04:52 -070041import com.android.services.telephony.common.AudioMode;
Santos Cordonf4046882013-07-25 18:49:27 -070042import com.android.services.telephony.common.Call;
Santos Cordon345350e2013-07-19 17:16:14 -070043import com.android.services.telephony.common.ICallHandlerService;
Chiao Cheng6c6b2722013-08-22 18:35:54 -070044import com.google.common.collect.Lists;
Santos Cordon89647a62013-07-16 13:38:09 -070045
Santos Cordona3d05142013-07-29 11:25:17 -070046import java.util.List;
47
Santos Cordon89647a62013-07-16 13:38:09 -070048/**
Santos Cordon345350e2013-07-19 17:16:14 -070049 * This class is responsible for passing through call state changes to the CallHandlerService.
Santos Cordon89647a62013-07-16 13:38:09 -070050 */
Chiao Cheng6c6b2722013-08-22 18:35:54 -070051public class CallHandlerServiceProxy extends Handler
52 implements CallModeler.Listener, AudioModeListener {
Santos Cordon89647a62013-07-16 13:38:09 -070053
Santos Cordon345350e2013-07-19 17:16:14 -070054 private static final String TAG = CallHandlerServiceProxy.class.getSimpleName();
Santos Cordon71d5c6e2013-09-05 21:34:33 -070055 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt(
Chiao Cheng6c6b2722013-08-22 18:35:54 -070056 "ro.debuggable", 0) == 1);
Chiao Chenge41661c2013-07-23 13:28:26 -070057
Chiao Cheng6c6b2722013-08-22 18:35:54 -070058 public static final int RETRY_DELAY_MILLIS = 2000;
59 private static final int BIND_RETRY_MSG = 1;
60 private static final int MAX_RETRY_COUNT = 5;
Santos Cordon89647a62013-07-16 13:38:09 -070061
Santos Cordon593ab382013-08-06 21:58:23 -070062 private AudioRouter mAudioRouter;
Santos Cordoncba1b442013-07-18 12:43:58 -070063 private CallCommandService mCallCommandService;
Santos Cordon593ab382013-08-06 21:58:23 -070064 private CallModeler mCallModeler;
65 private Context mContext;
Chiao Chengd38eebc2013-08-28 14:38:14 -070066
Chiao Cheng6c6b2722013-08-22 18:35:54 -070067 private ICallHandlerService mCallHandlerServiceGuarded; // Guarded by mServiceAndQueueLock
Chiao Chengd38eebc2013-08-28 14:38:14 -070068 // Single queue to guarantee ordering
69 private List<QueueParams> mQueue; // Guarded by mServiceAndQueueLock
70
Chiao Cheng6c6b2722013-08-22 18:35:54 -070071 private final Object mServiceAndQueueLock = new Object();
72 private int mBindRetryCount = 0;
73
74 @Override
75 public void handleMessage(Message msg) {
76 super.handleMessage(msg);
77
78 switch (msg.what) {
79 case BIND_RETRY_MSG:
80 setupServiceConnection();
81 break;
82 }
83 }
Santos Cordon89647a62013-07-16 13:38:09 -070084
Santos Cordon63a84242013-07-23 13:32:52 -070085 public CallHandlerServiceProxy(Context context, CallModeler callModeler,
Santos Cordon593ab382013-08-06 21:58:23 -070086 CallCommandService callCommandService, AudioRouter audioRouter) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -070087 if (DBG) {
88 Log.d(TAG, "init CallHandlerServiceProxy");
89 }
Santos Cordon89647a62013-07-16 13:38:09 -070090 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070091 mCallCommandService = callCommandService;
Santos Cordon63a84242013-07-23 13:32:52 -070092 mCallModeler = callModeler;
Santos Cordon593ab382013-08-06 21:58:23 -070093 mAudioRouter = audioRouter;
Santos Cordon89647a62013-07-16 13:38:09 -070094
Santos Cordon593ab382013-08-06 21:58:23 -070095 mAudioRouter.addAudioModeListener(this);
Christine Chendaf7bf62013-08-05 19:12:31 -070096 mCallModeler.addListener(this);
Santos Cordon63a84242013-07-23 13:32:52 -070097 }
Santos Cordon89647a62013-07-16 13:38:09 -070098
Santos Cordon63a84242013-07-23 13:32:52 -070099 @Override
Santos Cordon995c8162013-07-29 09:22:22 -0700100 public void onDisconnect(Call call) {
Chiao Cheng26c6e922013-09-14 16:45:38 -0700101 // Wake up in case the screen was off.
102 wakeUpScreen();
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700103 synchronized (mServiceAndQueueLock) {
104 if (mCallHandlerServiceGuarded == null) {
105 if (DBG) {
106 Log.d(TAG, "CallHandlerService not connected. Enqueue disconnect");
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700107 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700108 enqueueDisconnect(call);
109 setupServiceConnection();
110 return;
Santos Cordon63a84242013-07-23 13:32:52 -0700111 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700112 }
113 processDisconnect(call);
114 }
115
Chiao Cheng26c6e922013-09-14 16:45:38 -0700116 private void wakeUpScreen() {
117 Log.d(TAG, "wakeUpScreen()");
118 final PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
119 pm.wakeUp(SystemClock.uptimeMillis());
120 }
121
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700122 private void processDisconnect(Call call) {
123 try {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700124 if (DBG) {
125 Log.d(TAG, "onDisconnect: " + call);
126 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700127 synchronized (mServiceAndQueueLock) {
128 if (mCallHandlerServiceGuarded != null) {
129 mCallHandlerServiceGuarded.onDisconnect(call);
130 }
131 }
132 if (!mCallModeler.hasLiveCall()) {
133 unbind();
134 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700135 } catch (Exception e) {
136 Log.e(TAG, "Remote exception handling onDisconnect ", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700137 }
138 }
139
Santos Cordona3d05142013-07-29 11:25:17 -0700140 @Override
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700141 public void onIncoming(Call call) {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700142 synchronized (mServiceAndQueueLock) {
143 if (mCallHandlerServiceGuarded == null) {
144 if (DBG) {
145 Log.d(TAG, "CallHandlerService not connected. Enqueue incoming.");
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700146 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700147 enqueueIncoming(call);
148 setupServiceConnection();
149 return;
Christine Chenee09a492013-08-06 16:02:29 -0700150 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700151 }
152 processIncoming(call);
153 }
154
155 private void processIncoming(Call call) {
156 if (DBG) {
157 Log.d(TAG, "onIncoming: " + call);
158 }
159 try {
Christine Chen3e0f0412013-09-18 20:33:49 -0700160 // TODO: check RespondViaSmsManager.allowRespondViaSmsForCall()
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700161 // must refactor call method to accept proper call object.
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700162 synchronized (mServiceAndQueueLock) {
163 if (mCallHandlerServiceGuarded != null) {
164 mCallHandlerServiceGuarded.onIncoming(call,
165 RejectWithTextMessageManager.loadCannedResponses());
166 }
167 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700168 } catch (Exception e) {
169 Log.e(TAG, "Remote exception handling onUpdate", e);
Christine Chenee09a492013-08-06 16:02:29 -0700170 }
171 }
172
173 @Override
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700174 public void onUpdate(List<Call> calls) {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700175 synchronized (mServiceAndQueueLock) {
176 if (mCallHandlerServiceGuarded == null) {
177 if (DBG) {
178 Log.d(TAG, "CallHandlerService not connected. Enqueue update.");
179 }
180 enqueueUpdate(calls);
181 setupServiceConnection();
182 return;
183 }
184 }
185 processUpdate(calls);
186 }
187
188 private void processUpdate(List<Call> calls) {
189 if (DBG) {
190 Log.d(TAG, "onUpdate: " + calls.toString());
191 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700192 try {
193 synchronized (mServiceAndQueueLock) {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700194 if (mCallHandlerServiceGuarded != null) {
195 mCallHandlerServiceGuarded.onUpdate(calls);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700196 }
Santos Cordona3d05142013-07-29 11:25:17 -0700197 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700198 if (!mCallModeler.hasLiveCall()) {
199 // TODO: unbinding happens in both onUpdate and onDisconnect because the ordering
200 // is not deterministic. Unbinding in both ensures that the service is unbound.
201 // But it also makes this in-efficient because we are unbinding twice, which leads
202 // to the CallHandlerService performing onCreate() and onDestroy() twice for each
203 // disconnect.
204 unbind();
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700205 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700206 } catch (Exception e) {
207 Log.e(TAG, "Remote exception handling onUpdate", e);
Santos Cordona3d05142013-07-29 11:25:17 -0700208 }
209 }
210
Chiao Cheng3f015c92013-09-06 15:56:27 -0700211
212 @Override
Yorke Leede41f672013-09-19 13:46:55 -0700213 public void onPostDialAction(Connection.PostDialState state, int callId, String remainingChars,
214 char currentChar) {
215 if (state != PostDialState.WAIT) return;
Chiao Cheng3f015c92013-09-06 15:56:27 -0700216 try {
217 synchronized (mServiceAndQueueLock) {
218 if (mCallHandlerServiceGuarded == null) {
219 if (DBG) {
220 Log.d(TAG, "CallHandlerService not conneccted. Skipping "
221 + "onPostDialWait().");
222 }
223 return;
224 }
225 }
226
227 mCallHandlerServiceGuarded.onPostDialWait(callId, remainingChars);
228 } catch (Exception e) {
229 Log.e(TAG, "Remote exception handling onUpdate", e);
230 }
231 }
232
Santos Cordon9b7bac72013-08-06 08:04:52 -0700233 @Override
Santos Cordoncd95f622013-08-29 03:38:52 -0700234 public void onAudioModeChange(int newMode, boolean muted) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700235 try {
236 synchronized (mServiceAndQueueLock) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700237 if (mCallHandlerServiceGuarded == null) {
238 if (DBG) {
239 Log.d(TAG, "CallHandlerService not conneccted. Skipping "
240 + "onAudioModeChange().");
241 }
242 return;
243 }
Santos Cordon9b7bac72013-08-06 08:04:52 -0700244 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700245
246 // Just do a simple log for now.
247 Log.i(TAG, "Updating with new audio mode: " + AudioMode.toString(newMode) +
Santos Cordoncd95f622013-08-29 03:38:52 -0700248 " with mute " + muted);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700249
Santos Cordoncd95f622013-08-29 03:38:52 -0700250 mCallHandlerServiceGuarded.onAudioModeChange(newMode, muted);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700251 } catch (Exception e) {
252 Log.e(TAG, "Remote exception handling onAudioModeChange", e);
Santos Cordon9b7bac72013-08-06 08:04:52 -0700253 }
254 }
255
Santos Cordon593ab382013-08-06 21:58:23 -0700256 @Override
257 public void onSupportedAudioModeChange(int modeMask) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700258 try {
259 synchronized (mServiceAndQueueLock) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700260 if (mCallHandlerServiceGuarded == null) {
261 if (DBG) {
262 Log.d(TAG, "CallHandlerService not conneccted. Skipping"
263 + "onSupportedAudioModeChange().");
264 }
265 return;
266 }
267 }
Santos Cordon593ab382013-08-06 21:58:23 -0700268
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700269 if (DBG) {
270 Log.d(TAG, "onSupportAudioModeChange: " + AudioMode.toString(modeMask));
271 }
272
273 mCallHandlerServiceGuarded.onSupportedAudioModeChange(modeMask);
274 } catch (Exception e) {
275 Log.e(TAG, "Remote exception handling onAudioModeChange", e);
276 }
277
278 }
279
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700280 private ServiceConnection mConnection = null;
281
282 private class InCallServiceConnection implements ServiceConnection {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700283 @Override public void onServiceConnected (ComponentName className, IBinder service){
284 if (DBG) {
285 Log.d(TAG, "Service Connected");
286 }
287 onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service));
Chiao Cheng11a4b652013-09-02 01:08:19 -0700288 mBindRetryCount = 0;
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700289 }
290
291 @Override public void onServiceDisconnected (ComponentName className){
292 Log.i(TAG, "Disconnected from UI service.");
293 synchronized (mServiceAndQueueLock) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700294 // Technically, unbindService is un-necessary since the framework will schedule and
295 // restart the crashed service. But there is a exponential backoff for the restart.
296 // Unbind explicitly and setup again to avoid the backoff since it's important to
297 // always have an in call ui.
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700298 unbind();
299
Santos Cordonde10b752013-09-19 04:11:33 -0700300 // We were disconnected from the UI. End all calls.
301 PhoneUtils.hangupAllCalls(PhoneGlobals.getInstance().getCallManager());
Santos Cordon593ab382013-08-06 21:58:23 -0700302 }
303 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700304 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700305
Makoto Onukibcf20992013-09-12 17:59:30 -0700306 public void bringToForeground(boolean showDialpad) {
Santos Cordon406c0342013-08-28 00:07:47 -0700307 // only support this call if the service is already connected.
Santos Cordon19d814b2013-08-28 14:58:17 -0700308 synchronized (mServiceAndQueueLock) {
309 if (mCallHandlerServiceGuarded != null && mCallModeler.hasLiveCall()) {
310 try {
Makoto Onukibcf20992013-09-12 17:59:30 -0700311 if (DBG) Log.d(TAG, "bringToForeground: " + showDialpad);
312 mCallHandlerServiceGuarded.bringToForeground(showDialpad);
Santos Cordon19d814b2013-08-28 14:58:17 -0700313 } catch (RemoteException e) {
314 Log.e(TAG, "Exception handling bringToForeground", e);
315 }
Santos Cordon406c0342013-08-28 00:07:47 -0700316 }
317 }
318 }
319
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700320 private static Intent getInCallServiceIntent(Context context) {
321 final Intent serviceIntent = new Intent(ICallHandlerService.class.getName());
322 final ComponentName component = new ComponentName(context.getResources().getString(
Yorke Leed3105fe2013-09-25 12:44:45 -0700323 R.string.ui_default_package), context.getResources().getString(
324 R.string.incall_default_class));
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700325 serviceIntent.setComponent(component);
326 return serviceIntent;
327 }
328
Santos Cordon89647a62013-07-16 13:38:09 -0700329 /**
Santos Cordon345350e2013-07-19 17:16:14 -0700330 * Sets up the connection with ICallHandlerService
Santos Cordon89647a62013-07-16 13:38:09 -0700331 */
332 private void setupServiceConnection() {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700333 if (!PhoneGlobals.sVoiceCapable) {
334 return;
335 }
Chiao Cheng11a4b652013-09-02 01:08:19 -0700336
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700337 final Intent serviceIntent = getInCallServiceIntent(mContext);
Chiao Cheng11a4b652013-09-02 01:08:19 -0700338 if (DBG) {
339 Log.d(TAG, "binding to service " + serviceIntent);
340 }
341
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700342 synchronized (mServiceAndQueueLock) {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700343 if (mConnection == null) {
344 mConnection = new InCallServiceConnection();
345
346 final PackageManager packageManger = mContext.getPackageManager();
347 final List<ResolveInfo> services = packageManger.queryIntentServices(serviceIntent,
348 0);
Yorke Lee79840002013-09-16 14:30:56 -0700349
350 ServiceInfo serviceInfo = null;
351
352 for (int i = 0; i < services.size(); i++) {
353 final ResolveInfo info = services.get(i);
354 if (info.serviceInfo != null) {
355 if (Manifest.permission.BIND_CALL_SERVICE.equals(
356 info.serviceInfo.permission)) {
357 serviceInfo = info.serviceInfo;
358 break;
359 }
360 }
361 }
362
363 if (serviceInfo == null) {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700364 // Service not found, retry again after some delay
365 // This can happen if the service is being installed by the package manager.
366 // Between deletes and installs, bindService could get a silent service not
367 // found error.
368 mBindRetryCount++;
369 if (mBindRetryCount < MAX_RETRY_COUNT) {
370 Log.w(TAG, "InCallUI service not found. " + serviceIntent
371 + ". This happens if the service is being installed and should be"
372 + " transient. Retrying" + RETRY_DELAY_MILLIS + " ms.");
373 sendMessageDelayed(Message.obtain(this, BIND_RETRY_MSG),
374 RETRY_DELAY_MILLIS);
375 } else {
376 Log.e(TAG, "Tried to bind to in-call UI " + MAX_RETRY_COUNT + " times."
377 + " Giving up.");
378 }
379 return;
380 }
381
Yorke Lee79840002013-09-16 14:30:56 -0700382 // Bind to the first service that has a permission
383 // TODO: Add UI to allow us to select between services
384
385 serviceIntent.setComponent(new ComponentName(serviceInfo.packageName,
386 serviceInfo.name));
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700387 if (DBG) {
388 Log.d(TAG, "binding to service " + serviceIntent);
389 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700390 if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700391 // This happens when the in-call package is in the middle of being
392 // installed.
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700393 // Delay the retry.
394 mBindRetryCount++;
395 if (mBindRetryCount < MAX_RETRY_COUNT) {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700396 Log.e(TAG, "bindService failed on " + serviceIntent + ". Retrying in "
397 + RETRY_DELAY_MILLIS + " ms.");
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700398 sendMessageDelayed(Message.obtain(this, BIND_RETRY_MSG),
399 RETRY_DELAY_MILLIS);
400 } else {
401 Log.wtf(TAG, "Tried to bind to in-call UI " + MAX_RETRY_COUNT + " times."
402 + " Giving up.");
403 }
404 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700405
406 } else {
407 Log.d(TAG, "Service connection to in call service already started.");
Santos Cordonaf763a12013-08-19 20:04:58 -0700408 }
409 }
410 }
411
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700412 private void unbind() {
413 synchronized (mServiceAndQueueLock) {
Yorke Lee362cec22013-09-18 15:20:26 -0700414 // On unbind, reenable the notification shade and navigation bar just in case the
415 // in-call UI crashed on an incoming call.
416 final StatusBarHelper statusBarHelper = PhoneGlobals.getInstance().notificationMgr.
417 statusBarHelper;
418 statusBarHelper.enableSystemBarNavigation(true);
419 statusBarHelper.enableExpandedView(true);
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700420 if (mCallHandlerServiceGuarded != null) {
421 Log.d(TAG, "Unbinding service.");
422 mCallHandlerServiceGuarded = null;
423 mContext.unbindService(mConnection);
424 }
425 mConnection = null;
426 }
427 }
428
Santos Cordonaf763a12013-08-19 20:04:58 -0700429 /**
Santos Cordoncba1b442013-07-18 12:43:58 -0700430 * Called when the in-call UI service is connected. Send command interface to in-call.
431 */
Santos Cordon63a84242013-07-23 13:32:52 -0700432 private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) {
Chiao Chengd38eebc2013-08-28 14:38:14 -0700433
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700434 synchronized (mServiceAndQueueLock) {
435 mCallHandlerServiceGuarded = callHandlerService;
436
Santos Cordonad078192013-08-28 15:14:54 -0700437 // Before we send any updates, we need to set up the initial service calls.
438 makeInitialServiceCalls();
439
Chiao Chengd38eebc2013-08-28 14:38:14 -0700440 processQueue();
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700441 }
Santos Cordonad078192013-08-28 15:14:54 -0700442 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700443
Santos Cordonad078192013-08-28 15:14:54 -0700444 /**
445 * Makes initial service calls to set up callcommandservice and audio modes.
446 */
447 private void makeInitialServiceCalls() {
Santos Cordoncba1b442013-07-18 12:43:58 -0700448 try {
Santos Cordon12a03aa2013-09-12 23:34:05 -0700449 mCallHandlerServiceGuarded.startCallService(mCallCommandService);
Santos Cordonad078192013-08-28 15:14:54 -0700450
451 onSupportedAudioModeChange(mAudioRouter.getSupportedAudioModes());
Santos Cordon8fd0ec72013-08-29 16:44:43 -0700452 onAudioModeChange(mAudioRouter.getAudioMode(), mAudioRouter.getMute());
Santos Cordoncba1b442013-07-18 12:43:58 -0700453 } catch (RemoteException e) {
Santos Cordon63a84242013-07-23 13:32:52 -0700454 Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700455 }
456 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700457
Chiao Chengd38eebc2013-08-28 14:38:14 -0700458 private List<QueueParams> getQueue() {
459 if (mQueue == null) {
460 mQueue = Lists.newArrayList();
461 }
462 return mQueue;
463 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700464
465 private void enqueueDisconnect(Call call) {
Chiao Chengd38eebc2013-08-28 14:38:14 -0700466 getQueue().add(new QueueParams(QueueParams.METHOD_DISCONNECT, new Call(call)));
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700467 }
468
469 private void enqueueIncoming(Call call) {
Chiao Chengd38eebc2013-08-28 14:38:14 -0700470 getQueue().add(new QueueParams(QueueParams.METHOD_INCOMING, new Call(call)));
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700471 }
472
473 private void enqueueUpdate(List<Call> calls) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700474 final List<Call> copy = Lists.newArrayList();
475 for (Call call : calls) {
476 copy.add(new Call(call));
477 }
Chiao Chengc340ba92013-08-30 13:04:46 -0700478 getQueue().add(new QueueParams(QueueParams.METHOD_UPDATE, copy));
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700479 }
480
Chiao Chengd38eebc2013-08-28 14:38:14 -0700481 private void processQueue() {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700482 synchronized (mServiceAndQueueLock) {
483 if (mQueue != null) {
484 for (QueueParams params : mQueue) {
485 switch (params.mMethod) {
486 case QueueParams.METHOD_INCOMING:
487 processIncoming((Call) params.mArg);
488 break;
489 case QueueParams.METHOD_UPDATE:
490 processUpdate((List<Call>) params.mArg);
491 break;
492 case QueueParams.METHOD_DISCONNECT:
493 processDisconnect((Call) params.mArg);
494 break;
495 default:
496 throw new IllegalArgumentException("Method type " + params.mMethod +
497 " not recognized.");
498 }
499 }
500 mQueue.clear();
501 mQueue = null;
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700502 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700503 }
504 }
505
Chiao Chengd38eebc2013-08-28 14:38:14 -0700506 /**
507 * Holds method parameters.
508 */
509 private static class QueueParams {
510 private static final int METHOD_INCOMING = 1;
511 private static final int METHOD_UPDATE = 2;
512 private static final int METHOD_DISCONNECT = 3;
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700513
Chiao Chengd38eebc2013-08-28 14:38:14 -0700514 private final int mMethod;
515 private final Object mArg;
516
517 private QueueParams(int method, Object arg) {
518 mMethod = method;
519 this.mArg = arg;
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700520 }
521 }
Santos Cordon89647a62013-07-16 13:38:09 -0700522}