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