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