SharedUserSetting.java revision 0a4c63f958d133f15398900a26668d1e2661f783
1/*
2 * Copyright (C) 2011 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 android.annotation.Nullable;
20import android.content.pm.ApplicationInfo;
21import android.content.pm.PackageParser;
22import android.service.pm.PackageServiceDumpProto;
23import android.util.ArraySet;
24import android.util.proto.ProtoOutputStream;
25
26import java.util.ArrayList;
27import java.util.Collection;
28import java.util.List;
29
30/**
31 * Settings data for a particular shared user ID we know about.
32 */
33public final class SharedUserSetting extends SettingBase {
34    final String name;
35
36    int userId;
37
38    // flags that are associated with this uid, regardless of any package flags
39    int uidFlags;
40    int uidPrivateFlags;
41
42    final ArraySet<PackageSetting> packages = new ArraySet<PackageSetting>();
43
44    final PackageSignatures signatures = new PackageSignatures();
45
46    SharedUserSetting(String _name, int _pkgFlags, int _pkgPrivateFlags) {
47        super(_pkgFlags, _pkgPrivateFlags);
48        uidFlags =  _pkgFlags;
49        uidPrivateFlags = _pkgPrivateFlags;
50        name = _name;
51    }
52
53    @Override
54    public String toString() {
55        return "SharedUserSetting{" + Integer.toHexString(System.identityHashCode(this)) + " "
56                + name + "/" + userId + "}";
57    }
58
59    public void writeToProto(ProtoOutputStream proto, long fieldId) {
60        long token = proto.start(fieldId);
61        proto.write(PackageServiceDumpProto.SharedUserProto.USER_ID, userId);
62        proto.write(PackageServiceDumpProto.SharedUserProto.NAME, name);
63        proto.end(token);
64    }
65
66    void removePackage(PackageSetting packageSetting) {
67        if (packages.remove(packageSetting)) {
68            // recalculate the pkgFlags for this shared user if needed
69            if ((this.pkgFlags & packageSetting.pkgFlags) != 0) {
70                int aggregatedFlags = uidFlags;
71                for (PackageSetting ps : packages) {
72                    aggregatedFlags |= ps.pkgFlags;
73                }
74                setFlags(aggregatedFlags);
75            }
76            if ((this.pkgPrivateFlags & packageSetting.pkgPrivateFlags) != 0) {
77                int aggregatedPrivateFlags = uidPrivateFlags;
78                for (PackageSetting ps : packages) {
79                    aggregatedPrivateFlags |= ps.pkgPrivateFlags;
80                }
81                setPrivateFlags(aggregatedPrivateFlags);
82            }
83        }
84    }
85
86    void addPackage(PackageSetting packageSetting) {
87        if (packages.add(packageSetting)) {
88            setFlags(this.pkgFlags | packageSetting.pkgFlags);
89            setPrivateFlags(this.pkgPrivateFlags | packageSetting.pkgPrivateFlags);
90        }
91    }
92
93    public @Nullable List<PackageParser.Package> getPackages() {
94        if (packages == null || packages.size() == 0) {
95            return null;
96        }
97        final ArrayList<PackageParser.Package> pkgList = new ArrayList<>(packages.size());
98        for (PackageSetting ps : packages) {
99            if ((ps == null) || (ps.pkg == null)) {
100                continue;
101            }
102            pkgList.add(ps.pkg);
103        }
104        return pkgList;
105    }
106
107    public boolean isPrivileged() {
108        return (this.pkgPrivateFlags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
109    }
110}
111