1 /*
2  * Copyright (C) 2019 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 
17 package com.android.server.pm;
18 
19 import android.content.pm.SigningDetails;
20 import android.util.SparseArray;
21 
22 import com.android.server.pm.pkg.AndroidPackage;
23 import com.android.server.pm.pkg.PackageUserStateImpl;
24 
25 import java.io.File;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.UUID;
29 
30 public class PackageSettingBuilder {
31     private String mName;
32     private String mRealName;
33     private String mCodePath;
34     private String mLegacyNativeLibraryPathString;
35     private String mPrimaryCpuAbiString;
36     private String mSecondaryCpuAbiString;
37     private String mCpuAbiOverrideString;
38     private long mPVersionCode;
39     private int mPkgFlags;
40     private int mPrivateFlags;
41     private int mSharedUserId;
42     private String mVolumeUuid;
43     private int mAppId;
44     private SparseArray<PackageUserStateImpl> mUserStates = new SparseArray<>();
45     private AndroidPackage mPkg;
46     private InstallSource mInstallSource;
47     private Map<String, Set<String>> mMimeGroups;
48     private SigningDetails mSigningDetails;
49     private UUID mDomainSetId = UUID.randomUUID();
50 
setPackage(AndroidPackage pkg)51     public PackageSettingBuilder setPackage(AndroidPackage pkg) {
52         this.mPkg = pkg;
53         return this;
54     }
55 
setName(String name)56     public PackageSettingBuilder setName(String name) {
57         this.mName = name;
58         return this;
59     }
60 
setAppId(int appId)61     public PackageSettingBuilder setAppId(int appId) {
62         this.mAppId = appId;
63         return this;
64     }
65 
setRealName(String realName)66     public PackageSettingBuilder setRealName(String realName) {
67         this.mRealName = realName;
68         return this;
69     }
70 
setCodePath(String codePath)71     public PackageSettingBuilder setCodePath(String codePath) {
72         this.mCodePath = codePath;
73         return this;
74     }
75 
setLegacyNativeLibraryPathString( String legacyNativeLibraryPathString)76     public PackageSettingBuilder setLegacyNativeLibraryPathString(
77             String legacyNativeLibraryPathString) {
78         this.mLegacyNativeLibraryPathString = legacyNativeLibraryPathString;
79         return this;
80     }
81 
setPrimaryCpuAbiString(String primaryCpuAbiString)82     public PackageSettingBuilder setPrimaryCpuAbiString(String primaryCpuAbiString) {
83         this.mPrimaryCpuAbiString = primaryCpuAbiString;
84         return this;
85     }
86 
setSecondaryCpuAbiString(String secondaryCpuAbiString)87     public PackageSettingBuilder setSecondaryCpuAbiString(String secondaryCpuAbiString) {
88         this.mSecondaryCpuAbiString = secondaryCpuAbiString;
89         return this;
90     }
91 
setCpuAbiOverrideString(String cpuAbiOverrideString)92     public PackageSettingBuilder setCpuAbiOverrideString(String cpuAbiOverrideString) {
93         this.mCpuAbiOverrideString = cpuAbiOverrideString;
94         return this;
95     }
96 
setPVersionCode(long pVersionCode)97     public PackageSettingBuilder setPVersionCode(long pVersionCode) {
98         this.mPVersionCode = pVersionCode;
99         return this;
100     }
101 
setPkgFlags(int pkgFlags)102     public PackageSettingBuilder setPkgFlags(int pkgFlags) {
103         this.mPkgFlags = pkgFlags;
104         return this;
105     }
106 
setPrivateFlags(int privateFlags)107     public PackageSettingBuilder setPrivateFlags(int privateFlags) {
108         this.mPrivateFlags = privateFlags;
109         return this;
110     }
111 
setSharedUserId(int sharedUserId)112     public PackageSettingBuilder setSharedUserId(int sharedUserId) {
113         this.mSharedUserId = sharedUserId;
114         return this;
115     }
116 
setMimeGroups(Map<String, Set<String>> mimeGroups)117     public PackageSettingBuilder setMimeGroups(Map<String, Set<String>> mimeGroups) {
118         this.mMimeGroups = mimeGroups;
119         return this;
120     }
121 
setVolumeUuid(String volumeUuid)122     public PackageSettingBuilder setVolumeUuid(String volumeUuid) {
123         this.mVolumeUuid = volumeUuid;
124         return this;
125     }
126 
setInstantAppUserState(int userId, boolean isInstant)127     public PackageSettingBuilder setInstantAppUserState(int userId, boolean isInstant) {
128         if (mUserStates.indexOfKey(userId) < 0) {
129             mUserStates.put(userId, new PackageUserStateImpl());
130         }
131         mUserStates.get(userId).setInstantApp(isInstant);
132         return this;
133     }
134 
setInstallState(int userId, boolean installed)135     public PackageSettingBuilder setInstallState(int userId, boolean installed) {
136         if (mUserStates.indexOfKey(userId) < 0) {
137             mUserStates.put(userId, new PackageUserStateImpl());
138         }
139         mUserStates.get(userId).setInstalled(installed);
140         return this;
141     }
142 
setInstallSource(InstallSource installSource)143     public PackageSettingBuilder setInstallSource(InstallSource installSource) {
144         mInstallSource = installSource;
145         return this;
146     }
147 
setSigningDetails( SigningDetails signingDetails)148     public PackageSettingBuilder setSigningDetails(
149             SigningDetails signingDetails) {
150         mSigningDetails = signingDetails;
151         return this;
152     }
153 
setDomainSetId(UUID domainSetId)154     public PackageSettingBuilder setDomainSetId(UUID domainSetId) {
155         mDomainSetId = domainSetId;
156         return this;
157     }
158 
build()159     public PackageSetting build() {
160         final PackageSetting packageSetting = new PackageSetting(mName, mRealName,
161                 new File(mCodePath), mPkgFlags, mPrivateFlags, mDomainSetId)
162                 .setLegacyNativeLibraryPath(mLegacyNativeLibraryPathString)
163                 .setPrimaryCpuAbi(mPrimaryCpuAbiString)
164                 .setSecondaryCpuAbi(mSecondaryCpuAbiString)
165                 .setCpuAbiOverride(mCpuAbiOverrideString)
166                 .setLongVersionCode(mPVersionCode)
167                 .setSharedUserAppId(mSharedUserId)
168                 .setMimeGroups(mMimeGroups)
169                 .setSignatures(mSigningDetails != null
170                         ? new PackageSignatures(mSigningDetails) : new PackageSignatures())
171                 .setPkg(mPkg)
172                 .setAppId(mAppId)
173                 .setVolumeUuid(this.mVolumeUuid);
174         if (mInstallSource != null) {
175             packageSetting.setInstallSource(mInstallSource);
176         }
177         for (int i = 0; i < mUserStates.size(); i++) {
178             packageSetting.setUserState(mUserStates.keyAt(i), mUserStates.valueAt(i));
179         }
180         return packageSetting;
181     }
182 }
183