UserRestrictionsUtils.java revision 068c54a5be697c3df4657dcda33cd17c4b547710
1/*
2 * Copyright (C) 2015 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.server.pm;
18
19import com.google.android.collect.Sets;
20
21import com.android.internal.util.Preconditions;
22
23import android.os.Bundle;
24import android.os.UserManager;
25
26import org.xmlpull.v1.XmlPullParser;
27import org.xmlpull.v1.XmlSerializer;
28
29import java.io.IOException;
30import java.io.PrintWriter;
31import java.util.Set;
32
33public class UserRestrictionsUtils {
34    private UserRestrictionsUtils() {
35    }
36
37    public static final String[] USER_RESTRICTIONS = {
38            UserManager.DISALLOW_CONFIG_WIFI,
39            UserManager.DISALLOW_MODIFY_ACCOUNTS,
40            UserManager.DISALLOW_INSTALL_APPS,
41            UserManager.DISALLOW_UNINSTALL_APPS,
42            UserManager.DISALLOW_SHARE_LOCATION,
43            UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
44            UserManager.DISALLOW_CONFIG_BLUETOOTH,
45            UserManager.DISALLOW_USB_FILE_TRANSFER,
46            UserManager.DISALLOW_CONFIG_CREDENTIALS,
47            UserManager.DISALLOW_REMOVE_USER,
48            UserManager.DISALLOW_DEBUGGING_FEATURES,
49            UserManager.DISALLOW_CONFIG_VPN,
50            UserManager.DISALLOW_CONFIG_TETHERING,
51            UserManager.DISALLOW_NETWORK_RESET,
52            UserManager.DISALLOW_FACTORY_RESET,
53            UserManager.DISALLOW_ADD_USER,
54            UserManager.ENSURE_VERIFY_APPS,
55            UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
56            UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
57            UserManager.DISALLOW_APPS_CONTROL,
58            UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
59            UserManager.DISALLOW_UNMUTE_MICROPHONE,
60            UserManager.DISALLOW_ADJUST_VOLUME,
61            UserManager.DISALLOW_OUTGOING_CALLS,
62            UserManager.DISALLOW_SMS,
63            UserManager.DISALLOW_FUN,
64            UserManager.DISALLOW_CREATE_WINDOWS,
65            UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE,
66            UserManager.DISALLOW_OUTGOING_BEAM,
67            UserManager.DISALLOW_WALLPAPER,
68            UserManager.DISALLOW_SAFE_BOOT,
69            UserManager.ALLOW_PARENT_PROFILE_APP_LINKING,
70            UserManager.DISALLOW_RECORD_AUDIO,
71    };
72
73    /**
74     * Set of user restrictions, which can only be enforced by the system.
75     */
76    public static final Set<String> SYSTEM_CONTROLLED_USER_RESTRICTIONS = Sets.newArraySet(
77            UserManager.DISALLOW_RECORD_AUDIO);
78
79    /**
80     * Set of user restriction which we don't want to persist.
81     */
82    public static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet(
83            UserManager.DISALLOW_RECORD_AUDIO);
84
85    public static void writeRestrictions(XmlSerializer serializer, Bundle restrictions,
86            String tag) throws IOException {
87        serializer.startTag(null, tag);
88        for (String key : USER_RESTRICTIONS) {
89            if (restrictions.getBoolean(key)
90                    && !NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
91                serializer.attribute(null, key, "true");
92            }
93        }
94        serializer.endTag(null, tag);
95    }
96
97    public static void readRestrictions(XmlPullParser parser, Bundle restrictions)
98            throws IOException {
99        for (String key : USER_RESTRICTIONS) {
100            final String value = parser.getAttributeValue(null, key);
101            if (value != null) {
102                restrictions.putBoolean(key, Boolean.parseBoolean(value));
103            }
104        }
105    }
106
107    public static void merge(Bundle dest, Bundle in) {
108        if (in == null) {
109            return;
110        }
111        for (String key : in.keySet()) {
112            if (in.getBoolean(key, false)) {
113                dest.putBoolean(key, true);
114            }
115        }
116    }
117
118    public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) {
119        boolean noneSet = true;
120        if (restrictions != null) {
121            for (String key : restrictions.keySet()) {
122                if (restrictions.getBoolean(key, false)) {
123                    pw.println(prefix + key);
124                    noneSet = false;
125                }
126            }
127            if (noneSet) {
128                pw.println(prefix + "none");
129            }
130        } else {
131            pw.println(prefix + "null");
132        }
133    }
134}
135