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 | |
| 144 | /** |
| 145 | * Unbinds an existing bound connection to the in-call app. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 146 | */ |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 147 | void unbind() { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 148 | ThreadUtil.checkOnMainThread(); |
| 149 | if (mInCallService != null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 150 | Log.i(this, "Unbinding from InCallService"); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 151 | TelecommApp.getInstance().unbindService(mConnection); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 152 | mInCallService = null; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 157 | * Binds to the in-call app if not already connected by binding directly to the saved |
| 158 | * component name of the {@link IInCallService} implementation. |
| 159 | */ |
| 160 | private void bind() { |
| 161 | ThreadUtil.checkOnMainThread(); |
| 162 | if (mInCallService == null) { |
| 163 | ComponentName component = |
| 164 | new ComponentName(IN_CALL_PACKAGE_NAME, IN_CALL_SERVICE_CLASS_NAME); |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 165 | Log.i(this, "Attempting to bind to InCallService: %s", component); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 166 | |
| 167 | Intent serviceIntent = new Intent(IInCallService.class.getName()); |
| 168 | serviceIntent.setComponent(component); |
| 169 | |
| 170 | Context context = TelecommApp.getInstance(); |
| 171 | if (!context.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 172 | 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] | 173 | |
| 174 | // TODO(santoscordon): Implement retry or fall-back-to-default logic. |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 180 | * Persists the {@link IInCallService} instance and starts the communication between |
| 181 | * CallsManager and in-call app by sending the first update to in-call app. This method is |
| 182 | * called after a successful binding connection is established. |
| 183 | * |
| 184 | * @param service The {@link IInCallService} implementation. |
| 185 | */ |
| 186 | private void onConnected(IBinder service) { |
| 187 | ThreadUtil.checkOnMainThread(); |
| 188 | mInCallService = IInCallService.Stub.asInterface(service); |
| 189 | |
| 190 | try { |
| 191 | mInCallService.setInCallAdapter(new InCallAdapter(mCallsManager)); |
| 192 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 193 | Log.e(this, e, "Failed to set the in-call adapter."); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 194 | mInCallService = null; |
| 195 | } |
| 196 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 197 | // Upon successful connection, send the state of the world to the in-call app. |
| 198 | if (mInCallService != null) { |
| 199 | mCallsManager.updateInCall(); |
| 200 | } |
| 201 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Cleans up the instance of in-call app after the service has been unbound. |
| 206 | */ |
| 207 | private void onDisconnected() { |
| 208 | ThreadUtil.checkOnMainThread(); |
| 209 | mInCallService = null; |
| 210 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 211 | } |