summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Harms <danharms@google.com>2020-06-04 15:21:40 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2020-06-04 15:21:40 +0000
commitb9417482e278b049a7319bbf2c6b0eee8ebb93e7 (patch)
treec4ccc0f47effa00805f2f27b0c495f72ec3af01c
parent27da261c449fd1f90f2438d197f372aa058ecd41 (diff)
parent553cdc394edde231119007906618127f33dd98c0 (diff)
downloadCompanionDeviceSupport-b9417482e278b049a7319bbf2c6b0eee8ebb93e7.tar.gz
Merge "RemoteFeature retry binding to service if it fails" into rvc-dev
-rw-r--r--src/com/android/car/companiondevicesupport/feature/RemoteFeature.java36
1 files changed, 31 insertions, 5 deletions
diff --git a/src/com/android/car/companiondevicesupport/feature/RemoteFeature.java b/src/com/android/car/companiondevicesupport/feature/RemoteFeature.java
index 34ec4cd..d4551e5 100644
--- a/src/com/android/car/companiondevicesupport/feature/RemoteFeature.java
+++ b/src/com/android/car/companiondevicesupport/feature/RemoteFeature.java
@@ -18,6 +18,7 @@ package com.android.car.companiondevicesupport.feature;
import static com.android.car.connecteddevice.util.SafeLog.logd;
import static com.android.car.connecteddevice.util.SafeLog.loge;
+import static com.android.car.connecteddevice.util.SafeLog.logw;
import android.annotation.CallSuper;
import android.annotation.NonNull;
@@ -26,7 +27,9 @@ 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.Looper;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.os.UserHandle;
@@ -39,6 +42,7 @@ import com.android.car.companiondevicesupport.api.external.IDeviceAssociationCal
import com.android.car.companiondevicesupport.api.external.IDeviceCallback;
import com.android.car.companiondevicesupport.service.CompanionDeviceSupportService;
+import java.time.Duration;
import java.util.List;
/**
@@ -55,12 +59,18 @@ public abstract class RemoteFeature {
private static final String FULLY_QUALIFIED_SERVICE_NAME = SERVICE_PACKAGE_NAME
+ ".service.CompanionDeviceSupportService";
+ private static final Duration BIND_RETRY_DURATION = Duration.ofSeconds(1);
+
+ private static final int MAX_BIND_ATTEMPTS = 3;
+
private final Context mContext;
private final ParcelUuid mFeatureId;
private IConnectedDeviceManager mConnectedDeviceManager;
+ private int mBindAttempts;
+
public RemoteFeature(@NonNull Context context, @NonNull ParcelUuid featureId) {
mContext = context;
mFeatureId = featureId;
@@ -69,11 +79,8 @@ public abstract class RemoteFeature {
/** Start setup process and begin binding to {@link CompanionDeviceSupportService}. */
@CallSuper
public void start() {
- Intent intent = new Intent();
- intent.setComponent(new ComponentName(SERVICE_PACKAGE_NAME, FULLY_QUALIFIED_SERVICE_NAME));
- intent.setAction(CompanionDeviceSupportService.ACTION_BIND_CONNECTED_DEVICE_MANAGER);
- mContext.bindServiceAsUser(intent, mServiceConnection, Context.BIND_AUTO_CREATE,
- UserHandle.SYSTEM);
+ mBindAttempts = 0;
+ bindToService();
}
/** Called when the hosting service is being destroyed. Cleans up internal feature logic. */
@@ -217,6 +224,25 @@ public abstract class RemoteFeature {
/** Called when an {@link AssociatedDevice} is updated for the given user. */
protected void onAssociatedDeviceUpdated(@NonNull AssociatedDevice device) { }
+ private void bindToService() {
+ Intent intent = new Intent();
+ intent.setComponent(new ComponentName(SERVICE_PACKAGE_NAME, FULLY_QUALIFIED_SERVICE_NAME));
+ intent.setAction(CompanionDeviceSupportService.ACTION_BIND_CONNECTED_DEVICE_MANAGER);
+ boolean success = mContext.bindServiceAsUser(intent, mServiceConnection,
+ Context.BIND_AUTO_CREATE, UserHandle.SYSTEM);
+ if (!success) {
+ mBindAttempts++;
+ if (mBindAttempts > MAX_BIND_ATTEMPTS) {
+ loge(TAG, "Failed to bind to CompanionDeviceSupportService after " + mBindAttempts
+ + " attempts. Aborting.");
+ return;
+ }
+ logw(TAG, "Unable to bind to CompanionDeviceSupportService. Trying again.");
+ new Handler(Looper.getMainLooper()).postDelayed(this::bindToService,
+ BIND_RETRY_DURATION.toMillis());
+ }
+ }
+
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {