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; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 26 | import android.telecomm.IInCallService; |
| 27 | import android.util.Log; |
| 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 | |
| 55 | private static final String TAG = InCallController.class.getSimpleName(); |
| 56 | |
| 57 | /** |
| 58 | * 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] | 59 | * ultimately compiled into the dialer APK, hence the difference in namespaces between this and |
| 60 | * {@link #IN_CALL_SERVICE_CLASS_NAME}. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 61 | * TODO(santoscordon): Change this into config.xml resource entry. |
| 62 | */ |
| 63 | private static final String IN_CALL_PACKAGE_NAME = "com.google.android.dialer"; |
| 64 | |
| 65 | /** |
| 66 | * Class name of the component within in-call app which implements {@link IInCallService}. |
| 67 | */ |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 68 | private static final String IN_CALL_SERVICE_CLASS_NAME = "com.android.incallui.InCallService"; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 69 | |
| 70 | /** Maintains a binding connection to the in-call app. */ |
| 71 | private final InCallServiceConnection mConnection = new InCallServiceConnection(); |
| 72 | |
| 73 | private final CallsManager mCallsManager; |
| 74 | |
| 75 | /** The in-call app implementation, see {@link IInCallService}. */ |
| 76 | private IInCallService mInCallService; |
| 77 | |
| 78 | /** |
| 79 | * Persists the specified parameters. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 80 | */ |
Santos Cordon | 6242b13 | 2014-02-07 16:24:42 -0800 | [diff] [blame] | 81 | InCallController(CallsManager callsManager) { |
| 82 | mCallsManager = callsManager; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // TODO(santoscordon): May be better to expose the IInCallService methods directly from this |
| 86 | // class as its own method to make the CallsManager code easier to read. |
| 87 | IInCallService getService() { |
| 88 | return mInCallService; |
| 89 | } |
| 90 | |
| 91 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 92 | * Indicates to the in-call app that a new call has been created and an appropriate |
| 93 | * user-interface should be built and shown to notify the user. Information about the call |
| 94 | * including its current state is passed in through the callInfo object. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 95 | * |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 96 | * @param callInfo Details about the new call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 97 | */ |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 98 | void addCall(CallInfo callInfo) { |
| 99 | try { |
| 100 | if (mInCallService == null) { |
| 101 | bind(); |
| 102 | } else { |
| 103 | // TODO(santoscordon): Protect against logging phone number. |
| 104 | Log.i(TAG, "Adding call: " + callInfo); |
| 105 | mInCallService.addCall(callInfo); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 106 | } |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 107 | } catch (RemoteException e) { |
| 108 | Log.e(TAG, "Exception attempting to addCall.", e); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Indicates to the in-call app that a call has moved to the active state. |
| 114 | * |
| 115 | * @param callId The identifier of the call that became active. |
| 116 | */ |
| 117 | void markCallAsActive(String callId) { |
| 118 | try { |
| 119 | if (mInCallService != null) { |
| 120 | Log.i(TAG, "Mark call as ACTIVE: " + callId); |
| 121 | mInCallService.setActive(callId); |
| 122 | } |
| 123 | } catch (RemoteException e) { |
| 124 | Log.e(TAG, "Exception attempting to markCallAsActive.", e); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Indicates to the in-call app that a call has been disconnected and the user should be |
| 130 | * notified. |
| 131 | * |
| 132 | * @param callId The identifier of the call that was disconnected. |
| 133 | */ |
| 134 | void markCallAsDisconnected(String callId) { |
| 135 | try { |
| 136 | if (mInCallService != null) { |
| 137 | Log.i(TAG, "Mark call as DISCONNECTED: " + callId); |
| 138 | mInCallService.setDisconnected(callId); |
| 139 | } |
| 140 | } catch (RemoteException e) { |
| 141 | Log.e(TAG, "Exception attempting to markCallAsDisconnected.", e); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Unbinds an existing bound connection to the in-call app. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 147 | */ |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 148 | void unbind() { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 149 | ThreadUtil.checkOnMainThread(); |
| 150 | if (mInCallService != null) { |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 151 | Log.i(TAG, "Unbinding from InCallService"); |
| 152 | TelecommApp.getInstance().unbindService(mConnection); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 153 | mInCallService = null; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 158 | * Binds to the in-call app if not already connected by binding directly to the saved |
| 159 | * component name of the {@link IInCallService} implementation. |
| 160 | */ |
| 161 | private void bind() { |
| 162 | ThreadUtil.checkOnMainThread(); |
| 163 | if (mInCallService == null) { |
| 164 | ComponentName component = |
| 165 | new ComponentName(IN_CALL_PACKAGE_NAME, IN_CALL_SERVICE_CLASS_NAME); |
| 166 | Log.i(TAG, "Attempting to bind to InCallService: " + component); |
| 167 | |
| 168 | Intent serviceIntent = new Intent(IInCallService.class.getName()); |
| 169 | serviceIntent.setComponent(component); |
| 170 | |
| 171 | Context context = TelecommApp.getInstance(); |
| 172 | if (!context.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) { |
| 173 | Log.e(TAG, "Could not connect to the in-call app (" + component + ")"); |
| 174 | |
| 175 | // TODO(santoscordon): Implement retry or fall-back-to-default logic. |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 181 | * Persists the {@link IInCallService} instance and starts the communication between |
| 182 | * CallsManager and in-call app by sending the first update to in-call app. This method is |
| 183 | * called after a successful binding connection is established. |
| 184 | * |
| 185 | * @param service The {@link IInCallService} implementation. |
| 186 | */ |
| 187 | private void onConnected(IBinder service) { |
| 188 | ThreadUtil.checkOnMainThread(); |
| 189 | mInCallService = IInCallService.Stub.asInterface(service); |
| 190 | |
| 191 | try { |
| 192 | mInCallService.setInCallAdapter(new InCallAdapter(mCallsManager)); |
| 193 | } catch (RemoteException e) { |
| 194 | Log.e(TAG, "Failed to set the in-call adapter.", e); |
| 195 | mInCallService = null; |
| 196 | } |
| 197 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 198 | // Upon successful connection, send the state of the world to the in-call app. |
| 199 | if (mInCallService != null) { |
| 200 | mCallsManager.updateInCall(); |
| 201 | } |
| 202 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Cleans up the instance of in-call app after the service has been unbound. |
| 207 | */ |
| 208 | private void onDisconnected() { |
| 209 | ThreadUtil.checkOnMainThread(); |
| 210 | mInCallService = null; |
| 211 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 212 | } |