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 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 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; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 24 | import android.os.UserHandle; |
Santos Cordon | 4bc0245 | 2014-11-19 15:51:12 -0800 | [diff] [blame] | 25 | import android.text.TextUtils; |
| 26 | import android.util.ArraySet; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 27 | |
Brad Ebinger | f190007 | 2015-11-12 17:25:06 -0800 | [diff] [blame] | 28 | import com.android.internal.annotations.VisibleForTesting; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 29 | import com.android.internal.util.Preconditions; |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 30 | |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 31 | import java.util.Collections; |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 32 | import java.util.Set; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 33 | import java.util.concurrent.ConcurrentHashMap; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Abstract class to perform the work of binding and unbinding to the specified service interface. |
| 37 | * Subclasses supply the service intent and component name and this class will invoke protected |
| 38 | * methods when the class is bound, unbound, or upon failure. |
| 39 | */ |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 40 | abstract class ServiceBinder { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 41 | |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Callback to notify after a binding succeeds or fails. |
| 44 | */ |
| 45 | interface BindCallback { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 46 | void onSuccess(); |
| 47 | void onFailure(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Listener for bind events on ServiceBinder. |
| 52 | */ |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 53 | interface Listener<ServiceBinderClass extends ServiceBinder> { |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame] | 54 | void onUnbind(ServiceBinderClass 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 | */ |
Ihab Awad | 8731faf | 2015-03-26 13:43:22 -0700 | [diff] [blame] | 60 | final class Binder2 { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 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. |
Santos Cordon | 165c1ce | 2015-07-10 16:07:59 -0700 | [diff] [blame] | 66 | * @param call The call for which we are being bound. |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 67 | */ |
Santos Cordon | 165c1ce | 2015-07-10 16:07:59 -0700 | [diff] [blame] | 68 | void bind(BindCallback callback, Call call) { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 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); |
Santos Cordon | 165c1ce | 2015-07-10 16:07:59 -0700 | [diff] [blame] | 83 | ServiceConnection connection = new ServiceBinderConnection(call); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 84 | |
Santos Cordon | 165c1ce | 2015-07-10 16:07:59 -0700 | [diff] [blame] | 85 | Log.event(call, Log.Events.BIND_CS, mComponentName); |
Santos Cordon | 8e6edbf | 2015-05-29 12:25:37 -0700 | [diff] [blame] | 86 | final int bindingFlags = Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE; |
| 87 | final boolean isBound; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 88 | if (mUserHandle != null) { |
Santos Cordon | 8e6edbf | 2015-05-29 12:25:37 -0700 | [diff] [blame] | 89 | isBound = mContext.bindServiceAsUser(serviceIntent, connection, bindingFlags, |
| 90 | mUserHandle); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 91 | } else { |
Santos Cordon | 8e6edbf | 2015-05-29 12:25:37 -0700 | [diff] [blame] | 92 | isBound = mContext.bindService(serviceIntent, connection, bindingFlags); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 93 | } |
Santos Cordon | 8e6edbf | 2015-05-29 12:25:37 -0700 | [diff] [blame] | 94 | if (!isBound) { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 95 | handleFailedConnection(); |
| 96 | return; |
| 97 | } |
| 98 | } else { |
| 99 | Log.d(ServiceBinder.this, "Service is already bound."); |
| 100 | Preconditions.checkNotNull(mBinder); |
| 101 | handleSuccessfulConnection(); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 106 | private final class ServiceBinderConnection implements ServiceConnection { |
Santos Cordon | 165c1ce | 2015-07-10 16:07:59 -0700 | [diff] [blame] | 107 | /** |
| 108 | * The initial call for which the service was bound. |
| 109 | */ |
| 110 | private Call mCall; |
| 111 | |
| 112 | ServiceBinderConnection(Call call) { |
| 113 | mCall = call; |
| 114 | } |
| 115 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 116 | @Override |
| 117 | public void onServiceConnected(ComponentName componentName, IBinder binder) { |
Brad Ebinger | 11623a3 | 2015-11-25 13:52:02 -0800 | [diff] [blame] | 118 | try { |
| 119 | Log.startSession("SBC.oSC"); |
| 120 | synchronized (mLock) { |
| 121 | Log.i(this, "Service bound %s", componentName); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 122 | |
Brad Ebinger | 11623a3 | 2015-11-25 13:52:02 -0800 | [diff] [blame] | 123 | Log.event(mCall, Log.Events.CS_BOUND, componentName); |
| 124 | mCall = null; |
Santos Cordon | 165c1ce | 2015-07-10 16:07:59 -0700 | [diff] [blame] | 125 | |
Brad Ebinger | 11623a3 | 2015-11-25 13:52:02 -0800 | [diff] [blame] | 126 | // Unbind request was queued so unbind immediately. |
| 127 | if (mIsBindingAborted) { |
| 128 | clearAbort(); |
| 129 | logServiceDisconnected("onServiceConnected"); |
| 130 | mContext.unbindService(this); |
| 131 | handleFailedConnection(); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | mServiceConnection = this; |
| 136 | setBinder(binder); |
| 137 | handleSuccessfulConnection(); |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 138 | } |
Brad Ebinger | 11623a3 | 2015-11-25 13:52:02 -0800 | [diff] [blame] | 139 | } finally { |
| 140 | Log.endSession(); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 141 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void onServiceDisconnected(ComponentName componentName) { |
Brad Ebinger | 11623a3 | 2015-11-25 13:52:02 -0800 | [diff] [blame] | 146 | try { |
| 147 | Log.startSession("SBC.oSD"); |
| 148 | synchronized (mLock) { |
| 149 | logServiceDisconnected("onServiceDisconnected"); |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 150 | |
Brad Ebinger | 11623a3 | 2015-11-25 13:52:02 -0800 | [diff] [blame] | 151 | mServiceConnection = null; |
| 152 | clearAbort(); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 153 | |
Brad Ebinger | 11623a3 | 2015-11-25 13:52:02 -0800 | [diff] [blame] | 154 | handleServiceDisconnected(); |
| 155 | } |
| 156 | } finally { |
| 157 | Log.endSession(); |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 158 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | /** The application context. */ |
| 163 | private final Context mContext; |
| 164 | |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 165 | /** The Telecom lock object. */ |
| 166 | protected final TelecomSystem.SyncRoot mLock; |
| 167 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 168 | /** The intent action to use when binding through {@link Context#bindService}. */ |
| 169 | private final String mServiceAction; |
| 170 | |
| 171 | /** The component name of the service to bind to. */ |
| 172 | private final ComponentName mComponentName; |
| 173 | |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 174 | /** The set of callbacks waiting for notification of the binding's success or failure. */ |
Santos Cordon | 4bc0245 | 2014-11-19 15:51:12 -0800 | [diff] [blame] | 175 | private final Set<BindCallback> mCallbacks = new ArraySet<>(); |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 176 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 177 | /** Used to bind and unbind from the service. */ |
| 178 | private ServiceConnection mServiceConnection; |
| 179 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 180 | /** {@link UserHandle} to use for binding, to support work profiles and multi-user. */ |
| 181 | private UserHandle mUserHandle; |
| 182 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 183 | /** The binder provided by {@link ServiceConnection#onServiceConnected} */ |
| 184 | private IBinder mBinder; |
| 185 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 186 | private int mAssociatedCallCount = 0; |
| 187 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 188 | /** |
| 189 | * Indicates that an unbind request was made when the service was not yet bound. If the service |
| 190 | * successfully connects when this is true, it should be unbound immediately. |
| 191 | */ |
| 192 | private boolean mIsBindingAborted; |
| 193 | |
| 194 | /** |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 195 | * Set of currently registered listeners. |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 196 | * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is |
| 197 | * load factor before resizing, 1 means we only expect a single thread to |
| 198 | * access the map so make only a single shard |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 199 | */ |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 200 | private final Set<Listener> mListeners = Collections.newSetFromMap( |
| 201 | new ConcurrentHashMap<Listener, Boolean>(8, 0.9f, 1)); |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 202 | |
| 203 | /** |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 204 | * Persists the specified parameters and initializes the new instance. |
| 205 | * |
| 206 | * @param serviceAction The intent-action used with {@link Context#bindService}. |
| 207 | * @param componentName The component name of the service with which to bind. |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 208 | * @param context The context. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 209 | * @param userHandle The {@link UserHandle} to use for binding. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 210 | */ |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 211 | protected ServiceBinder(String serviceAction, ComponentName componentName, Context context, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 212 | TelecomSystem.SyncRoot lock, UserHandle userHandle) { |
Santos Cordon | 4bc0245 | 2014-11-19 15:51:12 -0800 | [diff] [blame] | 213 | Preconditions.checkState(!TextUtils.isEmpty(serviceAction)); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 214 | Preconditions.checkNotNull(componentName); |
| 215 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 216 | mContext = context; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 217 | mLock = lock; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 218 | mServiceAction = serviceAction; |
| 219 | mComponentName = componentName; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 220 | mUserHandle = userHandle; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 223 | final void incrementAssociatedCallCount() { |
| 224 | mAssociatedCallCount++; |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 225 | Log.v(this, "Call count increment %d, %s", mAssociatedCallCount, |
| 226 | mComponentName.flattenToShortString()); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | final void decrementAssociatedCallCount() { |
| 230 | if (mAssociatedCallCount > 0) { |
| 231 | mAssociatedCallCount--; |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 232 | Log.v(this, "Call count decrement %d, %s", mAssociatedCallCount, |
| 233 | mComponentName.flattenToShortString()); |
| 234 | |
| 235 | if (mAssociatedCallCount == 0) { |
| 236 | unbind(); |
| 237 | } |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 238 | } else { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 239 | Log.wtf(this, "%s: ignoring a request to decrement mAssociatedCallCount below zero", |
| 240 | mComponentName.getClassName()); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | final int getAssociatedCallCount() { |
| 245 | return mAssociatedCallCount; |
| 246 | } |
| 247 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 248 | /** |
| 249 | * Unbinds from the service if already bound, no-op otherwise. |
| 250 | */ |
| 251 | final void unbind() { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 252 | if (mServiceConnection == null) { |
| 253 | // We're not yet bound, so queue up an abort request. |
| 254 | mIsBindingAborted = true; |
| 255 | } else { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 256 | logServiceDisconnected("unbind"); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 257 | mContext.unbindService(mServiceConnection); |
| 258 | mServiceConnection = null; |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 259 | setBinder(null); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Ben Gilad | bb167cd | 2014-02-25 16:24:05 -0800 | [diff] [blame] | 263 | final ComponentName getComponentName() { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 264 | return mComponentName; |
| 265 | } |
| 266 | |
Brad Ebinger | f190007 | 2015-11-12 17:25:06 -0800 | [diff] [blame] | 267 | @VisibleForTesting |
| 268 | public boolean isServiceValid(String actionName) { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 269 | if (mBinder == null) { |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 270 | Log.w(this, "%s invoked while service is unbound", actionName); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 271 | return false; |
| 272 | } |
| 273 | |
| 274 | return true; |
| 275 | } |
| 276 | |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 277 | final void addListener(Listener listener) { |
| 278 | mListeners.add(listener); |
| 279 | } |
| 280 | |
| 281 | final void removeListener(Listener listener) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 282 | if (listener != null) { |
| 283 | mListeners.remove(listener); |
| 284 | } |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Logs a standard message upon service disconnection. This method exists because there is no |
| 289 | * single method called whenever the service unbinds and we want to log the same string in all |
| 290 | * instances where that occurs. (Context.unbindService() does not cause onServiceDisconnected |
| 291 | * to execute). |
| 292 | * |
| 293 | * @param sourceTag Tag to disambiguate |
| 294 | */ |
| 295 | private void logServiceDisconnected(String sourceTag) { |
| 296 | Log.i(this, "Service unbound %s, from %s.", mComponentName, sourceTag); |
| 297 | } |
| 298 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 299 | /** |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 300 | * Notifies all the outstanding callbacks that the service is successfully bound. The list of |
| 301 | * outstanding callbacks is cleared afterwards. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 302 | */ |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 303 | private void handleSuccessfulConnection() { |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 304 | for (BindCallback callback : mCallbacks) { |
| 305 | callback.onSuccess(); |
| 306 | } |
| 307 | mCallbacks.clear(); |
| 308 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 309 | |
| 310 | /** |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 311 | * Notifies all the outstanding callbacks that the service failed to bind. The list of |
| 312 | * outstanding callbacks is cleared afterwards. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 313 | */ |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 314 | private void handleFailedConnection() { |
| 315 | for (BindCallback callback : mCallbacks) { |
| 316 | callback.onFailure(); |
| 317 | } |
| 318 | mCallbacks.clear(); |
| 319 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 320 | |
| 321 | /** |
| 322 | * Handles a service disconnection. |
| 323 | */ |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 324 | private void handleServiceDisconnected() { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 325 | setBinder(null); |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 326 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 327 | |
| 328 | private void clearAbort() { |
| 329 | mIsBindingAborted = false; |
| 330 | } |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 331 | |
| 332 | /** |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 333 | * Sets the (private) binder and updates the child class. |
| 334 | * |
| 335 | * @param binder The new binder value. |
| 336 | */ |
| 337 | private void setBinder(IBinder binder) { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 338 | if (mBinder != binder) { |
| 339 | mBinder = binder; |
| 340 | |
| 341 | setServiceInterface(binder); |
| 342 | |
| 343 | if (binder == null) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 344 | for (Listener l : mListeners) { |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 345 | l.onUnbind(this); |
| 346 | } |
| 347 | } |
| 348 | } |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /** |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 352 | * Sets the service interface after the service is bound or unbound. |
| 353 | * |
| 354 | * @param binder The actual bound service implementation. |
| 355 | */ |
| 356 | protected abstract void setServiceInterface(IBinder binder); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 357 | } |