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(); |
Santos Cordon | 71d5c6e | 2013-09-05 21:34:33 -0700 | [diff] [blame] | 47 | private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 1) && (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 | |
Santos Cordon | cd95f62 | 2013-08-29 03:38:52 -0700 | [diff] [blame] | 232 | mCallHandlerServiceGuarded.onAudioModeChange(newMode, muted); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 233 | } catch (Exception e) { |
| 234 | Log.e(TAG, "Remote exception handling onAudioModeChange", e); |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 238 | @Override |
| 239 | public void onSupportedAudioModeChange(int modeMask) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 240 | try { |
| 241 | synchronized (mServiceAndQueueLock) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 242 | if (mCallHandlerServiceGuarded == null) { |
| 243 | if (DBG) { |
| 244 | Log.d(TAG, "CallHandlerService not conneccted. Skipping" |
| 245 | + "onSupportedAudioModeChange()."); |
| 246 | } |
| 247 | return; |
| 248 | } |
| 249 | } |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 250 | |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 251 | 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 Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 262 | private ServiceConnection mConnection = null; |
| 263 | |
| 264 | private class InCallServiceConnection implements ServiceConnection { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 265 | @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 Cheng | 11a4b65 | 2013-09-02 01:08:19 -0700 | [diff] [blame] | 270 | mBindRetryCount = 0; |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | @Override public void onServiceDisconnected (ComponentName className){ |
| 274 | Log.i(TAG, "Disconnected from UI service."); |
| 275 | synchronized (mServiceAndQueueLock) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 276 | // 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 Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 280 | unbind(); |
| 281 | |
| 282 | // TODO(klp): hang up all calls. |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 285 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 286 | |
Santos Cordon | 406c034 | 2013-08-28 00:07:47 -0700 | [diff] [blame] | 287 | public void bringToForeground() { |
| 288 | // only support this call if the service is already connected. |
Santos Cordon | 19d814b | 2013-08-28 14:58:17 -0700 | [diff] [blame] | 289 | 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 Cordon | 406c034 | 2013-08-28 00:07:47 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 301 | 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 Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 310 | /** |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 311 | * Sets up the connection with ICallHandlerService |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 312 | */ |
| 313 | private void setupServiceConnection() { |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 314 | if (!PhoneGlobals.sVoiceCapable) { |
| 315 | return; |
| 316 | } |
Chiao Cheng | 11a4b65 | 2013-09-02 01:08:19 -0700 | [diff] [blame] | 317 | |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 318 | final Intent serviceIntent = getInCallServiceIntent(mContext); |
Chiao Cheng | 11a4b65 | 2013-09-02 01:08:19 -0700 | [diff] [blame] | 319 | if (DBG) { |
| 320 | Log.d(TAG, "binding to service " + serviceIntent); |
| 321 | } |
| 322 | |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 323 | synchronized (mServiceAndQueueLock) { |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 324 | 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 Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 352 | if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) { |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 353 | // This happens when the in-call package is in the middle of being |
| 354 | // installed. |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 355 | // Delay the retry. |
| 356 | mBindRetryCount++; |
| 357 | if (mBindRetryCount < MAX_RETRY_COUNT) { |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 358 | Log.e(TAG, "bindService failed on " + serviceIntent + ". Retrying in " |
| 359 | + RETRY_DELAY_MILLIS + " ms."); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 360 | 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 Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 367 | |
| 368 | } else { |
| 369 | Log.d(TAG, "Service connection to in call service already started."); |
Santos Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 374 | 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 Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame] | 385 | /** |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 386 | * Called when the in-call UI service is connected. Send command interface to in-call. |
| 387 | */ |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 388 | private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) { |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 389 | |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 390 | synchronized (mServiceAndQueueLock) { |
| 391 | mCallHandlerServiceGuarded = callHandlerService; |
| 392 | |
Santos Cordon | ad07819 | 2013-08-28 15:14:54 -0700 | [diff] [blame] | 393 | // Before we send any updates, we need to set up the initial service calls. |
| 394 | makeInitialServiceCalls(); |
| 395 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 396 | processQueue(); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 397 | } |
Santos Cordon | ad07819 | 2013-08-28 15:14:54 -0700 | [diff] [blame] | 398 | } |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 399 | |
Santos Cordon | ad07819 | 2013-08-28 15:14:54 -0700 | [diff] [blame] | 400 | /** |
| 401 | * Makes initial service calls to set up callcommandservice and audio modes. |
| 402 | */ |
| 403 | private void makeInitialServiceCalls() { |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 404 | try { |
Santos Cordon | 12a03aa | 2013-09-12 23:34:05 -0700 | [diff] [blame^] | 405 | mCallHandlerServiceGuarded.startCallService(mCallCommandService); |
Santos Cordon | ad07819 | 2013-08-28 15:14:54 -0700 | [diff] [blame] | 406 | |
| 407 | onSupportedAudioModeChange(mAudioRouter.getSupportedAudioModes()); |
Santos Cordon | 8fd0ec7 | 2013-08-29 16:44:43 -0700 | [diff] [blame] | 408 | onAudioModeChange(mAudioRouter.getAudioMode(), mAudioRouter.getMute()); |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 409 | } catch (RemoteException e) { |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 410 | Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 413 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 414 | private List<QueueParams> getQueue() { |
| 415 | if (mQueue == null) { |
| 416 | mQueue = Lists.newArrayList(); |
| 417 | } |
| 418 | return mQueue; |
| 419 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 420 | |
| 421 | private void enqueueDisconnect(Call call) { |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 422 | getQueue().add(new QueueParams(QueueParams.METHOD_DISCONNECT, new Call(call))); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | private void enqueueIncoming(Call call) { |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 426 | getQueue().add(new QueueParams(QueueParams.METHOD_INCOMING, new Call(call))); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | private void enqueueUpdate(List<Call> calls) { |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 430 | final List<Call> copy = Lists.newArrayList(); |
| 431 | for (Call call : calls) { |
| 432 | copy.add(new Call(call)); |
| 433 | } |
Chiao Cheng | c340ba9 | 2013-08-30 13:04:46 -0700 | [diff] [blame] | 434 | getQueue().add(new QueueParams(QueueParams.METHOD_UPDATE, copy)); |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 437 | private void processQueue() { |
Chiao Cheng | 3e6486e | 2013-09-03 19:27:46 -0700 | [diff] [blame] | 438 | 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 Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 458 | } |
Chiao Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 462 | /** |
| 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 Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 469 | |
Chiao Cheng | d38eebc | 2013-08-28 14:38:14 -0700 | [diff] [blame] | 470 | 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 Cheng | 6c6b272 | 2013-08-22 18:35:54 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 478 | } |