PackageSetting.java revision 0eb9738d1708d9aa7846782046e6828ffc9fe901
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.content.pm.ApplicationInfo;
20import android.content.pm.PackageManager;
21import android.content.pm.PackageParser;
22import android.content.pm.UserInfo;
23import android.service.pm.PackageProto;
24import android.util.proto.ProtoOutputStream;
25
26import com.android.server.pm.permission.PermissionsState;
27
28import java.io.File;
29import java.util.List;
30
31/**
32 * Settings data for a particular package we know about.
33 */
34public final class PackageSetting extends PackageSettingBase {
35    int appId;
36    PackageParser.Package pkg;
37    /**
38     * WARNING. The object reference is important. We perform integer equality and NOT
39     * object equality to check whether shared user settings are the same.
40     */
41    SharedUserSetting sharedUser;
42    /**
43     * Temporary holding space for the shared user ID. While parsing package settings, the
44     * shared users tag may come after the packages. In this case, we must delay linking the
45     * shared user setting with the package setting. The shared user ID lets us link the
46     * two objects.
47     */
48    private int sharedUserId;
49
50    PackageSetting(String name, String realName, File codePath, File resourcePath,
51            String legacyNativeLibraryPathString, String primaryCpuAbiString,
52            String secondaryCpuAbiString, String cpuAbiOverrideString,
53            int pVersionCode, int pkgFlags, int privateFlags, String parentPackageName,
54            List<String> childPackageNames, int sharedUserId, String[] usesStaticLibraries,
55            int[] usesStaticLibrariesVersions) {
56        super(name, realName, codePath, resourcePath, legacyNativeLibraryPathString,
57                primaryCpuAbiString, secondaryCpuAbiString, cpuAbiOverrideString,
58                pVersionCode, pkgFlags, privateFlags, parentPackageName, childPackageNames,
59                usesStaticLibraries, usesStaticLibrariesVersions);
60        this.sharedUserId = sharedUserId;
61    }
62
63    /**
64     * New instance of PackageSetting replicating the original settings.
65     * Note that it keeps the same PackageParser.Package instance.
66     */
67    PackageSetting(PackageSetting orig) {
68        super(orig, orig.realName);
69        doCopy(orig);
70    }
71
72    /**
73     * New instance of PackageSetting replicating the original settings, but, allows specifying
74     * a real package name.
75     * Note that it keeps the same PackageParser.Package instance.
76     */
77    PackageSetting(PackageSetting orig, String realPkgName) {
78        super(orig, realPkgName);
79        doCopy(orig);
80    }
81
82    public int getSharedUserId() {
83        if (sharedUser != null) {
84            return sharedUser.userId;
85        }
86        return sharedUserId;
87    }
88
89    @Override
90    public String toString() {
91        return "PackageSetting{"
92            + Integer.toHexString(System.identityHashCode(this))
93            + " " + name + "/" + appId + "}";
94    }
95
96    public void copyFrom(PackageSetting orig) {
97        super.copyFrom(orig);
98        doCopy(orig);
99    }
100
101    private void doCopy(PackageSetting orig) {
102        appId = orig.appId;
103        pkg = orig.pkg;
104        sharedUser = orig.sharedUser;
105        sharedUserId = orig.sharedUserId;
106    }
107
108    @Override
109    public PermissionsState getPermissionsState() {
110        return (sharedUser != null)
111                ? sharedUser.getPermissionsState()
112                : super.getPermissionsState();
113    }
114
115    public PackageParser.Package getPackage() {
116        return pkg;
117    }
118
119    public int getAppId() {
120        return appId;
121    }
122
123    public boolean isPrivileged() {
124        return (pkgPrivateFlags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
125    }
126
127    public boolean isOem() {
128        return (pkgPrivateFlags & ApplicationInfo.PRIVATE_FLAG_OEM) != 0;
129    }
130
131    public boolean isForwardLocked() {
132        return (pkgPrivateFlags & ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK) != 0;
133    }
134
135    public boolean isSystem() {
136        return (pkgFlags & ApplicationInfo.FLAG_SYSTEM) != 0;
137    }
138
139    @Override
140    public boolean isSharedUser() {
141        return sharedUser != null;
142    }
143
144    public boolean isMatch(int flags) {
145        if ((flags & PackageManager.MATCH_SYSTEM_ONLY) != 0) {
146            return isSystem();
147        }
148        return true;
149    }
150
151    public boolean hasChildPackages() {
152        return childPackageNames != null && !childPackageNames.isEmpty();
153    }
154
155    public void writeToProto(ProtoOutputStream proto, long fieldId, List<UserInfo> users) {
156        final long packageToken = proto.start(fieldId);
157        proto.write(PackageProto.NAME, (realName != null ? realName : name));
158        proto.write(PackageProto.UID, appId);
159        proto.write(PackageProto.VERSION_CODE, versionCode);
160        proto.write(PackageProto.VERSION_STRING, pkg.mVersionName);
161        proto.write(PackageProto.INSTALL_TIME_MS, firstInstallTime);
162        proto.write(PackageProto.UPDATE_TIME_MS, lastUpdateTime);
163        proto.write(PackageProto.INSTALLER_NAME, installerPackageName);
164
165        if (pkg != null) {
166            long splitToken = proto.start(PackageProto.SPLITS);
167            proto.write(PackageProto.SplitProto.NAME, "base");
168            proto.write(PackageProto.SplitProto.REVISION_CODE, pkg.baseRevisionCode);
169            proto.end(splitToken);
170            if (pkg.splitNames != null) {
171                for (int i = 0; i < pkg.splitNames.length; i++) {
172                    splitToken = proto.start(PackageProto.SPLITS);
173                    proto.write(PackageProto.SplitProto.NAME, pkg.splitNames[i]);
174                    proto.write(PackageProto.SplitProto.REVISION_CODE, pkg.splitRevisionCodes[i]);
175                    proto.end(splitToken);
176                }
177            }
178        }
179        writeUsersInfoToProto(proto, PackageProto.USERS);
180        proto.end(packageToken);
181    }
182}
183