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

Commit 0549cf12 authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

Don't crash TileService app when systemui crashes

Prevent the host app from crashing if systemui crashes while binding to
it. Instead, just return null and let the new systemui process bind
again, as the service will be unbound.

Test: atest FrameworksCoreTests:TileServiceTest
Test: manual, tiles still work
Fixes: 168756844
Change-Id: I70e0e8a223a84874415f7e649106398a5e039097
parent 3fda2bf2
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -353,7 +353,13 @@ public class TileService extends Service {
        try {
            mTile = mService.getTile(mTileToken);
        } catch (RemoteException e) {
            throw new RuntimeException("Unable to reach IQSService", e);
            String name = TileService.this.getClass().getSimpleName();
            Log.w(TAG, name + " - Couldn't get tile from IQSService.", e);
            // If we couldn't receive the tile, there's not much reason to continue as users won't
            // be able to interact. Returning `null` will trigger an unbind in SystemUI and
            // eventually we'll rebind when needed. This usually means that SystemUI crashed
            // right after binding and therefore `mService` is outdated.
            return null;
        }
        if (mTile != null) {
            mTile.setService(mService, mTileToken);
+107 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.service.quicksettings;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.testing.TestableLooper;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@SmallTest
@RunWith(AndroidJUnit4.class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
public class TileServiceTest {

    @Mock
    private IQSService.Stub mIQSService;

    private IBinder mTileToken;
    private TileService mTileService;
    private Tile mTile;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        mTileToken = new Binder();
        when(mIQSService.asBinder()).thenCallRealMethod();
        when(mIQSService.queryLocalInterface(anyString())).thenReturn(mIQSService);

        mTile = new Tile();

        mTileService = new TileService();
    }

    @Test
    public void testErrorRetrievingTile_nullBinding() throws RemoteException {
        Intent intent = new Intent();
        intent.putExtra(TileService.EXTRA_SERVICE, mIQSService);
        intent.putExtra(TileService.EXTRA_TOKEN, mTileToken);
        when(mIQSService.getTile(mTileToken)).thenThrow(new RemoteException());

        IBinder result = mTileService.onBind(intent);
        assertNull(result);
    }

    @Test
    public void testNullTile_doesntSendStartSuccessful() throws RemoteException {
        Intent intent = new Intent();
        intent.putExtra(TileService.EXTRA_SERVICE, mIQSService);
        intent.putExtra(TileService.EXTRA_TOKEN, mTileToken);
        when(mIQSService.getTile(mTileToken)).thenReturn(null);

        IBinder result = mTileService.onBind(intent);

        assertNotNull(result);
        verify(mIQSService, never()).onStartSuccessful(any());
    }

    @Test
    public void testBindSuccessful() throws RemoteException {
        Intent intent = new Intent();
        intent.putExtra(TileService.EXTRA_SERVICE, mIQSService);
        intent.putExtra(TileService.EXTRA_TOKEN, mTileToken);
        when(mIQSService.getTile(mTileToken)).thenReturn(mTile);

        IBinder result = mTileService.onBind(intent);

        assertNotNull(result);
        verify(mIQSService).onStartSuccessful(mTileToken);

        mTile.updateTile();
        verify(mIQSService).updateQsTile(mTile, mTileToken);
    }

}