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