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