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 
17 package com.android.tv.settings.enterprise;
18 
19 import android.content.Context;
20 import android.icu.text.MessageFormat;
21 import android.os.UserHandle;
22 import android.os.UserManager;
23 
24 import androidx.preference.Preference;
25 
26 import com.android.settingslib.core.AbstractPreferenceController;
27 import com.android.tv.settings.R;
28 import com.android.tv.settings.enterprise.apps.ApplicationFeatureProvider;
29 import com.android.tv.settings.enterprise.apps.EnterpriseDefaultApps;
30 import com.android.tv.settings.overlay.FlavorUtils;
31 
32 import java.util.HashMap;
33 import java.util.Locale;
34 import java.util.Map;
35 
36 /**
37  * Forked from:
38  * Settings/src/com/android/settings/enterprise/EnterpriseSetDefaultAppsPreferenceController.java
39  */
40 public class EnterpriseSetDefaultAppsPreferenceController extends AbstractPreferenceController {
41 
42     private static final String KEY_DEFAULT_APPS = "number_enterprise_set_default_apps";
43     private final ApplicationFeatureProvider mApplicationFeatureProvider;
44     private final UserManager mUserManager;
45 
EnterpriseSetDefaultAppsPreferenceController(Context context)46     public EnterpriseSetDefaultAppsPreferenceController(Context context) {
47         super(context);
48         mApplicationFeatureProvider = FlavorUtils.getFeatureFactory(
49                 context).getApplicationFeatureProvider(
50                 context);
51         mUserManager = context.getSystemService(UserManager.class);
52     }
53 
54     @Override
updateState(Preference preference)55     public void updateState(Preference preference) {
56         final int num = getNumberOfEnterpriseSetDefaultApps();
57         MessageFormat msgFormat = new MessageFormat(
58                 mContext.getResources().getString(
59                         R.string.enterprise_privacy_number_packages),
60                 Locale.getDefault());
61         Map<String, Object> arguments = new HashMap<>();
62         arguments.put("count", num);
63         preference.setSummary(msgFormat.format(arguments));
64     }
65 
66     @Override
isAvailable()67     public boolean isAvailable() {
68         return getNumberOfEnterpriseSetDefaultApps() > 0;
69     }
70 
71     @Override
getPreferenceKey()72     public String getPreferenceKey() {
73         return KEY_DEFAULT_APPS;
74     }
75 
getNumberOfEnterpriseSetDefaultApps()76     private int getNumberOfEnterpriseSetDefaultApps() {
77         int num = 0;
78 
79         for (UserHandle user : mUserManager.getUserProfiles()) {
80             for (EnterpriseDefaultApps app : EnterpriseDefaultApps.values()) {
81                 num += mApplicationFeatureProvider
82                         .findPersistentPreferredActivities(user.getIdentifier(),
83                                 app.getIntents()).size();
84             }
85         }
86         return num;
87     }
88 }
89