Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 15 | package com.android.server.slice; |
| 16 | |
| 17 | import android.content.Context; |
| 18 | import android.content.pm.UserInfo; |
Jason Monk | b715b04 | 2018-02-01 15:00:05 -0500 | [diff] [blame] | 19 | import android.os.UserHandle; |
Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 20 | import android.os.UserManager; |
| 21 | import android.util.ArraySet; |
Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 22 | import android.util.SparseArray; |
| 23 | |
| 24 | import com.android.internal.util.XmlUtils; |
| 25 | |
| 26 | import org.xmlpull.v1.XmlPullParser; |
| 27 | import org.xmlpull.v1.XmlPullParserException; |
| 28 | import org.xmlpull.v1.XmlSerializer; |
| 29 | |
| 30 | import java.io.IOException; |
| 31 | import java.util.List; |
| 32 | |
| 33 | public 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 Monk | 68ff6aa | 2018-01-31 15:51:52 -0500 | [diff] [blame] | 66 | 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 Monk | b715b04 | 2018-02-01 15:00:05 -0500 | [diff] [blame] | 75 | public void writeXml(XmlSerializer out, int user) throws IOException { |
Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 76 | 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 Monk | b715b04 | 2018-02-01 15:00:05 -0500 | [diff] [blame] | 83 | if (user != UserHandle.USER_ALL && user != userId) { |
| 84 | continue; |
| 85 | } |
Jason Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 86 | 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 Monk | ddb8a96 | 2018-01-30 13:27:33 -0500 | [diff] [blame] | 94 | } |
| 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 | } |