summaryrefslogtreecommitdiff
path: root/Settings/src/com/android/tv/settings/enterprise/EnterpriseSetDefaultAppsListPreferenceController.java
blob: d8d374054ba5d4ae83a449508a06fe4dece7f057 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.settings.enterprise;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.icu.text.MessageFormat;
import android.os.UserHandle;
import android.os.UserManager;

import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;

import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.utils.ThreadUtils;
import com.android.tv.settings.R;
import com.android.tv.settings.SettingsPreferenceFragment;
import com.android.tv.settings.library.enterprise.EnterprisePrivacyFeatureProvider;
import com.android.tv.settings.library.enterprise.apps.ApplicationFeatureProvider;
import com.android.tv.settings.library.enterprise.apps.EnterpriseDefaultApps;
import com.android.tv.settings.library.enterprise.apps.UserAppInfo;
import com.android.tv.settings.library.overlay.FeatureFactory;
import com.android.tv.settings.library.overlay.FlavorUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;


/**
 * PreferenceController that builds a dynamic list of default apps set by device or profile owner.
 * Forked from:
 * Settings/src/com/android/settings/enterprise/EnterpriseSetDefaultAppsListPreferenceController
 * .java
 */
public class EnterpriseSetDefaultAppsListPreferenceController extends
        AbstractPreferenceController {
    private static final String KEY_DASHBOARD_TITLE_PLACEHOLDER = "dashboard_tile_placeholder";

    private final PackageManager mPm;
    private final SettingsPreferenceFragment mParent;
    private final ApplicationFeatureProvider mApplicationFeatureProvider;
    private final EnterprisePrivacyFeatureProvider mEnterprisePrivacyFeatureProvider;
    private final UserManager mUserManager;

    private List<UserInfo> mUsers = Collections.emptyList();
    private List<EnumMap<EnterpriseDefaultApps, List<ApplicationInfo>>> mApps =
            Collections.emptyList();

    public EnterpriseSetDefaultAppsListPreferenceController(Context context,
            SettingsPreferenceFragment parent, PackageManager packageManager) {
        super(context);
        mPm = packageManager;
        mParent = parent;
        final FeatureFactory factory = FlavorUtils.getFeatureFactory(context);
        mApplicationFeatureProvider = factory.getApplicationFeatureProvider(context);
        mEnterprisePrivacyFeatureProvider = factory.getEnterprisePrivacyFeatureProvider(context);
        mUserManager = context.getSystemService(UserManager.class);
        buildAppList();
    }

    /**
     * Builds data for UI. Updates mUsers and mApps so that they contain non-empty list.
     */
    private void buildAppList() {
        mUsers = new ArrayList<>();
        mApps = new ArrayList<>();
        for (UserHandle user : mUserManager.getUserProfiles()) {
            boolean hasDefaultsForUser = false;
            EnumMap<EnterpriseDefaultApps, List<ApplicationInfo>> userMap = null;

            for (EnterpriseDefaultApps typeOfDefault : EnterpriseDefaultApps.values()) {
                List<UserAppInfo> apps =
                        mApplicationFeatureProvider.findPersistentPreferredActivities(
                                user.getIdentifier(), typeOfDefault.getIntents());
                if (apps.isEmpty()) {
                    continue;
                }
                if (!hasDefaultsForUser) {
                    hasDefaultsForUser = true;
                    mUsers.add(apps.get(0).userInfo);
                    userMap = new EnumMap<>(EnterpriseDefaultApps.class);
                    mApps.add(userMap);
                }
                ArrayList<ApplicationInfo> applicationInfos = new ArrayList<>();
                for (UserAppInfo userAppInfo : apps) {
                    applicationInfos.add(userAppInfo.appInfo);
                }
                userMap.put(typeOfDefault, applicationInfos);
            }
        }
        ThreadUtils.postOnMainThread(() -> updateUi());
    }

    @Override
    public boolean isAvailable() {
        return true;
    }

    @Override
    public String getPreferenceKey() {
        return KEY_DASHBOARD_TITLE_PLACEHOLDER;
    }

    private void updateUi() {
        final Context prefContext = mParent.getPreferenceManager().getContext();
        final PreferenceScreen screen = mParent.getPreferenceScreen();
        if (screen == null) {
            return;
        }
        if (!mEnterprisePrivacyFeatureProvider.isInCompMode() && mUsers.size() == 1) {
            createPreferences(prefContext, screen, mApps.get(0));
        } else {
            for (int i = 0; i < mUsers.size(); i++) {
                final UserInfo userInfo = mUsers.get(i);
                final PreferenceCategory category = new PreferenceCategory(prefContext);
                screen.addPreference(category);
                if (userInfo.isManagedProfile()) {
                    category.setTitle(R.string.category_work);
                } else {
                    category.setTitle(R.string.category_personal);
                }
                category.setOrder(i);
                createPreferences(prefContext, category, mApps.get(i));
            }
        }
    }

    private void createPreferences(Context prefContext, PreferenceGroup group,
            EnumMap<EnterpriseDefaultApps, List<ApplicationInfo>> apps) {
        if (group == null) {
            return;
        }
        for (EnterpriseDefaultApps typeOfDefault : EnterpriseDefaultApps.values()) {
            final List<ApplicationInfo> appsForCategory = apps.get(typeOfDefault);
            if (appsForCategory == null || appsForCategory.isEmpty()) {
                continue;
            }
            final Preference preference = new Preference(prefContext);
            preference.setTitle(getTitle(prefContext, typeOfDefault, appsForCategory.size()));
            preference.setSummary(buildSummaryString(prefContext, appsForCategory));
            preference.setOrder(typeOfDefault.ordinal());
            preference.setSelectable(false);
            group.addPreference(preference);
        }
    }

    private CharSequence buildSummaryString(Context context, List<ApplicationInfo> apps) {
        final CharSequence[] appNames = new String[apps.size()];
        for (int i = 0; i < apps.size(); i++) {
            appNames[i] = apps.get(i).loadLabel(mPm);
        }
        if (apps.size() == 1) {
            return appNames[0];
        } else if (apps.size() == 2) {
            return context.getString(R.string.app_names_concatenation_template_2, appNames[0],
                    appNames[1]);
        } else {
            return context.getString(R.string.app_names_concatenation_template_3, appNames[0],
                    appNames[1], appNames[2]);
        }
    }

    private String getTitle(Context context, EnterpriseDefaultApps typeOfDefault, int appCount) {
        switch (typeOfDefault) {
            case BROWSER:
                return context.getString(R.string.default_browser_title);
            case CALENDAR:
                return context.getString(R.string.default_calendar_app_title);
            case CONTACTS:
                return context.getString(R.string.default_contacts_app_title);
            case PHONE:
                MessageFormat msgFormatPhone = new MessageFormat(
                        context.getString(R.string.default_phone_app_title),
                        Locale.getDefault());
                Map<String, Object> argumentsPhone = new HashMap<>();
                argumentsPhone.put("count", appCount);
                return  msgFormatPhone.format(argumentsPhone);
            case MAP:
                return context.getString(R.string.default_map_app_title);
            case EMAIL:
                MessageFormat msgFormatEmail = new MessageFormat(
                        context.getString(R.string.default_email_app_title),
                        Locale.getDefault());
                Map<String, Object> argumentsEmail = new HashMap<>();
                argumentsEmail.put("count", appCount);
                return  msgFormatEmail.format(argumentsEmail);
            case CAMERA:
                MessageFormat msgFormatCamera = new MessageFormat(
                        context.getString(R.string.default_camera_app_title),
                        Locale.getDefault());
                Map<String, Object> argumentsCamera = new HashMap<>();
                argumentsCamera.put("count", appCount);
                return  msgFormatCamera.format(argumentsCamera);
            default:
                throw new IllegalStateException("Unknown type of default " + typeOfDefault);
        }
    }

}