3939import android .content .pm .UserInfo ;
4040import android .os .Binder ;
4141import android .os .Build ;
42+ import android .os .Handler ;
4243import android .os .IBinder ;
4344import android .os .IInterface ;
45+ import android .os .Looper ;
4446import android .os .RemoteException ;
4547import android .os .UserHandle ;
4648import android .os .UserManager ;
@@ -82,6 +84,7 @@ abstract public class ManagedServices {
8284 protected final String TAG = getClass ().getSimpleName ();
8385 protected final boolean DEBUG = Log .isLoggable (TAG , Log .DEBUG );
8486
87+ private static final int ON_BINDING_DIED_REBIND_DELAY_MS = 10000 ;
8588 protected static final String ENABLED_SERVICES_SEPARATOR = ":" ;
8689
8790 /**
@@ -101,12 +104,15 @@ abstract public class ManagedServices {
101104 private final IPackageManager mPm ;
102105 private final UserManager mUm ;
103106 private final Config mConfig ;
107+ private final Handler mHandler = new Handler (Looper .getMainLooper ());
104108
105109 // contains connections to all connected services, including app services
106110 // and system services
107111 private final ArrayList <ManagedServiceInfo > mServices = new ArrayList <>();
108112 // things that will be put into mServices as soon as they're ready
109113 private final ArrayList <String > mServicesBinding = new ArrayList <>();
114+ private final ArraySet <String > mServicesRebinding = new ArraySet <>();
115+
110116 // lists the component names of all enabled (and therefore potentially connected)
111117 // app services for current profiles.
112118 private ArraySet <ComponentName > mEnabledServicesForCurrentProfiles
@@ -823,6 +829,7 @@ private void registerServiceLocked(final ComponentName name, final int userid,
823829
824830 final String servicesBindingTag = name .toString () + "/" + userid ;
825831 if (mServicesBinding .contains (servicesBindingTag )) {
832+ Slog .v (TAG , "Not registering " + name + " as bind is already in progress" );
826833 // stop registering this thing already! we're working on it
827834 return ;
828835 }
@@ -871,6 +878,7 @@ public void onServiceConnected(ComponentName name, IBinder binder) {
871878 boolean added = false ;
872879 ManagedServiceInfo info = null ;
873880 synchronized (mMutex ) {
881+ mServicesRebinding .remove (servicesBindingTag );
874882 mServicesBinding .remove (servicesBindingTag );
875883 try {
876884 mService = asInterface (binder );
@@ -892,6 +900,27 @@ public void onServiceDisconnected(ComponentName name) {
892900 mServicesBinding .remove (servicesBindingTag );
893901 Slog .v (TAG , getCaption () + " connection lost: " + name );
894902 }
903+
904+ @ Override
905+ public void onBindingDied (ComponentName name ) {
906+ Slog .w (TAG , getCaption () + " binding died: " + name );
907+ synchronized (mMutex ) {
908+ mServicesBinding .remove (servicesBindingTag );
909+ mContext .unbindService (this );
910+ if (!mServicesRebinding .contains (servicesBindingTag )) {
911+ mServicesRebinding .add (servicesBindingTag );
912+ mHandler .postDelayed (new Runnable () {
913+ @ Override
914+ public void run () {
915+ registerService (name , userid );
916+ }
917+ }, ON_BINDING_DIED_REBIND_DELAY_MS );
918+ } else {
919+ Slog .v (TAG , getCaption () + " not rebinding as "
920+ + "a previous rebind attempt was made: " + name );
921+ }
922+ }
923+ }
895924 };
896925 if (!mContext .bindServiceAsUser (intent ,
897926 serviceConnection ,
0 commit comments