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