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 | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 26 | import android.telecomm.CallState; |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 27 | |
| 28 | import com.android.internal.telecomm.IInCallService; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 29 | import com.google.common.collect.ImmutableCollection; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Binds to {@link IInCallService} and provides the service to {@link CallsManager} through which it |
| 33 | * can send updates to the in-call app. This class is created and owned by CallsManager and retains |
| 34 | * a binding to the {@link IInCallService} (implemented by the in-call app) until CallsManager |
| 35 | * explicitly disconnects it. CallsManager starts the connection by calling {@link #connect} and |
| 36 | * retains the connection as long as it has calls which need UI. When all calls are disconnected, |
| 37 | * CallsManager will invoke {@link #disconnect} to sever the binding until the in-call UI is needed |
| 38 | * again. |
| 39 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 40 | public final class InCallController extends CallsManagerListenerBase { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 41 | /** |
| 42 | * Used to bind to the in-call app and triggers the start of communication between |
| 43 | * CallsManager and in-call app. |
| 44 | */ |
| 45 | private class InCallServiceConnection implements ServiceConnection { |
| 46 | /** {@inheritDoc} */ |
| 47 | @Override public void onServiceConnected(ComponentName name, IBinder service) { |
| 48 | onConnected(service); |
| 49 | } |
| 50 | |
| 51 | /** {@inheritDoc} */ |
| 52 | @Override public void onServiceDisconnected(ComponentName name) { |
| 53 | onDisconnected(); |
| 54 | } |
| 55 | } |
| 56 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 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 | */ |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 68 | private static final String IN_CALL_SERVICE_CLASS_NAME = |
| 69 | "com.android.incallui.InCallServiceImpl"; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 70 | |
| 71 | /** Maintains a binding connection to the in-call app. */ |
| 72 | private final InCallServiceConnection mConnection = new InCallServiceConnection(); |
| 73 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 74 | /** The in-call app implementation, see {@link IInCallService}. */ |
| 75 | private IInCallService mInCallService; |
| 76 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 77 | // TODO(santoscordon): May be better to expose the IInCallService methods directly from this |
| 78 | // class as its own method to make the CallsManager code easier to read. |
| 79 | IInCallService getService() { |
| 80 | return mInCallService; |
| 81 | } |
| 82 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 83 | @Override |
| 84 | public void onCallAdded(Call call) { |
| 85 | if (mInCallService == null) { |
| 86 | bind(); |
| 87 | } else { |
| 88 | Log.i(this, "Adding call: %s", call); |
| 89 | try { |
| 90 | mInCallService.addCall(call.toCallInfo()); |
| 91 | } catch (RemoteException e) { |
| 92 | Log.e(this, e, "Exception attempting to addCall."); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 93 | } |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 97 | @Override |
| 98 | public void onCallRemoved(Call call) { |
| 99 | if (CallsManager.getInstance().getCalls().isEmpty()) { |
| 100 | // TODO(sail): Wait for all messages to be delivered to the service before unbinding. |
| 101 | unbind(); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 105 | @Override |
| 106 | public void onCallStateChanged(Call call, CallState oldState, CallState newState) { |
| 107 | if (mInCallService == null) { |
| 108 | return; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 109 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 110 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 111 | switch (newState) { |
| 112 | case ACTIVE: |
| 113 | Log.i(this, "Mark call as ACTIVE: %s", call.getId()); |
| 114 | try { |
| 115 | mInCallService.setActive(call.getId()); |
| 116 | } catch (RemoteException e) { |
| 117 | Log.e(this, e, "Exception attempting to call setActive."); |
| 118 | } |
| 119 | break; |
| 120 | case ON_HOLD: |
| 121 | Log.i(this, "Mark call as HOLD: %s", call.getId()); |
| 122 | try { |
| 123 | mInCallService.setOnHold(call.getId()); |
| 124 | } catch (RemoteException e) { |
| 125 | Log.e(this, e, "Exception attempting to call setOnHold."); |
| 126 | } |
| 127 | break; |
| 128 | case DISCONNECTED: |
| 129 | Log.i(this, "Mark call as DISCONNECTED: %s", call.getId()); |
| 130 | try { |
| 131 | mInCallService.setDisconnected(call.getId()); |
| 132 | } catch (RemoteException e) { |
| 133 | Log.e(this, e, "Exception attempting to call setDisconnected."); |
| 134 | } |
| 135 | break; |
| 136 | default: |
| 137 | break; |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 141 | /** |
| 142 | * Unbinds an existing bound connection to the in-call app. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 143 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 144 | private void unbind() { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 145 | ThreadUtil.checkOnMainThread(); |
| 146 | if (mInCallService != null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 147 | Log.i(this, "Unbinding from InCallService"); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 148 | TelecommApp.getInstance().unbindService(mConnection); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 149 | mInCallService = null; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 154 | * Binds to the in-call app if not already connected by binding directly to the saved |
| 155 | * component name of the {@link IInCallService} implementation. |
| 156 | */ |
| 157 | private void bind() { |
| 158 | ThreadUtil.checkOnMainThread(); |
| 159 | if (mInCallService == null) { |
| 160 | ComponentName component = |
| 161 | new ComponentName(IN_CALL_PACKAGE_NAME, IN_CALL_SERVICE_CLASS_NAME); |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 162 | Log.i(this, "Attempting to bind to InCallService: %s", component); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 163 | |
| 164 | Intent serviceIntent = new Intent(IInCallService.class.getName()); |
| 165 | serviceIntent.setComponent(component); |
| 166 | |
| 167 | Context context = TelecommApp.getInstance(); |
| 168 | if (!context.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 169 | 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] | 170 | |
| 171 | // TODO(santoscordon): Implement retry or fall-back-to-default logic. |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 177 | * Persists the {@link IInCallService} instance and starts the communication between |
| 178 | * CallsManager and in-call app by sending the first update to in-call app. This method is |
| 179 | * called after a successful binding connection is established. |
| 180 | * |
| 181 | * @param service The {@link IInCallService} implementation. |
| 182 | */ |
| 183 | private void onConnected(IBinder service) { |
| 184 | ThreadUtil.checkOnMainThread(); |
| 185 | mInCallService = IInCallService.Stub.asInterface(service); |
| 186 | |
| 187 | try { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 188 | mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance())); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 189 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 190 | Log.e(this, e, "Failed to set the in-call adapter."); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 191 | mInCallService = null; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 192 | return; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 195 | // Upon successful connection, send the state of the world to the in-call app. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 196 | ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls(); |
| 197 | if (!calls.isEmpty()) { |
| 198 | for (Call call : calls) { |
| 199 | onCallAdded(call); |
| 200 | } |
| 201 | } else { |
| 202 | unbind(); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 203 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Cleans up the instance of in-call app after the service has been unbound. |
| 208 | */ |
| 209 | private void onDisconnected() { |
| 210 | ThreadUtil.checkOnMainThread(); |
| 211 | mInCallService = null; |
| 212 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 213 | } |