ronnie
2023-02-07 4382dc0b492f08fac9cc178333329b28204dfb09
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
/**
 * Copyright (c) 2016, 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.utils;
 
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.PendingIntent;
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.IBinder.DeathRecipient;
import android.os.IInterface;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.UserHandle;
import android.util.Slog;
 
import java.text.SimpleDateFormat;
import java.util.Objects;
import java.util.Date;
 
/**
 * Manages the lifecycle of an application-provided service bound from system server.
 *
 * @hide
 */
public class ManagedApplicationService {
    private final String TAG = getClass().getSimpleName();
 
    /**
     * Attempt to reconnect service forever if an onBindingDied or onServiceDisconnected event
     * is received.
     */
    public static final int RETRY_FOREVER = 1;
 
    /**
     * Never attempt to reconnect the service - a single onBindingDied or onServiceDisconnected
     * event will cause this to fully unbind the service and never attempt to reconnect.
     */
    public static final int RETRY_NEVER = 2;
 
    /**
     * Attempt to reconnect the service until the maximum number of retries is reached, then stop.
     *
     * The first retry will occur MIN_RETRY_DURATION_MS after the disconnection, and each
     * subsequent retry will occur after 2x the duration used for the previous retry up to the
     * MAX_RETRY_DURATION_MS duration.
     *
     * In this case, retries mean a full unbindService/bindService pair to handle cases when the
     * usual service re-connection logic in ActiveServices has very high backoff times or when the
     * serviceconnection has fully died due to a package update or similar.
     */
    public static final int RETRY_BEST_EFFORT = 3;
 
    // Maximum number of retries before giving up (for RETRY_BEST_EFFORT).
    private static final int MAX_RETRY_COUNT = 4;
    // Max time between retry attempts.
    private static final long MAX_RETRY_DURATION_MS = 16000;
    // Min time between retry attempts.
    private static final long MIN_RETRY_DURATION_MS = 2000;
    // Time since the last retry attempt after which to clear the retry attempt counter.
    private static final long RETRY_RESET_TIME_MS = MAX_RETRY_DURATION_MS * 4;
 
    private final Context mContext;
    private final int mUserId;
    private final ComponentName mComponent;
    private final int mClientLabel;
    private final String mSettingsAction;
    private final BinderChecker mChecker;
    private final boolean mIsImportant;
    private final int mRetryType;
    private final Handler mHandler;
    private final Runnable mRetryRunnable = this::doRetry;
    private final EventCallback mEventCb;
 
    private final Object mLock = new Object();
 
    // State protected by mLock
    private ServiceConnection mConnection;
    private IInterface mBoundInterface;
    private PendingEvent mPendingEvent;
    private int mRetryCount;
    private long mLastRetryTimeMs;
    private long mNextRetryDurationMs = MIN_RETRY_DURATION_MS;
    private boolean mRetrying;
 
    public static interface LogFormattable {
       String toLogString(SimpleDateFormat dateFormat);
    }
 
    /**
     * Lifecycle event of this managed service.
     */
    public static class LogEvent implements LogFormattable {
        public static final int EVENT_CONNECTED = 1;
        public static final int EVENT_DISCONNECTED = 2;
        public static final int EVENT_BINDING_DIED = 3;
        public static final int EVENT_STOPPED_PERMANENTLY = 4;
 
        // Time of the events in "current time ms" timebase.
        public final long timestamp;
        // Name of the component for this system service.
        public final ComponentName component;
        // ID of the event that occurred.
        public final int event;
 
        public LogEvent(long timestamp, ComponentName component, int event) {
            this.timestamp = timestamp;
            this.component = component;
            this.event = event;
        }
 
        @Override
        public String toLogString(SimpleDateFormat dateFormat) {
            return dateFormat.format(new Date(timestamp)) + "   " + eventToString(event)
                    + " Managed Service: "
                    + ((component == null) ? "None" : component.flattenToString());
        }
 
        public static String eventToString(int event) {
            switch (event) {
                case EVENT_CONNECTED:
                    return "Connected";
                case EVENT_DISCONNECTED:
                    return "Disconnected";
                case EVENT_BINDING_DIED:
                    return "Binding Died For";
                case EVENT_STOPPED_PERMANENTLY:
                    return "Permanently Stopped";
                default:
                    return "Unknown Event Occurred";
            }
        }
    }
 
    private ManagedApplicationService(final Context context, final ComponentName component,
            final int userId, int clientLabel, String settingsAction,
            BinderChecker binderChecker, boolean isImportant, int retryType, Handler handler,
            EventCallback eventCallback) {
        mContext = context;
        mComponent = component;
        mUserId = userId;
        mClientLabel = clientLabel;
        mSettingsAction = settingsAction;
        mChecker = binderChecker;
        mIsImportant = isImportant;
        mRetryType = retryType;
        mHandler = handler;
        mEventCb = eventCallback;
    }
 
