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