blob: 572bdae58369c97d7ebc1974a4a2324fcc8fcc6d [file] [log] [blame]
Santos Cordon89647a62013-07-16 13:38:09 -07001/*
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
17package com.android.phone;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
Santos Cordon89647a62013-07-16 13:38:09 -070023import android.os.Handler;
24import android.os.IBinder;
25import android.os.Message;
26import android.os.RemoteException;
27import android.os.SystemProperties;
28import android.util.Log;
29
Santos Cordon9b7bac72013-08-06 08:04:52 -070030import com.android.phone.AudioRouter.AudioModeListener;
31import com.android.services.telephony.common.AudioMode;
Santos Cordonf4046882013-07-25 18:49:27 -070032import com.android.services.telephony.common.Call;
Santos Cordon345350e2013-07-19 17:16:14 -070033import com.android.services.telephony.common.ICallHandlerService;
Chiao Cheng6c6b2722013-08-22 18:35:54 -070034import com.google.common.collect.Lists;
Santos Cordon89647a62013-07-16 13:38:09 -070035
Santos Cordona3d05142013-07-29 11:25:17 -070036import java.util.List;
37
Santos Cordon89647a62013-07-16 13:38:09 -070038/**
Santos Cordon345350e2013-07-19 17:16:14 -070039 * This class is responsible for passing through call state changes to the CallHandlerService.
Santos Cordon89647a62013-07-16 13:38:09 -070040 */
Chiao Cheng6c6b2722013-08-22 18:35:54 -070041public class CallHandlerServiceProxy extends Handler
42 implements CallModeler.Listener, AudioModeListener {
Santos Cordon89647a62013-07-16 13:38:09 -070043
Santos Cordon345350e2013-07-19 17:16:14 -070044 private static final String TAG = CallHandlerServiceProxy.class.getSimpleName();
Chiao Cheng6c6b2722013-08-22 18:35:54 -070045 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt(
46 "ro.debuggable", 0) == 1);
Chiao Chenge41661c2013-07-23 13:28:26 -070047
Chiao Cheng6c6b2722013-08-22 18:35:54 -070048 public static final int RETRY_DELAY_MILLIS = 2000;
49 private static final int BIND_RETRY_MSG = 1;
50 private static final int MAX_RETRY_COUNT = 5;
Santos Cordon89647a62013-07-16 13:38:09 -070051
Santos Cordon593ab382013-08-06 21:58:23 -070052 private AudioRouter mAudioRouter;
Santos Cordoncba1b442013-07-18 12:43:58 -070053 private CallCommandService mCallCommandService;
Santos Cordon593ab382013-08-06 21:58:23 -070054 private CallModeler mCallModeler;
55 private Context mContext;
Chiao Cheng6c6b2722013-08-22 18:35:54 -070056 private ICallHandlerService mCallHandlerServiceGuarded; // Guarded by mServiceAndQueueLock
57 private List<Call> mIncomingCallQueueGuarded; // Guarded by mServiceAndQueueLock
58 private List<List<Call>> mUpdateCallQueueGuarded; // Guarded by mServiceAndQueueLock
59 private List<Call> mDisconnectCallQueueGuarded; // Guarded by mServiceAndQueueLock
60 private final Object mServiceAndQueueLock = new Object();
61 private int mBindRetryCount = 0;
62
63 @Override
64 public void handleMessage(Message msg) {
65 super.handleMessage(msg);
66
67 switch (msg.what) {
68 case BIND_RETRY_MSG:
69 setupServiceConnection();
70 break;
71 }
72 }
Santos Cordon89647a62013-07-16 13:38:09 -070073
Santos Cordon63a84242013-07-23 13:32:52 -070074 public CallHandlerServiceProxy(Context context, CallModeler callModeler,
Santos Cordon593ab382013-08-06 21:58:23 -070075 CallCommandService callCommandService, AudioRouter audioRouter) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -070076 if (DBG) {
77 Log.d(TAG, "init CallHandlerServiceProxy");
78 }
Santos Cordon89647a62013-07-16 13:38:09 -070079 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070080 mCallCommandService = callCommandService;
Santos Cordon63a84242013-07-23 13:32:52 -070081 mCallModeler = callModeler;
Santos Cordon593ab382013-08-06 21:58:23 -070082 mAudioRouter = audioRouter;
Santos Cordon89647a62013-07-16 13:38:09 -070083
Santos Cordon593ab382013-08-06 21:58:23 -070084 mAudioRouter.addAudioModeListener(this);
Christine Chendaf7bf62013-08-05 19:12:31 -070085 mCallModeler.addListener(this);
Chiao Cheng6c6b2722013-08-22 18:35:54 -070086
87 setupServiceConnection();
Santos Cordon63a84242013-07-23 13:32:52 -070088 }
Santos Cordon89647a62013-07-16 13:38:09 -070089
Santos Cordon63a84242013-07-23 13:32:52 -070090 @Override
Santos Cordon995c8162013-07-29 09:22:22 -070091 public void onDisconnect(Call call) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -070092 try {
93 synchronized (mServiceAndQueueLock) {
94 if (mCallHandlerServiceGuarded == null) {
95 if (DBG) {
96 Log.d(TAG, "CallHandlerService not connected. Enqueue disconnect");
97 }
98 enqueueDisconnect(call);
99 return;
100 }
Santos Cordon63a84242013-07-23 13:32:52 -0700101 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700102 if (DBG) {
103 Log.d(TAG, "onDisconnect: " + call);
104 }
105 mCallHandlerServiceGuarded.onDisconnect(call);
106 } catch (Exception e) {
107 Log.e(TAG, "Remote exception handling onDisconnect ", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700108 }
109 }
110
Santos Cordona3d05142013-07-29 11:25:17 -0700111 @Override
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700112 public void onIncoming(Call call) {
113 try {
114 synchronized (mServiceAndQueueLock) {
115 if (mCallHandlerServiceGuarded == null) {
116 if (DBG) {
117 Log.d(TAG, "CallHandlerService not connected. Enqueue incoming.");
118 }
119 enqueueIncoming(call);
120 return;
121 }
Christine Chenee09a492013-08-06 16:02:29 -0700122 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700123 if (DBG) {
124 Log.d(TAG, "onIncoming: " + call);
125 }
126 // TODO(klp): check RespondViaSmsManager.allowRespondViaSmsForCall()
127 // must refactor call method to accept proper call object.
128 mCallHandlerServiceGuarded.onIncoming(call,
129 RejectWithTextMessageManager.loadCannedResponses());
130 } catch (Exception e) {
131 Log.e(TAG, "Remote exception handling onUpdate", e);
Christine Chenee09a492013-08-06 16:02:29 -0700132 }
133 }
134
135 @Override
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700136 public void onUpdate(List<Call> calls) {
137 try {
138 synchronized (mServiceAndQueueLock) {
139 if (mCallHandlerServiceGuarded == null) {
140 if (DBG) {
141 Log.d(TAG, "CallHandlerService not connected. Enqueue update.");
142 }
143 enqueueUpdate(calls);
144 return;
145 }
Santos Cordona3d05142013-07-29 11:25:17 -0700146 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700147
148 if (DBG) {
149 Log.d(TAG, "onUpdate: " + calls.toString());
150 }
151 mCallHandlerServiceGuarded.onUpdate(calls);
152 } catch (Exception e) {
153 Log.e(TAG, "Remote exception handling onUpdate", e);
Santos Cordona3d05142013-07-29 11:25:17 -0700154 }
155 }
156
Santos Cordon9b7bac72013-08-06 08:04:52 -0700157 @Override
Santos Cordoncd95f622013-08-29 03:38:52 -0700158 public void onAudioModeChange(int newMode, boolean muted) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700159 try {
160 synchronized (mServiceAndQueueLock) {
161 // TODO(klp): does this need to be enqueued?
162 if (mCallHandlerServiceGuarded == null) {
163 if (DBG) {
164 Log.d(TAG, "CallHandlerService not conneccted. Skipping "
165 + "onAudioModeChange().");
166 }
167 return;
168 }
Santos Cordon9b7bac72013-08-06 08:04:52 -0700169 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700170
171 // Just do a simple log for now.
172 Log.i(TAG, "Updating with new audio mode: " + AudioMode.toString(newMode) +
Santos Cordoncd95f622013-08-29 03:38:52 -0700173 " with mute " + muted);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700174
175 if (DBG) {
176 Log.d(TAG, "onSupportAudioModeChange");
177 }
178
Santos Cordoncd95f622013-08-29 03:38:52 -0700179 mCallHandlerServiceGuarded.onAudioModeChange(newMode, muted);
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700180 } catch (Exception e) {
181 Log.e(TAG, "Remote exception handling onAudioModeChange", e);
Santos Cordon9b7bac72013-08-06 08:04:52 -0700182 }
183 }
184
Santos Cordon593ab382013-08-06 21:58:23 -0700185 @Override
186 public void onSupportedAudioModeChange(int modeMask) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700187 try {
188 synchronized (mServiceAndQueueLock) {
189 // TODO(klp): does this need to be enqueued?
190 if (mCallHandlerServiceGuarded == null) {
191 if (DBG) {
192 Log.d(TAG, "CallHandlerService not conneccted. Skipping"
193 + "onSupportedAudioModeChange().");
194 }
195 return;
196 }
197 }
Santos Cordon593ab382013-08-06 21:58:23 -0700198
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700199 if (DBG) {
200 Log.d(TAG, "onSupportAudioModeChange: " + AudioMode.toString(modeMask));
201 }
202
203 mCallHandlerServiceGuarded.onSupportedAudioModeChange(modeMask);
204 } catch (Exception e) {
205 Log.e(TAG, "Remote exception handling onAudioModeChange", e);
206 }
207
208 }
209
210 private ServiceConnection mConnection = new ServiceConnection() {
211 @Override public void onServiceConnected (ComponentName className, IBinder service){
212 if (DBG) {
213 Log.d(TAG, "Service Connected");
214 }
215 onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service));
216 }
217
218 @Override public void onServiceDisconnected (ComponentName className){
219 Log.i(TAG, "Disconnected from UI service.");
220 synchronized (mServiceAndQueueLock) {
221 mCallHandlerServiceGuarded = null;
222
223 // Technically, unbindService is un-necessary since the framework will schedule and
224 // restart the crashed service. But there is a exponential backoff for the restart.
225 // Unbind explicitly and setup again to avoid the backoff since it's important to
226 // always have an in call ui.
227 mContext.unbindService(mConnection);
228 setupServiceConnection();
Santos Cordon593ab382013-08-06 21:58:23 -0700229 }
230 }
Santos Cordon19d814b2013-08-28 14:58:17 -0700231 };
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700232
Santos Cordon406c0342013-08-28 00:07:47 -0700233 public void bringToForeground() {
234 // only support this call if the service is already connected.
Santos Cordon19d814b2013-08-28 14:58:17 -0700235 synchronized (mServiceAndQueueLock) {
236 if (mCallHandlerServiceGuarded != null && mCallModeler.hasLiveCall()) {
237 try {
238 if (DBG) Log.d(TAG, "bringToForeground");
239 mCallHandlerServiceGuarded.bringToForeground();
240 } catch (RemoteException e) {
241 Log.e(TAG, "Exception handling bringToForeground", e);
242 }
Santos Cordon406c0342013-08-28 00:07:47 -0700243 }
244 }
245 }
246
Santos Cordon89647a62013-07-16 13:38:09 -0700247 /**
Santos Cordon345350e2013-07-19 17:16:14 -0700248 * Sets up the connection with ICallHandlerService
Santos Cordon89647a62013-07-16 13:38:09 -0700249 */
250 private void setupServiceConnection() {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700251 synchronized (mServiceAndQueueLock) {
252 if (mCallHandlerServiceGuarded == null) {
253
254
255 final Intent serviceIntent = new Intent(ICallHandlerService.class.getName());
256 final ComponentName component = new ComponentName(mContext.getResources().getString(
257 R.string.incall_ui_default_package), mContext.getResources().getString(
258 R.string.incall_ui_default_class));
259 serviceIntent.setComponent(component);
260
Santos Cordon89647a62013-07-16 13:38:09 -0700261 if (DBG) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700262 Log.d(TAG, "binding to service " + serviceIntent);
Santos Cordon89647a62013-07-16 13:38:09 -0700263 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700264 if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
265 // This happens when the in-call package is in the middle of being installed.
266 // Delay the retry.
267 mBindRetryCount++;
268 if (mBindRetryCount < MAX_RETRY_COUNT) {
269 Log.e(TAG, "bindService failed on " + serviceIntent + ". Retrying in " +
270 RETRY_DELAY_MILLIS + " ms.");
271 sendMessageDelayed(Message.obtain(this, BIND_RETRY_MSG),
272 RETRY_DELAY_MILLIS);
273 } else {
274 Log.wtf(TAG, "Tried to bind to in-call UI " + MAX_RETRY_COUNT + " times."
275 + " Giving up.");
276 }
277 }
Santos Cordonaf763a12013-08-19 20:04:58 -0700278 }
279 }
280 }
281
282 /**
Santos Cordoncba1b442013-07-18 12:43:58 -0700283 * Called when the in-call UI service is connected. Send command interface to in-call.
284 */
Santos Cordon63a84242013-07-23 13:32:52 -0700285 private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700286 synchronized (mServiceAndQueueLock) {
287 mCallHandlerServiceGuarded = callHandlerService;
288
289 // TODO(klp): combine queues into a single ordered queue.
290 processIncomingCallQueue();
291 processUpdateCallQueue();
292 processDisconnectQueue();
293 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700294
295 try {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700296 mCallHandlerServiceGuarded.setCallCommandService(mCallCommandService);
Santos Cordoncba1b442013-07-18 12:43:58 -0700297 } catch (RemoteException e) {
Santos Cordon63a84242013-07-23 13:32:52 -0700298 Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700299 }
300 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700301
302
303 private void enqueueDisconnect(Call call) {
304 if (mDisconnectCallQueueGuarded == null) {
305 mDisconnectCallQueueGuarded = Lists.newArrayList();
306 }
307 mDisconnectCallQueueGuarded.add(new Call(call));
308 }
309
310 private void enqueueIncoming(Call call) {
311 if (mIncomingCallQueueGuarded == null) {
312 mIncomingCallQueueGuarded = Lists.newArrayList();
313 }
314 mIncomingCallQueueGuarded.add(new Call(call));
315 }
316
317 private void enqueueUpdate(List<Call> calls) {
318 if (mUpdateCallQueueGuarded == null) {
319 mUpdateCallQueueGuarded = Lists.newArrayList();
320 }
321 final List<Call> copy = Lists.newArrayList();
322 for (Call call : calls) {
323 copy.add(new Call(call));
324 }
325 mUpdateCallQueueGuarded.add(copy);
326 }
327
328 private void processDisconnectQueue() {
329 if (mDisconnectCallQueueGuarded != null) {
330 for (Call call : mDisconnectCallQueueGuarded) {
331 onDisconnect(call);
332 }
333 mDisconnectCallQueueGuarded.clear();
334 mDisconnectCallQueueGuarded = null;
335 }
336 }
337
338 private void processIncomingCallQueue() {
339 if (mIncomingCallQueueGuarded != null) {
340 for (Call call : mIncomingCallQueueGuarded) {
341 onIncoming(call);
342 }
343 mIncomingCallQueueGuarded.clear();
344 mIncomingCallQueueGuarded = null;
345 }
346 }
347
348 private void processUpdateCallQueue() {
349 if (mUpdateCallQueueGuarded != null) {
350 for (List<Call> calls : mUpdateCallQueueGuarded) {
351 onUpdate(calls);
352 }
353 mUpdateCallQueueGuarded.clear();
354 mUpdateCallQueueGuarded = null;
355 }
356 }
Santos Cordon89647a62013-07-16 13:38:09 -0700357}