Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.telecomm; |
| 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.IBinder; |
| 24 | import android.os.RemoteException; |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 25 | import android.telecomm.CallInfo; |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 26 | |
| 27 | import com.android.internal.telecomm.IInCallService; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Binds to {@link IInCallService} and provides the service to {@link CallsManager} through which it |
| 31 | * can send updates to the in-call app. This class is created and owned by CallsManager and retains |
| 32 | * a binding to the {@link IInCallService} (implemented by the in-call app) until CallsManager |
| 33 | * explicitly disconnects it. CallsManager starts the connection by calling {@link #connect} and |
| 34 | * retains the connection as long as it has calls which need UI. When all calls are disconnected, |
| 35 | * CallsManager will invoke {@link #disconnect} to sever the binding until the in-call UI is needed |
| 36 | * again. |
| 37 | */ |
| 38 | public final class InCallController { |
| 39 | /** |
| 40 | * Used to bind to the in-call app and triggers the start of communication between |
| 41 | * CallsManager and in-call app. |
| 42 | */ |
| 43 | private class InCallServiceConnection implements ServiceConnection { |
| 44 | /** {@inheritDoc} */ |
| 45 | @Override public void onServiceConnected(ComponentName name, IBinder service) { |
| 46 | onConnected(service); |
| 47 | } |
| 48 | |
| 49 | /** {@inheritDoc} */ |
| 50 | @Override public void onServiceDisconnected(ComponentName name) { |
| 51 | onDisconnected(); |
| 52 | } |
| 53 | } |
| 54 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Package name of the in-call app. Although in-call code in kept in its own namespace, it is |
Ben Gilad | 13329fd | 2014-02-11 17:20:29 -0800 | [diff] [blame] | 57 | * ultimately compiled into the dialer APK, hence the difference in namespaces between this and |
| 58 | * {@link #IN_CALL_SERVICE_CLASS_NAME}. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 59 | * TODO(santoscordon): Change this into config.xml resource entry. |
| 60 | */ |
| 61 | private static final String IN_CALL_PACKAGE_NAME = "com.google.android.dialer"; |
| 62 | |
| 63 | /** |
| 64 | * Class name of the component within in-call app which implements {@link IInCallService}. |
| 65 | */ |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 66 | private static final String IN_CALL_SERVICE_CLASS_NAME = |
| 67 | "com.android.incallui.InCallServiceImpl"; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 68 | |
| 69 | /** Maintains a binding connection to the in-call app. */ |
| 70 | private final InCallServiceConnection mConnection = new InCallServiceConnection(); |
| 71 | |
| 72 | private final CallsManager mCallsManager; |
| 73 | |
| 74 | /** The in-call app implementation, see {@link IInCallService}. */ |
| 75 | private IInCallService mInCallService; |
| 76 | |
| 77 | /** |
| 78 | * Persists the specified parameters. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 79 | */ |
Santos Cordon | 6242b13 | 2014-02-07 16:24:42 -0800 | [diff] [blame] | 80 | InCallController(CallsManager callsManager) { |
| 81 | mCallsManager = callsManager; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // TODO(santoscordon): May be better to expose the IInCallService methods directly from this |
| 85 | // class as its own method to make the CallsManager code easier to read. |
| 86 | IInCallService getService() { |
| 87 | return mInCallService; |
| 88 | } |
| 89 | |
| 90 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 91 | * Indicates to the in-call app that a new call has been created and an appropriate |
| 92 | * user-interface should be built and shown to notify the user. Information about the call |
| 93 | * including its current state is passed in through the callInfo object. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 94 | * |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 95 | * @param callInfo Details about the new call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 96 | */ |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 97 | void addCall(CallInfo callInfo) { |
| 98 | try { |
| 99 | if (mInCallService == null) { |
| 100 | bind(); |
| 101 | } else { |
| 102 | // TODO(santoscordon): Protect against logging phone number. |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 103 | Log.i(this, "Adding call: %s", callInfo); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 104 | mInCallService.addCall(callInfo); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 105 | } |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 106 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 107 | Log.e(this, e, "Exception attempting to addCall."); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Indicates to the in-call app that a call has moved to the active state. |
| 113 | * |
| 114 | * @param callId The identifier of the call that became active. |
| 115 | */ |
| 116 | void markCallAsActive(String callId) { |
| 117 | try { |
| 118 | if (mInCallService != null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 119 | Log.i(this, "Mark call as ACTIVE: %s", callId); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 120 | mInCallService.setActive(callId); |
| 121 | } |
| 122 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 123 | Log.e(this, e, "Exception attempting to markCallAsActive."); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Indicates to the in-call app that a call has been disconnected and the user should be |
| 129 | * notified. |
| 130 | * |
| 131 | * @param callId The identifier of the call that was disconnected. |
| 132 | */ |
| 133 | void markCallAsDisconnected(String callId) { |
| 134 | try { |
| 135 | if (mInCallService != null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 136 | Log.i(this, "Mark call as DISCONNECTED: %s", callId); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 137 | mInCallService.setDisconnected(callId); |
| 138 | } |
| 139 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 140 | Log.e(this, e, "Exception attempting to markCallAsDisconnected."); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 144 | void markCallAsOnHold(String callId) { |
| 145 | try { |
| 146 | if (mInCallService != null) { |
| 147 | Log.i(this, "Mark call as HOLD: %s", callId); |
| 148 | mInCallService.setOnHold(callId); |
| 149 | } |
| 150 | } catch (RemoteException e) { |
| 151 | Log.e(this, e, "Exception attempting to markCallAsHeld."); |
| 152 | } |
| 153 | } |
| 154 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 155 | /** |
| 156 | * Unbinds an existing bound connection to the in-call app. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 157 | */ |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 158 | void unbind() { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 159 | ThreadUtil.checkOnMainThread(); |
| 160 | if (mInCallService != null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 161 | Log.i(this, "Unbinding from InCallService"); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 162 | TelecommApp.getInstance().unbindService(mConnection); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 163 | mInCallService = null; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 168 | * Binds to the in-call app if not already connected by binding directly to the saved |
| 169 | * component name of the {@link IInCallService} implementation. |
| 170 | */ |
| 171 | private void bind() { |
| 172 | ThreadUtil.checkOnMainThread(); |
| 173 | if (mInCallService == null) { |
| 174 | ComponentName component = |
| 175 | new ComponentName(IN_CALL_PACKAGE_NAME, IN_CALL_SERVICE_CLASS_NAME); |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 176 | Log.i(this, "Attempting to bind to InCallService: %s", component); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 177 | |
| 178 | Intent serviceIntent = new Intent(IInCallService.class.getName()); |
| 179 | serviceIntent.setComponent(component); |
| 180 | |
| 181 | Context context = TelecommApp.getInstance(); |
| 182 | if (!context.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 183 | Log.w(this, "Could not connect to the in-call app (%s)", component); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 184 | |
| 185 | // TODO(santoscordon): Implement retry or fall-back-to-default logic. |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 191 | * Persists the {@link IInCallService} instance and starts the communication between |
| 192 | * CallsManager and in-call app by sending the first update to in-call app. This method is |
| 193 | * called after a successful binding connection is established. |
| 194 | * |
| 195 | * @param service The {@link IInCallService} implementation. |
| 196 | */ |
| 197 | private void onConnected(IBinder service) { |
| 198 | ThreadUtil.checkOnMainThread(); |
| 199 | mInCallService = IInCallService.Stub.asInterface(service); |
| 200 | |
| 201 | try { |
| 202 | mInCallService.setInCallAdapter(new InCallAdapter(mCallsManager)); |
| 203 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 204 | Log.e(this, e, "Failed to set the in-call adapter."); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 205 | mInCallService = null; |
| 206 | } |
| 207 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 208 | // Upon successful connection, send the state of the world to the in-call app. |
| 209 | if (mInCallService != null) { |
| 210 | mCallsManager.updateInCall(); |
| 211 | } |
| 212 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Cleans up the instance of in-call app after the service has been unbound. |
| 217 | */ |
| 218 | private void onDisconnected() { |
| 219 | ThreadUtil.checkOnMainThread(); |
| 220 | mInCallService = null; |
| 221 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 222 | } |