blob: f5247f59d4b5cd51649dc038e988c1c012dea084 [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 Cordon345350e2013-07-19 17:16:14 -070031import com.android.services.telephony.common.ICallHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -070032import com.android.services.telephony.common.ICallCommandService;
Santos Cordon89647a62013-07-16 13:38:09 -070033
34/**
Santos Cordon345350e2013-07-19 17:16:14 -070035 * This class is responsible for passing through call state changes to the CallHandlerService.
Santos Cordon89647a62013-07-16 13:38:09 -070036 */
Santos Cordon63a84242013-07-23 13:32:52 -070037public class CallHandlerServiceProxy extends Handler implements CallModeler.Listener {
Santos Cordon89647a62013-07-16 13:38:09 -070038
Santos Cordon345350e2013-07-19 17:16:14 -070039 private static final String TAG = CallHandlerServiceProxy.class.getSimpleName();
Santos Cordon89647a62013-07-16 13:38:09 -070040 private static final boolean DBG =
41 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
Chiao Chenge41661c2013-07-23 13:28:26 -070042
Santos Cordon89647a62013-07-16 13:38:09 -070043
44 private Context mContext;
Santos Cordon63a84242013-07-23 13:32:52 -070045 private CallModeler mCallModeler;
Santos Cordon89647a62013-07-16 13:38:09 -070046 private ServiceConnection mConnection;
Santos Cordon345350e2013-07-19 17:16:14 -070047 private ICallHandlerService mCallHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -070048 private CallCommandService mCallCommandService;
Santos Cordon89647a62013-07-16 13:38:09 -070049
Santos Cordon63a84242013-07-23 13:32:52 -070050 public CallHandlerServiceProxy(Context context, CallModeler callModeler,
Santos Cordoncba1b442013-07-18 12:43:58 -070051 CallCommandService callCommandService) {
Santos Cordon89647a62013-07-16 13:38:09 -070052 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070053 mCallCommandService = callCommandService;
Santos Cordon63a84242013-07-23 13:32:52 -070054 mCallModeler = callModeler;
Santos Cordon89647a62013-07-16 13:38:09 -070055
Santos Cordon63a84242013-07-23 13:32:52 -070056 mCallModeler.addListener(this);
Santos Cordon89647a62013-07-16 13:38:09 -070057 setupServiceConnection();
58 }
59
60 @Override
Santos Cordon63a84242013-07-23 13:32:52 -070061 public void onNewCall(int callId) {
62 if (mCallHandlerService != null) {
63 try {
64 mCallHandlerService.onIncomingCall(callId);
65 } catch (RemoteException e) {
66 Log.e(TAG, "Remote exception handling onIncomingCall", e);
67 }
68 } else {
69 Log.wtf(TAG, "Call handle service has not connected! Cannot accept incoming call.");
70 }
71 }
Santos Cordon89647a62013-07-16 13:38:09 -070072
Santos Cordon63a84242013-07-23 13:32:52 -070073 @Override
74 public void onDisconnect(int callId) {
75 if (mCallHandlerService != null) {
76 try {
77 mCallHandlerService.onDisconnect(callId);
78 } catch (RemoteException e) {
79 Log.e(TAG, "Remote exception handling onDisconnect ", e);
80 }
Santos Cordon89647a62013-07-16 13:38:09 -070081 }
82 }
83
84 /**
Santos Cordon345350e2013-07-19 17:16:14 -070085 * Sets up the connection with ICallHandlerService
Santos Cordon89647a62013-07-16 13:38:09 -070086 */
87 private void setupServiceConnection() {
88 mConnection = new ServiceConnection() {
89 @Override
90 public void onServiceConnected(ComponentName className, IBinder service) {
91 if (DBG) {
92 Log.d(TAG, "Service Connected");
93 }
Santos Cordon345350e2013-07-19 17:16:14 -070094 onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service));
Santos Cordon89647a62013-07-16 13:38:09 -070095 }
96
97 @Override
98 public void onServiceDisconnected(ComponentName className) {
Chiao Chenge41661c2013-07-23 13:28:26 -070099 // TODO(klp): handle the case where the in call ui crashed or gets destroyed.
100 // In the near term, we need to re-bind to the service when ever it's gone.
101 // Longer term, we need a way to catch the crash and allow the users to choose
102 // a different in-call screen.
103 Log.e(TAG, "Yikes! no in call ui!");
Santos Cordon345350e2013-07-19 17:16:14 -0700104 mCallHandlerService = null;
Santos Cordon89647a62013-07-16 13:38:09 -0700105 }
106 };
107
Santos Cordon345350e2013-07-19 17:16:14 -0700108 Intent serviceIntent = new Intent(ICallHandlerService.class.getName());
Santos Cordon89647a62013-07-16 13:38:09 -0700109 if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
Santos Cordon345350e2013-07-19 17:16:14 -0700110 Log.e(TAG, "Cound not bind to ICallHandlerService");
Santos Cordon89647a62013-07-16 13:38:09 -0700111 }
112 }
113
114 /**
Santos Cordoncba1b442013-07-18 12:43:58 -0700115 * Called when the in-call UI service is connected. Send command interface to in-call.
116 */
Santos Cordon63a84242013-07-23 13:32:52 -0700117 private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) {
118 mCallHandlerService = callHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -0700119
120 try {
Santos Cordon345350e2013-07-19 17:16:14 -0700121 mCallHandlerService.setCallCommandService(mCallCommandService);
Santos Cordoncba1b442013-07-18 12:43:58 -0700122 } catch (RemoteException e) {
Santos Cordon63a84242013-07-23 13:32:52 -0700123 Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700124 }
125 }
126}