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