1 
2 /*
3  * Copyright (C) 2017 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
6  * except in compliance with the License. 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 distributed under the
11  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
12  * KIND, either express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 package com.android.settings.enterprise;
16 
17 import android.content.Context;
18 import android.os.UserHandle;
19 import android.support.v7.preference.Preference;
20 
21 import com.android.settings.R;
22 import com.android.settings.applications.ApplicationFeatureProvider;
23 import com.android.settings.applications.EnterpriseDefaultApps;
24 import com.android.settings.core.DynamicAvailabilityPreferenceController;
25 import com.android.settings.core.lifecycle.Lifecycle;
26 import com.android.settings.overlay.FeatureFactory;
27 import com.android.settings.users.UserFeatureProvider;
28 
29 public class EnterpriseSetDefaultAppsPreferenceController
30         extends DynamicAvailabilityPreferenceController {
31 
32     private static final String KEY_DEFAULT_APPS = "number_enterprise_set_default_apps";
33     private final ApplicationFeatureProvider mApplicationFeatureProvider;
34     private final UserFeatureProvider mUserFeatureProvider;
35 
EnterpriseSetDefaultAppsPreferenceController(Context context, Lifecycle lifecycle)36     public EnterpriseSetDefaultAppsPreferenceController(Context context, Lifecycle lifecycle) {
37         super(context, lifecycle);
38         final FeatureFactory factory = FeatureFactory.getFactory(context);
39         mApplicationFeatureProvider = factory.getApplicationFeatureProvider(context);
40         mUserFeatureProvider = factory.getUserFeatureProvider(context);
41     }
42 
43     @Override
updateState(Preference preference)44     public void updateState(Preference preference) {
45         final int num = getNumberOfEnterpriseSetDefaultApps();
46         preference.setSummary(mContext.getResources().getQuantityString(
47                 R.plurals.enterprise_privacy_number_packages, num, num));
48     }
49 
50     @Override
isAvailable()51     public boolean isAvailable() {
52         final boolean available = getNumberOfEnterpriseSetDefaultApps() > 0;
53         notifyOnAvailabilityUpdate(available);
54         return available;
55     }
56 
57     @Override
getPreferenceKey()58     public String getPreferenceKey() {
59         return KEY_DEFAULT_APPS;
60     }
61 
getNumberOfEnterpriseSetDefaultApps()62     private int getNumberOfEnterpriseSetDefaultApps() {
63         int num = 0;
64         for (UserHandle user : mUserFeatureProvider.getUserProfiles()) {
65             for (EnterpriseDefaultApps app : EnterpriseDefaultApps.values()) {
66                 num += mApplicationFeatureProvider
67                         .findPersistentPreferredActivities(user.getIdentifier(),
68                                 app.getIntents()).size();
69             }
70         }
71         return num;
72     }
73 }
74