From 50490a35ecaeed96dab9fcfc25618967c8213e0e Mon Sep 17 00:00:00 2001 From: Jing Ji Date: Fri, 6 Jan 2023 02:10:42 -0800 Subject: [PATCH] Add a feature flag for the special access to the long background task The entry should be hidden if the RUN_LONG_JOBS can't be toggled. Bug: 255821578 Test: atest AppFilterRegistryTest Test: make -j RunSettingsRoboTests \ ROBOTTEST_FILTER="LongBackgroundTasksDetailsTest| LongBackgroundTasksDetailsPreferenceControllerTest" Test: Manually check the Settings page Change-Id: Ib1c58d93b40afefdf3ca666c661e213d01c542c6 --- AndroidManifest.xml | 4 +- res/xml/special_access.xml | 3 +- .../ApplicationFeatureProvider.java | 6 +++ .../ApplicationFeatureProviderImpl.java | 9 ++++ ...roundTasksDetailsPreferenceController.java | 16 +++++++ .../LongBackgroundTaskController.java | 42 +++++++++++++++++++ ...dTasksDetailsPreferenceControllerTest.java | 7 +++- 7 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 src/com/android/settings/applications/specialaccess/applications/LongBackgroundTaskController.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index a794d8e1384..03bcfe16847 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -853,7 +853,7 @@ android:exported="true" android:label="@string/long_background_tasks_label"> - + - + diff --git a/res/xml/special_access.xml b/res/xml/special_access.xml index 0d2ee51bf78..1a8e4311114 100644 --- a/res/xml/special_access.xml +++ b/res/xml/special_access.xml @@ -111,7 +111,8 @@ android:key="long_background_tasks" android:title="@string/long_background_tasks_title" android:fragment="com.android.settings.applications.manageapplications.ManageApplications" - settings:keywords="@string/keywords_long_background_tasks"> + settings:keywords="@string/keywords_long_background_tasks" + settings:controller="com.android.settings.applications.specialaccess.applications.LongBackgroundTaskController"> diff --git a/src/com/android/settings/applications/ApplicationFeatureProvider.java b/src/com/android/settings/applications/ApplicationFeatureProvider.java index 8a9f000f769..9272b432d9f 100644 --- a/src/com/android/settings/applications/ApplicationFeatureProvider.java +++ b/src/com/android/settings/applications/ApplicationFeatureProvider.java @@ -92,6 +92,12 @@ public interface ApplicationFeatureProvider { return ""; } + /** + * @return {@code true} if the device supports the toggling of the long background task + * permission. + */ + boolean isLongBackgroundTaskPermissionToggleSupported(); + /** * Callback that receives the number of packages installed on the device. */ diff --git a/src/com/android/settings/applications/ApplicationFeatureProviderImpl.java b/src/com/android/settings/applications/ApplicationFeatureProviderImpl.java index 5b74a8884aa..f2a3d6a30c3 100644 --- a/src/com/android/settings/applications/ApplicationFeatureProviderImpl.java +++ b/src/com/android/settings/applications/ApplicationFeatureProviderImpl.java @@ -16,6 +16,10 @@ package com.android.settings.applications; +import static android.Manifest.permission.RUN_LONG_JOBS; +import static android.app.AppOpsManager.OP_RUN_LONG_JOBS; +import static android.app.AppOpsManager.opToPermission; + import android.Manifest; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; @@ -338,4 +342,9 @@ public class ApplicationFeatureProviderImpl implements ApplicationFeatureProvide } throw new IllegalStateException("Missing ComponentInfo!"); } + + @Override + public boolean isLongBackgroundTaskPermissionToggleSupported() { + return TextUtils.equals(RUN_LONG_JOBS, opToPermission(OP_RUN_LONG_JOBS)); + } } diff --git a/src/com/android/settings/applications/appinfo/LongBackgroundTasksDetailsPreferenceController.java b/src/com/android/settings/applications/appinfo/LongBackgroundTasksDetailsPreferenceController.java index a41280bb284..68f893c3602 100644 --- a/src/com/android/settings/applications/appinfo/LongBackgroundTasksDetailsPreferenceController.java +++ b/src/com/android/settings/applications/appinfo/LongBackgroundTasksDetailsPreferenceController.java @@ -24,6 +24,8 @@ import androidx.preference.Preference; import com.android.settings.SettingsPreferenceFragment; import com.android.settings.applications.AppStateLongBackgroundTasksBridge; +import com.android.settings.applications.ApplicationFeatureProvider; +import com.android.settings.overlay.FeatureFactory; /** * Preference controller for @@ -32,14 +34,28 @@ import com.android.settings.applications.AppStateLongBackgroundTasksBridge; public class LongBackgroundTasksDetailsPreferenceController extends AppInfoPreferenceControllerBase { + private final ApplicationFeatureProvider mAppFeatureProvider; + private String mPackageName; public LongBackgroundTasksDetailsPreferenceController(Context context, String key) { super(context, key); + mAppFeatureProvider = FeatureFactory.getFactory(context) + .getApplicationFeatureProvider(context); + } + + @VisibleForTesting + LongBackgroundTasksDetailsPreferenceController(Context context, String key, + ApplicationFeatureProvider appFeatureProvider) { + super(context, key); + mAppFeatureProvider = appFeatureProvider; } @Override public int getAvailabilityStatus() { + if (!mAppFeatureProvider.isLongBackgroundTaskPermissionToggleSupported()) { + return UNSUPPORTED_ON_DEVICE; + } return isCandidate() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; } diff --git a/src/com/android/settings/applications/specialaccess/applications/LongBackgroundTaskController.java b/src/com/android/settings/applications/specialaccess/applications/LongBackgroundTaskController.java new file mode 100644 index 00000000000..586980c2a52 --- /dev/null +++ b/src/com/android/settings/applications/specialaccess/applications/LongBackgroundTaskController.java @@ -0,0 +1,42 @@ +/* + * 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 com.android.settings.applications.specialaccess.applications; + +import android.content.Context; + +import com.android.settings.applications.ApplicationFeatureProvider; +import com.android.settings.core.BasePreferenceController; +import com.android.settings.overlay.FeatureFactory; + +/** + * The controller for the special access to the long background task. + */ +public class LongBackgroundTaskController extends BasePreferenceController { + private final ApplicationFeatureProvider mAppFeatureProvider; + + public LongBackgroundTaskController(Context context, String preferenceKey) { + super(context, preferenceKey); + mAppFeatureProvider = FeatureFactory.getFactory(context) + .getApplicationFeatureProvider(context); + } + + @Override + public int getAvailabilityStatus() { + return mAppFeatureProvider.isLongBackgroundTaskPermissionToggleSupported() + ? AVAILABLE : UNSUPPORTED_ON_DEVICE; + } +} diff --git a/tests/robotests/src/com/android/settings/applications/appinfo/LongBackgroundTasksDetailsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/applications/appinfo/LongBackgroundTasksDetailsPreferenceControllerTest.java index 1b361d288be..cc42c84fbc2 100644 --- a/tests/robotests/src/com/android/settings/applications/appinfo/LongBackgroundTasksDetailsPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/applications/appinfo/LongBackgroundTasksDetailsPreferenceControllerTest.java @@ -27,6 +27,7 @@ import android.content.Context; import androidx.preference.Preference; +import com.android.settings.applications.ApplicationFeatureProvider; import com.android.settings.core.BasePreferenceController; import org.junit.Before; @@ -44,6 +45,8 @@ public class LongBackgroundTasksDetailsPreferenceControllerTest { private AppInfoDashboardFragment mFragment; @Mock private Preference mPreference; + @Mock + private ApplicationFeatureProvider mAppFeatureProvider; private Context mContext; private LongBackgroundTasksDetailsPreferenceController mController; @@ -52,11 +55,13 @@ public class LongBackgroundTasksDetailsPreferenceControllerTest { public void setUp() { MockitoAnnotations.initMocks(this); mContext = spy(RuntimeEnvironment.application); - mController = spy(new LongBackgroundTasksDetailsPreferenceController(mContext, "test_key")); + mController = spy(new LongBackgroundTasksDetailsPreferenceController(mContext, "test_key", + mAppFeatureProvider)); mController.setPackageName("Package1"); mController.setParentFragment(mFragment); final String key = mController.getPreferenceKey(); when(mPreference.getKey()).thenReturn(key); + when(mAppFeatureProvider.isLongBackgroundTaskPermissionToggleSupported()).thenReturn(true); } @Test