Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.phone; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.ServiceConnection; |
| 23 | import android.os.AsyncResult; |
| 24 | import android.os.Handler; |
| 25 | import android.os.IBinder; |
| 26 | import android.os.Message; |
| 27 | import android.os.RemoteException; |
| 28 | import android.os.SystemProperties; |
| 29 | import android.util.Log; |
| 30 | |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 31 | import com.android.phone.AudioRouter.AudioModeListener; |
| 32 | import com.android.services.telephony.common.AudioMode; |
Santos Cordon | f404688 | 2013-07-25 18:49:27 -0700 | [diff] [blame] | 33 | import com.android.services.telephony.common.Call; |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 34 | import com.android.services.telephony.common.ICallHandlerService; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 35 | import com.android.services.telephony.common.ICallCommandService; |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 36 | |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 38 | import java.util.List; |
| 39 | |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 40 | /** |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 41 | * This class is responsible for passing through call state changes to the CallHandlerService. |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 42 | */ |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 43 | public class CallHandlerServiceProxy extends Handler implements CallModeler.Listener, |
| 44 | AudioModeListener { |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 45 | |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 46 | private static final String TAG = CallHandlerServiceProxy.class.getSimpleName(); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 47 | private static final boolean DBG = |
| 48 | (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1); |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame] | 49 | |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 50 | |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 51 | private AudioRouter mAudioRouter; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 52 | private CallCommandService mCallCommandService; |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 53 | private CallModeler mCallModeler; |
| 54 | private Context mContext; |
| 55 | private ICallHandlerService mCallHandlerService; |
| 56 | private ServiceConnection mConnection; |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 57 | |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 58 | public CallHandlerServiceProxy(Context context, CallModeler callModeler, |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 59 | CallCommandService callCommandService, AudioRouter audioRouter) { |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 60 | mContext = context; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 61 | mCallCommandService = callCommandService; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 62 | mCallModeler = callModeler; |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 63 | mAudioRouter = audioRouter; |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 64 | |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 65 | mAudioRouter.addAudioModeListener(this); |
Christine Chen | daf7bf6 | 2013-08-05 19:12:31 -0700 | [diff] [blame] | 66 | mCallModeler.addListener(this); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 67 | } |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 68 | |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 69 | @Override |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 70 | public void onDisconnect(Call call) { |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 71 | if (mCallHandlerService != null) { |
| 72 | try { |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame] | 73 | if (DBG) Log.d(TAG, "onDisconnect: " + call); |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 74 | mCallHandlerService.onDisconnect(call); |
Santos Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame^] | 75 | maybeUnbind(); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 76 | } catch (RemoteException e) { |
| 77 | Log.e(TAG, "Remote exception handling onDisconnect ", e); |
| 78 | } |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 82 | @Override |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 83 | public void onIncoming(Call call, ArrayList<String> textResponses) { |
Santos Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame^] | 84 | if (maybeBindToService() && mCallHandlerService != null) { |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 85 | try { |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame] | 86 | if (DBG) Log.d(TAG, "onIncoming: " + call); |
Christine Chen | ee09a49 | 2013-08-06 16:02:29 -0700 | [diff] [blame] | 87 | mCallHandlerService.onIncoming(call, textResponses); |
| 88 | } catch (RemoteException e) { |
| 89 | Log.e(TAG, "Remote exception handling onUpdate", e); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @Override |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 95 | public void onUpdate(List<Call> calls, boolean fullUpdate) { |
Santos Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame^] | 96 | if (maybeBindToService() && mCallHandlerService != null) { |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 97 | try { |
Santos Cordon | 4ad64cd | 2013-08-15 00:36:14 -0700 | [diff] [blame] | 98 | if (DBG) Log.d(TAG, "onUpdate: " + calls.toString()); |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 99 | mCallHandlerService.onUpdate(calls, fullUpdate); |
Santos Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame^] | 100 | maybeUnbind(); |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 101 | } catch (RemoteException e) { |
| 102 | Log.e(TAG, "Remote exception handling onUpdate", e); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 107 | @Override |
| 108 | public void onAudioModeChange(int previousMode, int newMode) { |
| 109 | // Just do a simple log for now. |
| 110 | Log.i(TAG, "Updating with new audio mode: " + AudioMode.toString(newMode) + |
| 111 | " from " + AudioMode.toString(previousMode)); |
| 112 | |
| 113 | if (mCallHandlerService != null) { |
| 114 | try { |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 115 | if (DBG) Log.d(TAG, "onSupportAudioModeChange"); |
| 116 | |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame] | 117 | mCallHandlerService.onAudioModeChange(newMode); |
| 118 | } catch (RemoteException e) { |
| 119 | Log.e(TAG, "Remote exception handling onAudioModeChange", e); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
Santos Cordon | 593ab38 | 2013-08-06 21:58:23 -0700 | [diff] [blame] | 124 | @Override |
| 125 | public void onSupportedAudioModeChange(int modeMask) { |
| 126 | if (mCallHandlerService != null) { |
| 127 | try { |
| 128 | if (DBG) Log.d(TAG, "onSupportAudioModeChange: " + AudioMode.toString(modeMask)); |
| 129 | |
| 130 | mCallHandlerService.onSupportedAudioModeChange(modeMask); |
| 131 | } catch (RemoteException e) { |
| 132 | Log.e(TAG, "Remote exception handling onAudioModeChange", e); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 137 | /** |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 138 | * Sets up the connection with ICallHandlerService |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 139 | */ |
| 140 | private void setupServiceConnection() { |
| 141 | mConnection = new ServiceConnection() { |
| 142 | @Override |
| 143 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 144 | if (DBG) { |
| 145 | Log.d(TAG, "Service Connected"); |
| 146 | } |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 147 | onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service)); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void onServiceDisconnected(ComponentName className) { |
Santos Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame^] | 152 | Log.i(TAG, "Disconnected from UI service."); |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 153 | mCallHandlerService = null; |
Santos Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame^] | 154 | |
| 155 | // clean up our current binding. |
| 156 | mContext.unbindService(mConnection); |
| 157 | mConnection = null; |
| 158 | |
| 159 | // potentially attempt to rebind if there are still active calls. |
| 160 | maybeBindToService(); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 161 | } |
| 162 | }; |
| 163 | |
Santos Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame^] | 164 | final Intent serviceIntent = new Intent(ICallHandlerService.class.getName()); |
| 165 | final ComponentName component = new ComponentName( |
| 166 | mContext.getResources().getString(R.string.incall_ui_default_package), |
| 167 | mContext.getResources().getString(R.string.incall_ui_default_class)); |
| 168 | serviceIntent.setComponent(component); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 169 | if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) { |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 170 | Log.e(TAG, "Cound not bind to ICallHandlerService"); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
Santos Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame^] | 175 | * Checks To see if there are any calls left. If not, unbind the callhandler service. |
| 176 | */ |
| 177 | private void maybeUnbind() { |
| 178 | if (!mCallModeler.hasLiveCall()) { |
| 179 | if (mConnection != null) { |
| 180 | mContext.unbindService(mConnection); |
| 181 | mConnection = null; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Checks to see if there are any active calls. If so, binds the call handler service. |
| 188 | * @return true if already bound. False otherwise. |
| 189 | */ |
| 190 | private boolean maybeBindToService() { |
| 191 | if (mCallModeler.hasLiveCall()) { |
| 192 | // mConnection is set to non-null once an attempt is made to connect. |
| 193 | // We do not check against mCallHandlerService here because we could potentially |
| 194 | // create multiple bindings to the UI. |
| 195 | if (mConnection != null) { |
| 196 | return true; |
| 197 | } |
| 198 | setupServiceConnection(); |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | /** |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 204 | * Called when the in-call UI service is connected. Send command interface to in-call. |
| 205 | */ |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 206 | private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) { |
| 207 | mCallHandlerService = callHandlerService; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 208 | |
| 209 | try { |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 210 | mCallHandlerService.setCallCommandService(mCallCommandService); |
Santos Cordon | af763a1 | 2013-08-19 20:04:58 -0700 | [diff] [blame^] | 211 | |
| 212 | // start with a full update |
| 213 | onUpdate(mCallModeler.getFullList(), true); |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 214 | } catch (RemoteException e) { |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 215 | Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } |