diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/NfcTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/NfcTile.java index fb281169b2f16..63e27796f4418 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tiles/NfcTile.java +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/NfcTile.java @@ -47,6 +47,7 @@ import javax.inject.Inject; /** Quick settings tile: Enable/Disable NFC **/ public class NfcTile extends QSTileImpl { + 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 { @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 diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/NfcTileTest.java b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/NfcTileTest.java new file mode 100644 index 0000000000000..b37ac4a2b759e --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/NfcTileTest.java @@ -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()); + } +}