Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -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.IInterface; |
| 25 | |
| 26 | import com.google.common.base.Preconditions; |
| 27 | import com.google.common.base.Strings; |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 28 | |
| 29 | import com.google.common.collect.ImmutableSet; |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 30 | import com.google.common.collect.Sets; |
| 31 | |
| 32 | import java.util.Set; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Abstract class to perform the work of binding and unbinding to the specified service interface. |
| 36 | * Subclasses supply the service intent and component name and this class will invoke protected |
| 37 | * methods when the class is bound, unbound, or upon failure. |
| 38 | */ |
| 39 | abstract class ServiceBinder<ServiceInterface extends IInterface> { |
| 40 | |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 41 | /** |
| 42 | * Callback to notify after a binding succeeds or fails. |
| 43 | */ |
| 44 | interface BindCallback { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 45 | void onSuccess(); |
| 46 | void onFailure(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Listener for bind events on ServiceBinder. |
| 51 | */ |
| 52 | interface Listener { |
| 53 | @SuppressWarnings("rawtypes") |
| 54 | void onUnbind(ServiceBinder serviceBinder); |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Helper class to perform on-demand binding. |
| 59 | */ |
| 60 | final class Binder { |
| 61 | /** |
| 62 | * Performs an asynchronous bind to the service (only if not already bound) and executes the |
| 63 | * specified callback. |
| 64 | * |
| 65 | * @param callback The callback to notify of the binding's success or failure. |
| 66 | */ |
| 67 | void bind(BindCallback callback) { |
| 68 | ThreadUtil.checkOnMainThread(); |
| 69 | Log.d(ServiceBinder.this, "bind()"); |
| 70 | |
| 71 | // Reset any abort request if we're asked to bind again. |
| 72 | clearAbort(); |
| 73 | |
| 74 | if (!mCallbacks.isEmpty()) { |
| 75 | // Binding already in progress, append to the list of callbacks and bail out. |
| 76 | mCallbacks.add(callback); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | mCallbacks.add(callback); |
| 81 | if (mServiceConnection == null) { |
| 82 | Intent serviceIntent = new Intent(mServiceAction).setComponent(mComponentName); |
| 83 | ServiceConnection connection = new ServiceBinderConnection(); |
| 84 | |
| 85 | Log.d(ServiceBinder.this, "Binding to call service with intent: %s", serviceIntent); |
| 86 | if (!mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) { |
| 87 | handleFailedConnection(); |
| 88 | return; |
| 89 | } |
| 90 | } else { |
| 91 | Log.d(ServiceBinder.this, "Service is already bound."); |
| 92 | Preconditions.checkNotNull(mBinder); |
| 93 | handleSuccessfulConnection(); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 98 | private final class ServiceBinderConnection implements ServiceConnection { |
| 99 | @Override |
| 100 | public void onServiceConnected(ComponentName componentName, IBinder binder) { |
| 101 | ThreadUtil.checkOnMainThread(); |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 102 | Log.i(this, "Service bound %s", componentName); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 103 | |
| 104 | // Unbind request was queued so unbind immediately. |
| 105 | if (mIsBindingAborted) { |
| 106 | clearAbort(); |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 107 | logServiceDisconnected("onServiceConnected"); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 108 | mContext.unbindService(this); |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 109 | handleFailedConnection(); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
| 113 | mServiceConnection = this; |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 114 | setBinder(binder); |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 115 | handleSuccessfulConnection(); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void onServiceDisconnected(ComponentName componentName) { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 120 | logServiceDisconnected("onServiceDisconnected"); |
| 121 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 122 | mServiceConnection = null; |
| 123 | clearAbort(); |
| 124 | |
| 125 | handleServiceDisconnected(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** The application context. */ |
| 130 | private final Context mContext; |
| 131 | |
| 132 | /** The intent action to use when binding through {@link Context#bindService}. */ |
| 133 | private final String mServiceAction; |
| 134 | |
| 135 | /** The component name of the service to bind to. */ |
| 136 | private final ComponentName mComponentName; |
| 137 | |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 138 | /** The set of callbacks waiting for notification of the binding's success or failure. */ |
| 139 | private final Set<BindCallback> mCallbacks = Sets.newHashSet(); |
| 140 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 141 | /** Used to bind and unbind from the service. */ |
| 142 | private ServiceConnection mServiceConnection; |
| 143 | |
| 144 | /** The binder provided by {@link ServiceConnection#onServiceConnected} */ |
| 145 | private IBinder mBinder; |
| 146 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 147 | private int mAssociatedCallCount = 0; |
| 148 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 149 | /** |
| 150 | * Indicates that an unbind request was made when the service was not yet bound. If the service |
| 151 | * successfully connects when this is true, it should be unbound immediately. |
| 152 | */ |
| 153 | private boolean mIsBindingAborted; |
| 154 | |
| 155 | /** |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 156 | * Set of currently registered listeners. |
| 157 | */ |
| 158 | private Set<Listener> mListeners = Sets.newHashSet(); |
| 159 | |
| 160 | /** |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 161 | * Persists the specified parameters and initializes the new instance. |
| 162 | * |
| 163 | * @param serviceAction The intent-action used with {@link Context#bindService}. |
| 164 | * @param componentName The component name of the service with which to bind. |
| 165 | */ |
| 166 | protected ServiceBinder(String serviceAction, ComponentName componentName) { |
| 167 | Preconditions.checkState(!Strings.isNullOrEmpty(serviceAction)); |
| 168 | Preconditions.checkNotNull(componentName); |
| 169 | |
| 170 | mContext = TelecommApp.getInstance(); |
| 171 | mServiceAction = serviceAction; |
| 172 | mComponentName = componentName; |
| 173 | } |
| 174 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 175 | final void incrementAssociatedCallCount() { |
| 176 | mAssociatedCallCount++; |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 177 | Log.v(this, "Call count increment %d, %s", mAssociatedCallCount, |
| 178 | mComponentName.flattenToShortString()); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | final void decrementAssociatedCallCount() { |
| 182 | if (mAssociatedCallCount > 0) { |
| 183 | mAssociatedCallCount--; |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 184 | Log.v(this, "Call count decrement %d, %s", mAssociatedCallCount, |
| 185 | mComponentName.flattenToShortString()); |
| 186 | |
| 187 | if (mAssociatedCallCount == 0) { |
| 188 | unbind(); |
| 189 | } |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 190 | } else { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 191 | Log.wtf(this, "%s: ignoring a request to decrement mAssociatedCallCount below zero", |
| 192 | mComponentName.getClassName()); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | final int getAssociatedCallCount() { |
| 197 | return mAssociatedCallCount; |
| 198 | } |
| 199 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 200 | /** |
| 201 | * Unbinds from the service if already bound, no-op otherwise. |
| 202 | */ |
| 203 | final void unbind() { |
| 204 | ThreadUtil.checkOnMainThread(); |
| 205 | |
| 206 | if (mServiceConnection == null) { |
| 207 | // We're not yet bound, so queue up an abort request. |
| 208 | mIsBindingAborted = true; |
| 209 | } else { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 210 | logServiceDisconnected("unbind"); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 211 | mContext.unbindService(mServiceConnection); |
| 212 | mServiceConnection = null; |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 213 | setBinder(null); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Ben Gilad | bb167cd | 2014-02-25 16:24:05 -0800 | [diff] [blame] | 217 | final ComponentName getComponentName() { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 218 | return mComponentName; |
| 219 | } |
| 220 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 221 | final boolean isServiceValid(String actionName) { |
| 222 | if (mBinder == null) { |
| 223 | Log.wtf(this, "%s invoked while service is unbound", actionName); |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | return true; |
| 228 | } |
| 229 | |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 230 | final void addListener(Listener listener) { |
| 231 | mListeners.add(listener); |
| 232 | } |
| 233 | |
| 234 | final void removeListener(Listener listener) { |
| 235 | mListeners.remove(listener); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Logs a standard message upon service disconnection. This method exists because there is no |
| 240 | * single method called whenever the service unbinds and we want to log the same string in all |
| 241 | * instances where that occurs. (Context.unbindService() does not cause onServiceDisconnected |
| 242 | * to execute). |
| 243 | * |
| 244 | * @param sourceTag Tag to disambiguate |
| 245 | */ |
| 246 | private void logServiceDisconnected(String sourceTag) { |
| 247 | Log.i(this, "Service unbound %s, from %s.", mComponentName, sourceTag); |
| 248 | } |
| 249 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 250 | /** |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 251 | * Notifies all the outstanding callbacks that the service is successfully bound. The list of |
| 252 | * outstanding callbacks is cleared afterwards. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 253 | */ |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 254 | private void handleSuccessfulConnection() { |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 255 | for (BindCallback callback : mCallbacks) { |
| 256 | callback.onSuccess(); |
| 257 | } |
| 258 | mCallbacks.clear(); |
| 259 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 260 | |
| 261 | /** |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 262 | * Notifies all the outstanding callbacks that the service failed to bind. The list of |
| 263 | * outstanding callbacks is cleared afterwards. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 264 | */ |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 265 | private void handleFailedConnection() { |
| 266 | for (BindCallback callback : mCallbacks) { |
| 267 | callback.onFailure(); |
| 268 | } |
| 269 | mCallbacks.clear(); |
| 270 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 271 | |
| 272 | /** |
| 273 | * Handles a service disconnection. |
| 274 | */ |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 275 | private void handleServiceDisconnected() { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 276 | setBinder(null); |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 277 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 278 | |
| 279 | private void clearAbort() { |
| 280 | mIsBindingAborted = false; |
| 281 | } |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 282 | |
| 283 | /** |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 284 | * Sets the (private) binder and updates the child class. |
| 285 | * |
| 286 | * @param binder The new binder value. |
| 287 | */ |
| 288 | private void setBinder(IBinder binder) { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 289 | if (mBinder != binder) { |
| 290 | mBinder = binder; |
| 291 | |
| 292 | setServiceInterface(binder); |
| 293 | |
| 294 | if (binder == null) { |
| 295 | // Use a copy of the listener list to allow the listeners to unregister themselves |
| 296 | // as part of the unbind without causing issues. |
| 297 | for (Listener l : ImmutableSet.copyOf(mListeners)) { |
| 298 | l.onUnbind(this); |
| 299 | } |
| 300 | } |
| 301 | } |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /** |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 305 | * Sets the service interface after the service is bound or unbound. |
| 306 | * |
| 307 | * @param binder The actual bound service implementation. |
| 308 | */ |
| 309 | protected abstract void setServiceInterface(IBinder binder); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 310 | } |