Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 776f2232 authored by Jordan Liu's avatar Jordan Liu
Browse files

Only unbind if bind() returned success

ServiceConnection may become disconnected asynchonously for various
reasons so calling unbind() on a disconnected SC is actually handled by
the framework. What we don't want to do is call unbind() on a service
which we never successfully called bindService() on, so in this CL we
will track the client-side binding instead of the connection status
through callbacks. This is less racy and handles the root cause of the
exception.

(clean cp of aosp/1231672)

Bug: 145661922
Test: testUnbindWhenNotBound
Change-Id: Ia9bd746440faa047523a750afcae84c347de86d6
Merged-In: Ia9bd746440faa047523a750afcae84c347de86d6
parent be8186cc
Loading
Loading
Loading
Loading
+11 −2
Original line number Diff line number Diff line
@@ -63,6 +63,9 @@ public class CarrierServiceBindHelper {
    private String[] mLastSimState;
    private final PackageMonitor mPackageMonitor = new CarrierServicePackageMonitor();

    // whether we have successfully bound to the service
    private boolean mServiceBound = false;

    private BroadcastReceiver mUserUnlockedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
@@ -235,6 +238,7 @@ public class CarrierServiceBindHelper {
                if (mContext.bindServiceAsUser(carrierService, connection,
                        Context.BIND_AUTO_CREATE |  Context.BIND_FOREGROUND_SERVICE,
                        mHandler, Process.myUserHandle())) {
                    mServiceBound = true;
                    return;
                }

@@ -289,8 +293,13 @@ public class CarrierServiceBindHelper {
            carrierServiceClass = null;

            // Actually unbind
            if (mServiceBound) {
                log("Unbinding from carrier app");
                mServiceBound = false;
                mContext.unbindService(connection);
            } else {
                log("Not bound, skipping unbindService call");
            }
            connection = null;
            mUnbindScheduledUptimeMillis = -1;
        }