blob: c58791c3fcfeda941b3db044c4c0420309407e52 [file] [log] [blame]
Santos Cordon63aeb162014-02-10 09:20:40 -08001/*
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
17package com.android.telecomm;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
23import android.os.IBinder;
24import android.os.IInterface;
25
26import com.google.common.base.Preconditions;
27import com.google.common.base.Strings;
Santos Cordon5c12c6e2014-02-13 14:35:31 -080028import com.google.common.collect.Sets;
29
30import java.util.Set;
Santos Cordon63aeb162014-02-10 09:20:40 -080031
32/**
33 * Abstract class to perform the work of binding and unbinding to the specified service interface.
34 * Subclasses supply the service intent and component name and this class will invoke protected
35 * methods when the class is bound, unbound, or upon failure.
36 */
37abstract class ServiceBinder<ServiceInterface extends IInterface> {
38
Santos Cordon5c12c6e2014-02-13 14:35:31 -080039 /**
40 * Callback to notify after a binding succeeds or fails.
41 */
42 interface BindCallback {
43 public void onSuccess();
44 public void onFailure();
45 }
46
Santos Cordon63aeb162014-02-10 09:20:40 -080047 private final class ServiceBinderConnection implements ServiceConnection {
48 @Override
49 public void onServiceConnected(ComponentName componentName, IBinder binder) {
50 ThreadUtil.checkOnMainThread();
51
52 // Unbind request was queued so unbind immediately.
53 if (mIsBindingAborted) {
54 clearAbort();
55 mContext.unbindService(this);
Santos Cordon5c12c6e2014-02-13 14:35:31 -080056 handleFailedConnection();
Santos Cordon63aeb162014-02-10 09:20:40 -080057 return;
58 }
59
60 mServiceConnection = this;
61 mBinder = binder;
62 handleSuccessfulConnection(binder);
63 }
64
65 @Override
66 public void onServiceDisconnected(ComponentName componentName) {
67 mServiceConnection = null;
68 clearAbort();
69
70 handleServiceDisconnected();
71 }
72 }
73
74 /** The application context. */
75 private final Context mContext;
76
77 /** The intent action to use when binding through {@link Context#bindService}. */
78 private final String mServiceAction;
79
80 /** The component name of the service to bind to. */
81 private final ComponentName mComponentName;
82
Santos Cordon5c12c6e2014-02-13 14:35:31 -080083 /** The set of callbacks waiting for notification of the binding's success or failure. */
84 private final Set<BindCallback> mCallbacks = Sets.newHashSet();
85
Santos Cordon63aeb162014-02-10 09:20:40 -080086 /** Used to bind and unbind from the service. */
87 private ServiceConnection mServiceConnection;
88
89 /** The binder provided by {@link ServiceConnection#onServiceConnected} */
90 private IBinder mBinder;
91
92 /**
93 * Indicates that an unbind request was made when the service was not yet bound. If the service
94 * successfully connects when this is true, it should be unbound immediately.
95 */
96 private boolean mIsBindingAborted;
97
98 /**
99 * Persists the specified parameters and initializes the new instance.
100 *
101 * @param serviceAction The intent-action used with {@link Context#bindService}.
102 * @param componentName The component name of the service with which to bind.
103 */
104 protected ServiceBinder(String serviceAction, ComponentName componentName) {
105 Preconditions.checkState(!Strings.isNullOrEmpty(serviceAction));
106 Preconditions.checkNotNull(componentName);
107
108 mContext = TelecommApp.getInstance();
109 mServiceAction = serviceAction;
110 mComponentName = componentName;
111 }
112
113 /**
114 * Performs an asynchronous bind to the service if not already bound.
115 *
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800116 * @param callback The callback to notify of the binding's success or failure.
Santos Cordon63aeb162014-02-10 09:20:40 -0800117 * @return The result of {#link Context#bindService} or true if already bound.
118 */
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800119 final boolean bind(BindCallback callback) {
Santos Cordon63aeb162014-02-10 09:20:40 -0800120 ThreadUtil.checkOnMainThread();
121
122 // Reset any abort request if we're asked to bind again.
123 clearAbort();
124
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800125 // If we are already waiting on a binding request, simply append to the list of waiting
126 // callbacks.
127 if (!mCallbacks.isEmpty()) {
128 mCallbacks.add(callback);
129 return true;
130 }
131
Santos Cordon63aeb162014-02-10 09:20:40 -0800132 if (mServiceConnection == null) {
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800133 mCallbacks.add(callback);
Santos Cordon63aeb162014-02-10 09:20:40 -0800134 Intent serviceIntent = new Intent(mServiceAction).setComponent(mComponentName);
135 ServiceConnection connection = new ServiceBinderConnection();
136
137 if (!mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) {
138 handleFailedConnection();
139 return false;
140 }
141 } else {
142 Preconditions.checkNotNull(mBinder);
143 handleSuccessfulConnection(mBinder);
144 }
145
146 return true;
147 }
148
149 /**
150 * Unbinds from the service if already bound, no-op otherwise.
151 */
152 final void unbind() {
153 ThreadUtil.checkOnMainThread();
154
155 if (mServiceConnection == null) {
156 // We're not yet bound, so queue up an abort request.
157 mIsBindingAborted = true;
158 } else {
159 mContext.unbindService(mServiceConnection);
160 mServiceConnection = null;
161 mBinder = null;
162 }
163 }
164
165 ComponentName getComponentName() {
166 return mComponentName;
167 }
168
169 /**
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800170 * Notifies all the outstanding callbacks that the service is successfully bound. The list of
171 * outstanding callbacks is cleared afterwards.
Santos Cordon63aeb162014-02-10 09:20:40 -0800172 *
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800173 * @param binder The actual bound service implementation.
Santos Cordon63aeb162014-02-10 09:20:40 -0800174 */
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800175 private void handleSuccessfulConnection(IBinder binder) {
176 setServiceInterface(binder);
177
178 for (BindCallback callback : mCallbacks) {
179 callback.onSuccess();
180 }
181 mCallbacks.clear();
182 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800183
184 /**
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800185 * Notifies all the outstanding callbacks that the service failed to bind. The list of
186 * outstanding callbacks is cleared afterwards.
Santos Cordon63aeb162014-02-10 09:20:40 -0800187 */
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800188 private void handleFailedConnection() {
189 for (BindCallback callback : mCallbacks) {
190 callback.onFailure();
191 }
192 mCallbacks.clear();
193 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800194
195 /**
196 * Handles a service disconnection.
197 */
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800198 private void handleServiceDisconnected() {
199 setServiceInterface(null);
200 }
Santos Cordon63aeb162014-02-10 09:20:40 -0800201
202 private void clearAbort() {
203 mIsBindingAborted = false;
204 }
Santos Cordon5c12c6e2014-02-13 14:35:31 -0800205
206 /**
207 * Sets the service interface after the service is bound or unbound.
208 *
209 * @param binder The actual bound service implementation.
210 */
211 protected abstract void setServiceInterface(IBinder binder);
Santos Cordon63aeb162014-02-10 09:20:40 -0800212}