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