    /**
     * Implement to validate returned IBinder instance.
     */
    public interface BinderChecker {
        IInterface asInterface(IBinder binder);
        boolean checkType(IInterface service);
    }
 
    /**
     * Implement to call IInterface methods after service is connected.
     */
    public interface PendingEvent {
        void runEvent(IInterface service) throws RemoteException;
    }
 
    /**
     * Implement to be notified about any problems with remote service.
     */
    public interface EventCallback {
        /**
         * Called when an sevice lifecycle event occurs.
         */
        void onServiceEvent(LogEvent event);
    }
 
    /**
     * Create a new ManagedApplicationService object but do not yet bind to the user service.
     *
     * @param context a Context to use for binding the application service.
     * @param component the {@link ComponentName} of the application service to bind.
     * @param userId the user ID of user to bind the application service as.
     * @param clientLabel the resource ID of a label displayed to the user indicating the
     *      binding service, or 0 if none is desired.
     * @param settingsAction an action that can be used to open the Settings UI to enable/disable
     *      binding to these services, or null if none is desired.
     * @param binderChecker an interface used to validate the returned binder object, or null if
     *      this interface is unchecked.
     * @param isImportant bind the user service with BIND_IMPORTANT.
     * @param retryType reconnect behavior to have when bound service is disconnected.
     * @param handler the Handler to use for retries and delivering EventCallbacks.
     * @param eventCallback a callback used to deliver disconnection events, or null if you
     *      don't care.
     * @return a ManagedApplicationService instance.
     */
    public static ManagedApplicationService build(@NonNull final Context context,
            @NonNull final ComponentName component, final int userId, int clientLabel,
            @Nullable String settingsAction, @Nullable BinderChecker binderChecker,
            boolean isImportant, int retryType, @NonNull Handler handler,
            @Nullable EventCallback eventCallback) {
        return new ManagedApplicationService(context, component, userId, clientLabel,
            settingsAction, binderChecker, isImportant, retryType, handler, eventCallback);
    }
 
 
    /**
     * @return the user ID of the user that owns the bound service.
     */
    public int getUserId() {
        return mUserId;
    }
 
    /**
     * @return the component of the bound service.
     */
    public ComponentName getComponent() {
        return mComponent;
    }
 
    /**
     * Asynchronously unbind from the application service if the bound service component and user
     * does not match the given signature.
     *
     * @param componentName the component that must match.
     * @param userId the user ID that must match.
     * @return {@code true} if not matching.
     */
    public boolean disconnectIfNotMatching(final ComponentName componentName, final int userId) {
        if (matches(componentName, userId)) {
            return false;
        }
        disconnect();
        return true;
    }
 
    /**
     * Send an event to run as soon as the binder interface is available.
     *
     * @param event a {@link PendingEvent} to send.
     */
    public void sendEvent(@NonNull PendingEvent event) {
        IInterface iface;
        synchronized (mLock) {
            iface = mBoundInterface;
            if (iface == null) {
                mPendingEvent = event;
            }
        }
 
        if (iface != null) {
            try {
                event.runEvent(iface);
            } catch (RuntimeException | RemoteException ex) {
                Slog.e(TAG, "Received exception from user service: ", ex);
            }
        }
    }
 
    /**
     * Asynchronously unbind from the application service if bound.
     */
    public void disconnect() {
        synchronized (mLock) {
            // Unbind existing connection, if it exists
            if (mConnection == null) {
                return;
            }
 
            mContext.unbindService(mConnection);
            mConnection = null;
            mBoundInterface = null;
        }
    }
 
