ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.server.am;
 
import android.annotation.NonNull;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.os.UserHandle;
import android.util.Log;
import android.util.TimeUtils;
 
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
 
import java.io.PrintWriter;
 
/**
 * Connects to a given service component on a given user.
 *
 * - Call {@link #bind()} to create a connection.
 * - Call {@link #unbind()} to disconnect.  Make sure to disconnect when the user stops.
 *
 * Add onConnected/onDisconnected callbacks as needed.
 *
 * When the target process gets killed (by OOM-killer, etc), then the activity manager will
 * re-connect the connection automatically, in which case onServiceDisconnected() gets called
 * and then onServiceConnected().
 *
 * However sometimes the activity manager just "kills" the connection -- like when the target
 * package gets updated or the target process crashes multiple times in a row, in which case
 * onBindingDied() gets called.  This class handles this case by re-connecting in the time
 * {@link #mRebindBackoffMs}.  If this happens again, this class increases the back-off time
 * by {@link #mRebindBackoffIncrease} and retry.  The back-off time is capped at
 * {@link #mRebindMaxBackoffMs}.
 *
 * The back-off time will never be reset until {@link #unbind()} and {@link #bind()} are called
 * explicitly.
 *
 * NOTE: This class does *not* handle package-updates -- i.e. even if the binding dies due to
 * the target package being updated, this class won't reconnect.  This is because this class doesn't
 * know what to do when the service component has gone missing, for example.  If the user of this
 * class wants to restore the connection, then it should call {@link #unbind()} and {@link #bind}
 * explicitly.
 *
 * atest ${ANDROID_BUILD_TOP}/frameworks/base/services/tests/mockingservicestests/src/com/android/server/am/PersistentConnectionTest.java
 */
public abstract class PersistentConnection<T> {
    private final Object mLock = new Object();
 
    private final static boolean DEBUG = false;
 
    private final String mTag;
    private final Context mContext;
    private final Handler mHandler;
    private final int mUserId;
    private final ComponentName mComponentName;
 
    private long mNextBackoffMs;
 
    private final long mRebindBackoffMs;
    private final double mRebindBackoffIncrease;
    private final long mRebindMaxBackoffMs;
    private final long mResetBackoffDelay;
 
    private long mReconnectTime;
 
    // TODO too many booleans... Should clean up.
 
    @GuardedBy("mLock")
    private boolean mBound;
 
    /**
     * Whether {@link #bind()} has been called and {@link #unbind()} hasn't been yet; meaning this
     * is the expected bind state from the caller's point of view.
     */
    @GuardedBy("mLock")
    private boolean mShouldBeBound;
 
    @GuardedBy("mLock")
    private boolean mRebindScheduled;
 
    @GuardedBy("mLock")
    private boolean mIsConnected;
 
    @GuardedBy("mLock")
    private T mService;
 
    @GuardedBy("mLock")
    private int mNumConnected;
 
    @GuardedBy("mLock")
    private int mNumDisconnected;
 
    @GuardedBy("mLock")
    private int mNumBindingDied;
 
    @GuardedBy("mLock")
    private long mLastConnectedTime;
 
