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 | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 47 | private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 0) && (SystemProperties.getInt( |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 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); |
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 | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 93 | synchronized (mServiceAndQueueLock) { |
| 94 | if (mCallHandlerServiceGuarded == null) { |
| 95 | if (DBG) { |
| 96 | Log.d(TAG, "CallHandlerService not connected. Enqueue disconnect"); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 97 | } |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 98 | enqueueDisconnect(call); |
| 99 | setupServiceConnection(); |
| 100 | return; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 101 | } |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 102 | } |
| 103 | processDisconnect(call); |
| 104 | } |
| 105 | |
| 106 | private void processDisconnect(Call call) { |
| 107 | try { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 108 | if (DBG) { |
| 109 | Log.d(TAG, "onDisconnect: " + call); |
| 110 | } |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 111 | synchronized (mServiceAndQueueLock) { |
| 112 | if (mCallHandlerServiceGuarded != null) { |
| 113 | mCallHandlerServiceGuarded.onDisconnect(call); |
| 114 | } |
| 115 | } |
| 116 | if (!mCallModeler.hasLiveCall()) { |
| 117 | unbind(); |
| 118 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 119 | } catch (Exception e) { |
| 120 | Log.e(TAG, "Remote exception handling onDisconnect ", e); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 124 | @Override |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 125 | public void onIncoming(Call call) { |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 126 | synchronized (mServiceAndQueueLock) { |
| 127 | if (mCallHandlerServiceGuarded == null) { |
| 128 | if (DBG) { |
| 129 | Log.d(TAG, "CallHandlerService not connected. Enqueue incoming."); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 130 | } |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 131 | enqueueIncoming(call); |
| 132 | setupServiceConnection(); |
| 133 | return; |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 134 | } |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 135 | } |
| 136 | processIncoming(call); |
| 137 | } |
| 138 | |
| 139 | private void processIncoming(Call call) { |
| 140 | if (DBG) { |
| 141 | Log.d(TAG, "onIncoming: " + call); |
| 142 | } |
| 143 | try { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 144 | // TODO(klp): check RespondViaSmsManager.allowRespondViaSmsForCall() |
| 145 | // must refactor call method to accept proper call object. |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 146 | synchronized (mServiceAndQueueLock) { |
| 147 | if (mCallHandlerServiceGuarded != null) { |
| 148 | mCallHandlerServiceGuarded.onIncoming(call, |
| 149 | RejectWithTextMessageManager.loadCannedResponses()); |
| 150 | } |
| 151 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 152 | } catch (Exception e) { |
| 153 | Log.e(TAG, "Remote exception handling onUpdate", e); |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | @Override |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 158 | public void onUpdate(List<Call> calls) { |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 159 | 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 Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 176 | try { |
| 177 | synchronized (mServiceAndQueueLock) { |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 178 | if (mCallHandlerServiceGuarded != null) { |
| 179 | mCallHandlerServiceGuarded.onUpdate(calls); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 180 | } |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 181 | } |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 182 | 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 Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 189 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 190 | } catch (Exception e) { |
| 191 | Log.e(TAG, "Remote exception handling onUpdate", e); |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Chiao Cheng | 3f015c9 | 2013-09-06 15:56:27 -0700 | [diff] [blame^] | 195 | |
| 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 Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 215 | @Override |
Santos Cordon | cd95f62 | 2013-08-29 03:38:52 -0700 | [diff] [blame] | 216 | public void onAudioModeChange(int newMode, boolean muted) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 217 | try { |
| 218 | synchronized (mServiceAndQueueLock) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 219 | if (mCallHandlerServiceGuarded == null) { |
| 220 | if (DBG) { |
| 221 | Log.d(TAG, "CallHandlerService not conneccted. Skipping " |
| 222 | + "onAudioModeChange()."); |
| 223 | } |
| 224 | return; |
| 225 | } |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 226 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 227 | |
| 228 | // Just do a simple log for now. |
| 229 | Log.i(TAG, "Updating with new audio mode: " + AudioMode.toString(newMode) + |
Santos Cordon | cd95f62 | 2013-08-29 03:38:52 -0700 | [diff] [blame] | 230 | " with mute " + muted); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 231 | |
| 232 | if (DBG) { |
| 233 | Log.d(TAG, "onSupportAudioModeChange"); |
| 234 | } |
| 235 | |
Santos Cordon | cd95f62 | 2013-08-29 03:38:52 -0700 | [diff] [blame] | 236 | mCallHandlerServiceGuarded.onAudioModeChange(newMode, muted); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 237 | } catch (Exception e) { |
| 238 | Log.e(TAG, "Remote exception handling onAudioModeChange", e); |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 242 | @Override |
| 243 | public void onSupportedAudioModeChange(int modeMask) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 244 | try { |
| 245 | synchronized (mServiceAndQueueLock) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 246 | if (mCallHandlerServiceGuarded == null) { |
| 247 | if (DBG) { |
| 248 | Log.d(TAG, "CallHandlerService not conneccted. Skipping" |
| 249 | + "onSupportedAudioModeChange()."); |
| 250 | } |
| 251 | return; |
| 252 | } |
| 253 | } |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 254 | |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 255 | if (DBG) { |
| 256 | Log.d(TAG, "onSupportAudioModeChange: " + AudioMode.toString(modeMask)); |
| 257 | } |
| 258 | |
| 259 | mCallHandlerServiceGuarded.onSupportedAudioModeChange(modeMask); |
| 260 | } catch (Exception e) { |
| 261 | Log.e(TAG, "Remote exception handling onAudioModeChange", e); |
| 262 | } |
| 263 | |
| 264 | } |
| 265 | |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 266 | private ServiceConnection mConnection = null; |
| 267 | |
| 268 | private class InCallServiceConnection implements ServiceConnection { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 269 | @Override public void onServiceConnected (ComponentName className, IBinder service){ |
| 270 | if (DBG) { |
| 271 | Log.d(TAG, "Service Connected"); |
| 272 | } |
| 273 | onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service)); |
Chiao Cheng | 11a4b65 | 2013-09-02 01:08:19 -0700 | [diff] [blame] | 274 | mBindRetryCount = 0; |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | @Override public void onServiceDisconnected (ComponentName className){ |
| 278 | Log.i(TAG, "Disconnected from UI service."); |
| 279 | synchronized (mServiceAndQueueLock) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 280 | // Technically, unbindService is un-necessary since the framework will schedule and |
| 281 | // restart the crashed service. But there is a exponential backoff for the restart. |
| 282 | // Unbind explicitly and setup again to avoid the backoff since it's important to |
| 283 | // always have an in call ui. |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 284 | unbind(); |
| 285 | |
| 286 | // TODO(klp): hang up all calls. |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 289 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 290 | |
Santos Cordon | 406c034 | 2013-08-28 00:07:47 -0700 | [diff] [blame] | 291 | public void bringToForeground() { |
| 292 | // only support this call if the service is already connected. |
Santos Cordon | 19d814b | 2013-08-28 14:58:17 -0700 | [diff] [blame] | 293 | synchronized (mServiceAndQueueLock) { |
| 294 | if (mCallHandlerServiceGuarded != null && mCallModeler.hasLiveCall()) { |
| 295 | try { |
| 296 | if (DBG) Log.d(TAG, "bringToForeground"); |
| 297 | mCallHandlerServiceGuarded.bringToForeground(); |
| 298 | } catch (RemoteException e) { |
| 299 | Log.e(TAG, "Exception handling bringToForeground", e); |
| 300 | } |
Santos Cordon | 406c034 | 2013-08-28 00:07:47 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 305 | private static Intent getInCallServiceIntent(Context context) { |
| 306 | final Intent serviceIntent = new Intent(ICallHandlerService.class.getName()); |
| 307 | final ComponentName component = new ComponentName(context.getResources().getString( |
| 308 | R.string.incall_ui_default_package), context.getResources().getString( |
| 309 | R.string.incall_ui_default_class)); |
| 310 | serviceIntent.setComponent(component); |
| 311 | return serviceIntent; |
| 312 | } |
| 313 | |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 314 | /** |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 315 | * Sets up the connection with ICallHandlerService |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 316 | */ |
| 317 | private void setupServiceConnection() { |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 318 | if (!PhoneGlobals.sVoiceCapable) { |
| 319 | return; |
| 320 | } |
Chiao Cheng | 11a4b65 | 2013-09-02 01:08:19 -0700 | [diff] [blame] | 321 | |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 322 | final Intent serviceIntent = getInCallServiceIntent(mContext); |
Chiao Cheng | 11a4b65 | 2013-09-02 01:08:19 -0700 | [diff] [blame] | 323 | if (DBG) { |
| 324 | Log.d(TAG, "binding to service " + serviceIntent); |
| 325 | } |
| 326 | |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 327 | synchronized (mServiceAndQueueLock) { |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 328 | if (mConnection == null) { |
| 329 | mConnection = new InCallServiceConnection(); |
| 330 | |
| 331 | final PackageManager packageManger = mContext.getPackageManager(); |
| 332 | final List<ResolveInfo> services = packageManger.queryIntentServices(serviceIntent, |
| 333 | 0); |
| 334 | if (services.size() == 0) { |
| 335 | // Service not found, retry again after some delay |
| 336 | // This can happen if the service is being installed by the package manager. |
| 337 | // Between deletes and installs, bindService could get a silent service not |
| 338 | // found error. |
| 339 | mBindRetryCount++; |
| 340 | if (mBindRetryCount < MAX_RETRY_COUNT) { |
| 341 | Log.w(TAG, "InCallUI service not found. " + serviceIntent |
| 342 | + ". This happens if the service is being installed and should be" |
| 343 | + " transient. Retrying" + RETRY_DELAY_MILLIS + " ms."); |
| 344 | sendMessageDelayed(Message.obtain(this, BIND_RETRY_MSG), |
| 345 | RETRY_DELAY_MILLIS); |
| 346 | } else { |
| 347 | Log.e(TAG, "Tried to bind to in-call UI " + MAX_RETRY_COUNT + " times." |
| 348 | + " Giving up."); |
| 349 | } |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | if (DBG) { |
| 354 | Log.d(TAG, "binding to service " + serviceIntent); |
| 355 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 356 | if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) { |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 357 | // This happens when the in-call package is in the middle of being |
| 358 | // installed. |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 359 | // Delay the retry. |
| 360 | mBindRetryCount++; |
| 361 | if (mBindRetryCount < MAX_RETRY_COUNT) { |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 362 | Log.e(TAG, "bindService failed on " + serviceIntent + ". Retrying in " |
| 363 | + RETRY_DELAY_MILLIS + " ms."); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 364 | sendMessageDelayed(Message.obtain(this, BIND_RETRY_MSG), |
| 365 | RETRY_DELAY_MILLIS); |
| 366 | } else { |
| 367 | Log.wtf(TAG, "Tried to bind to in-call UI " + MAX_RETRY_COUNT + " times." |
| 368 | + " Giving up."); |
| 369 | } |
| 370 | } |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 371 | |
| 372 | } else { |
| 373 | Log.d(TAG, "Service connection to in call service already started."); |
Santos Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 378 | private void unbind() { |
| 379 | synchronized (mServiceAndQueueLock) { |
| 380 | if (mCallHandlerServiceGuarded != null) { |
| 381 | Log.d(TAG, "Unbinding service."); |
| 382 | mCallHandlerServiceGuarded = null; |
| 383 | mContext.unbindService(mConnection); |
| 384 | } |
| 385 | mConnection = null; |
| 386 | } |
| 387 | } |
| 388 | |
Santos Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame] | 389 | /** |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 390 | * Called when the in-call UI service is connected. Send command interface to in-call. |
| 391 | */ |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 392 | private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) { |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 393 | |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 394 | synchronized (mServiceAndQueueLock) { |
| 395 | mCallHandlerServiceGuarded = callHandlerService; |
| 396 | |
Santos Cordon | ad07819 | 2013-08-28 15:14:54 -0700 | [diff] [blame] | 397 | // Before we send any updates, we need to set up the initial service calls. |
| 398 | makeInitialServiceCalls(); |
| 399 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 400 | processQueue(); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 401 | } |
Santos Cordon | ad07819 | 2013-08-28 15:14:54 -0700 | [diff] [blame] | 402 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 403 | |
Santos Cordon | ad07819 | 2013-08-28 15:14:54 -0700 | [diff] [blame] | 404 | /** |
| 405 | * Makes initial service calls to set up callcommandservice and audio modes. |
| 406 | */ |
| 407 | private void makeInitialServiceCalls() { |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 408 | try { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 409 | mCallHandlerServiceGuarded.setCallCommandService(mCallCommandService); |
Santos Cordon | ad07819 | 2013-08-28 15:14:54 -0700 | [diff] [blame] | 410 | |
| 411 | onSupportedAudioModeChange(mAudioRouter.getSupportedAudioModes()); |
Santos Cordon | 8fd0ec7 | 2013-08-29 16:44:43 -0700 | [diff] [blame] | 412 | onAudioModeChange(mAudioRouter.getAudioMode(), mAudioRouter.getMute()); |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 413 | } catch (RemoteException e) { |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 414 | Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 417 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 418 | private List<QueueParams> getQueue() { |
| 419 | if (mQueue == null) { |
| 420 | mQueue = Lists.newArrayList(); |
| 421 | } |
| 422 | return mQueue; |
| 423 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 424 | |
| 425 | private void enqueueDisconnect(Call call) { |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 426 | getQueue().add(new QueueParams(QueueParams.METHOD_DISCONNECT, new Call(call))); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | private void enqueueIncoming(Call call) { |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 430 | getQueue().add(new QueueParams(QueueParams.METHOD_INCOMING, new Call(call))); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | private void enqueueUpdate(List<Call> calls) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 434 | final List<Call> copy = Lists.newArrayList(); |
| 435 | for (Call call : calls) { |
| 436 | copy.add(new Call(call)); |
| 437 | } |
Chiao Cheng | c340ba9 | 2013-08-30 13:04:46 -0700 | [diff] [blame] | 438 | getQueue().add(new QueueParams(QueueParams.METHOD_UPDATE, copy)); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 441 | private void processQueue() { |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 442 | synchronized (mServiceAndQueueLock) { |
| 443 | if (mQueue != null) { |
| 444 | for (QueueParams params : mQueue) { |
| 445 | switch (params.mMethod) { |
| 446 | case QueueParams.METHOD_INCOMING: |
| 447 | processIncoming((Call) params.mArg); |
| 448 | break; |
| 449 | case QueueParams.METHOD_UPDATE: |
| 450 | processUpdate((List<Call>) params.mArg); |
| 451 | break; |
| 452 | case QueueParams.METHOD_DISCONNECT: |
| 453 | processDisconnect((Call) params.mArg); |
| 454 | break; |
| 455 | default: |
| 456 | throw new IllegalArgumentException("Method type " + params.mMethod + |
| 457 | " not recognized."); |
| 458 | } |
| 459 | } |
| 460 | mQueue.clear(); |
| 461 | mQueue = null; |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 462 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 466 | /** |
| 467 | * Holds method parameters. |
| 468 | */ |
| 469 | private static class QueueParams { |
| 470 | private static final int METHOD_INCOMING = 1; |
| 471 | private static final int METHOD_UPDATE = 2; |
| 472 | private static final int METHOD_DISCONNECT = 3; |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 473 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 474 | private final int mMethod; |
| 475 | private final Object mArg; |
| 476 | |
| 477 | private QueueParams(int method, Object arg) { |
| 478 | mMethod = method; |
| 479 | this.mArg = arg; |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 482 | } |