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

Commit 277fcc69 authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

Add unbind if bind call returns false

This should prevent target processes to remain at high priority.

Test: atest TileLifecycleManagerTest
Test: atest ControlsProviderLifecycleManagerTest
Test: atest CtsTileServiceTest
Test: atest CtsControlsDeviceTestCases
Bug: 221223837
Change-Id: Ife0e1fed8f0f28273623b1f23ab59d58e2123dfd
parent bfdb2def
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -97,7 +97,11 @@ class ControlsProviderLifecycleManager(
                    }
                    bindTryCount++
                    try {
                        context.bindServiceAsUser(intent, serviceConnection, BIND_FLAGS, user)
                        val bound = context
                            .bindServiceAsUser(intent, serviceConnection, BIND_FLAGS, user)
                        if (!bound) {
                            context.unbindService(serviceConnection)
                        }
                    } catch (e: SecurityException) {
                        Log.e(TAG, "Failed to bind to service", e)
                    }
+3 −0
Original line number Diff line number Diff line
@@ -204,6 +204,9 @@ public class TileLifecycleManager extends BroadcastReceiver implements
                                | Context.BIND_ALLOW_BACKGROUND_ACTIVITY_STARTS
                                | Context.BIND_WAIVE_PRIORITY,
                        mUser);
                if (!mIsBound) {
                    mContext.unbindService(this);
                }
            } catch (SecurityException e) {
                Log.e(TAG, "Failed to bind to service", e);
                mIsBound = false;
+21 −0
Original line number Diff line number Diff line
@@ -210,4 +210,25 @@ class ControlsProviderLifecycleManagerTest : SysuiTestCase() {
                eq(actionCallbackService))
        assertEquals(action, wrapperCaptor.getValue().getWrappedAction())
    }

    @Test
    fun testFalseBindCallsUnbind() {
        val falseContext = mock(Context::class.java)
        `when`(falseContext.bindServiceAsUser(any(), any(), anyInt(), any())).thenReturn(false)
        val manager = ControlsProviderLifecycleManager(
            falseContext,
            executor,
            actionCallbackService,
            UserHandle.of(0),
            componentName
        )
        manager.bindService()
        executor.runAllReady()

        val captor = ArgumentCaptor.forClass(
            ServiceConnection::class.java
        )
        verify(falseContext).bindServiceAsUser(any(), captor.capture(), anyInt(), any())
        verify(falseContext).unbindService(captor.value)
    }
}
+26 −5
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -34,6 +35,7 @@ import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.content.pm.ServiceInfo;
import android.net.Uri;
@@ -56,7 +58,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.ArgumentCaptor;

@SmallTest
@RunWith(AndroidJUnit4.class)
@@ -64,10 +66,10 @@ public class TileLifecycleManagerTest extends SysuiTestCase {
    private static final int TEST_FAIL_TIMEOUT = 5000;

    private final PackageManagerAdapter mMockPackageManagerAdapter =
            Mockito.mock(PackageManagerAdapter.class);
            mock(PackageManagerAdapter.class);
    private final BroadcastDispatcher mMockBroadcastDispatcher =
            Mockito.mock(BroadcastDispatcher.class);
    private final IQSTileService.Stub mMockTileService = Mockito.mock(IQSTileService.Stub.class);
            mock(BroadcastDispatcher.class);
    private final IQSTileService.Stub mMockTileService = mock(IQSTileService.Stub.class);
    private ComponentName mTileServiceComponentName;
    private Intent mTileServiceIntent;
    private UserHandle mUser;
@@ -95,7 +97,7 @@ public class TileLifecycleManagerTest extends SysuiTestCase {
        mThread.start();
        mHandler = Handler.createAsync(mThread.getLooper());
        mStateManager = new TileLifecycleManager(mHandler, mWrappedContext,
                Mockito.mock(IQSService.class),
                mock(IQSService.class),
                mMockPackageManagerAdapter,
                mMockBroadcastDispatcher,
                mTileServiceIntent,
@@ -269,6 +271,25 @@ public class TileLifecycleManagerTest extends SysuiTestCase {
        assertTrue(mStateManager.isToggleableTile());
    }

    @Test
    public void testFalseBindCallsUnbind() {
        Context falseContext = mock(Context.class);
        when(falseContext.bindServiceAsUser(any(), any(), anyInt(), any())).thenReturn(false);
        TileLifecycleManager manager = new TileLifecycleManager(mHandler, falseContext,
                mock(IQSService.class),
                mMockPackageManagerAdapter,
                mMockBroadcastDispatcher,
                mTileServiceIntent,
                mUser);

        manager.setBindService(true);

        ArgumentCaptor<ServiceConnection> captor = ArgumentCaptor.forClass(ServiceConnection.class);
        verify(falseContext).bindServiceAsUser(any(), captor.capture(), anyInt(), any());

        verify(falseContext).unbindService(captor.getValue());
    }

    private static class TestContextWrapper extends ContextWrapper {
        private IntentFilter mLastIntentFilter;
        private int mLastFlag;