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

Commit 2fc40017 authored by Automerger Merge Worker's avatar Automerger Merge Worker
Browse files

Merge "Fix disconnection bug in ServiceWatcher" into rvc-dev am: 49867265 am: 07cd555b

Change-Id: I290fe652d74f55459dce03186b864842989fa67b
parents 5d5eacf9 07cd555b
Loading
Loading
Loading
Loading
+10 −14
Original line number Original line Diff line number Diff line
@@ -100,7 +100,7 @@ public class ServiceWatcher implements ServiceConnection {
        @Nullable public final ComponentName component;
        @Nullable public final ComponentName component;
        @UserIdInt public final int userId;
        @UserIdInt public final int userId;


        private ServiceInfo(ResolveInfo resolveInfo, int currentUserId) {
        ServiceInfo(ResolveInfo resolveInfo, int currentUserId) {
            Preconditions.checkArgument(resolveInfo.serviceInfo.getComponentName() != null);
            Preconditions.checkArgument(resolveInfo.serviceInfo.getComponentName() != null);


            Bundle metadata = resolveInfo.serviceInfo.metaData;
            Bundle metadata = resolveInfo.serviceInfo.metaData;
@@ -316,6 +316,7 @@ public class ServiceWatcher implements ServiceConnection {
            }
            }


            mContext.unbindService(this);
            mContext.unbindService(this);
            onServiceDisconnected(mServiceInfo.component);
            mServiceInfo = ServiceInfo.NONE;
            mServiceInfo = ServiceInfo.NONE;
        }
        }


@@ -339,15 +340,13 @@ public class ServiceWatcher implements ServiceConnection {
    @Override
    @Override
    public final void onServiceConnected(ComponentName component, IBinder binder) {
    public final void onServiceConnected(ComponentName component, IBinder binder) {
        Preconditions.checkState(Looper.myLooper() == mHandler.getLooper());
        Preconditions.checkState(Looper.myLooper() == mHandler.getLooper());
        Preconditions.checkState(mBinder == null);


        if (D) {
        if (D) {
            Log.i(TAG, getLogPrefix() + " connected to " + component.toShortString());
            Log.i(TAG, getLogPrefix() + " connected to " + component.toShortString());
        }
        }


        mBinder = binder;
        mBinder = binder;

        // we always run the on bind callback even if we know that the binder is dead already so
        // that there are always balance pairs of bind/unbind callbacks
        if (mOnBind != null) {
        if (mOnBind != null) {
            try {
            try {
                mOnBind.run(binder);
                mOnBind.run(binder);
@@ -357,19 +356,16 @@ public class ServiceWatcher implements ServiceConnection {
                Log.e(TAG, getLogPrefix() + " exception running on " + mServiceInfo, e);
                Log.e(TAG, getLogPrefix() + " exception running on " + mServiceInfo, e);
            }
            }
        }
        }

        try {
            // setting the binder to null lets us skip queued transactions
            binder.linkToDeath(() -> mBinder = null, 0);
        } catch (RemoteException e) {
            mBinder = null;
        }
    }
    }


    @Override
    @Override
    public final void onServiceDisconnected(ComponentName component) {
    public final void onServiceDisconnected(ComponentName component) {
        Preconditions.checkState(Looper.myLooper() == mHandler.getLooper());
        Preconditions.checkState(Looper.myLooper() == mHandler.getLooper());


        if (mBinder == null) {
            return;
        }

        if (D) {
        if (D) {
            Log.i(TAG, getLogPrefix() + " disconnected from " + component.toShortString());
            Log.i(TAG, getLogPrefix() + " disconnected from " + component.toShortString());
        }
        }
@@ -391,18 +387,18 @@ public class ServiceWatcher implements ServiceConnection {
        onBestServiceChanged(true);
        onBestServiceChanged(true);
    }
    }


    private void onUserSwitched(@UserIdInt int userId) {
    void onUserSwitched(@UserIdInt int userId) {
        mCurrentUserId = userId;
        mCurrentUserId = userId;
        onBestServiceChanged(false);
        onBestServiceChanged(false);
    }
    }


    private void onUserUnlocked(@UserIdInt int userId) {
    void onUserUnlocked(@UserIdInt int userId) {
        if (userId == mCurrentUserId) {
        if (userId == mCurrentUserId) {
            onBestServiceChanged(false);
            onBestServiceChanged(false);
        }
        }
    }
    }


    private void onPackageChanged(String packageName) {
    void onPackageChanged(String packageName) {
        // force a rebind if the changed package was the currently connected package
        // force a rebind if the changed package was the currently connected package
        String currentPackageName =
        String currentPackageName =
                mServiceInfo.component != null ? mServiceInfo.component.getPackageName() : null;
                mServiceInfo.component != null ? mServiceInfo.component.getPackageName() : null;