blob: 2762c98bd4b81f2afb5d7f8ae181d3f9dfbf3885 [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
Santos Cordona3d05142013-07-29 11:25:17 -070037import java.util.List;
38
Santos Cordon89647a62013-07-16 13:38:09 -070039/**
Santos Cordon345350e2013-07-19 17:16:14 -070040 * This class is responsible for passing through call state changes to the CallHandlerService.
Santos Cordon89647a62013-07-16 13:38:09 -070041 */
Santos Cordon9b7bac72013-08-06 08:04:52 -070042public class CallHandlerServiceProxy extends Handler implements CallModeler.Listener,
43 AudioModeListener {
Santos Cordon89647a62013-07-16 13:38:09 -070044
Santos Cordon345350e2013-07-19 17:16:14 -070045 private static final String TAG = CallHandlerServiceProxy.class.getSimpleName();
Santos Cordon89647a62013-07-16 13:38:09 -070046 private static final boolean DBG =
47 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
Chiao Chenge41661c2013-07-23 13:28:26 -070048
Santos Cordon89647a62013-07-16 13:38:09 -070049
50 private Context mContext;
Santos Cordon63a84242013-07-23 13:32:52 -070051 private CallModeler mCallModeler;
Santos Cordon89647a62013-07-16 13:38:09 -070052 private ServiceConnection mConnection;
Santos Cordon345350e2013-07-19 17:16:14 -070053 private ICallHandlerService mCallHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -070054 private CallCommandService mCallCommandService;
Santos Cordon89647a62013-07-16 13:38:09 -070055
Santos Cordon63a84242013-07-23 13:32:52 -070056 public CallHandlerServiceProxy(Context context, CallModeler callModeler,
Santos Cordoncba1b442013-07-18 12:43:58 -070057 CallCommandService callCommandService) {
Santos Cordon89647a62013-07-16 13:38:09 -070058 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070059 mCallCommandService = callCommandService;
Santos Cordon63a84242013-07-23 13:32:52 -070060 mCallModeler = callModeler;
Santos Cordon89647a62013-07-16 13:38:09 -070061
Santos Cordon89647a62013-07-16 13:38:09 -070062 setupServiceConnection();
Christine Chendaf7bf62013-08-05 19:12:31 -070063 mCallModeler.addListener(this);
Santos Cordon89647a62013-07-16 13:38:09 -070064
Santos Cordon998f42b2013-08-02 16:13:12 -070065 // start the whole process
66 onUpdate(mCallModeler.getFullList(), true);
Santos Cordon63a84242013-07-23 13:32:52 -070067 }
Santos Cordon89647a62013-07-16 13:38:09 -070068
Santos Cordon63a84242013-07-23 13:32:52 -070069 @Override
Santos Cordon995c8162013-07-29 09:22:22 -070070 public void onDisconnect(Call call) {
Santos Cordon63a84242013-07-23 13:32:52 -070071 if (mCallHandlerService != null) {
72 try {
Santos Cordon995c8162013-07-29 09:22:22 -070073 mCallHandlerService.onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -070074 } catch (RemoteException e) {
75 Log.e(TAG, "Remote exception handling onDisconnect ", e);
76 }
Santos Cordon89647a62013-07-16 13:38:09 -070077 }
78 }
79
Santos Cordona3d05142013-07-29 11:25:17 -070080 @Override
Santos Cordon998f42b2013-08-02 16:13:12 -070081 public void onUpdate(List<Call> calls, boolean fullUpdate) {
Santos Cordona3d05142013-07-29 11:25:17 -070082 if (mCallHandlerService != null) {
83 try {
Santos Cordon998f42b2013-08-02 16:13:12 -070084 mCallHandlerService.onUpdate(calls, fullUpdate);
Santos Cordona3d05142013-07-29 11:25:17 -070085 } catch (RemoteException e) {
86 Log.e(TAG, "Remote exception handling onUpdate", e);
87 }
88 }
89 }
90
Santos Cordon9b7bac72013-08-06 08:04:52 -070091 @Override
92 public void onAudioModeChange(int previousMode, int newMode) {
93 // Just do a simple log for now.
94 Log.i(TAG, "Updating with new audio mode: " + AudioMode.toString(newMode) +
95 " from " + AudioMode.toString(previousMode));
96
97 if (mCallHandlerService != null) {
98 try {
99 mCallHandlerService.onAudioModeChange(newMode);
100 } catch (RemoteException e) {
101 Log.e(TAG, "Remote exception handling onAudioModeChange", e);
102 }
103 }
104 }
105
Santos Cordon89647a62013-07-16 13:38:09 -0700106 /**
Santos Cordon345350e2013-07-19 17:16:14 -0700107 * Sets up the connection with ICallHandlerService
Santos Cordon89647a62013-07-16 13:38:09 -0700108 */
109 private void setupServiceConnection() {
110 mConnection = new ServiceConnection() {
111 @Override
112 public void onServiceConnected(ComponentName className, IBinder service) {
113 if (DBG) {
114 Log.d(TAG, "Service Connected");
115 }
Santos Cordon345350e2013-07-19 17:16:14 -0700116 onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service));
Santos Cordon89647a62013-07-16 13:38:09 -0700117 }
118
119 @Override
120 public void onServiceDisconnected(ComponentName className) {
Chiao Chenge41661c2013-07-23 13:28:26 -0700121 // TODO(klp): handle the case where the in call ui crashed or gets destroyed.
122 // In the near term, we need to re-bind to the service when ever it's gone.
123 // Longer term, we need a way to catch the crash and allow the users to choose
124 // a different in-call screen.
125 Log.e(TAG, "Yikes! no in call ui!");
Santos Cordon345350e2013-07-19 17:16:14 -0700126 mCallHandlerService = null;
Santos Cordon89647a62013-07-16 13:38:09 -0700127 }
128 };
129
Santos Cordon345350e2013-07-19 17:16:14 -0700130 Intent serviceIntent = new Intent(ICallHandlerService.class.getName());
Santos Cordon89647a62013-07-16 13:38:09 -0700131 if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
Santos Cordon345350e2013-07-19 17:16:14 -0700132 Log.e(TAG, "Cound not bind to ICallHandlerService");
Santos Cordon89647a62013-07-16 13:38:09 -0700133 }
134 }
135
136 /**
Santos Cordoncba1b442013-07-18 12:43:58 -0700137 * Called when the in-call UI service is connected. Send command interface to in-call.
138 */
Santos Cordon63a84242013-07-23 13:32:52 -0700139 private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) {
140 mCallHandlerService = callHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -0700141
142 try {
Santos Cordon345350e2013-07-19 17:16:14 -0700143 mCallHandlerService.setCallCommandService(mCallCommandService);
Santos Cordoncba1b442013-07-18 12:43:58 -0700144 } catch (RemoteException e) {
Santos Cordon63a84242013-07-23 13:32:52 -0700145 Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700146 }
147 }
148}