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