blob: 3c7b517bc50c4f2dd7a120ade76391b2b1e35504 [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
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
Chiao Cheng11a4b652013-09-02 01:08:19 -070023import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
Santos Cordon89647a62013-07-16 13:38:09 -070025import android.os.Handler;
26import android.os.IBinder;
27import android.os.Message;
28import android.os.RemoteException;
29import android.os.SystemProperties;
30import android.util.Log;
31
Santos Cordon9b7bac72013-08-06 08:04:52 -070032import com.android.phone.AudioRouter.AudioModeListener;
33import com.android.services.telephony.common.AudioMode;
Santos Cordonf4046882013-07-25 18:49:27 -070034import com.android.services.telephony.common.Call;
Santos Cordon345350e2013-07-19 17:16:14 -070035import com.android.services.telephony.common.ICallHandlerService;
Chiao Cheng6c6b2722013-08-22 18:35:54 -070036import com.google.common.collect.Lists;
Santos Cordon89647a62013-07-16 13:38:09 -070037
Santos Cordona3d05142013-07-29 11:25:17 -070038import java.util.List;
39
Santos Cordon89647a62013-07-16 13:38:09 -070040/**
Santos Cordon345350e2013-07-19 17:16:14 -070041 * This class is responsible for passing through call state changes to the CallHandlerService.
Santos Cordon89647a62013-07-16 13:38:09 -070042 */
Chiao Cheng6c6b2722013-08-22 18:35:54 -070043public class CallHandlerServiceProxy extends Handler
44 implements CallModeler.Listener, AudioModeListener {
Santos Cordon89647a62013-07-16 13:38:09 -070045
Santos Cordon345350e2013-07-19 17:16:14 -070046 private static final String TAG = CallHandlerServiceProxy.class.getSimpleName();
Santos Cordon71d5c6e2013-09-05 21:34:33 -070047 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt(
Chiao Cheng6c6b2722013-08-22 18:35:54 -070048 "ro.debuggable", 0) == 1);
Chiao Chenge41661c2013-07-23 13:28:26 -070049
Chiao Cheng6c6b2722013-08-22 18:35:54 -070050 public static final int RETRY_DELAY_MILLIS = 2000;
51 private static final int BIND_RETRY_MSG = 1;
52 private static final int MAX_RETRY_COUNT = 5;
Santos Cordon89647a62013-07-16 13:38:09 -070053
Santos Cordon593ab382013-08-06 21:58:23 -070054 private AudioRouter mAudioRouter;
Santos Cordoncba1b442013-07-18 12:43:58 -070055 private CallCommandService mCallCommandService;
Santos Cordon593ab382013-08-06 21:58:23 -070056 private CallModeler mCallModeler;
57 private Context mContext;
Chiao Chengd38eebc2013-08-28 14:38:14 -070058
Chiao Cheng6c6b2722013-08-22 18:35:54 -070059 private ICallHandlerService mCallHandlerServiceGuarded; // Guarded by mServiceAndQueueLock
Chiao Chengd38eebc2013-08-28 14:38:14 -070060 // Single queue to guarantee ordering
61 private List<QueueParams> mQueue; // Guarded by mServiceAndQueueLock
62
Chiao Cheng6c6b2722013-08-22 18:35:54 -070063 private final Object mServiceAndQueueLock = new Object();
64 private int mBindRetryCount = 0;
65
66 @Override
67 public void handleMessage(Message msg) {
68 super.handleMessage(msg);
69
70 switch (msg.what) {
71 case BIND_RETRY_MSG:
72 setupServiceConnection();
73 break;
74 }
75 }
Santos Cordon89647a62013-07-16 13:38:09 -070076
Santos Cordon63a84242013-07-23 13:32:52 -070077 public CallHandlerServiceProxy(Context context, CallModeler callModeler,
Santos Cordon593ab382013-08-06 21:58:23 -070078 CallCommandService callCommandService, AudioRouter audioRouter) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -070079 if (DBG) {
80 Log.d(TAG, "init CallHandlerServiceProxy");
81 }
Santos Cordon89647a62013-07-16 13:38:09 -070082 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070083 mCallCommandService = callCommandService;
Santos Cordon63a84242013-07-23 13:32:52 -070084 mCallModeler = callModeler;
Santos Cordon593ab382013-08-06 21:58:23 -070085 mAudioRouter = audioRouter;
Santos Cordon89647a62013-07-16 13:38:09 -070086
Santos Cordon593ab382013-08-06 21:58:23 -070087 mAudioRouter.addAudioModeListener(this);
Christine Chendaf7bf62013-08-05 19:12:31 -070088 mCallModeler.addListener(this);
Santos Cordon63a84242013-07-23 13:32:52 -070089 }
Santos Cordon89647a62013-07-16 13:38:09 -070090
Santos Cordon63a84242013-07-23 13:32:52 -070091 @Override
Santos Cordon995c8162013-07-29 09:22:22 -070092 public void onDisconnect(Call call) {
Chiao Cheng3e6486e2013-09-03 19:27:46 -070093 synchronized (mServiceAndQueueLock) {
94 if (mCallHandlerServiceGuarded == null) {
95 if (DBG) {
96 Log.d(TAG, "CallHandlerService not connected. Enqueue disconnect");
Chiao Cheng6c6b2722013-08-22 18:35:54 -070097 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -070098 enqueueDisconnect(call);
99 setupServiceConnection();
100 return;
Santos Cordon63a84242013-07-23 13:32:52 -0700101 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700102 }
103 processDisconnect(call);
104 }
105
106 private void processDisconnect(Call call) {
107 try {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700108 if (DBG) {
109 Log.d(TAG, "onDisconnect: " + call);
110 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700111 synchronized (mServiceAndQueueLock) {
112 if (mCallHandlerServiceGuarded != null) {
113 mCallHandlerServiceGuarded.onDisconnect(call);
114 }
115 }
116 if (!mCallModeler.hasLiveCall()) {
117 unbind();
118 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700119 } catch (Exception e) {
120 Log.e(TAG, "Remote exception handling onDisconnect ", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700121 }
122 }
123
Santos Cordona3d05142013-07-29 11:25:17 -0700124 @Override
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700125 public void onIncoming(Call call) {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700126 synchronized (mServiceAndQueueLock) {
127 if (mCallHandlerServiceGuarded == null) {
128 if (DBG) {
129 Log.d(TAG, "CallHandlerService not connected. Enqueue incoming.");
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700130 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700131 enqueueIncoming(call);
132 setupServiceConnection();
133 return;
Christine Chenee09a492013-08-06 16:02:29 -0700134 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700135 }
136 processIncoming(call);
137 }
138
139 private void processIncoming(Call call) {
140 if (DBG) {
141 Log.d(TAG, "onIncoming: " + call);
142 }
143 try {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700144 // TODO(klp): check RespondViaSmsManager.allowRespondViaSmsForCall()
145 // must refactor call method to accept proper call object.
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700146 synchronized (mServiceAndQueueLock) {
147 if (mCallHandlerServiceGuarded != null) {
148 mCallHandlerServiceGuarded.onIncoming(call,
149 RejectWithTextMessageManager.loadCannedResponses());
150 }
151 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700152 } catch (Exception e) {
153 Log.e(TAG, "Remote exception handling onUpdate", e);
Christine Chenee09a492013-08-06 16:02:29 -0700154 }
155 }
156
157 @Override
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700158 public void onUpdate(List<Call> calls) {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700159 synchronized (mServiceAndQueueLock) {
160 if (mCallHandlerServiceGuarded == null) {
161 if (DBG) {
162 Log.d(TAG, "CallHandlerService not connected. Enqueue update.");
163 }
164 enqueueUpdate(calls);
165 setupServiceConnection();
166 return;
167 }
168 }
169 processUpdate(calls);
170 }
171
172 private void processUpdate(List<Call> calls) {
173 if (DBG) {
174 Log.d(TAG, "onUpdate: " + calls.toString());
175 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700176 try {
177 synchronized (mServiceAndQueueLock) {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700178 if (mCallHandlerServiceGuarded != null) {
179 mCallHandlerServiceGuarded.onUpdate(calls);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700180 }
Santos Cordona3d05142013-07-29 11:25:17 -0700181 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700182 if (!mCallModeler.hasLiveCall()) {
183 // TODO: unbinding happens in both onUpdate and onDisconnect because the ordering
184 // is not deterministic. Unbinding in both ensures that the service is unbound.
185 // But it also makes this in-efficient because we are unbinding twice, which leads
186 // to the CallHandlerService performing onCreate() and onDestroy() twice for each
187 // disconnect.
188 unbind();
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700189 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700190 } catch (Exception e) {
191 Log.e(TAG, "Remote exception handling onUpdate", e);
Santos Cordona3d05142013-07-29 11:25:17 -0700192 }
193 }
194
Chiao Cheng3f015c92013-09-06 15:56:27 -0700195
196 @Override
197 public void onPostDialWait(int callId, String remainingChars) {
198 try {
199 synchronized (mServiceAndQueueLock) {
200 if (mCallHandlerServiceGuarded == null) {
201 if (DBG) {
202 Log.d(TAG, "CallHandlerService not conneccted. Skipping "
203 + "onPostDialWait().");
204 }
205 return;
206 }
207 }
208
209 mCallHandlerServiceGuarded.onPostDialWait(callId, remainingChars);
210 } catch (Exception e) {
211 Log.e(TAG, "Remote exception handling onUpdate", e);
212 }
213 }
214
Santos Cordon9b7bac72013-08-06 08:04:52 -0700215 @Override
Santos Cordoncd95f622013-08-29 03:38:52 -0700216 public void onAudioModeChange(int newMode, boolean muted) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700217 try {
218 synchronized (mServiceAndQueueLock) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700219 if (mCallHandlerServiceGuarded == null) {
220 if (DBG) {
221 Log.d(TAG, "CallHandlerService not conneccted. Skipping "
222 + "onAudioModeChange().");
223 }
224 return;
225 }
Santos Cordon9b7bac72013-08-06 08:04:52 -0700226 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700227
228 // Just do a simple log for now.
229 Log.i(TAG, "Updating with new audio mode: " + AudioMode.toString(newMode) +
Santos Cordoncd95f622013-08-29 03:38:52 -0700230 " with mute " + muted);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700231
Santos Cordoncd95f622013-08-29 03:38:52 -0700232 mCallHandlerServiceGuarded.onAudioModeChange(newMode, muted);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700233 } catch (Exception e) {
234 Log.e(TAG, "Remote exception handling onAudioModeChange", e);
Santos Cordon9b7bac72013-08-06 08:04:52 -0700235 }
236 }
237
Santos Cordon593ab382013-08-06 21:58:23 -0700238 @Override
239 public void onSupportedAudioModeChange(int modeMask) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700240 try {
241 synchronized (mServiceAndQueueLock) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700242 if (mCallHandlerServiceGuarded == null) {
243 if (DBG) {
244 Log.d(TAG, "CallHandlerService not conneccted. Skipping"
245 + "onSupportedAudioModeChange().");
246 }
247 return;
248 }
249 }
Santos Cordon593ab382013-08-06 21:58:23 -0700250
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700251 if (DBG) {
252 Log.d(TAG, "onSupportAudioModeChange: " + AudioMode.toString(modeMask));
253 }
254
255 mCallHandlerServiceGuarded.onSupportedAudioModeChange(modeMask);
256 } catch (Exception e) {
257 Log.e(TAG, "Remote exception handling onAudioModeChange", e);
258 }
259
260 }
261
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700262 private ServiceConnection mConnection = null;
263
264 private class InCallServiceConnection implements ServiceConnection {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700265 @Override public void onServiceConnected (ComponentName className, IBinder service){
266 if (DBG) {
267 Log.d(TAG, "Service Connected");
268 }
269 onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service));
Chiao Cheng11a4b652013-09-02 01:08:19 -0700270 mBindRetryCount = 0;
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700271 }
272
273 @Override public void onServiceDisconnected (ComponentName className){
274 Log.i(TAG, "Disconnected from UI service.");
275 synchronized (mServiceAndQueueLock) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700276 // Technically, unbindService is un-necessary since the framework will schedule and
277 // restart the crashed service. But there is a exponential backoff for the restart.
278 // Unbind explicitly and setup again to avoid the backoff since it's important to
279 // always have an in call ui.
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700280 unbind();
281
282 // TODO(klp): hang up all calls.
Santos Cordon593ab382013-08-06 21:58:23 -0700283 }
284 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700285 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700286
Santos Cordon406c0342013-08-28 00:07:47 -0700287 public void bringToForeground() {
288 // only support this call if the service is already connected.
Santos Cordon19d814b2013-08-28 14:58:17 -0700289 synchronized (mServiceAndQueueLock) {
290 if (mCallHandlerServiceGuarded != null && mCallModeler.hasLiveCall()) {
291 try {
292 if (DBG) Log.d(TAG, "bringToForeground");
293 mCallHandlerServiceGuarded.bringToForeground();
294 } catch (RemoteException e) {
295 Log.e(TAG, "Exception handling bringToForeground", e);
296 }
Santos Cordon406c0342013-08-28 00:07:47 -0700297 }
298 }
299 }
300
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700301 private static Intent getInCallServiceIntent(Context context) {
302 final Intent serviceIntent = new Intent(ICallHandlerService.class.getName());
303 final ComponentName component = new ComponentName(context.getResources().getString(
304 R.string.incall_ui_default_package), context.getResources().getString(
305 R.string.incall_ui_default_class));
306 serviceIntent.setComponent(component);
307 return serviceIntent;
308 }
309
Santos Cordon89647a62013-07-16 13:38:09 -0700310 /**
Santos Cordon345350e2013-07-19 17:16:14 -0700311 * Sets up the connection with ICallHandlerService
Santos Cordon89647a62013-07-16 13:38:09 -0700312 */
313 private void setupServiceConnection() {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700314 if (!PhoneGlobals.sVoiceCapable) {
315 return;
316 }
Chiao Cheng11a4b652013-09-02 01:08:19 -0700317
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700318 final Intent serviceIntent = getInCallServiceIntent(mContext);
Chiao Cheng11a4b652013-09-02 01:08:19 -0700319 if (DBG) {
320 Log.d(TAG, "binding to service " + serviceIntent);
321 }
322
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700323 synchronized (mServiceAndQueueLock) {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700324 if (mConnection == null) {
325 mConnection = new InCallServiceConnection();
326
327 final PackageManager packageManger = mContext.getPackageManager();
328 final List<ResolveInfo> services = packageManger.queryIntentServices(serviceIntent,
329 0);
330 if (services.size() == 0) {
331 // Service not found, retry again after some delay
332 // This can happen if the service is being installed by the package manager.
333 // Between deletes and installs, bindService could get a silent service not
334 // found error.
335 mBindRetryCount++;
336 if (mBindRetryCount < MAX_RETRY_COUNT) {
337 Log.w(TAG, "InCallUI service not found. " + serviceIntent
338 + ". This happens if the service is being installed and should be"
339 + " transient. Retrying" + RETRY_DELAY_MILLIS + " ms.");
340 sendMessageDelayed(Message.obtain(this, BIND_RETRY_MSG),
341 RETRY_DELAY_MILLIS);
342 } else {
343 Log.e(TAG, "Tried to bind to in-call UI " + MAX_RETRY_COUNT + " times."
344 + " Giving up.");
345 }
346 return;
347 }
348
349 if (DBG) {
350 Log.d(TAG, "binding to service " + serviceIntent);
351 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700352 if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700353 // This happens when the in-call package is in the middle of being
354 // installed.
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700355 // Delay the retry.
356 mBindRetryCount++;
357 if (mBindRetryCount < MAX_RETRY_COUNT) {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700358 Log.e(TAG, "bindService failed on " + serviceIntent + ". Retrying in "
359 + RETRY_DELAY_MILLIS + " ms.");
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700360 sendMessageDelayed(Message.obtain(this, BIND_RETRY_MSG),
361 RETRY_DELAY_MILLIS);
362 } else {
363 Log.wtf(TAG, "Tried to bind to in-call UI " + MAX_RETRY_COUNT + " times."
364 + " Giving up.");
365 }
366 }
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700367
368 } else {
369 Log.d(TAG, "Service connection to in call service already started.");
Santos Cordonaf763a12013-08-19 20:04:58 -0700370 }
371 }
372 }
373
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700374 private void unbind() {
375 synchronized (mServiceAndQueueLock) {
376 if (mCallHandlerServiceGuarded != null) {
377 Log.d(TAG, "Unbinding service.");
378 mCallHandlerServiceGuarded = null;
379 mContext.unbindService(mConnection);
380 }
381 mConnection = null;
382 }
383 }
384
Santos Cordonaf763a12013-08-19 20:04:58 -0700385 /**
Santos Cordoncba1b442013-07-18 12:43:58 -0700386 * Called when the in-call UI service is connected. Send command interface to in-call.
387 */
Santos Cordon63a84242013-07-23 13:32:52 -0700388 private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) {
Chiao Chengd38eebc2013-08-28 14:38:14 -0700389
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700390 synchronized (mServiceAndQueueLock) {
391 mCallHandlerServiceGuarded = callHandlerService;
392
Santos Cordonad078192013-08-28 15:14:54 -0700393 // Before we send any updates, we need to set up the initial service calls.
394 makeInitialServiceCalls();
395
Chiao Chengd38eebc2013-08-28 14:38:14 -0700396 processQueue();
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700397 }
Santos Cordonad078192013-08-28 15:14:54 -0700398 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700399
Santos Cordonad078192013-08-28 15:14:54 -0700400 /**
401 * Makes initial service calls to set up callcommandservice and audio modes.
402 */
403 private void makeInitialServiceCalls() {
Santos Cordoncba1b442013-07-18 12:43:58 -0700404 try {
Santos Cordon12a03aa2013-09-12 23:34:05 -0700405 mCallHandlerServiceGuarded.startCallService(mCallCommandService);
Santos Cordonad078192013-08-28 15:14:54 -0700406
407 onSupportedAudioModeChange(mAudioRouter.getSupportedAudioModes());
Santos Cordon8fd0ec72013-08-29 16:44:43 -0700408 onAudioModeChange(mAudioRouter.getAudioMode(), mAudioRouter.getMute());
Santos Cordoncba1b442013-07-18 12:43:58 -0700409 } catch (RemoteException e) {
Santos Cordon63a84242013-07-23 13:32:52 -0700410 Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700411 }
412 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700413
Chiao Chengd38eebc2013-08-28 14:38:14 -0700414 private List<QueueParams> getQueue() {
415 if (mQueue == null) {
416 mQueue = Lists.newArrayList();
417 }
418 return mQueue;
419 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700420
421 private void enqueueDisconnect(Call call) {
Chiao Chengd38eebc2013-08-28 14:38:14 -0700422 getQueue().add(new QueueParams(QueueParams.METHOD_DISCONNECT, new Call(call)));
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700423 }
424
425 private void enqueueIncoming(Call call) {
Chiao Chengd38eebc2013-08-28 14:38:14 -0700426 getQueue().add(new QueueParams(QueueParams.METHOD_INCOMING, new Call(call)));
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700427 }
428
429 private void enqueueUpdate(List<Call> calls) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700430 final List<Call> copy = Lists.newArrayList();
431 for (Call call : calls) {
432 copy.add(new Call(call));
433 }
Chiao Chengc340ba92013-08-30 13:04:46 -0700434 getQueue().add(new QueueParams(QueueParams.METHOD_UPDATE, copy));
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700435 }
436
Chiao Chengd38eebc2013-08-28 14:38:14 -0700437 private void processQueue() {
Chiao Cheng3e6486e2013-09-03 19:27:46 -0700438 synchronized (mServiceAndQueueLock) {
439 if (mQueue != null) {
440 for (QueueParams params : mQueue) {
441 switch (params.mMethod) {
442 case QueueParams.METHOD_INCOMING:
443 processIncoming((Call) params.mArg);
444 break;
445 case QueueParams.METHOD_UPDATE:
446 processUpdate((List<Call>) params.mArg);
447 break;
448 case QueueParams.METHOD_DISCONNECT:
449 processDisconnect((Call) params.mArg);
450 break;
451 default:
452 throw new IllegalArgumentException("Method type " + params.mMethod +
453 " not recognized.");
454 }
455 }
456 mQueue.clear();
457 mQueue = null;
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700458 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700459 }
460 }
461
Chiao Chengd38eebc2013-08-28 14:38:14 -0700462 /**
463 * Holds method parameters.
464 */
465 private static class QueueParams {
466 private static final int METHOD_INCOMING = 1;
467 private static final int METHOD_UPDATE = 2;
468 private static final int METHOD_DISCONNECT = 3;
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700469
Chiao Chengd38eebc2013-08-28 14:38:14 -0700470 private final int mMethod;
471 private final Object mArg;
472
473 private QueueParams(int method, Object arg) {
474 mMethod = method;
475 this.mArg = arg;
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700476 }
477 }
Santos Cordon89647a62013-07-16 13:38:09 -0700478}