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 | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 56 | private ICallHandlerService mCallHandlerServiceGuarded; // Guarded by mServiceAndQueueLock |
| 57 | private List<Call> mIncomingCallQueueGuarded; // Guarded by mServiceAndQueueLock |
| 58 | private List<List<Call>> mUpdateCallQueueGuarded; // Guarded by mServiceAndQueueLock |
| 59 | private List<Call> mDisconnectCallQueueGuarded; // Guarded by mServiceAndQueueLock |
| 60 | private final Object mServiceAndQueueLock = new Object(); |
| 61 | private int mBindRetryCount = 0; |
| 62 | |
| 63 | @Override |
| 64 | public void handleMessage(Message msg) { |
| 65 | super.handleMessage(msg); |
| 66 | |
| 67 | switch (msg.what) { |
| 68 | case BIND_RETRY_MSG: |
| 69 | setupServiceConnection(); |
| 70 | break; |
| 71 | } |
| 72 | } |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 73 | |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 74 | public CallHandlerServiceProxy(Context context, CallModeler callModeler, |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 75 | CallCommandService callCommandService, AudioRouter audioRouter) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 76 | if (DBG) { |
| 77 | Log.d(TAG, "init CallHandlerServiceProxy"); |
| 78 | } |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 79 | mContext = context; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 80 | mCallCommandService = callCommandService; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 81 | mCallModeler = callModeler; |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 82 | mAudioRouter = audioRouter; |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 83 | |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 84 | mAudioRouter.addAudioModeListener(this); |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 85 | mCallModeler.addListener(this); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 86 | |
| 87 | setupServiceConnection(); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 88 | } |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 89 | |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 90 | @Override |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 91 | public void onDisconnect(Call call) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 92 | try { |
| 93 | synchronized (mServiceAndQueueLock) { |
| 94 | if (mCallHandlerServiceGuarded == null) { |
| 95 | if (DBG) { |
| 96 | Log.d(TAG, "CallHandlerService not connected. Enqueue disconnect"); |
| 97 | } |
| 98 | enqueueDisconnect(call); |
| 99 | return; |
| 100 | } |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 101 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 102 | if (DBG) { |
| 103 | Log.d(TAG, "onDisconnect: " + call); |
| 104 | } |
| 105 | mCallHandlerServiceGuarded.onDisconnect(call); |
| 106 | } catch (Exception e) { |
| 107 | Log.e(TAG, "Remote exception handling onDisconnect ", e); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 111 | @Override |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 112 | public void onIncoming(Call call) { |
| 113 | try { |
| 114 | synchronized (mServiceAndQueueLock) { |
| 115 | if (mCallHandlerServiceGuarded == null) { |
| 116 | if (DBG) { |
| 117 | Log.d(TAG, "CallHandlerService not connected. Enqueue incoming."); |
| 118 | } |
| 119 | enqueueIncoming(call); |
| 120 | return; |
| 121 | } |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 122 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 123 | if (DBG) { |
| 124 | Log.d(TAG, "onIncoming: " + call); |
| 125 | } |
| 126 | // TODO(klp): check RespondViaSmsManager.allowRespondViaSmsForCall() |
| 127 | // must refactor call method to accept proper call object. |
| 128 | mCallHandlerServiceGuarded.onIncoming(call, |
| 129 | RejectWithTextMessageManager.loadCannedResponses()); |
| 130 | } catch (Exception e) { |
| 131 | Log.e(TAG, "Remote exception handling onUpdate", e); |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | @Override |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 136 | public void onUpdate(List<Call> calls) { |
| 137 | try { |
| 138 | synchronized (mServiceAndQueueLock) { |
| 139 | if (mCallHandlerServiceGuarded == null) { |
| 140 | if (DBG) { |
| 141 | Log.d(TAG, "CallHandlerService not connected. Enqueue update."); |
| 142 | } |
| 143 | enqueueUpdate(calls); |
| 144 | return; |
| 145 | } |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 146 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 147 | |
| 148 | if (DBG) { |
| 149 | Log.d(TAG, "onUpdate: " + calls.toString()); |
| 150 | } |
| 151 | mCallHandlerServiceGuarded.onUpdate(calls); |
| 152 | } catch (Exception e) { |
| 153 | Log.e(TAG, "Remote exception handling onUpdate", e); |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 157 | @Override |
| 158 | public void onAudioModeChange(int previousMode, int newMode) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 159 | try { |
| 160 | synchronized (mServiceAndQueueLock) { |
| 161 | // TODO(klp): does this need to be enqueued? |
| 162 | if (mCallHandlerServiceGuarded == null) { |
| 163 | if (DBG) { |
| 164 | Log.d(TAG, "CallHandlerService not conneccted. Skipping " |
| 165 | + "onAudioModeChange()."); |
| 166 | } |
| 167 | return; |
| 168 | } |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 169 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 170 | |
| 171 | // Just do a simple log for now. |
| 172 | Log.i(TAG, "Updating with new audio mode: " + AudioMode.toString(newMode) + |
| 173 | " from " + AudioMode.toString(previousMode)); |
| 174 | |
| 175 | if (DBG) { |
| 176 | Log.d(TAG, "onSupportAudioModeChange"); |
| 177 | } |
| 178 | |
| 179 | mCallHandlerServiceGuarded.onAudioModeChange(newMode); |
| 180 | } catch (Exception e) { |
| 181 | Log.e(TAG, "Remote exception handling onAudioModeChange", e); |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 185 | @Override |
| 186 | public void onSupportedAudioModeChange(int modeMask) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 187 | try { |
| 188 | synchronized (mServiceAndQueueLock) { |
| 189 | // TODO(klp): does this need to be enqueued? |
| 190 | if (mCallHandlerServiceGuarded == null) { |
| 191 | if (DBG) { |
| 192 | Log.d(TAG, "CallHandlerService not conneccted. Skipping" |
| 193 | + "onSupportedAudioModeChange()."); |
| 194 | } |
| 195 | return; |
| 196 | } |
| 197 | } |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 198 | |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 199 | if (DBG) { |
| 200 | Log.d(TAG, "onSupportAudioModeChange: " + AudioMode.toString(modeMask)); |
| 201 | } |
| 202 | |
| 203 | mCallHandlerServiceGuarded.onSupportedAudioModeChange(modeMask); |
| 204 | } catch (Exception e) { |
| 205 | Log.e(TAG, "Remote exception handling onAudioModeChange", e); |
| 206 | } |
| 207 | |
| 208 | } |
| 209 | |
| 210 | private ServiceConnection mConnection = new ServiceConnection() { |
| 211 | @Override public void onServiceConnected (ComponentName className, IBinder service){ |
| 212 | if (DBG) { |
| 213 | Log.d(TAG, "Service Connected"); |
| 214 | } |
| 215 | onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service)); |
| 216 | } |
| 217 | |
| 218 | @Override public void onServiceDisconnected (ComponentName className){ |
| 219 | Log.i(TAG, "Disconnected from UI service."); |
| 220 | synchronized (mServiceAndQueueLock) { |
| 221 | mCallHandlerServiceGuarded = null; |
| 222 | |
| 223 | // Technically, unbindService is un-necessary since the framework will schedule and |
| 224 | // restart the crashed service. But there is a exponential backoff for the restart. |
| 225 | // Unbind explicitly and setup again to avoid the backoff since it's important to |
| 226 | // always have an in call ui. |
| 227 | mContext.unbindService(mConnection); |
| 228 | setupServiceConnection(); |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 233 | ; |
| 234 | |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 235 | /** |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 236 | * Sets up the connection with ICallHandlerService |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 237 | */ |
| 238 | private void setupServiceConnection() { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 239 | synchronized (mServiceAndQueueLock) { |
| 240 | if (mCallHandlerServiceGuarded == null) { |
| 241 | |
| 242 | |
| 243 | final Intent serviceIntent = new Intent(ICallHandlerService.class.getName()); |
| 244 | final ComponentName component = new ComponentName(mContext.getResources().getString( |
| 245 | R.string.incall_ui_default_package), mContext.getResources().getString( |
| 246 | R.string.incall_ui_default_class)); |
| 247 | serviceIntent.setComponent(component); |
| 248 | |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 249 | if (DBG) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 250 | Log.d(TAG, "binding to service " + serviceIntent); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 251 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 252 | if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) { |
| 253 | // This happens when the in-call package is in the middle of being installed. |
| 254 | // Delay the retry. |
| 255 | mBindRetryCount++; |
| 256 | if (mBindRetryCount < MAX_RETRY_COUNT) { |
| 257 | Log.e(TAG, "bindService failed on " + serviceIntent + ". Retrying in " + |
| 258 | RETRY_DELAY_MILLIS + " ms."); |
| 259 | sendMessageDelayed(Message.obtain(this, BIND_RETRY_MSG), |
| 260 | RETRY_DELAY_MILLIS); |
| 261 | } else { |
| 262 | Log.wtf(TAG, "Tried to bind to in-call UI " + MAX_RETRY_COUNT + " times." |
| 263 | + " Giving up."); |
| 264 | } |
| 265 | } |
Santos Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 271 | * Called when the in-call UI service is connected. Send command interface to in-call. |
| 272 | */ |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 273 | private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 274 | synchronized (mServiceAndQueueLock) { |
| 275 | mCallHandlerServiceGuarded = callHandlerService; |
| 276 | |
| 277 | // TODO(klp): combine queues into a single ordered queue. |
| 278 | processIncomingCallQueue(); |
| 279 | processUpdateCallQueue(); |
| 280 | processDisconnectQueue(); |
| 281 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 282 | |
| 283 | try { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 284 | mCallHandlerServiceGuarded.setCallCommandService(mCallCommandService); |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 285 | } catch (RemoteException e) { |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 286 | Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame^] | 289 | |
| 290 | |
| 291 | private void enqueueDisconnect(Call call) { |
| 292 | if (mDisconnectCallQueueGuarded == null) { |
| 293 | mDisconnectCallQueueGuarded = Lists.newArrayList(); |
| 294 | } |
| 295 | mDisconnectCallQueueGuarded.add(new Call(call)); |
| 296 | } |
| 297 | |
| 298 | private void enqueueIncoming(Call call) { |
| 299 | if (mIncomingCallQueueGuarded == null) { |
| 300 | mIncomingCallQueueGuarded = Lists.newArrayList(); |
| 301 | } |
| 302 | mIncomingCallQueueGuarded.add(new Call(call)); |
| 303 | } |
| 304 | |
| 305 | private void enqueueUpdate(List<Call> calls) { |
| 306 | if (mUpdateCallQueueGuarded == null) { |
| 307 | mUpdateCallQueueGuarded = Lists.newArrayList(); |
| 308 | } |
| 309 | final List<Call> copy = Lists.newArrayList(); |
| 310 | for (Call call : calls) { |
| 311 | copy.add(new Call(call)); |
| 312 | } |
| 313 | mUpdateCallQueueGuarded.add(copy); |
| 314 | } |
| 315 | |
| 316 | private void processDisconnectQueue() { |
| 317 | if (mDisconnectCallQueueGuarded != null) { |
| 318 | for (Call call : mDisconnectCallQueueGuarded) { |
| 319 | onDisconnect(call); |
| 320 | } |
| 321 | mDisconnectCallQueueGuarded.clear(); |
| 322 | mDisconnectCallQueueGuarded = null; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | private void processIncomingCallQueue() { |
| 327 | if (mIncomingCallQueueGuarded != null) { |
| 328 | for (Call call : mIncomingCallQueueGuarded) { |
| 329 | onIncoming(call); |
| 330 | } |
| 331 | mIncomingCallQueueGuarded.clear(); |
| 332 | mIncomingCallQueueGuarded = null; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | private void processUpdateCallQueue() { |
| 337 | if (mUpdateCallQueueGuarded != null) { |
| 338 | for (List<Call> calls : mUpdateCallQueueGuarded) { |
| 339 | onUpdate(calls); |
| 340 | } |
| 341 | mUpdateCallQueueGuarded.clear(); |
| 342 | mUpdateCallQueueGuarded = null; |
| 343 | } |
| 344 | } |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 345 | } |