1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.applications.appinfo;
18 
19 import android.content.Context;
20 import android.content.pm.PackageInfo;
21 
22 import androidx.annotation.VisibleForTesting;
23 import androidx.preference.Preference;
24 
25 import com.android.settings.SettingsPreferenceFragment;
26 import com.android.settings.applications.AppStateLongBackgroundTasksBridge;
27 import com.android.settings.applications.ApplicationFeatureProvider;
28 import com.android.settings.overlay.FeatureFactory;
29 
30 /**
31  * Preference controller for
32  * {@link LongBackgroundTasksDetails} Settings fragment.
33  */
34 public class LongBackgroundTasksDetailsPreferenceController extends
35         AppInfoPreferenceControllerBase {
36 
37     private final ApplicationFeatureProvider mAppFeatureProvider;
38 
39     private String mPackageName;
40 
LongBackgroundTasksDetailsPreferenceController(Context context, String key)41     public LongBackgroundTasksDetailsPreferenceController(Context context, String key) {
42         super(context, key);
43         mAppFeatureProvider = FeatureFactory.getFeatureFactory()
44                 .getApplicationFeatureProvider();
45     }
46 
47     @VisibleForTesting
LongBackgroundTasksDetailsPreferenceController(Context context, String key, ApplicationFeatureProvider appFeatureProvider)48     LongBackgroundTasksDetailsPreferenceController(Context context, String key,
49             ApplicationFeatureProvider appFeatureProvider) {
50         super(context, key);
51         mAppFeatureProvider = appFeatureProvider;
52     }
53 
54     @Override
getAvailabilityStatus()55     public int getAvailabilityStatus() {
56         if (!mAppFeatureProvider.isLongBackgroundTaskPermissionToggleSupported()) {
57             return UNSUPPORTED_ON_DEVICE;
58         }
59         return isCandidate() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
60     }
61 
62     @Override
updateState(Preference preference)63     public void updateState(Preference preference) {
64         preference.setSummary(getPreferenceSummary());
65     }
66 
67     @Override
getDetailFragmentClass()68     protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() {
69         return LongBackgroundTasksDetails.class;
70     }
71 
72     @VisibleForTesting
getPreferenceSummary()73     CharSequence getPreferenceSummary() {
74         return LongBackgroundTasksDetails.getSummary(mContext, mParent.getAppEntry());
75     }
76 
77     @VisibleForTesting
isCandidate()78     boolean isCandidate() {
79         final PackageInfo packageInfo = mParent.getPackageInfo();
80         if (packageInfo == null) {
81             return false;
82         }
83         final AppStateLongBackgroundTasksBridge.LongBackgroundTasksState appState =
84                 new AppStateLongBackgroundTasksBridge(
85                         mContext, /*appState=*/null, /*callback=*/null)
86                         .createPermissionState(mPackageName, packageInfo.applicationInfo.uid);
87         return appState.shouldBeVisible();
88     }
89 
setPackageName(String packageName)90     void setPackageName(String packageName) {
91         mPackageName = packageName;
92     }
93 }
94