blob: a9f6b21b59539093b9c12a35f10b6d529b666e4f [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 Cordon345350e2013-07-19 17:16:14 -070037public class CallHandlerServiceProxy extends Handler {
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;
45 private CallStateMonitor mCallStateMonitor;
46 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 Cordon345350e2013-07-19 17:16:14 -070050 public CallHandlerServiceProxy(Context context, CallStateMonitor callStateMonitor,
Santos Cordoncba1b442013-07-18 12:43:58 -070051 CallCommandService callCommandService) {
Santos Cordon89647a62013-07-16 13:38:09 -070052 mContext = context;
53 mCallStateMonitor = callStateMonitor;
Santos Cordoncba1b442013-07-18 12:43:58 -070054 mCallCommandService = callCommandService;
Santos Cordon89647a62013-07-16 13:38:09 -070055
56 mCallStateMonitor.addListener(this);
57 setupServiceConnection();
58 }
59
60 @Override
61 public void handleMessage(Message msg) {
62 switch (msg.what) {
63 case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION:
64 onNewRingingConnection((AsyncResult) msg.obj);
65 break;
66
67 default:
68 //super.handleMessage(msg);
69 break;
70 }
71 }
72
73 /**
Santos Cordon345350e2013-07-19 17:16:14 -070074 * Sets up the connection with ICallHandlerService
Santos Cordon89647a62013-07-16 13:38:09 -070075 */
76 private void setupServiceConnection() {
77 mConnection = new ServiceConnection() {
78 @Override
79 public void onServiceConnected(ComponentName className, IBinder service) {
80 if (DBG) {
81 Log.d(TAG, "Service Connected");
82 }
Santos Cordon345350e2013-07-19 17:16:14 -070083 onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service));
Santos Cordon89647a62013-07-16 13:38:09 -070084 }
85
86 @Override
87 public void onServiceDisconnected(ComponentName className) {
Chiao Chenge41661c2013-07-23 13:28:26 -070088 // TODO(klp): handle the case where the in call ui crashed or gets destroyed.
89 // In the near term, we need to re-bind to the service when ever it's gone.
90 // Longer term, we need a way to catch the crash and allow the users to choose
91 // a different in-call screen.
92 Log.e(TAG, "Yikes! no in call ui!");
Santos Cordon345350e2013-07-19 17:16:14 -070093 mCallHandlerService = null;
Santos Cordon89647a62013-07-16 13:38:09 -070094 }
95 };
96
Santos Cordon345350e2013-07-19 17:16:14 -070097 Intent serviceIntent = new Intent(ICallHandlerService.class.getName());
Santos Cordon89647a62013-07-16 13:38:09 -070098 if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
Santos Cordon345350e2013-07-19 17:16:14 -070099 Log.e(TAG, "Cound not bind to ICallHandlerService");
Santos Cordon89647a62013-07-16 13:38:09 -0700100 }
101 }
102
103 /**
Santos Cordoncba1b442013-07-18 12:43:58 -0700104 * Called when the in-call UI service is connected. Send command interface to in-call.
105 */
Santos Cordon345350e2013-07-19 17:16:14 -0700106 private void onCallHandlerServiceConnected(ICallHandlerService CallHandlerService) {
107 mCallHandlerService = CallHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -0700108
109 try {
Santos Cordon345350e2013-07-19 17:16:14 -0700110 mCallHandlerService.setCallCommandService(mCallCommandService);
Santos Cordoncba1b442013-07-18 12:43:58 -0700111 } catch (RemoteException e) {
Santos Cordon345350e2013-07-19 17:16:14 -0700112 Log.e(TAG, "Remote exception calling CallHandlerService::onConnected. " + e);
Santos Cordoncba1b442013-07-18 12:43:58 -0700113 }
114 }
115
116 /**
Santos Cordon89647a62013-07-16 13:38:09 -0700117 * Send notification of a new incoming call.
118 */
119 private void onNewRingingConnection(AsyncResult result) {
Santos Cordon345350e2013-07-19 17:16:14 -0700120 if (mCallHandlerService != null) {
Santos Cordon89647a62013-07-16 13:38:09 -0700121 try {
Santos Cordon345350e2013-07-19 17:16:14 -0700122 mCallHandlerService.onIncomingCall(0);
Santos Cordon89647a62013-07-16 13:38:09 -0700123 } catch (RemoteException e) {
124 Log.e(TAG, "Remote exception handling onIncomingCall:" + e);
125 }
Chiao Chenge41661c2013-07-23 13:28:26 -0700126 } else {
127 Log.wtf(TAG, "Call handle service has not connected! Cannot accept incoming call.");
Santos Cordon89647a62013-07-16 13:38:09 -0700128 }
129 }
130}