blob: 165e69316ad36647b933e5078cd5b08b96cbdd18 [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 Cordon9b7bac72013-08-06 08:04:52 -070031import com.android.phone.AudioRouter.AudioModeListener;
32import com.android.services.telephony.common.AudioMode;
Santos Cordonf4046882013-07-25 18:49:27 -070033import com.android.services.telephony.common.Call;
Santos Cordon345350e2013-07-19 17:16:14 -070034import com.android.services.telephony.common.ICallHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -070035import com.android.services.telephony.common.ICallCommandService;
Santos Cordon89647a62013-07-16 13:38:09 -070036
Christine Chenee09a492013-08-06 16:02:29 -070037import java.util.ArrayList;
Santos Cordona3d05142013-07-29 11:25:17 -070038import java.util.List;
39
Santos Cordon89647a62013-07-16 13:38:09 -070040/**
Santos Cordon345350e2013-07-19 17:16:14 -070041 * This class is responsible for passing through call state changes to the CallHandlerService.
Santos Cordon89647a62013-07-16 13:38:09 -070042 */
Santos Cordon9b7bac72013-08-06 08:04:52 -070043public class CallHandlerServiceProxy extends Handler implements CallModeler.Listener,
44 AudioModeListener {
Santos Cordon89647a62013-07-16 13:38:09 -070045
Santos Cordon345350e2013-07-19 17:16:14 -070046 private static final String TAG = CallHandlerServiceProxy.class.getSimpleName();
Santos Cordon89647a62013-07-16 13:38:09 -070047 private static final boolean DBG =
48 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
Chiao Chenge41661c2013-07-23 13:28:26 -070049
Santos Cordon89647a62013-07-16 13:38:09 -070050
51 private Context mContext;
Santos Cordon63a84242013-07-23 13:32:52 -070052 private CallModeler mCallModeler;
Santos Cordon89647a62013-07-16 13:38:09 -070053 private ServiceConnection mConnection;
Santos Cordon345350e2013-07-19 17:16:14 -070054 private ICallHandlerService mCallHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -070055 private CallCommandService mCallCommandService;
Santos Cordon89647a62013-07-16 13:38:09 -070056
Santos Cordon63a84242013-07-23 13:32:52 -070057 public CallHandlerServiceProxy(Context context, CallModeler callModeler,
Santos Cordoncba1b442013-07-18 12:43:58 -070058 CallCommandService callCommandService) {
Santos Cordon89647a62013-07-16 13:38:09 -070059 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070060 mCallCommandService = callCommandService;
Santos Cordon63a84242013-07-23 13:32:52 -070061 mCallModeler = callModeler;
Santos Cordon89647a62013-07-16 13:38:09 -070062
Santos Cordon89647a62013-07-16 13:38:09 -070063 setupServiceConnection();
Christine Chendaf7bf62013-08-05 19:12:31 -070064 mCallModeler.addListener(this);
Santos Cordon89647a62013-07-16 13:38:09 -070065
Santos Cordon998f42b2013-08-02 16:13:12 -070066 // start the whole process
67 onUpdate(mCallModeler.getFullList(), true);
Santos Cordon63a84242013-07-23 13:32:52 -070068 }
Santos Cordon89647a62013-07-16 13:38:09 -070069
Santos Cordon63a84242013-07-23 13:32:52 -070070 @Override
Santos Cordon995c8162013-07-29 09:22:22 -070071 public void onDisconnect(Call call) {
Santos Cordon63a84242013-07-23 13:32:52 -070072 if (mCallHandlerService != null) {
73 try {
Santos Cordon995c8162013-07-29 09:22:22 -070074 mCallHandlerService.onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -070075 } catch (RemoteException e) {
76 Log.e(TAG, "Remote exception handling onDisconnect ", e);
77 }
Santos Cordon89647a62013-07-16 13:38:09 -070078 }
79 }
80
Santos Cordona3d05142013-07-29 11:25:17 -070081 @Override
Christine Chenee09a492013-08-06 16:02:29 -070082 public void onIncoming(Call call, ArrayList<String> textResponses) {
83 if (mCallHandlerService != null) {
84 try {
85 mCallHandlerService.onIncoming(call, textResponses);
86 } catch (RemoteException e) {
87 Log.e(TAG, "Remote exception handling onUpdate", e);
88 }
89 }
90 }
91
92 @Override
Santos Cordon998f42b2013-08-02 16:13:12 -070093 public void onUpdate(List<Call> calls, boolean fullUpdate) {
Santos Cordona3d05142013-07-29 11:25:17 -070094 if (mCallHandlerService != null) {
95 try {
Santos Cordon998f42b2013-08-02 16:13:12 -070096 mCallHandlerService.onUpdate(calls, fullUpdate);
Santos Cordona3d05142013-07-29 11:25:17 -070097 } catch (RemoteException e) {
98 Log.e(TAG, "Remote exception handling onUpdate", e);
99 }
100 }
101 }
102
Santos Cordon9b7bac72013-08-06 08:04:52 -0700103 @Override
104 public void onAudioModeChange(int previousMode, int newMode) {
105 // Just do a simple log for now.
106 Log.i(TAG, "Updating with new audio mode: " + AudioMode.toString(newMode) +
107 " from " + AudioMode.toString(previousMode));
108
109 if (mCallHandlerService != null) {
110 try {
111 mCallHandlerService.onAudioModeChange(newMode);
112 } catch (RemoteException e) {
113 Log.e(TAG, "Remote exception handling onAudioModeChange", e);
114 }
115 }
116 }
117
Santos Cordon89647a62013-07-16 13:38:09 -0700118 /**
Santos Cordon345350e2013-07-19 17:16:14 -0700119 * Sets up the connection with ICallHandlerService
Santos Cordon89647a62013-07-16 13:38:09 -0700120 */
121 private void setupServiceConnection() {
122 mConnection = new ServiceConnection() {
123 @Override
124 public void onServiceConnected(ComponentName className, IBinder service) {
125 if (DBG) {
126 Log.d(TAG, "Service Connected");
127 }
Santos Cordon345350e2013-07-19 17:16:14 -0700128 onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service));
Santos Cordon89647a62013-07-16 13:38:09 -0700129 }
130
131 @Override
132 public void onServiceDisconnected(ComponentName className) {
Chiao Chenge41661c2013-07-23 13:28:26 -0700133 // TODO(klp): handle the case where the in call ui crashed or gets destroyed.
134 // In the near term, we need to re-bind to the service when ever it's gone.
135 // Longer term, we need a way to catch the crash and allow the users to choose
136 // a different in-call screen.
137 Log.e(TAG, "Yikes! no in call ui!");
Santos Cordon345350e2013-07-19 17:16:14 -0700138 mCallHandlerService = null;
Santos Cordon89647a62013-07-16 13:38:09 -0700139 }
140 };
141
Santos Cordon345350e2013-07-19 17:16:14 -0700142 Intent serviceIntent = new Intent(ICallHandlerService.class.getName());
Santos Cordon89647a62013-07-16 13:38:09 -0700143 if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
Santos Cordon345350e2013-07-19 17:16:14 -0700144 Log.e(TAG, "Cound not bind to ICallHandlerService");
Santos Cordon89647a62013-07-16 13:38:09 -0700145 }
146 }
147
148 /**
Santos Cordoncba1b442013-07-18 12:43:58 -0700149 * Called when the in-call UI service is connected. Send command interface to in-call.
150 */
Santos Cordon63a84242013-07-23 13:32:52 -0700151 private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) {
152 mCallHandlerService = callHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -0700153
154 try {
Santos Cordon345350e2013-07-19 17:16:14 -0700155 mCallHandlerService.setCallCommandService(mCallCommandService);
Santos Cordoncba1b442013-07-18 12:43:58 -0700156 } catch (RemoteException e) {
Santos Cordon63a84242013-07-23 13:32:52 -0700157 Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700158 }
159 }
160}