    /**
     * Asynchronously bind to the application service if not bound.
     */
    public void connect() {
        synchronized (mLock) {
            if (mConnection != null) {
                // We're already connected or are trying to connect
                return;
            }
 
            Intent intent  = new Intent().setComponent(mComponent);
            if (mClientLabel != 0) {
                intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mClientLabel);
            }
            if (mSettingsAction != null) {
                intent.putExtra(Intent.EXTRA_CLIENT_INTENT,
                        PendingIntent.getActivity(mContext, 0, new Intent(mSettingsAction), 0));
            }
 
            mConnection = new ServiceConnection() {
                @Override
                public void onBindingDied(ComponentName componentName) {
                    final long timestamp = System.currentTimeMillis();
                    Slog.w(TAG, "Service binding died: " + componentName);
                    synchronized (mLock) {
                        if (mConnection != this) {
                            return;
                        }
                        mHandler.post(() -> {
                            mEventCb.onServiceEvent(new LogEvent(timestamp, mComponent,
                                  LogEvent.EVENT_BINDING_DIED));
                        });
 
                        mBoundInterface = null;
                        startRetriesLocked();
                    }
                }
 
                @Override
                public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                    final long timestamp = System.currentTimeMillis();
                    Slog.i(TAG, "Service connected: " + componentName);
                    IInterface iface = null;
                    PendingEvent pendingEvent = null;
                    synchronized (mLock) {
                        if (mConnection != this) {
                            // Must've been unbound.
                            return;
                        }
                        mHandler.post(() -> {
                            mEventCb.onServiceEvent(new LogEvent(timestamp, mComponent,
                                  LogEvent.EVENT_CONNECTED));
                        });
 
                        stopRetriesLocked();
 
                        mBoundInterface = null;
                        if (mChecker != null) {
                            mBoundInterface = mChecker.asInterface(iBinder);
                            if (!mChecker.checkType(mBoundInterface)) {
                                // Received an invalid binder, disconnect.
                                mBoundInterface = null;
                                Slog.w(TAG, "Invalid binder from " + componentName);
                                startRetriesLocked();
                                return;
                            }
                            iface = mBoundInterface;
                            pendingEvent = mPendingEvent;
                            mPendingEvent = null;
                        }
                    }
                    if (iface != null && pendingEvent != null) {
                        try {
                            pendingEvent.runEvent(iface);
                        } catch (RuntimeException | RemoteException ex) {
                            Slog.e(TAG, "Received exception from user service: ", ex);
                            startRetriesLocked();
                        }
                    }
                }
 
                @Override
                public void onServiceDisconnected(ComponentName componentName) {
                    final long timestamp = System.currentTimeMillis();
                    Slog.w(TAG, "Service disconnected: " + componentName);
                    synchronized (mLock) {
                        if (mConnection != this) {
                            return;
                        }
 
                        mHandler.post(() -> {
                            mEventCb.onServiceEvent(new LogEvent(timestamp, mComponent,
                                  LogEvent.EVENT_DISCONNECTED));
                        });
 
                        mBoundInterface = null;
                        startRetriesLocked();
                    }
                }
            };
 
            int flags = Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE;
            if (mIsImportant) {
                flags |= Context.BIND_IMPORTANT;
            }
            try {
                if (!mContext.bindServiceAsUser(intent, mConnection, flags,
                        new UserHandle(mUserId))) {
                    Slog.w(TAG, "Unable to bind service: " + intent);
                    startRetriesLocked();
                }
            } catch (SecurityException e) {
                Slog.w(TAG, "Unable to bind service: " + intent, e);
                startRetriesLocked();
            }
        }
    }
 
    private boolean matches(final ComponentName component, final int userId) {
        return Objects.equals(mComponent, component) && mUserId == userId;
    }
 
    private void startRetriesLocked() {
        if (checkAndDeliverServiceDiedCbLocked()) {
            // If we delivered the service callback, disconnect and stop retrying.
            disconnect();
            return;
        }
 
        if (mRetrying) {
            // Retry already queued, don't queue a new one.
            return;
        }
        mRetrying = true;
        queueRetryLocked();
    }
 
    private void stopRetriesLocked() {
        mRetrying = false;
        mHandler.removeCallbacks(mRetryRunnable);
    }
 
    private void queueRetryLocked() {
        long now = SystemClock.uptimeMillis();
        if ((now - mLastRetryTimeMs) > RETRY_RESET_TIME_MS) {
            // It's been longer than the reset time since we last had to retry.  Re-initialize.
            mNextRetryDurationMs = MIN_RETRY_DURATION_MS;
            mRetryCount = 0;
        }
        mLastRetryTimeMs = now;
        mHandler.postDelayed(mRetryRunnable, mNextRetryDurationMs);
        mNextRetryDurationMs = Math.min(2 * mNextRetryDurationMs, MAX_RETRY_DURATION_MS);
        mRetryCount++;
    }
 
    private boolean checkAndDeliverServiceDiedCbLocked() {
 
       if (mRetryType == RETRY_NEVER || (mRetryType == RETRY_BEST_EFFORT
                && mRetryCount >= MAX_RETRY_COUNT)) {
            // If we never retry, or we've exhausted our retries, post the onServiceDied callback.
            Slog.e(TAG, "Service " + mComponent + " has died too much, not retrying.");
            if (mEventCb != null) {
                final long timestamp = System.currentTimeMillis();
                mHandler.post(() -> {
                  mEventCb.onServiceEvent(new LogEvent(timestamp, mComponent,
                        LogEvent.EVENT_STOPPED_PERMANENTLY));
                });
            }
            return true;
        }
        return false;
    }
 
    private void doRetry() {
        synchronized (mLock) {
            if (mConnection == null) {
                // We disconnected for good.  Don't attempt to retry.
                return;
            }
            if (!mRetrying) {
                // We successfully connected.  Don't attempt to retry.
                return;
            }
            Slog.i(TAG, "Attempting to reconnect " + mComponent + "...");
            // While frameworks may restart the remote Service if we stay bound, we have little
            // control of the backoff timing for reconnecting the service.  In the event of a
            // process crash, the backoff time can be very large (1-30 min), which is not
            // acceptable for the types of services this is used for.  Instead force an unbind/bind
            // sequence to cause a more immediate retry.
            disconnect();
            if (checkAndDeliverServiceDiedCbLocked()) {
                // No more retries.
                return;
            }
            queueRetryLocked();
            connect();
        }
    }
}