blob: b1343109d20a7e46258d8fa3f75e6c0b34b9cc5d [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;
23import android.os.AsyncResult;
24import android.os.Handler;
25import android.os.IBinder;
26import android.os.Message;
27import android.os.RemoteException;
28import android.os.SystemProperties;
29import android.util.Log;
30
Santos Cordonf4046882013-07-25 18:49:27 -070031import com.android.services.telephony.common.Call;
Santos Cordon345350e2013-07-19 17:16:14 -070032import com.android.services.telephony.common.ICallHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -070033import com.android.services.telephony.common.ICallCommandService;
Santos Cordon89647a62013-07-16 13:38:09 -070034
Santos Cordona3d05142013-07-29 11:25:17 -070035import java.util.List;
36
Santos Cordon89647a62013-07-16 13:38:09 -070037/**
Santos Cordon345350e2013-07-19 17:16:14 -070038 * This class is responsible for passing through call state changes to the CallHandlerService.
Santos Cordon89647a62013-07-16 13:38:09 -070039 */
Santos Cordon63a84242013-07-23 13:32:52 -070040public class CallHandlerServiceProxy extends Handler implements CallModeler.Listener {
Santos Cordon89647a62013-07-16 13:38:09 -070041
Santos Cordon345350e2013-07-19 17:16:14 -070042 private static final String TAG = CallHandlerServiceProxy.class.getSimpleName();
Santos Cordon89647a62013-07-16 13:38:09 -070043 private static final boolean DBG =
44 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
Chiao Chenge41661c2013-07-23 13:28:26 -070045
Santos Cordon89647a62013-07-16 13:38:09 -070046
47 private Context mContext;
Santos Cordon63a84242013-07-23 13:32:52 -070048 private CallModeler mCallModeler;
Santos Cordon89647a62013-07-16 13:38:09 -070049 private ServiceConnection mConnection;
Santos Cordon345350e2013-07-19 17:16:14 -070050 private ICallHandlerService mCallHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -070051 private CallCommandService mCallCommandService;
Santos Cordon89647a62013-07-16 13:38:09 -070052
Santos Cordon63a84242013-07-23 13:32:52 -070053 public CallHandlerServiceProxy(Context context, CallModeler callModeler,
Santos Cordoncba1b442013-07-18 12:43:58 -070054 CallCommandService callCommandService) {
Santos Cordon89647a62013-07-16 13:38:09 -070055 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070056 mCallCommandService = callCommandService;
Santos Cordon63a84242013-07-23 13:32:52 -070057 mCallModeler = callModeler;
Santos Cordon89647a62013-07-16 13:38:09 -070058
Santos Cordon89647a62013-07-16 13:38:09 -070059 setupServiceConnection();
Christine Chendaf7bf62013-08-05 19:12:31 -070060 mCallModeler.addListener(this);
Santos Cordon89647a62013-07-16 13:38:09 -070061
Santos Cordon998f42b2013-08-02 16:13:12 -070062 // start the whole process
63 onUpdate(mCallModeler.getFullList(), true);
Santos Cordon63a84242013-07-23 13:32:52 -070064 }
Santos Cordon89647a62013-07-16 13:38:09 -070065
Santos Cordon63a84242013-07-23 13:32:52 -070066 @Override
Santos Cordon995c8162013-07-29 09:22:22 -070067 public void onDisconnect(Call call) {
Santos Cordon63a84242013-07-23 13:32:52 -070068 if (mCallHandlerService != null) {
69 try {
Santos Cordon995c8162013-07-29 09:22:22 -070070 mCallHandlerService.onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -070071 } catch (RemoteException e) {
72 Log.e(TAG, "Remote exception handling onDisconnect ", e);
73 }
Santos Cordon89647a62013-07-16 13:38:09 -070074 }
75 }
76
Santos Cordona3d05142013-07-29 11:25:17 -070077 @Override
Santos Cordon998f42b2013-08-02 16:13:12 -070078 public void onUpdate(List<Call> calls, boolean fullUpdate) {
Santos Cordona3d05142013-07-29 11:25:17 -070079 if (mCallHandlerService != null) {
80 try {
Santos Cordon998f42b2013-08-02 16:13:12 -070081 mCallHandlerService.onUpdate(calls, fullUpdate);
Santos Cordona3d05142013-07-29 11:25:17 -070082 } catch (RemoteException e) {
83 Log.e(TAG, "Remote exception handling onUpdate", e);
84 }
85 }
86 }
87
Santos Cordon89647a62013-07-16 13:38:09 -070088 /**
Santos Cordon345350e2013-07-19 17:16:14 -070089 * Sets up the connection with ICallHandlerService
Santos Cordon89647a62013-07-16 13:38:09 -070090 */
91 private void setupServiceConnection() {
92 mConnection = new ServiceConnection() {
93 @Override
94 public void onServiceConnected(ComponentName className, IBinder service) {
95 if (DBG) {
96 Log.d(TAG, "Service Connected");
97 }
Santos Cordon345350e2013-07-19 17:16:14 -070098 onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service));
Santos Cordon89647a62013-07-16 13:38:09 -070099 }
100
101 @Override
102 public void onServiceDisconnected(ComponentName className) {
Chiao Chenge41661c2013-07-23 13:28:26 -0700103 // TODO(klp): handle the case where the in call ui crashed or gets destroyed.
104 // In the near term, we need to re-bind to the service when ever it's gone.
105 // Longer term, we need a way to catch the crash and allow the users to choose
106 // a different in-call screen.
107 Log.e(TAG, "Yikes! no in call ui!");
Santos Cordon345350e2013-07-19 17:16:14 -0700108 mCallHandlerService = null;
Santos Cordon89647a62013-07-16 13:38:09 -0700109 }
110 };
111
Santos Cordon345350e2013-07-19 17:16:14 -0700112 Intent serviceIntent = new Intent(ICallHandlerService.class.getName());
Santos Cordon89647a62013-07-16 13:38:09 -0700113 if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
Santos Cordon345350e2013-07-19 17:16:14 -0700114 Log.e(TAG, "Cound not bind to ICallHandlerService");
Santos Cordon89647a62013-07-16 13:38:09 -0700115 }
116 }
117
118 /**
Santos Cordoncba1b442013-07-18 12:43:58 -0700119 * Called when the in-call UI service is connected. Send command interface to in-call.
120 */
Santos Cordon63a84242013-07-23 13:32:52 -0700121 private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) {
122 mCallHandlerService = callHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -0700123
124 try {
Santos Cordon345350e2013-07-19 17:16:14 -0700125 mCallHandlerService.setCallCommandService(mCallCommandService);
Santos Cordoncba1b442013-07-18 12:43:58 -0700126 } catch (RemoteException e) {
Santos Cordon63a84242013-07-23 13:32:52 -0700127 Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700128 }
129 }
130}