blob: 6f5afa207d310b453d291a3ca3a78283b0878d1a [file] [log] [blame]
Jason Monkddb8a962018-01-30 13:27:33 -05001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.server.slice;
16
17import android.content.Context;
18import android.content.pm.UserInfo;
Jason Monkb715b042018-02-01 15:00:05 -050019import android.os.UserHandle;
Jason Monkddb8a962018-01-30 13:27:33 -050020import android.os.UserManager;
21import android.util.ArraySet;
Jason Monkddb8a962018-01-30 13:27:33 -050022import android.util.SparseArray;
23
24import com.android.internal.util.XmlUtils;
25
26import org.xmlpull.v1.XmlPullParser;
27import org.xmlpull.v1.XmlPullParserException;
28import org.xmlpull.v1.XmlSerializer;
29
30import java.io.IOException;
31import java.util.List;
32
33public class SliceFullAccessList {
34
35 static final int DB_VERSION = 1;
36 private static final String TAG = "SliceFullAccessList";
37
38 private static final String TAG_LIST = "slice-access-list";
39 private static final String TAG_PKG = "pkg";
40 private static final String TAG_USER = "user";
41
42 private final String ATT_USER_ID = "user";
43 private final String ATT_VERSION = "version";
44
45 private final SparseArray<ArraySet<String>> mFullAccessPkgs = new SparseArray<>();
46 private final Context mContext;
47
48 public SliceFullAccessList(Context context) {
49 mContext = context;
50 }
51
52 public boolean hasFullAccess(String pkg, int userId) {
53 ArraySet<String> pkgs = mFullAccessPkgs.get(userId, null);
54 return pkgs != null && pkgs.contains(pkg);
55 }
56
57 public void grantFullAccess(String pkg, int userId) {
58 ArraySet<String> pkgs = mFullAccessPkgs.get(userId, null);
59 if (pkgs == null) {
60 pkgs = new ArraySet<>();
61 mFullAccessPkgs.put(userId, pkgs);
62 }
63 pkgs.add(pkg);
64 }
65
Jason Monk68ff6aa2018-01-31 15:51:52 -050066 public void removeGrant(String pkg, int userId) {
67 ArraySet<String> pkgs = mFullAccessPkgs.get(userId, null);
68 if (pkgs == null) {
69 pkgs = new ArraySet<>();
70 mFullAccessPkgs.put(userId, pkgs);
71 }
72 pkgs.remove(pkg);
73 }
74
Jason Monkb715b042018-02-01 15:00:05 -050075 public void writeXml(XmlSerializer out, int user) throws IOException {
Jason Monkddb8a962018-01-30 13:27:33 -050076 out.startTag(null, TAG_LIST);
77 out.attribute(null, ATT_VERSION, String.valueOf(DB_VERSION));
78
79 final int N = mFullAccessPkgs.size();
80 for (int i = 0 ; i < N; i++) {
81 final int userId = mFullAccessPkgs.keyAt(i);
82 final ArraySet<String> pkgs = mFullAccessPkgs.valueAt(i);
Jason Monkb715b042018-02-01 15:00:05 -050083 if (user != UserHandle.USER_ALL && user != userId) {
84 continue;
85 }
Jason Monkddb8a962018-01-30 13:27:33 -050086 out.startTag(null, TAG_USER);
87 out.attribute(null, ATT_USER_ID, Integer.toString(userId));
88 if (pkgs != null) {
89 final int M = pkgs.size();
90 for (int j = 0; j < M; j++) {
91 out.startTag(null, TAG_PKG);
92 out.text(pkgs.valueAt(j));
93 out.endTag(null, TAG_PKG);
Jason Monkddb8a962018-01-30 13:27:33 -050094 }
95 }
96 out.endTag(null, TAG_USER);
97 }
98 out.endTag(null, TAG_LIST);
99 }
100
101 public void readXml(XmlPullParser parser) throws XmlPullParserException, IOException {
102 // upgrade xml
103 int xmlVersion = XmlUtils.readIntAttribute(parser, ATT_VERSION, 0);
104 final List<UserInfo> activeUsers = UserManager.get(mContext).getUsers(true);
105 for (UserInfo userInfo : activeUsers) {
106 upgradeXml(xmlVersion, userInfo.getUserHandle().getIdentifier());
107 }
108
109 mFullAccessPkgs.clear();
110 // read grants
111 int type;
112 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
113 String tag = parser.getName();
114 if (type == XmlPullParser.END_TAG
115 && TAG_LIST.equals(tag)) {
116 break;
117 }
118 if (type == XmlPullParser.START_TAG) {
119 if (TAG_USER.equals(tag)) {
120 final int userId = XmlUtils.readIntAttribute(parser, ATT_USER_ID, 0);
121 ArraySet<String> pkgs = new ArraySet<>();
122 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
123 String userTag = parser.getName();
124 if (type == XmlPullParser.END_TAG
125 && TAG_USER.equals(userTag)) {
126 break;
127 }
128 if (type == XmlPullParser.START_TAG) {
129 if (TAG_PKG.equals(userTag)) {
130 final String pkg = parser.nextText();
131 pkgs.add(pkg);
132 }
133 }
134 }
135 mFullAccessPkgs.put(userId, pkgs);
136 }
137 }
138 }
139 }
140
141 protected void upgradeXml(final int xmlVersion, final int userId) {}
142}