blob: c5edb26892f814eb6436ac8a02e783233b594619 [file] [log] [blame]
Makoto Onukifc73d792017-03-22 14:22:35 -07001/*
2 * Copyright (C) 2017 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 */
16package com.android.server.am;
17
18import android.annotation.NonNull;
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
23import android.os.Handler;
24import android.os.IBinder;
Makoto Onuki09c529a2017-05-01 10:05:28 -070025import android.os.SystemClock;
Makoto Onukifc73d792017-03-22 14:22:35 -070026import android.os.UserHandle;
27import android.util.Slog;
Makoto Onuki09c529a2017-05-01 10:05:28 -070028import android.util.TimeUtils;
Makoto Onukifc73d792017-03-22 14:22:35 -070029
30import com.android.internal.annotations.GuardedBy;
Makoto Onuki09c529a2017-05-01 10:05:28 -070031import com.android.internal.annotations.VisibleForTesting;
Makoto Onukifc73d792017-03-22 14:22:35 -070032
33import java.io.PrintWriter;
34
35/**
36 * Connects to a given service component on a given user.
37 *
Makoto Onuki09c529a2017-05-01 10:05:28 -070038 * - Call {@link #bind()} to create a connection.
39 * - Call {@link #unbind()} to disconnect. Make sure to disconnect when the user stops.
Makoto Onukifc73d792017-03-22 14:22:35 -070040 *
41 * Add onConnected/onDisconnected callbacks as needed.
Makoto Onuki09c529a2017-05-01 10:05:28 -070042 *
43 * When the target process gets killed (by OOM-killer, etc), then the activity manager will
44 * re-connect the connection automatically, in which case onServiceDisconnected() gets called
45 * and then onServiceConnected().
46 *
47 * However sometimes the activity manager just "kills" the connection -- like when the target
48 * package gets updated or the target process crashes multiple times in a row, in which case
49 * onBindingDied() gets called. This class handles this case by re-connecting in the time
50 * {@link #mRebindBackoffMs}. If this happens again, this class increases the back-off time
51 * by {@link #mRebindBackoffIncrease} and retry. The back-off time is capped at
52 * {@link #mRebindMaxBackoffMs}.
53 *
54 * The back-off time will never be reset until {@link #unbind()} and {@link #bind()} are called
55 * explicitly.
56 *
57 * NOTE: This class does *not* handle package-updates -- i.e. even if the binding dies due to
58 * the target package being updated, this class won't reconnect. This is because this class doesn't
59 * know what to do when the service component has gone missing, for example. If the user of this
60 * class wants to restore the connection, then it should call {@link #unbind()} and {@link #bind}
61 * explicitly.
Makoto Onukifc73d792017-03-22 14:22:35 -070062 */
63public abstract class PersistentConnection<T> {
64 private final Object mLock = new Object();
65
Makoto Onuki09c529a2017-05-01 10:05:28 -070066 private final static boolean DEBUG = false;
67
Makoto Onukifc73d792017-03-22 14:22:35 -070068 private final String mTag;
69 private final Context mContext;
70 private final Handler mHandler;
71 private final int mUserId;
72 private final ComponentName mComponentName;
73
Makoto Onuki09c529a2017-05-01 10:05:28 -070074 private long mNextBackoffMs;
75
76 private final long mRebindBackoffMs;
77 private final double mRebindBackoffIncrease;
78 private final long mRebindMaxBackoffMs;
79
80 private long mReconnectTime;
81
82 // TODO too many booleans... Should clean up.
83
Makoto Onukifc73d792017-03-22 14:22:35 -070084 @GuardedBy("mLock")
Makoto Onuki09c529a2017-05-01 10:05:28 -070085 private boolean mBound;
86
87 /**
88 * Whether {@link #bind()} has been called and {@link #unbind()} hasn't been yet; meaning this
89 * is the expected bind state from the caller's point of view.
90 */
91 @GuardedBy("mLock")
92 private boolean mShouldBeBound;
93
94 @GuardedBy("mLock")
95 private boolean mRebindScheduled;
Makoto Onukifc73d792017-03-22 14:22:35 -070096
97 @GuardedBy("mLock")
98 private boolean mIsConnected;
99
100 @GuardedBy("mLock")
101 private T mService;
102
103 private final ServiceConnection mServiceConnection = new ServiceConnection() {
104 @Override
105 public void onServiceConnected(ComponentName name, IBinder service) {
106 synchronized (mLock) {
Makoto Onuki09c529a2017-05-01 10:05:28 -0700107 if (!mBound) {
108 // Callback came in after PersistentConnection.unbind() was called.
109 // We just ignore this.
110 // (We've already called unbindService() already in unbind)
111 Slog.w(mTag, "Connected: " + mComponentName.flattenToShortString()
112 + " u" + mUserId + " but not bound, ignore.");
113 return;
114 }
Makoto Onukifc73d792017-03-22 14:22:35 -0700115 Slog.i(mTag, "Connected: " + mComponentName.flattenToShortString()
116 + " u" + mUserId);
117
118 mIsConnected = true;
119 mService = asInterface(service);
120 }
121 }
122
123 @Override
124 public void onServiceDisconnected(ComponentName name) {
125 synchronized (mLock) {
126 Slog.i(mTag, "Disconnected: " + mComponentName.flattenToShortString()
127 + " u" + mUserId);
128
129 cleanUpConnectionLocked();
130 }
131 }
Makoto Onuki09c529a2017-05-01 10:05:28 -0700132
133 @Override
134 public void onBindingDied(ComponentName name) {
135 // Activity manager gave up; we'll schedule a re-connect by ourselves.
136 synchronized (mLock) {
137 if (!mBound) {
138 // Callback came in late?
139 Slog.w(mTag, "Binding died: " + mComponentName.flattenToShortString()
140 + " u" + mUserId + " but not bound, ignore.");
141 return;
142 }
143
144 Slog.w(mTag, "Binding died: " + mComponentName.flattenToShortString()
145 + " u" + mUserId);
146 scheduleRebindLocked();
147 }
148 }
Makoto Onukifc73d792017-03-22 14:22:35 -0700149 };
150
Makoto Onuki09c529a2017-05-01 10:05:28 -0700151 private final Runnable mBindForBackoffRunnable = () -> bindForBackoff();
152
Makoto Onukifc73d792017-03-22 14:22:35 -0700153 public PersistentConnection(@NonNull String tag, @NonNull Context context,
Makoto Onuki09c529a2017-05-01 10:05:28 -0700154 @NonNull Handler handler, int userId, @NonNull ComponentName componentName,
155 long rebindBackoffSeconds, double rebindBackoffIncrease, long rebindMaxBackoffSeconds) {
Makoto Onukifc73d792017-03-22 14:22:35 -0700156 mTag = tag;
157 mContext = context;
158 mHandler = handler;
159 mUserId = userId;
160 mComponentName = componentName;
Makoto Onuki09c529a2017-05-01 10:05:28 -0700161
162 mRebindBackoffMs = rebindBackoffSeconds * 1000;
163 mRebindBackoffIncrease = rebindBackoffIncrease;
164 mRebindMaxBackoffMs = rebindMaxBackoffSeconds * 1000;
165
166 mNextBackoffMs = mRebindBackoffMs;
Makoto Onukifc73d792017-03-22 14:22:35 -0700167 }
168
169 public final ComponentName getComponentName() {
170 return mComponentName;
171 }
172
173 /**
Makoto Onuki09c529a2017-05-01 10:05:28 -0700174 * @return whether {@link #bind()} has been called and {@link #unbind()} hasn't.
175 *
176 * Note when the AM gives up on connection, this class detects it and un-bind automatically,
177 * and schedule rebind, and {@link #isBound} returns false when it's waiting for a retry.
178 */
179 public final boolean isBound() {
180 synchronized (mLock) {
181 return mBound;
182 }
183 }
184
185 /**
186 * @return whether re-bind is scheduled after the AM gives up on a connection.
187 */
188 public final boolean isRebindScheduled() {
189 synchronized (mLock) {
190 return mRebindScheduled;
191 }
192 }
193
194 /**
Makoto Onukifc73d792017-03-22 14:22:35 -0700195 * @return whether connected.
196 */
197 public final boolean isConnected() {
198 synchronized (mLock) {
199 return mIsConnected;
200 }
201 }
202
203 /**
204 * @return the service binder interface.
205 */
206 public final T getServiceBinder() {
207 synchronized (mLock) {
208 return mService;
209 }
210 }
211
212 /**
213 * Connects to the service.
214 */
Makoto Onuki09c529a2017-05-01 10:05:28 -0700215 public final void bind() {
Makoto Onukifc73d792017-03-22 14:22:35 -0700216 synchronized (mLock) {
Makoto Onuki09c529a2017-05-01 10:05:28 -0700217 mShouldBeBound = true;
218
219 bindInnerLocked(/* resetBackoff= */ true);
220 }
221 }
222
Andreas Gampea36dc622018-02-05 17:19:22 -0800223 @GuardedBy("mLock")
Makoto Onuki09c529a2017-05-01 10:05:28 -0700224 public final void bindInnerLocked(boolean resetBackoff) {
225 unscheduleRebindLocked();
226
227 if (mBound) {
228 return;
229 }
230 mBound = true;
231
232 if (resetBackoff) {
233 // Note this is the only place we reset the backoff time.
234 mNextBackoffMs = mRebindBackoffMs;
235 }
236
237 final Intent service = new Intent().setComponent(mComponentName);
238
239 if (DEBUG) {
240 Slog.d(mTag, "Attempting to connect to " + mComponentName);
241 }
242
243 final boolean success = mContext.bindServiceAsUser(service, mServiceConnection,
244 Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE,
245 mHandler, UserHandle.of(mUserId));
246
247 if (!success) {
248 Slog.e(mTag, "Binding: " + service.getComponent() + " u" + mUserId
249 + " failed.");
250 }
251 }
252
253 final void bindForBackoff() {
254 synchronized (mLock) {
255 if (!mShouldBeBound) {
256 // Race condition -- by the time we got here, unbind() has already been called.
Makoto Onukifc73d792017-03-22 14:22:35 -0700257 return;
258 }
Makoto Onukifc73d792017-03-22 14:22:35 -0700259
Makoto Onuki09c529a2017-05-01 10:05:28 -0700260 bindInnerLocked(/* resetBackoff= */ false);
Makoto Onukifc73d792017-03-22 14:22:35 -0700261 }
262 }
263
Andreas Gampea36dc622018-02-05 17:19:22 -0800264 @GuardedBy("mLock")
Makoto Onukifc73d792017-03-22 14:22:35 -0700265 private void cleanUpConnectionLocked() {
266 mIsConnected = false;
267 mService = null;
268 }
269
270 /**
271 * Disconnect from the service.
272 */
Makoto Onuki09c529a2017-05-01 10:05:28 -0700273 public final void unbind() {
Makoto Onukifc73d792017-03-22 14:22:35 -0700274 synchronized (mLock) {
Makoto Onuki09c529a2017-05-01 10:05:28 -0700275 mShouldBeBound = false;
Makoto Onukifc73d792017-03-22 14:22:35 -0700276
Makoto Onuki09c529a2017-05-01 10:05:28 -0700277 unbindLocked();
278 }
279 }
280
Andreas Gampea36dc622018-02-05 17:19:22 -0800281 @GuardedBy("mLock")
Makoto Onuki09c529a2017-05-01 10:05:28 -0700282 private final void unbindLocked() {
283 unscheduleRebindLocked();
284
285 if (!mBound) {
286 return;
287 }
288 Slog.i(mTag, "Stopping: " + mComponentName.flattenToShortString() + " u" + mUserId);
289 mBound = false;
290 mContext.unbindService(mServiceConnection);
291
292 cleanUpConnectionLocked();
293 }
294
Andreas Gampea36dc622018-02-05 17:19:22 -0800295 @GuardedBy("mLock")
Makoto Onuki09c529a2017-05-01 10:05:28 -0700296 void unscheduleRebindLocked() {
297 injectRemoveCallbacks(mBindForBackoffRunnable);
298 mRebindScheduled = false;
299 }
300
Andreas Gampea36dc622018-02-05 17:19:22 -0800301 @GuardedBy("mLock")
Makoto Onuki09c529a2017-05-01 10:05:28 -0700302 void scheduleRebindLocked() {
303 unbindLocked();
304
305 if (!mRebindScheduled) {
306 Slog.i(mTag, "Scheduling to reconnect in " + mNextBackoffMs + " ms (uptime)");
307
308 mReconnectTime = injectUptimeMillis() + mNextBackoffMs;
309
310 injectPostAtTime(mBindForBackoffRunnable, mReconnectTime);
311
312 mNextBackoffMs = Math.min(mRebindMaxBackoffMs,
313 (long) (mNextBackoffMs * mRebindBackoffIncrease));
314
315 mRebindScheduled = true;
Makoto Onukifc73d792017-03-22 14:22:35 -0700316 }
317 }
318
319 /** Must be implemented by a subclass to convert an {@link IBinder} to a stub. */
320 protected abstract T asInterface(IBinder binder);
321
322 public void dump(String prefix, PrintWriter pw) {
323 synchronized (mLock) {
324 pw.print(prefix);
325 pw.print(mComponentName.flattenToShortString());
Makoto Onuki09c529a2017-05-01 10:05:28 -0700326 pw.print(mBound ? " [bound]" : " [not bound]");
Makoto Onukifc73d792017-03-22 14:22:35 -0700327 pw.print(mIsConnected ? " [connected]" : " [not connected]");
Makoto Onuki09c529a2017-05-01 10:05:28 -0700328 if (mRebindScheduled) {
329 pw.print(" reconnect in ");
330 TimeUtils.formatDuration((mReconnectTime - injectUptimeMillis()), pw);
331 }
Makoto Onukifc73d792017-03-22 14:22:35 -0700332 pw.println();
Makoto Onuki09c529a2017-05-01 10:05:28 -0700333
334 pw.print(prefix);
335 pw.print(" Next backoff(sec): ");
336 pw.print(mNextBackoffMs / 1000);
Makoto Onukifc73d792017-03-22 14:22:35 -0700337 }
338 }
Makoto Onuki09c529a2017-05-01 10:05:28 -0700339
340 @VisibleForTesting
341 void injectRemoveCallbacks(Runnable r) {
342 mHandler.removeCallbacks(r);
343 }
344
345 @VisibleForTesting
346 void injectPostAtTime(Runnable r, long uptimeMillis) {
347 mHandler.postAtTime(r, uptimeMillis);
348 }
349
350 @VisibleForTesting
351 long injectUptimeMillis() {
352 return SystemClock.uptimeMillis();
353 }
354
355 @VisibleForTesting
356 long getNextBackoffMsForTest() {
357 return mNextBackoffMs;
358 }
359
360 @VisibleForTesting
361 long getReconnectTimeForTest() {
362 return mReconnectTime;
363 }
364
365 @VisibleForTesting
366 ServiceConnection getServiceConnectionForTest() {
367 return mServiceConnection;
368 }
369
370 @VisibleForTesting
371 Runnable getBindForBackoffRunnableForTest() {
372 return mBindForBackoffRunnable;
373 }
374
375 @VisibleForTesting
376 boolean shouldBeBoundForTest() {
377 return mShouldBeBound;
378 }
Makoto Onukifc73d792017-03-22 14:22:35 -0700379}