• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.os.UserHandle;
21 import android.os.UserManager;
22 
23 import androidx.preference.Preference;
24 
25 import com.android.settingslib.core.AbstractPreferenceController;
26 import com.android.tv.settings.R;
27 import com.android.tv.settings.enterprise.apps.ApplicationFeatureProvider;
28 import com.android.tv.settings.enterprise.apps.EnterpriseDefaultApps;
29 import com.android.tv.settings.overlay.FlavorUtils;
30 
31 /**
32  * Forked from:
33  * Settings/src/com/android/settings/enterprise/EnterpriseSetDefaultAppsPreferenceController.java
34  */
35 public class EnterpriseSetDefaultAppsPreferenceController extends AbstractPreferenceController {
36 
37     private static final String KEY_DEFAULT_APPS = "number_enterprise_set_default_apps";
38     private final ApplicationFeatureProvider mApplicationFeatureProvider;
39     private final UserManager mUserManager;
40 
EnterpriseSetDefaultAppsPreferenceController(Context context)41     public EnterpriseSetDefaultAppsPreferenceController(Context context) {
42         super(context);
43         mApplicationFeatureProvider = FlavorUtils.getFeatureFactory(
44                 context).getApplicationFeatureProvider(
45                 context);
46         mUserManager = context.getSystemService(UserManager.class);
47     }
48 
49     @Override
updateState(Preference preference)50     public void updateState(Preference preference) {
51         final int num = getNumberOfEnterpriseSetDefaultApps();
52         preference.setSummary(mContext.getResources().getQuantityString(
53                 R.plurals.enterprise_privacy_number_packages, num, num));
54     }
55 
56     @Override
isAvailable()57     public boolean isAvailable() {
58         return getNumberOfEnterpriseSetDefaultApps() > 0;
59     }
60 
61     @Override
getPreferenceKey()62     public String getPreferenceKey() {
63         return KEY_DEFAULT_APPS;
64     }
65 
getNumberOfEnterpriseSetDefaultApps()66     private int getNumberOfEnterpriseSetDefaultApps() {
67         int num = 0;
68 
69         for (UserHandle user : mUserManager.getUserProfiles()) {
70             for (EnterpriseDefaultApps app : EnterpriseDefaultApps.values()) {
71                 num += mApplicationFeatureProvider
72                         .findPersistentPreferredActivities(user.getIdentifier(),
73                                 app.getIntents()).size();
74             }
75         }
76         return num;
77     }
78 }
79