    private final ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            synchronized (mLock) {
                if (!mBound) {
                    // Callback came in after PersistentConnection.unbind() was called.
                    // We just ignore this.
                    // (We've already called unbindService() already in unbind)
                    Log.w(mTag, "Connected: " + mComponentName.flattenToShortString()
                            + " u" + mUserId + " but not bound, ignore.");
                    return;
                }
                Log.i(mTag, "Connected: " + mComponentName.flattenToShortString()
                        + " u" + mUserId);
 
                mNumConnected++;
 
                mIsConnected = true;
                mLastConnectedTime = injectUptimeMillis();
                mService = asInterface(service);
 
                scheduleStableCheckLocked();
            }
        }
 
        @Override
        public void onServiceDisconnected(ComponentName name) {
            synchronized (mLock) {
                Log.i(mTag, "Disconnected: " + mComponentName.flattenToShortString()
                        + " u" + mUserId);
 
                mNumDisconnected++;
 
                cleanUpConnectionLocked();
 
                // Note we won't increase the rebind timeout here, because we don't explicitly
                // rebind in this case.
            }
        }
 
        @Override
        public void onBindingDied(ComponentName name) {
            // Activity manager gave up; we'll schedule a re-connect by ourselves.
            synchronized (mLock) {
                if (!mBound) {
                    // Callback came in late?
                    Log.w(mTag, "Binding died: " + mComponentName.flattenToShortString()
                            + " u" + mUserId + " but not bound, ignore.");
                    return;
                }
 
                Log.w(mTag, "Binding died: " + mComponentName.flattenToShortString()
                        + " u" + mUserId);
 
                mNumBindingDied++;
 
                scheduleRebindLocked();
            }
        }
    };
 
    private final Runnable mBindForBackoffRunnable = () -> bindForBackoff();
 
    public PersistentConnection(@NonNull String tag, @NonNull Context context,
            @NonNull Handler handler, int userId, @NonNull ComponentName componentName,
            long rebindBackoffSeconds, double rebindBackoffIncrease, long rebindMaxBackoffSeconds,
            long resetBackoffDelay) {
        mTag = tag;
        mContext = context;
        mHandler = handler;
        mUserId = userId;
        mComponentName = componentName;
 
        mRebindBackoffMs = rebindBackoffSeconds * 1000;
        mRebindBackoffIncrease = rebindBackoffIncrease;
        mRebindMaxBackoffMs = rebindMaxBackoffSeconds * 1000;
        mResetBackoffDelay = resetBackoffDelay * 1000;
 
        mNextBackoffMs = mRebindBackoffMs;
    }
 
    public final ComponentName getComponentName() {
        return mComponentName;
    }
 
    public final int getUserId() {
        return mUserId;
    }
 
    protected abstract int getBindFlags();
 
    /**
     * @return whether {@link #bind()} has been called and {@link #unbind()} hasn't.
     *
     * Note when the AM gives up on connection, this class detects it and un-bind automatically,
     * and schedule rebind, and {@link #isBound} returns false when it's waiting for a retry.
     */
    public final boolean isBound() {
        synchronized (mLock) {
            return mBound;
        }
    }
 
    /**
     * @return whether re-bind is scheduled after the AM gives up on a connection.
     */
    public final boolean isRebindScheduled() {
        synchronized (mLock) {
            return mRebindScheduled;
        }
    }
 
    /**
     * @return whether connected.
     */
    public final boolean isConnected() {
        synchronized (mLock) {
            return mIsConnected;
        }
    }
 
    /**
     * @return the service binder interface.
     */
    public final T getServiceBinder() {
        synchronized (mLock) {
            return mService;
        }
    }
 
    /**
     * Connects to the service.
     */
    public final void bind() {
        synchronized (mLock) {
            mShouldBeBound = true;
 
            bindInnerLocked(/* resetBackoff= */ true);
        }
    }
 
    /** Return the next back-off time */
    public long getNextBackoffMs() {
        synchronized (mLock) {
            return mNextBackoffMs;
        }
    }
 
    /** Return the number of times the connected callback called. */
    public int getNumConnected() {
        synchronized (mLock) {
            return mNumConnected;
        }
    }
 
    /** Return the number of times the disconnected callback called. */
    public int getNumDisconnected() {
        synchronized (mLock) {
            return mNumDisconnected;
        }
    }
 
    /** Return the number of times the binding died callback called. */
    public int getNumBindingDied() {
        synchronized (mLock) {
            return mNumBindingDied;
        }
    }
 
    @GuardedBy("mLock")
    private void resetBackoffLocked() {
        if (mNextBackoffMs != mRebindBackoffMs) {
            mNextBackoffMs = mRebindBackoffMs;
            Log.i(mTag, "Backoff reset to " + mNextBackoffMs);
        }
    }
 
    @GuardedBy("mLock")
    public final void bindInnerLocked(boolean resetBackoff) {
        unscheduleRebindLocked();
 
        if (mBound) {
            return;
        }
        mBound = true;
 
        unscheduleStableCheckLocked();
 
        if (resetBackoff) {
            resetBackoffLocked();
        }
 
        final Intent service = new Intent().setComponent(mComponentName);
 
        if (DEBUG) {
            Log.d(mTag, "Attempting to connect to " + mComponentName);
        }
 
        final boolean success = mContext.bindServiceAsUser(service, mServiceConnection,
                Context.BIND_AUTO_CREATE | getBindFlags(),
                mHandler, UserHandle.of(mUserId));
 
        if (!success) {
            Log.e(mTag, "Binding: " + service.getComponent() + " u" + mUserId
                    + " failed.");
        }
    }
 
    final void bindForBackoff() {
        synchronized (mLock) {
            if (!mShouldBeBound) {
                // Race condition -- by the time we got here, unbind() has already been called.
                return;
            }
 
            bindInnerLocked(/* resetBackoff= */ false);
        }
    }
 
    @GuardedBy("mLock")
    private void cleanUpConnectionLocked() {
        mIsConnected = false;
        mService = null;
 
        unscheduleStableCheckLocked();
    }
 
    /**
     * Disconnect from the service.
     */
    public final void unbind() {
        synchronized (mLock) {
            mShouldBeBound = false;
 
            unbindLocked();
            unscheduleStableCheckLocked();
        }
    }
 
    @GuardedBy("mLock")
    private final void unbindLocked() {
        unscheduleRebindLocked();
 
        if (!mBound) {
            return;
        }
        Log.i(mTag, "Stopping: " + mComponentName.flattenToShortString() + " u" + mUserId);
        mBound = false;
        mContext.unbindService(mServiceConnection);
 
        cleanUpConnectionLocked();
    }
 
    @GuardedBy("mLock")
    void unscheduleRebindLocked() {
        injectRemoveCallbacks(mBindForBackoffRunnable);
        mRebindScheduled = false;
    }
 
    @GuardedBy("mLock")
    void scheduleRebindLocked() {
        unbindLocked();
 
        if (!mRebindScheduled) {
            Log.i(mTag, "Scheduling to reconnect in " + mNextBackoffMs + " ms (uptime)");
 
            mReconnectTime = injectUptimeMillis() + mNextBackoffMs;
 
            injectPostAtTime(mBindForBackoffRunnable, mReconnectTime);
 
            mNextBackoffMs = Math.min(mRebindMaxBackoffMs,
                    (long) (mNextBackoffMs * mRebindBackoffIncrease));
 
            mRebindScheduled = true;
        }
    }
 
    private final Runnable mStableCheck = this::stableConnectionCheck;
 
    private void stableConnectionCheck() {
        synchronized (mLock) {
            final long now = injectUptimeMillis();
            final long timeRemaining = (mLastConnectedTime + mResetBackoffDelay) - now;
            if (DEBUG) {
                Log.d(mTag, "stableConnectionCheck: bound=" + mBound + " connected=" + mIsConnected
                        + " remaining=" + timeRemaining);
            }
            if (mBound && mIsConnected && timeRemaining <= 0) {
                resetBackoffLocked();
            }
        }
    }
 
    @GuardedBy("mLock")
    private void unscheduleStableCheckLocked() {
        injectRemoveCallbacks(mStableCheck);
    }
 
    @GuardedBy("mLock")
    private void scheduleStableCheckLocked() {
        unscheduleStableCheckLocked();
        injectPostAtTime(mStableCheck, injectUptimeMillis() + mResetBackoffDelay);
    }
 
    /** Must be implemented by a subclass to convert an {@link IBinder} to a stub. */
    protected abstract T asInterface(IBinder binder);
 
    public void dump(String prefix, PrintWriter pw) {
        synchronized (mLock) {
            pw.print(prefix);
            pw.print(mComponentName.flattenToShortString());
            pw.print(" u");
            pw.print(mUserId);
            pw.print(mBound ? " [bound]" : " [not bound]");
            pw.print(mIsConnected ? " [connected]" : " [not connected]");
            if (mRebindScheduled) {
                pw.print(" reconnect in ");
                TimeUtils.formatDuration((mReconnectTime - injectUptimeMillis()), pw);
            }
            pw.println();
 
            pw.print(prefix);
            pw.print("  Next backoff(sec): ");
            pw.print(mNextBackoffMs / 1000);
            pw.println();
 
            pw.print(prefix);
            pw.print("  Connected: ");
            pw.print(mNumConnected);
            pw.print("  Disconnected: ");
            pw.print(mNumDisconnected);
            pw.print("  Died: ");
            pw.print(mNumBindingDied);
            if (mIsConnected) {
                pw.print("  Duration: ");
                TimeUtils.formatDuration((injectUptimeMillis() - mLastConnectedTime), pw);
            }
            pw.println();
        }
    }
 
    @VisibleForTesting
    void injectRemoveCallbacks(Runnable r) {
        mHandler.removeCallbacks(r);
    }
 
    @VisibleForTesting
    void injectPostAtTime(Runnable r, long uptimeMillis) {
        mHandler.postAtTime(r, uptimeMillis);
    }
 
    @VisibleForTesting
    long injectUptimeMillis() {
        return SystemClock.uptimeMillis();
    }
 
    @VisibleForTesting
    long getNextBackoffMsForTest() {
        return mNextBackoffMs;
    }
 
    @VisibleForTesting
    long getReconnectTimeForTest() {
        return mReconnectTime;
    }
 
    @VisibleForTesting
    ServiceConnection getServiceConnectionForTest() {
        return mServiceConnection;
    }
 
    @VisibleForTesting
    Runnable getBindForBackoffRunnableForTest() {
        return mBindForBackoffRunnable;
    }
 
    @VisibleForTesting
    Runnable getStableCheckRunnableForTest() {
        return mStableCheck;
    }
 
    @VisibleForTesting
    boolean shouldBeBoundForTest() {
        return mShouldBeBound;
    }
}