1/*
2 * Copyright (C) 2014 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
17package com.android.settings;
18
19import android.content.BroadcastReceiver;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
25import android.content.pm.UserInfo;
26import android.os.UserHandle;
27import android.os.UserManager;
28import android.util.Log;
29
30import java.util.List;
31
32import static android.content.pm.PackageManager.GET_ACTIVITIES;
33import static android.content.pm.PackageManager.GET_META_DATA;
34import static android.content.pm.PackageManager.GET_RESOLVED_FILTER;
35import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS;
36
37/**
38 * Listens to {@link Intent.ACTION_PRE_BOOT_COMPLETED} and {@link Intent.ACTION_USER_INITIALIZED}
39 * performs setup steps for a managed profile (disables the launcher icon of the Settings app,
40 * adds cross-profile intent filters for the appropriate Settings activities), and disables the
41 * webview setting for non-admin users.
42 */
43public class SettingsInitialize extends BroadcastReceiver {
44    private static final String TAG = "Settings";
45    private static final String PRIMARY_PROFILE_SETTING =
46            "com.android.settings.PRIMARY_PROFILE_CONTROLLED";
47    private static final String SETTINGS_PACKAGE = "com.android.settings";
48    private static final String WEBVIEW_IMPLEMENTATION_ACTIVITY = ".WebViewImplementation";
49
50    @Override
51    public void onReceive(Context context, Intent broadcast) {
52        final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
53        UserInfo userInfo = um.getUserInfo(UserHandle.myUserId());
54        final PackageManager pm  = context.getPackageManager();
55        managedProfileSetup(context, pm, broadcast, userInfo);
56        webviewSettingSetup(context, pm, userInfo);
57    }
58
59    private void managedProfileSetup(Context context, final PackageManager pm, Intent broadcast,
60            UserInfo userInfo) {
61        if (userInfo == null || !userInfo.isManagedProfile()) {
62            return;
63        }
64        Log.i(TAG, "Received broadcast: " + broadcast.getAction()
65                + ". Setting up intent forwarding for managed profile.");
66        // Clear any previous intent forwarding we set up
67        pm.clearCrossProfileIntentFilters(userInfo.id);
68
69        // Set up intent forwarding for implicit intents
70        Intent intent = new Intent();
71        intent.addCategory(Intent.CATEGORY_DEFAULT);
72        intent.setPackage(context.getPackageName());
73
74        // Resolves activities for the managed profile (which we're running as)
75        List<ResolveInfo> resolvedIntents = pm.queryIntentActivities(intent,
76                GET_ACTIVITIES | GET_META_DATA | GET_RESOLVED_FILTER | MATCH_DISABLED_COMPONENTS);
77        final int count = resolvedIntents.size();
78        for (int i = 0; i < count; i++) {
79            ResolveInfo info = resolvedIntents.get(i);
80            if (info.filter != null && info.activityInfo != null
81                    && info.activityInfo.metaData != null) {
82                boolean shouldForward = info.activityInfo.metaData.getBoolean(
83                        PRIMARY_PROFILE_SETTING);
84                if (shouldForward) {
85                    pm.addCrossProfileIntentFilter(info.filter, userInfo.id,
86                        userInfo.profileGroupId, PackageManager.SKIP_CURRENT_PROFILE);
87                }
88            }
89        }
90
91        // Disable launcher icon
92        ComponentName settingsComponentName = new ComponentName(context, Settings.class);
93        pm.setComponentEnabledSetting(settingsComponentName,
94                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
95        // Disable shortcut picker.
96        ComponentName shortcutComponentName = new ComponentName(context, CreateShortcut.class);
97        pm.setComponentEnabledSetting(shortcutComponentName,
98                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
99    }
100
101    // Disable WebView Setting if the current user is not an admin
102    private void webviewSettingSetup(Context context, PackageManager pm, UserInfo userInfo) {
103        if (userInfo == null) {
104            return;
105        }
106        ComponentName settingsComponentName =
107            new ComponentName(SETTINGS_PACKAGE, SETTINGS_PACKAGE + WEBVIEW_IMPLEMENTATION_ACTIVITY);
108        pm.setComponentEnabledSetting(settingsComponentName,
109                userInfo.isAdmin() ?
110                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
111                        PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
112                PackageManager.DONT_KILL_APP);
113    }
114}
115