Don't restore nfc qs tile if it is not in the stock

Bug: 162826368
Test: Restore and check qs tiles
Test: atest SystemUITests
Test: atest NfcTileTest
Merged-In: I2863d365aff9a3fe6afe1cb0915dd076bf543cd3
Change-Id: I2863d365aff9a3fe6afe1cb0915dd076bf543cd3
This commit is contained in:
George Chang
2021-01-21 17:18:43 +08:00
parent 8241f19a03
commit 3c4521a1f7
2 changed files with 118 additions and 1 deletions

View File

@@ -47,6 +47,7 @@ import javax.inject.Inject;
/** Quick settings tile: Enable/Disable NFC **/
public class NfcTile extends QSTileImpl<BooleanState> {
private static final String NFC = "nfc";
private final Icon mIcon = ResourceIcon.get(R.drawable.ic_qs_nfc);
private NfcAdapter mAdapter;
@@ -89,7 +90,13 @@ public class NfcTile extends QSTileImpl<BooleanState> {
@Override
public boolean isAvailable() {
return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC);
String stockTiles = mContext.getString(R.string.quick_settings_tiles_stock);
// For the restore from backup case
// Return false when "nfc" is not listed in quick_settings_tiles_stock.
if (stockTiles.contains(NFC)) {
return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC);
}
return false;
}
@Override

View File

@@ -0,0 +1,110 @@
/*
* Copyright (C) 2021 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 static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import androidx.test.filters.SmallTest;
import com.android.internal.logging.MetricsLogger;
import com.android.systemui.R;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.plugins.ActivityStarter;
import com.android.systemui.plugins.statusbar.StatusBarStateController;
import com.android.systemui.qs.QSTileHost;
import com.android.systemui.qs.logging.QSLogger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
@SmallTest
public class NfcTileTest extends SysuiTestCase {
private static final String TILES_STOCK_WITHOUT_NFC = "wifi,cell,battery,dnd,flashlight,bt";
private static final String TILES_STOCK_WITH_NFC = "wifi,cell,battery,dnd,nfc,flashlight,bt";
@Mock
private Context mMockContext;
@Mock
private PackageManager mPackageManager;
@Mock
private ActivityStarter mActivityStarter;
@Mock
private QSTileHost mHost;
@Mock
private MetricsLogger mMetricsLogger;
@Mock
private StatusBarStateController mStatusBarStateController;
@Mock
private QSLogger mQSLogger;
@Mock
private BroadcastDispatcher mBroadcastDispatcher;
private TestableLooper mTestableLooper;
private NfcTile mNfcTile;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mTestableLooper = TestableLooper.get(this);
when(mHost.getContext()).thenReturn(mMockContext);
when(mMockContext.getPackageManager()).thenReturn(mPackageManager);
mNfcTile = new NfcTile(
mHost,
mTestableLooper.getLooper(),
new Handler(mTestableLooper.getLooper()),
mMetricsLogger,
mStatusBarStateController,
mActivityStarter,
mQSLogger,
mBroadcastDispatcher
);
}
@Test
public void testIsAvailable_stockWithoutNfc_returnsFalse() {
when(mMockContext.getString(R.string.quick_settings_tiles_stock)).thenReturn(
TILES_STOCK_WITHOUT_NFC);
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
assertFalse(mNfcTile.isAvailable());
}
@Test
public void testIsAvailable_stockWithNfc_returnsTrue() {
when(mMockContext.getString(R.string.quick_settings_tiles_stock)).thenReturn(
TILES_STOCK_WITH_NFC);
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
assertTrue(mNfcTile.isAvailable());
}
}