Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.phone; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.ServiceConnection; |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 23 | import android.os.Handler; |
| 24 | import android.os.IBinder; |
| 25 | import android.os.Message; |
| 26 | import android.os.RemoteException; |
| 27 | import android.os.SystemProperties; |
| 28 | import android.util.Log; |
| 29 | |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 30 | import com.android.phone.AudioRouter.AudioModeListener; |
| 31 | import com.android.services.telephony.common.AudioMode; |
Santos Cordon | f404688 | 2013-07-25 18:49:27 -0700 | [diff] [blame] | 32 | import com.android.services.telephony.common.Call; |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 33 | import com.android.services.telephony.common.ICallHandlerService; |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 34 | import com.google.common.collect.Lists; |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 35 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 36 | import java.util.List; |
| 37 | |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 38 | /** |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 39 | * This class is responsible for passing through call state changes to the CallHandlerService. |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 40 | */ |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 41 | public class CallHandlerServiceProxy extends Handler |
| 42 | implements CallModeler.Listener, AudioModeListener { |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 43 | |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 44 | private static final String TAG = CallHandlerServiceProxy.class.getSimpleName(); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 45 | private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt( |
| 46 | "ro.debuggable", 0) == 1); |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 47 | |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 48 | public static final int RETRY_DELAY_MILLIS = 2000; |
| 49 | private static final int BIND_RETRY_MSG = 1; |
| 50 | private static final int MAX_RETRY_COUNT = 5; |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 51 | |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 52 | private AudioRouter mAudioRouter; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 53 | private CallCommandService mCallCommandService; |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 54 | private CallModeler mCallModeler; |
| 55 | private Context mContext; |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame^] | 56 | |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 57 | private ICallHandlerService mCallHandlerServiceGuarded; // Guarded by mServiceAndQueueLock |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame^] | 58 | // Single queue to guarantee ordering |
| 59 | private List<QueueParams> mQueue; // Guarded by mServiceAndQueueLock |
| 60 | |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 61 | private final Object mServiceAndQueueLock = new Object(); |
| 62 | private int mBindRetryCount = 0; |
| 63 | |
| 64 | @Override |
| 65 | public void handleMessage(Message msg) { |
| 66 | super.handleMessage(msg); |
| 67 | |
| 68 | switch (msg.what) { |
| 69 | case BIND_RETRY_MSG: |
| 70 | setupServiceConnection(); |
| 71 | break; |
| 72 | } |
| 73 | } |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 74 | |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 75 | public CallHandlerServiceProxy(Context context, CallModeler callModeler, |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 76 | CallCommandService callCommandService, AudioRouter audioRouter) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 77 | if (DBG) { |
| 78 | Log.d(TAG, "init CallHandlerServiceProxy"); |
| 79 | } |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 80 | mContext = context; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 81 | mCallCommandService = callCommandService; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 82 | mCallModeler = callModeler; |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 83 | mAudioRouter = audioRouter; |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 84 | |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 85 | mAudioRouter.addAudioModeListener(this); |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 86 | mCallModeler.addListener(this); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 87 | |
| 88 | setupServiceConnection(); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 89 | } |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 90 | |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 91 | @Override |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 92 | public void onDisconnect(Call call) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 93 | try { |
| 94 | synchronized (mServiceAndQueueLock) { |
| 95 | if (mCallHandlerServiceGuarded == null) { |
| 96 | if (DBG) { |
| 97 | Log.d(TAG, "CallHandlerService not connected. Enqueue disconnect"); |
| 98 | } |
| 99 | enqueueDisconnect(call); |
| 100 | return; |
| 101 | } |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 102 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 103 | if (DBG) { |
| 104 | Log.d(TAG, "onDisconnect: " + call); |
| 105 | } |
| 106 | mCallHandlerServiceGuarded.onDisconnect(call); |
| 107 | } catch (Exception e) { |
| 108 | Log.e(TAG, "Remote exception handling onDisconnect ", e); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 112 | @Override |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 113 | public void onIncoming(Call call) { |
| 114 | try { |
| 115 | synchronized (mServiceAndQueueLock) { |
| 116 | if (mCallHandlerServiceGuarded == null) { |
| 117 | if (DBG) { |
| 118 | Log.d(TAG, "CallHandlerService not connected. Enqueue incoming."); |
| 119 | } |
| 120 | enqueueIncoming(call); |
| 121 | return; |
| 122 | } |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 123 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 124 | if (DBG) { |
| 125 | Log.d(TAG, "onIncoming: " + call); |
| 126 | } |
| 127 | // TODO(klp): check RespondViaSmsManager.allowRespondViaSmsForCall() |
| 128 | // must refactor call method to accept proper call object. |
| 129 | mCallHandlerServiceGuarded.onIncoming(call, |
| 130 | RejectWithTextMessageManager.loadCannedResponses()); |
| 131 | } catch (Exception e) { |
| 132 | Log.e(TAG, "Remote exception handling onUpdate", e); |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | @Override |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 137 | public void onUpdate(List<Call> calls) { |
| 138 | try { |
| 139 | synchronized (mServiceAndQueueLock) { |
| 140 | if (mCallHandlerServiceGuarded == null) { |
| 141 | if (DBG) { |
| 142 | Log.d(TAG, "CallHandlerService not connected. Enqueue update."); |
| 143 | } |
| 144 | enqueueUpdate(calls); |
| 145 | return; |
| 146 | } |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 147 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 148 | |
| 149 | if (DBG) { |
| 150 | Log.d(TAG, "onUpdate: " + calls.toString()); |
| 151 | } |
| 152 | mCallHandlerServiceGuarded.onUpdate(calls); |
| 153 | } catch (Exception e) { |
| 154 | Log.e(TAG, "Remote exception handling onUpdate", e); |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 158 | @Override |
| 159 | public void onAudioModeChange(int previousMode, int newMode) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 160 | try { |
| 161 | synchronized (mServiceAndQueueLock) { |
| 162 | // TODO(klp): does this need to be enqueued? |
| 163 | if (mCallHandlerServiceGuarded == null) { |
| 164 | if (DBG) { |
| 165 | Log.d(TAG, "CallHandlerService not conneccted. Skipping " |
| 166 | + "onAudioModeChange()."); |
| 167 | } |
| 168 | return; |
| 169 | } |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 170 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 171 | |
| 172 | // Just do a simple log for now. |
| 173 | Log.i(TAG, "Updating with new audio mode: " + AudioMode.toString(newMode) + |
| 174 | " from " + AudioMode.toString(previousMode)); |
| 175 | |
| 176 | if (DBG) { |
| 177 | Log.d(TAG, "onSupportAudioModeChange"); |
| 178 | } |
| 179 | |
| 180 | mCallHandlerServiceGuarded.onAudioModeChange(newMode); |
| 181 | } catch (Exception e) { |
| 182 | Log.e(TAG, "Remote exception handling onAudioModeChange", e); |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 186 | @Override |
| 187 | public void onSupportedAudioModeChange(int modeMask) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 188 | try { |
| 189 | synchronized (mServiceAndQueueLock) { |
| 190 | // TODO(klp): does this need to be enqueued? |
| 191 | if (mCallHandlerServiceGuarded == null) { |
| 192 | if (DBG) { |
| 193 | Log.d(TAG, "CallHandlerService not conneccted. Skipping" |
| 194 | + "onSupportedAudioModeChange()."); |
| 195 | } |
| 196 | return; |
| 197 | } |
| 198 | } |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 199 | |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 200 | if (DBG) { |
| 201 | Log.d(TAG, "onSupportAudioModeChange: " + AudioMode.toString(modeMask)); |
| 202 | } |
| 203 | |
| 204 | mCallHandlerServiceGuarded.onSupportedAudioModeChange(modeMask); |
| 205 | } catch (Exception e) { |
| 206 | Log.e(TAG, "Remote exception handling onAudioModeChange", e); |
| 207 | } |
| 208 | |
| 209 | } |
| 210 | |
| 211 | private ServiceConnection mConnection = new ServiceConnection() { |
| 212 | @Override public void onServiceConnected (ComponentName className, IBinder service){ |
| 213 | if (DBG) { |
| 214 | Log.d(TAG, "Service Connected"); |
| 215 | } |
| 216 | onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service)); |
| 217 | } |
| 218 | |
| 219 | @Override public void onServiceDisconnected (ComponentName className){ |
| 220 | Log.i(TAG, "Disconnected from UI service."); |
| 221 | synchronized (mServiceAndQueueLock) { |
| 222 | mCallHandlerServiceGuarded = null; |
| 223 | |
| 224 | // Technically, unbindService is un-necessary since the framework will schedule and |
| 225 | // restart the crashed service. But there is a exponential backoff for the restart. |
| 226 | // Unbind explicitly and setup again to avoid the backoff since it's important to |
| 227 | // always have an in call ui. |
| 228 | mContext.unbindService(mConnection); |
| 229 | setupServiceConnection(); |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
Santos Cordon | 19d814b | 2013-08-28 14:58:17 -0700 | [diff] [blame] | 232 | }; |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 233 | |
Santos Cordon | 406c034 | 2013-08-28 00:07:47 -0700 | [diff] [blame] | 234 | public void bringToForeground() { |
| 235 | // only support this call if the service is already connected. |
Santos Cordon | 19d814b | 2013-08-28 14:58:17 -0700 | [diff] [blame] | 236 | synchronized (mServiceAndQueueLock) { |
| 237 | if (mCallHandlerServiceGuarded != null && mCallModeler.hasLiveCall()) { |
| 238 | try { |
| 239 | if (DBG) Log.d(TAG, "bringToForeground"); |
| 240 | mCallHandlerServiceGuarded.bringToForeground(); |
| 241 | } catch (RemoteException e) { |
| 242 | Log.e(TAG, "Exception handling bringToForeground", e); |
| 243 | } |
Santos Cordon | 406c034 | 2013-08-28 00:07:47 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 248 | /** |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 249 | * Sets up the connection with ICallHandlerService |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 250 | */ |
| 251 | private void setupServiceConnection() { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 252 | synchronized (mServiceAndQueueLock) { |
| 253 | if (mCallHandlerServiceGuarded == null) { |
| 254 | |
| 255 | |
| 256 | final Intent serviceIntent = new Intent(ICallHandlerService.class.getName()); |
| 257 | final ComponentName component = new ComponentName(mContext.getResources().getString( |
| 258 | R.string.incall_ui_default_package), mContext.getResources().getString( |
| 259 | R.string.incall_ui_default_class)); |
| 260 | serviceIntent.setComponent(component); |
| 261 | |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 262 | if (DBG) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 263 | Log.d(TAG, "binding to service " + serviceIntent); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 264 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 265 | if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) { |
| 266 | // This happens when the in-call package is in the middle of being installed. |
| 267 | // Delay the retry. |
| 268 | mBindRetryCount++; |
| 269 | if (mBindRetryCount < MAX_RETRY_COUNT) { |
| 270 | Log.e(TAG, "bindService failed on " + serviceIntent + ". Retrying in " + |
| 271 | RETRY_DELAY_MILLIS + " ms."); |
| 272 | sendMessageDelayed(Message.obtain(this, BIND_RETRY_MSG), |
| 273 | RETRY_DELAY_MILLIS); |
| 274 | } else { |
| 275 | Log.wtf(TAG, "Tried to bind to in-call UI " + MAX_RETRY_COUNT + " times." |
| 276 | + " Giving up."); |
| 277 | } |
| 278 | } |
Santos Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 284 | * Called when the in-call UI service is connected. Send command interface to in-call. |
| 285 | */ |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 286 | private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) { |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame^] | 287 | |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 288 | synchronized (mServiceAndQueueLock) { |
| 289 | mCallHandlerServiceGuarded = callHandlerService; |
| 290 | |
Santos Cordon | ad07819 | 2013-08-28 15:14:54 -0700 | [diff] [blame] | 291 | // Before we send any updates, we need to set up the initial service calls. |
| 292 | makeInitialServiceCalls(); |
| 293 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame^] | 294 | processQueue(); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 295 | } |
Santos Cordon | ad07819 | 2013-08-28 15:14:54 -0700 | [diff] [blame] | 296 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 297 | |
Santos Cordon | ad07819 | 2013-08-28 15:14:54 -0700 | [diff] [blame] | 298 | /** |
| 299 | * Makes initial service calls to set up callcommandservice and audio modes. |
| 300 | */ |
| 301 | private void makeInitialServiceCalls() { |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 302 | try { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 303 | mCallHandlerServiceGuarded.setCallCommandService(mCallCommandService); |
Santos Cordon | ad07819 | 2013-08-28 15:14:54 -0700 | [diff] [blame] | 304 | |
| 305 | onSupportedAudioModeChange(mAudioRouter.getSupportedAudioModes()); |
| 306 | final int mode = mAudioRouter.getAudioMode(); |
| 307 | onAudioModeChange(mode, mode); |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 308 | } catch (RemoteException e) { |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 309 | Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 312 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame^] | 313 | private List<QueueParams> getQueue() { |
| 314 | if (mQueue == null) { |
| 315 | mQueue = Lists.newArrayList(); |
| 316 | } |
| 317 | return mQueue; |
| 318 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 319 | |
| 320 | private void enqueueDisconnect(Call call) { |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame^] | 321 | getQueue().add(new QueueParams(QueueParams.METHOD_DISCONNECT, new Call(call))); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | private void enqueueIncoming(Call call) { |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame^] | 325 | getQueue().add(new QueueParams(QueueParams.METHOD_INCOMING, new Call(call))); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | private void enqueueUpdate(List<Call> calls) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 329 | final List<Call> copy = Lists.newArrayList(); |
| 330 | for (Call call : calls) { |
| 331 | copy.add(new Call(call)); |
| 332 | } |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame^] | 333 | getQueue().add(new QueueParams(QueueParams.METHOD_INCOMING, copy)); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame^] | 336 | private void processQueue() { |
| 337 | List<QueueParams> queue = getQueue(); |
| 338 | for (QueueParams params : queue) { |
| 339 | switch (params.mMethod) { |
| 340 | case QueueParams.METHOD_INCOMING: |
| 341 | onIncoming((Call) params.mArg); |
| 342 | break; |
| 343 | case QueueParams.METHOD_UPDATE: |
| 344 | onUpdate((List<Call>) params.mArg); |
| 345 | break; |
| 346 | case QueueParams.METHOD_DISCONNECT: |
| 347 | onDisconnect((Call) params.mArg); |
| 348 | break; |
| 349 | default: |
| 350 | throw new IllegalArgumentException("Method type " + params.mMethod + |
| 351 | " not recognized."); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 352 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 353 | } |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame^] | 354 | mQueue.clear(); |
| 355 | mQueue = null; |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame^] | 358 | /** |
| 359 | * Holds method parameters. |
| 360 | */ |
| 361 | private static class QueueParams { |
| 362 | private static final int METHOD_INCOMING = 1; |
| 363 | private static final int METHOD_UPDATE = 2; |
| 364 | private static final int METHOD_DISCONNECT = 3; |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 365 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame^] | 366 | private final int mMethod; |
| 367 | private final Object mArg; |
| 368 | |
| 369 | private QueueParams(int method, Object arg) { |
| 370 | mMethod = method; |
| 371 | this.mArg = arg; |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 374 | } |