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.PackageUserState;
20 import android.util.SparseArray;
21 
22 import java.io.File;
23 import java.util.List;
24 
25 class PackageSettingBuilder {
26     private String mName;
27     private String mRealName;
28     private String mCodePath;
29     private String mResourcePath;
30     private String mLegacyNativeLibraryPathString;
31     private String mPrimaryCpuAbiString;
32     private String mSecondaryCpuAbiString;
33     private String mCpuAbiOverrideString;
34     private long mPVersionCode;
35     private int mPkgFlags;
36     private int mPrivateFlags;
37     private String mParentPackageName;
38     private List<String> mChildPackageNames;
39     private int mSharedUserId;
40     private String[] mUsesStaticLibraries;
41     private long[] mUsesStaticLibrariesVersions;
42     private String mVolumeUuid;
43     private SparseArray<PackageUserState> mUserStates = new SparseArray<>();
44 
setName(String name)45     public PackageSettingBuilder setName(String name) {
46         this.mName = name;
47         return this;
48     }
49 
setRealName(String realName)50     public PackageSettingBuilder setRealName(String realName) {
51         this.mRealName = realName;
52         return this;
53     }
54 
setCodePath(String codePath)55     public PackageSettingBuilder setCodePath(String codePath) {
56         this.mCodePath = codePath;
57         return this;
58     }
59 
setResourcePath(String resourcePath)60     public PackageSettingBuilder setResourcePath(String resourcePath) {
61         this.mResourcePath = resourcePath;
62         return this;
63     }
64 
setLegacyNativeLibraryPathString( String legacyNativeLibraryPathString)65     public PackageSettingBuilder setLegacyNativeLibraryPathString(
66             String legacyNativeLibraryPathString) {
67         this.mLegacyNativeLibraryPathString = legacyNativeLibraryPathString;
68         return this;
69     }
70 
setPrimaryCpuAbiString(String primaryCpuAbiString)71     public PackageSettingBuilder setPrimaryCpuAbiString(String primaryCpuAbiString) {
72         this.mPrimaryCpuAbiString = primaryCpuAbiString;
73         return this;
74     }
75 
setSecondaryCpuAbiString(String secondaryCpuAbiString)76     public PackageSettingBuilder setSecondaryCpuAbiString(String secondaryCpuAbiString) {
77         this.mSecondaryCpuAbiString = secondaryCpuAbiString;
78         return this;
79     }
80 
setCpuAbiOverrideString(String cpuAbiOverrideString)81     public PackageSettingBuilder setCpuAbiOverrideString(String cpuAbiOverrideString) {
82         this.mCpuAbiOverrideString = cpuAbiOverrideString;
83         return this;
84     }
85 
setPVersionCode(long pVersionCode)86     public PackageSettingBuilder setPVersionCode(long pVersionCode) {
87         this.mPVersionCode = pVersionCode;
88         return this;
89     }
90 
setPkgFlags(int pkgFlags)91     public PackageSettingBuilder setPkgFlags(int pkgFlags) {
92         this.mPkgFlags = pkgFlags;
93         return this;
94     }
95 
setPrivateFlags(int privateFlags)96     public PackageSettingBuilder setPrivateFlags(int privateFlags) {
97         this.mPrivateFlags = privateFlags;
98         return this;
99     }
100 
setParentPackageName(String parentPackageName)101     public PackageSettingBuilder setParentPackageName(String parentPackageName) {
102         this.mParentPackageName = parentPackageName;
103         return this;
104     }
105 
setChildPackageNames(List<String> childPackageNames)106     public PackageSettingBuilder setChildPackageNames(List<String> childPackageNames) {
107         this.mChildPackageNames = childPackageNames;
108         return this;
109     }
110 
setSharedUserId(int sharedUserId)111     public PackageSettingBuilder setSharedUserId(int sharedUserId) {
112         this.mSharedUserId = sharedUserId;
113         return this;
114     }
115 
setUsesStaticLibraries(String[] usesStaticLibraries)116     public PackageSettingBuilder setUsesStaticLibraries(String[] usesStaticLibraries) {
117         this.mUsesStaticLibraries = usesStaticLibraries;
118         return this;
119     }
120 
setUsesStaticLibrariesVersions( long[] usesStaticLibrariesVersions)121     public PackageSettingBuilder setUsesStaticLibrariesVersions(
122             long[] usesStaticLibrariesVersions) {
123         this.mUsesStaticLibrariesVersions = usesStaticLibrariesVersions;
124         return this;
125     }
126 
setVolumeUuid(String volumeUuid)127     public PackageSettingBuilder setVolumeUuid(String volumeUuid) {
128         this.mVolumeUuid = volumeUuid;
129         return this;
130     }
131 
setInstantAppUserState(int userId, boolean isInstant)132     public PackageSettingBuilder setInstantAppUserState(int userId, boolean isInstant) {
133         if (mUserStates.indexOfKey(userId) < 0) {
134             mUserStates.put(userId, new PackageUserState());
135         }
136         mUserStates.get(userId).instantApp = isInstant;
137         return this;
138     }
139 
build()140     public PackageSetting build() {
141         final PackageSetting packageSetting = new PackageSetting(mName, mRealName,
142                 new File(mCodePath), new File(mResourcePath),
143                 mLegacyNativeLibraryPathString, mPrimaryCpuAbiString, mSecondaryCpuAbiString,
144                 mCpuAbiOverrideString, mPVersionCode, mPkgFlags, mPrivateFlags, mParentPackageName,
145                 mChildPackageNames, mSharedUserId, mUsesStaticLibraries,
146                 mUsesStaticLibrariesVersions);
147         packageSetting.volumeUuid = this.mVolumeUuid;
148         for (int i = 0; i < mUserStates.size(); i++) {
149             packageSetting.setUserState(mUserStates.keyAt(i), mUserStates.valueAt(i));
150         }
151         return packageSetting;
152 
153     }
154 }
155