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

Commit f8379d29 authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

Pass user to SecureSetting constructor

This prevents doing IPC potentially on the main thread

Fixes: 154087343
Test: atest BatterySaverTileTest
Change-Id: Ic58b9fb910acbc44f114429c64e335c89634fa2d
parent edaedb06
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -37,11 +37,15 @@ public abstract class SecureSetting extends ContentObserver implements Listenabl

    protected abstract void handleValueChanged(int value, boolean observedChange);

    public SecureSetting(Context context, Handler handler, String settingName) {
    protected SecureSetting(Context context, Handler handler, String settingName) {
        this(context, handler, settingName, ActivityManager.getCurrentUser());
    }

    public SecureSetting(Context context, Handler handler, String settingName, int userId) {
        super(handler);
        mContext = context;
        mSettingName = settingName;
        mUserId = ActivityManager.getCurrentUser();
        mUserId = userId;
    }

    public int getValue() {
@@ -80,4 +84,8 @@ public abstract class SecureSetting extends ContentObserver implements Listenabl
            setListening(true);
        }
    }

    public int getCurrentUser() {
        return mUserId;
    }
}
+11 −2
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.provider.Settings.Secure;
import android.service.quicksettings.Tile;
import android.widget.Switch;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.systemui.R;
import com.android.systemui.plugins.qs.QSTile.BooleanState;
@@ -34,7 +35,8 @@ public class BatterySaverTile extends QSTileImpl<BooleanState> implements
        BatteryController.BatteryStateChangeCallback {

    private final BatteryController mBatteryController;
    private final SecureSetting mSetting;
    @VisibleForTesting
    protected final SecureSetting mSetting;

    private int mLevel;
    private boolean mPowerSave;
@@ -48,7 +50,9 @@ public class BatterySaverTile extends QSTileImpl<BooleanState> implements
        super(host);
        mBatteryController = batteryController;
        mBatteryController.observe(getLifecycle(), this);
        mSetting = new SecureSetting(mContext, mHandler, Secure.LOW_POWER_WARNING_ACKNOWLEDGED) {
        int currentUser = host.getUserContext().getUserId();
        mSetting = new SecureSetting(mContext, mHandler, Secure.LOW_POWER_WARNING_ACKNOWLEDGED,
                currentUser) {
            @Override
            protected void handleValueChanged(int value, boolean observedChange) {
                handleRefreshState(null);
@@ -67,6 +71,11 @@ public class BatterySaverTile extends QSTileImpl<BooleanState> implements
        mSetting.setListening(false);
    }

    @Override
    protected void handleUserSwitch(int newUserId) {
        mSetting.setUserId(newUserId);
    }

    @Override
    public int getMetricsCategory() {
        return MetricsEvent.QS_BATTERY_TILE;
+78 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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 com.android.systemui.qs.tiles

import android.content.Context
import android.testing.AndroidTestingRunner
import android.testing.TestableLooper
import android.testing.TestableLooper.RunWithLooper
import androidx.test.filters.SmallTest
import com.android.systemui.Dependency
import com.android.systemui.SysuiTestCase
import com.android.systemui.qs.QSHost
import com.android.systemui.statusbar.policy.BatteryController
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations

@RunWith(AndroidTestingRunner::class)
@RunWithLooper
@SmallTest
class BatterySaverTileTest : SysuiTestCase() {

    companion object {
        private const val USER = 10
    }

    @Mock
    private lateinit var userContext: Context
    @Mock
    private lateinit var qsHost: QSHost
    @Mock
    private lateinit var batteryController: BatteryController
    private lateinit var testableLooper: TestableLooper
    private lateinit var tile: BatterySaverTile

    @Before
    fun setUp() {
        MockitoAnnotations.initMocks(this)
        testableLooper = TestableLooper.get(this)
        mDependency.injectTestDependency(Dependency.BG_LOOPER, testableLooper.looper)
        `when`(qsHost.userContext).thenReturn(userContext)
        `when`(userContext.userId).thenReturn(USER)

        tile = BatterySaverTile(qsHost, batteryController)
    }

    @Test
    fun testSettingWithCorrectUser() {
        assertEquals(USER, tile.mSetting.currentUser)
    }

    @Test
    fun testSettingChangesUser() {
        tile.userSwitch(USER + 1)

        testableLooper.processAllMessages()

        assertEquals(USER + 1, tile.mSetting.currentUser)
    }
}
 No newline at end of file