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