1 /*
2  * Copyright (C) 2021 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 package com.android.car.settings.enterprise;
17 
18 import android.annotation.Nullable;
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.os.UserHandle;
22 
23 import androidx.preference.Preference;
24 
25 import com.android.car.settings.R;
26 import com.android.car.settings.common.FragmentController;
27 import com.android.car.settingslib.applications.ApplicationFeatureProvider;
28 import com.android.car.settingslib.enterprise.EnterpriseDefaultApps;
29 import com.android.internal.annotations.VisibleForTesting;
30 
31 /**
32 * Controller to show the number of default apps were set by the device owner.
33 */
34 public final class EnterpriseSetDefaultAppsPreferenceController
35         extends BaseEnterprisePrivacyPreferenceController<Preference> {
36 
37     @Nullable
38     private Integer mCount;
39 
EnterpriseSetDefaultAppsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)40     public EnterpriseSetDefaultAppsPreferenceController(Context context, String preferenceKey,
41             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
42         this(context, preferenceKey, fragmentController, uxRestrictions,
43                 /* applicationFeatureProvider= */ null);
44     }
45 
46     @VisibleForTesting
EnterpriseSetDefaultAppsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, @Nullable ApplicationFeatureProvider applicationFeatureProvider)47     EnterpriseSetDefaultAppsPreferenceController(Context context, String preferenceKey,
48             FragmentController fragmentController, CarUxRestrictions uxRestrictions,
49             @Nullable ApplicationFeatureProvider applicationFeatureProvider) {
50         super(context, preferenceKey, fragmentController, uxRestrictions,
51                 /* enterprisePrivacyFeatureProvider= */ null, applicationFeatureProvider);
52     }
53 
54     @Override
getDefaultAvailabilityStatus()55     protected int getDefaultAvailabilityStatus() {
56         if (mCount == null) {
57             mCount = getNumberOfEnterpriseSetDefaultApps();
58             mLogger.d("Number of apps: " + mCount);
59         }
60 
61         return mCount > 0 ? AVAILABLE : DISABLED_FOR_PROFILE;
62     }
63 
64     @Override
updateState(Preference preference)65     protected void updateState(Preference preference) {
66         preference.setSummary(getContext().getResources().getQuantityString(
67                 R.plurals.enterprise_privacy_number_packages, mCount, mCount));
68     };
69 
getNumberOfEnterpriseSetDefaultApps()70     private int getNumberOfEnterpriseSetDefaultApps() {
71         int num = 0;
72         UserHandle user = getContext().getUser();
73         // TODO(b/1703325190): to support work profiles, it should use and outer lopp:
74         // for (UserHandle user : mUserFeatureProvider.getUserProfiles()) {
75         for (EnterpriseDefaultApps app : EnterpriseDefaultApps.values()) {
76             num += mApplicationFeatureProvider
77                     .findPersistentPreferredActivities(user.getIdentifier(), app.getIntents())
78                     .size();
79         }
80         return num;
81     }
82 
83 }
84