• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.PackageParser;
20 import android.content.pm.PackageUserState;
21 import android.util.ArraySet;
22 import android.util.SparseArray;
23 
24 import com.android.server.pm.parsing.pkg.AndroidPackage;
25 
26 import java.io.File;
27 import java.util.Map;
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<PackageUserState> mUserStates = new SparseArray<>();
45     private AndroidPackage mPkg;
46     private InstallSource mInstallSource;
47     private String[] mUsesStaticLibraries;
48     private long[] mUsesStaticLibrariesVersions;
49     private Map<String, ArraySet<String>> mMimeGroups;
50     private PackageParser.SigningDetails mSigningDetails;
51     private UUID mDomainSetId = UUID.randomUUID();
52 
setPackage(AndroidPackage pkg)53     public PackageSettingBuilder setPackage(AndroidPackage pkg) {
54         this.mPkg = pkg;
55         return this;
56     }
57 
setName(String name)58     public PackageSettingBuilder setName(String name) {
59         this.mName = name;
60         return this;
61     }
62 
setAppId(int appId)63     public PackageSettingBuilder setAppId(int appId) {
64         this.mAppId = appId;
65         return this;
66     }
67 
setRealName(String realName)68     public PackageSettingBuilder setRealName(String realName) {
69         this.mRealName = realName;
70         return this;
71     }
72 
setCodePath(String codePath)73     public PackageSettingBuilder setCodePath(String codePath) {
74         this.mCodePath = codePath;
75         return this;
76     }
77 
setLegacyNativeLibraryPathString( String legacyNativeLibraryPathString)78     public PackageSettingBuilder setLegacyNativeLibraryPathString(
79             String legacyNativeLibraryPathString) {
80         this.mLegacyNativeLibraryPathString = legacyNativeLibraryPathString;
81         return this;
82     }
83 
setPrimaryCpuAbiString(String primaryCpuAbiString)84     public PackageSettingBuilder setPrimaryCpuAbiString(String primaryCpuAbiString) {
85         this.mPrimaryCpuAbiString = primaryCpuAbiString;
86         return this;
87     }
88 
setSecondaryCpuAbiString(String secondaryCpuAbiString)89     public PackageSettingBuilder setSecondaryCpuAbiString(String secondaryCpuAbiString) {
90         this.mSecondaryCpuAbiString = secondaryCpuAbiString;
91         return this;
92     }
93 
setCpuAbiOverrideString(String cpuAbiOverrideString)94     public PackageSettingBuilder setCpuAbiOverrideString(String cpuAbiOverrideString) {
95         this.mCpuAbiOverrideString = cpuAbiOverrideString;
96         return this;
97     }
98 
setPVersionCode(long pVersionCode)99     public PackageSettingBuilder setPVersionCode(long pVersionCode) {
100         this.mPVersionCode = pVersionCode;
101         return this;
102     }
103 
setPkgFlags(int pkgFlags)104     public PackageSettingBuilder setPkgFlags(int pkgFlags) {
105         this.mPkgFlags = pkgFlags;
106         return this;
107     }
108 
setPrivateFlags(int privateFlags)109     public PackageSettingBuilder setPrivateFlags(int privateFlags) {
110         this.mPrivateFlags = privateFlags;
111         return this;
112     }
113 
setSharedUserId(int sharedUserId)114     public PackageSettingBuilder setSharedUserId(int sharedUserId) {
115         this.mSharedUserId = sharedUserId;
116         return this;
117     }
118 
setUsesStaticLibraries(String[] usesStaticLibraries)119     public PackageSettingBuilder setUsesStaticLibraries(String[] usesStaticLibraries) {
120         this.mUsesStaticLibraries = usesStaticLibraries;
121         return this;
122     }
123 
setUsesStaticLibrariesVersions( long[] usesStaticLibrariesVersions)124     public PackageSettingBuilder setUsesStaticLibrariesVersions(
125             long[] usesStaticLibrariesVersions) {
126         this.mUsesStaticLibrariesVersions = usesStaticLibrariesVersions;
127         return this;
128     }
129 
setMimeGroups(Map<String, ArraySet<String>> mimeGroups)130     public PackageSettingBuilder setMimeGroups(Map<String, ArraySet<String>> mimeGroups) {
131         this.mMimeGroups = mimeGroups;
132         return this;
133     }
134 
setVolumeUuid(String volumeUuid)135     public PackageSettingBuilder setVolumeUuid(String volumeUuid) {
136         this.mVolumeUuid = volumeUuid;
137         return this;
138     }
139 
setInstantAppUserState(int userId, boolean isInstant)140     public PackageSettingBuilder setInstantAppUserState(int userId, boolean isInstant) {
141         if (mUserStates.indexOfKey(userId) < 0) {
142             mUserStates.put(userId, new PackageUserState());
143         }
144         mUserStates.get(userId).instantApp = isInstant;
145         return this;
146     }
147 
setInstallState(int userId, boolean installed)148     public PackageSettingBuilder setInstallState(int userId, boolean installed) {
149         if (mUserStates.indexOfKey(userId) < 0) {
150             mUserStates.put(userId, new PackageUserState());
151         }
152         mUserStates.get(userId).installed = installed;
153         return this;
154     }
155 
setInstallSource(InstallSource installSource)156     public PackageSettingBuilder setInstallSource(InstallSource installSource) {
157         mInstallSource = installSource;
158         return this;
159     }
160 
setSigningDetails( PackageParser.SigningDetails signingDetails)161     public PackageSettingBuilder setSigningDetails(
162             PackageParser.SigningDetails signingDetails) {
163         mSigningDetails = signingDetails;
164         return this;
165     }
166 
setDomainSetId(UUID domainSetId)167     public PackageSettingBuilder setDomainSetId(UUID domainSetId) {
168         mDomainSetId = domainSetId;
169         return this;
170     }
171 
build()172     public PackageSetting build() {
173         final PackageSetting packageSetting = new PackageSetting(mName, mRealName,
174                 new File(mCodePath), mLegacyNativeLibraryPathString, mPrimaryCpuAbiString,
175                 mSecondaryCpuAbiString, mCpuAbiOverrideString, mPVersionCode, mPkgFlags,
176                 mPrivateFlags, mSharedUserId, mUsesStaticLibraries, mUsesStaticLibrariesVersions,
177                 mMimeGroups, mDomainSetId);
178         packageSetting.signatures = mSigningDetails != null
179                 ? new PackageSignatures(mSigningDetails)
180                 : new PackageSignatures();
181         packageSetting.pkg = mPkg;
182         packageSetting.appId = mAppId;
183         packageSetting.volumeUuid = this.mVolumeUuid;
184         if (mInstallSource != null) {
185             packageSetting.installSource = mInstallSource;
186         }
187         for (int i = 0; i < mUserStates.size(); i++) {
188             packageSetting.setUserState(mUserStates.keyAt(i), mUserStates.valueAt(i));
189         }
190         return packageSetting;
191     }
192 }
193