From 67d149ae377bd943eea3c136ebabd921c465afb7 Mon Sep 17 00:00:00 2001 From: Narayan Kamath Date: Wed, 15 May 2019 18:19:44 +0100 Subject: [PATCH] Hide AppButtonsPreferenceController for system modules. Bug: 131927465 Test: atest AppButtonsPreferenceControllerTest Change-Id: I2d3aa3429f61325afe49bfe322522fe9ccd03b2c --- .../AppButtonsPreferenceController.java | 11 +++- .../AppButtonsPreferenceControllerTest.java | 53 ++++++++++++++++--- 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/com/android/settings/applications/appinfo/AppButtonsPreferenceController.java b/src/com/android/settings/applications/appinfo/AppButtonsPreferenceController.java index 7339f2132b2..e15b0e3e4b1 100644 --- a/src/com/android/settings/applications/appinfo/AppButtonsPreferenceController.java +++ b/src/com/android/settings/applications/appinfo/AppButtonsPreferenceController.java @@ -160,8 +160,7 @@ public class AppButtonsPreferenceController extends BasePreferenceController imp @Override public int getAvailabilityStatus() { // TODO(b/37313605): Re-enable once this controller supports instant apps - return mAppEntry != null && !AppUtils.isInstant(mAppEntry.info) - ? AVAILABLE : DISABLED_FOR_USER; + return isInstantApp() || isSystemModule() ? DISABLED_FOR_USER : AVAILABLE; } @Override @@ -685,6 +684,14 @@ public class AppButtonsPreferenceController extends BasePreferenceController imp } } + private boolean isInstantApp() { + return mAppEntry != null && AppUtils.isInstant(mAppEntry.info); + } + + private boolean isSystemModule() { + return mAppEntry != null && AppUtils.isSystemModule(mContext, mAppEntry.info.packageName); + } + /** * Changes the status of disable/enable for a package */ diff --git a/tests/robotests/src/com/android/settings/applications/appinfo/AppButtonsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/applications/appinfo/AppButtonsPreferenceControllerTest.java index ff33d26ea21..e42c3d21f74 100644 --- a/tests/robotests/src/com/android/settings/applications/appinfo/AppButtonsPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/applications/appinfo/AppButtonsPreferenceControllerTest.java @@ -46,6 +46,7 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.RemoteException; import android.os.UserManager; +import android.util.ArraySet; import android.view.View; import com.android.settings.R; @@ -58,6 +59,8 @@ import com.android.settingslib.applications.instantapps.InstantAppDataProvider; import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.widget.ActionButtonsPreference; +import java.util.Set; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -67,6 +70,10 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.stubbing.Answer; import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; +import org.robolectric.annotation.Resetter; import org.robolectric.util.ReflectionHelpers; @RunWith(RobolectricTestRunner.class) @@ -149,6 +156,11 @@ public class AppButtonsPreferenceControllerTest { doAnswer(callable).when(mFragment).startActivityForResult(captor.capture(), anyInt()); } + @After + public void tearDown() { + ShadowAppUtils.reset(); + } + @Test public void retrieveAppEntry_hasAppEntry_notNull() throws PackageManager.NameNotFoundException { @@ -212,15 +224,9 @@ public class AppButtonsPreferenceControllerTest { } @Test + @Config(shadows = ShadowAppUtils.class) public void isAvailable_nonInstantApp() { mController.mAppEntry = mAppEntry; - ReflectionHelpers.setStaticField(AppUtils.class, "sInstantAppDataProvider", - new InstantAppDataProvider() { - @Override - public boolean isInstantApp(ApplicationInfo info) { - return false; - } - }); assertThat(mController.isAvailable()).isTrue(); } @@ -436,6 +442,14 @@ public class AppButtonsPreferenceControllerTest { // Should not crash in this method } + @Test + @Config(shadows = ShadowAppUtils.class) + public void getAvailabilityStatus_systemModule() { + ShadowAppUtils.addHiddenModule(mController.mPackageName); + assertThat(mController.getAvailabilityStatus()).isEqualTo( + AppButtonsPreferenceController.DISABLED_FOR_USER); + } + /** * The test fragment which implements * {@link ButtonActionDialogFragment.AppButtonsDialogListener} @@ -477,4 +491,29 @@ public class AppButtonsPreferenceControllerTest { priority, false /* isStatic */); } + + @Implements(AppUtils.class) + public static class ShadowAppUtils { + + public static Set sSystemModules = new ArraySet<>(); + + @Resetter + public static void reset() { + sSystemModules.clear(); + } + + public static void addHiddenModule(String pkg) { + sSystemModules.add(pkg); + } + + @Implementation + protected static boolean isInstant(ApplicationInfo info) { + return false; + } + + @Implementation + protected static boolean isSystemModule(Context context, String packageName) { + return sSystemModules.contains(packageName); + } + } }