blob: 75bb26cfb335097809c517bdf00d07007cbb8d16 [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
158 public void onAudioModeChange(int previousMode, int newMode) {
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) +
173 " from " + AudioMode.toString(previousMode));
174
175 if (DBG) {
176 Log.d(TAG, "onSupportAudioModeChange");
177 }
178
179 mCallHandlerServiceGuarded.onAudioModeChange(newMode);
180 } 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 }
231 }
232
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700233 ;
234
Santos Cordon89647a62013-07-16 13:38:09 -0700235 /**
Santos Cordon345350e2013-07-19 17:16:14 -0700236 * Sets up the connection with ICallHandlerService
Santos Cordon89647a62013-07-16 13:38:09 -0700237 */
238 private void setupServiceConnection() {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700239 synchronized (mServiceAndQueueLock) {
240 if (mCallHandlerServiceGuarded == null) {
241
242
243 final Intent serviceIntent = new Intent(ICallHandlerService.class.getName());
244 final ComponentName component = new ComponentName(mContext.getResources().getString(
245 R.string.incall_ui_default_package), mContext.getResources().getString(
246 R.string.incall_ui_default_class));
247 serviceIntent.setComponent(component);
248
Santos Cordon89647a62013-07-16 13:38:09 -0700249 if (DBG) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700250 Log.d(TAG, "binding to service " + serviceIntent);
Santos Cordon89647a62013-07-16 13:38:09 -0700251 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700252 if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
253 // This happens when the in-call package is in the middle of being installed.
254 // Delay the retry.
255 mBindRetryCount++;
256 if (mBindRetryCount < MAX_RETRY_COUNT) {
257 Log.e(TAG, "bindService failed on " + serviceIntent + ". Retrying in " +
258 RETRY_DELAY_MILLIS + " ms.");
259 sendMessageDelayed(Message.obtain(this, BIND_RETRY_MSG),
260 RETRY_DELAY_MILLIS);
261 } else {
262 Log.wtf(TAG, "Tried to bind to in-call UI " + MAX_RETRY_COUNT + " times."
263 + " Giving up.");
264 }
265 }
Santos Cordonaf763a12013-08-19 20:04:58 -0700266 }
267 }
268 }
269
270 /**
Santos Cordoncba1b442013-07-18 12:43:58 -0700271 * Called when the in-call UI service is connected. Send command interface to in-call.
272 */
Santos Cordon63a84242013-07-23 13:32:52 -0700273 private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700274 synchronized (mServiceAndQueueLock) {
275 mCallHandlerServiceGuarded = callHandlerService;
276
277 // TODO(klp): combine queues into a single ordered queue.
278 processIncomingCallQueue();
279 processUpdateCallQueue();
280 processDisconnectQueue();
281 }
Santos Cordoncba1b442013-07-18 12:43:58 -0700282
283 try {
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700284 mCallHandlerServiceGuarded.setCallCommandService(mCallCommandService);
Santos Cordoncba1b442013-07-18 12:43:58 -0700285 } catch (RemoteException e) {
Santos Cordon63a84242013-07-23 13:32:52 -0700286 Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700287 }
288 }
Chiao Cheng6c6b2722013-08-22 18:35:54 -0700289
290
291 private void enqueueDisconnect(Call call) {
292 if (mDisconnectCallQueueGuarded == null) {
293 mDisconnectCallQueueGuarded = Lists.newArrayList();
294 }
295 mDisconnectCallQueueGuarded.add(new Call(call));
296 }
297
298 private void enqueueIncoming(Call call) {
299 if (mIncomingCallQueueGuarded == null) {
300 mIncomingCallQueueGuarded = Lists.newArrayList();
301 }
302 mIncomingCallQueueGuarded.add(new Call(call));
303 }
304
305 private void enqueueUpdate(List<Call> calls) {
306 if (mUpdateCallQueueGuarded == null) {
307 mUpdateCallQueueGuarded = Lists.newArrayList();
308 }
309 final List<Call> copy = Lists.newArrayList();
310 for (Call call : calls) {
311 copy.add(new Call(call));
312 }
313 mUpdateCallQueueGuarded.add(copy);
314 }
315
316 private void processDisconnectQueue() {
317 if (mDisconnectCallQueueGuarded != null) {
318 for (Call call : mDisconnectCallQueueGuarded) {
319 onDisconnect(call);
320 }
321 mDisconnectCallQueueGuarded.clear();
322 mDisconnectCallQueueGuarded = null;
323 }
324 }
325
326 private void processIncomingCallQueue() {
327 if (mIncomingCallQueueGuarded != null) {
328 for (Call call : mIncomingCallQueueGuarded) {
329 onIncoming(call);
330 }
331 mIncomingCallQueueGuarded.clear();
332 mIncomingCallQueueGuarded = null;
333 }
334 }
335
336 private void processUpdateCallQueue() {
337 if (mUpdateCallQueueGuarded != null) {
338 for (List<Call> calls : mUpdateCallQueueGuarded) {
339 onUpdate(calls);
340 }
341 mUpdateCallQueueGuarded.clear();
342 mUpdateCallQueueGuarded = null;
343 }
344 }
Santos Cordon89647a62013-07-16 13:38:09 -0700345}