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; |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 25 | import android.util.Log; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 26 | |
| 27 | import com.google.common.base.Preconditions; |
| 28 | import com.google.common.base.Strings; |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 29 | import com.google.common.collect.Sets; |
| 30 | |
| 31 | import java.util.Set; |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Abstract class to perform the work of binding and unbinding to the specified service interface. |
| 35 | * Subclasses supply the service intent and component name and this class will invoke protected |
| 36 | * methods when the class is bound, unbound, or upon failure. |
| 37 | */ |
| 38 | abstract class ServiceBinder<ServiceInterface extends IInterface> { |
| 39 | |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Callback to notify after a binding succeeds or fails. |
| 42 | */ |
| 43 | interface BindCallback { |
| 44 | public void onSuccess(); |
| 45 | public void onFailure(); |
| 46 | } |
| 47 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 48 | private final class ServiceBinderConnection implements ServiceConnection { |
| 49 | @Override |
| 50 | public void onServiceConnected(ComponentName componentName, IBinder binder) { |
| 51 | ThreadUtil.checkOnMainThread(); |
| 52 | |
| 53 | // Unbind request was queued so unbind immediately. |
| 54 | if (mIsBindingAborted) { |
| 55 | clearAbort(); |
| 56 | mContext.unbindService(this); |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 57 | handleFailedConnection(); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 58 | return; |
| 59 | } |
| 60 | |
| 61 | mServiceConnection = this; |
| 62 | mBinder = binder; |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 63 | setServiceInterface(binder); |
| 64 | handleSuccessfulConnection(); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void onServiceDisconnected(ComponentName componentName) { |
| 69 | mServiceConnection = null; |
| 70 | clearAbort(); |
| 71 | |
| 72 | handleServiceDisconnected(); |
| 73 | } |
| 74 | } |
| 75 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 76 | private static final String TAG = ServiceBinder.class.getSimpleName(); |
| 77 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 78 | /** The application context. */ |
| 79 | private final Context mContext; |
| 80 | |
| 81 | /** The intent action to use when binding through {@link Context#bindService}. */ |
| 82 | private final String mServiceAction; |
| 83 | |
| 84 | /** The component name of the service to bind to. */ |
| 85 | private final ComponentName mComponentName; |
| 86 | |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 87 | /** The set of callbacks waiting for notification of the binding's success or failure. */ |
| 88 | private final Set<BindCallback> mCallbacks = Sets.newHashSet(); |
| 89 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 90 | /** Used to bind and unbind from the service. */ |
| 91 | private ServiceConnection mServiceConnection; |
| 92 | |
| 93 | /** The binder provided by {@link ServiceConnection#onServiceConnected} */ |
| 94 | private IBinder mBinder; |
| 95 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 96 | /** The number of calls currently associated with this service. */ |
| 97 | private int mAssociatedCallCount = 0; |
| 98 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 99 | /** |
| 100 | * Indicates that an unbind request was made when the service was not yet bound. If the service |
| 101 | * successfully connects when this is true, it should be unbound immediately. |
| 102 | */ |
| 103 | private boolean mIsBindingAborted; |
| 104 | |
| 105 | /** |
| 106 | * Persists the specified parameters and initializes the new instance. |
| 107 | * |
| 108 | * @param serviceAction The intent-action used with {@link Context#bindService}. |
| 109 | * @param componentName The component name of the service with which to bind. |
| 110 | */ |
| 111 | protected ServiceBinder(String serviceAction, ComponentName componentName) { |
| 112 | Preconditions.checkState(!Strings.isNullOrEmpty(serviceAction)); |
| 113 | Preconditions.checkNotNull(componentName); |
| 114 | |
| 115 | mContext = TelecommApp.getInstance(); |
| 116 | mServiceAction = serviceAction; |
| 117 | mComponentName = componentName; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Performs an asynchronous bind to the service if not already bound. |
| 122 | * |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 123 | * @param callback The callback to notify of the binding's success or failure. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 124 | * @return The result of {#link Context#bindService} or true if already bound. |
| 125 | */ |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 126 | final boolean bind(BindCallback callback) { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 127 | ThreadUtil.checkOnMainThread(); |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 128 | Log.d(TAG, "bind()"); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 129 | |
| 130 | // Reset any abort request if we're asked to bind again. |
| 131 | clearAbort(); |
| 132 | |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 133 | // If we are already waiting on a binding request, simply append to the list of waiting |
| 134 | // callbacks. |
| 135 | if (!mCallbacks.isEmpty()) { |
| 136 | mCallbacks.add(callback); |
| 137 | return true; |
| 138 | } |
| 139 | |
Sailesh Nepal | c319375 | 2014-02-22 14:25:03 -0800 | [diff] [blame] | 140 | mCallbacks.add(callback); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 141 | if (mServiceConnection == null) { |
| 142 | Intent serviceIntent = new Intent(mServiceAction).setComponent(mComponentName); |
| 143 | ServiceConnection connection = new ServiceBinderConnection(); |
| 144 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 145 | Log.d(TAG, "Binding to call service with intent: " + serviceIntent); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 146 | if (!mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) { |
| 147 | handleFailedConnection(); |
| 148 | return false; |
| 149 | } |
| 150 | } else { |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 151 | Log.d(TAG, "Service is already bound."); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 152 | Preconditions.checkNotNull(mBinder); |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 153 | handleSuccessfulConnection(); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 159 | final void incrementAssociatedCallCount() { |
| 160 | mAssociatedCallCount++; |
| 161 | } |
| 162 | |
| 163 | final void decrementAssociatedCallCount() { |
| 164 | if (mAssociatedCallCount > 0) { |
| 165 | mAssociatedCallCount--; |
| 166 | } else { |
| 167 | Log.wtf(TAG, mComponentName.getClassName() + |
| 168 | ": ignoring a request to decrement mAssociatedCallCount below zero"); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | final int getAssociatedCallCount() { |
| 173 | return mAssociatedCallCount; |
| 174 | } |
| 175 | |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 176 | /** |
| 177 | * Unbinds from the service if already bound, no-op otherwise. |
| 178 | */ |
| 179 | final void unbind() { |
| 180 | ThreadUtil.checkOnMainThread(); |
| 181 | |
| 182 | if (mServiceConnection == null) { |
| 183 | // We're not yet bound, so queue up an abort request. |
| 184 | mIsBindingAborted = true; |
| 185 | } else { |
| 186 | mContext.unbindService(mServiceConnection); |
| 187 | mServiceConnection = null; |
| 188 | mBinder = null; |
| 189 | } |
| 190 | } |
| 191 | |
Ben Gilad | bb167cd | 2014-02-25 16:24:05 -0800 | [diff] [blame] | 192 | final ComponentName getComponentName() { |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 193 | return mComponentName; |
| 194 | } |
| 195 | |
| 196 | /** |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 197 | * Notifies all the outstanding callbacks that the service is successfully bound. The list of |
| 198 | * outstanding callbacks is cleared afterwards. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 199 | */ |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 200 | private void handleSuccessfulConnection() { |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 201 | for (BindCallback callback : mCallbacks) { |
| 202 | callback.onSuccess(); |
| 203 | } |
| 204 | mCallbacks.clear(); |
| 205 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 206 | |
| 207 | /** |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 208 | * Notifies all the outstanding callbacks that the service failed to bind. The list of |
| 209 | * outstanding callbacks is cleared afterwards. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 210 | */ |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 211 | private void handleFailedConnection() { |
| 212 | for (BindCallback callback : mCallbacks) { |
| 213 | callback.onFailure(); |
| 214 | } |
| 215 | mCallbacks.clear(); |
| 216 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * Handles a service disconnection. |
| 220 | */ |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 221 | private void handleServiceDisconnected() { |
| 222 | setServiceInterface(null); |
| 223 | } |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 224 | |
| 225 | private void clearAbort() { |
| 226 | mIsBindingAborted = false; |
| 227 | } |
Santos Cordon | 5c12c6e | 2014-02-13 14:35:31 -0800 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * Sets the service interface after the service is bound or unbound. |
| 231 | * |
| 232 | * @param binder The actual bound service implementation. |
| 233 | */ |
| 234 | protected abstract void setServiceInterface(IBinder binder); |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 235 | } |