blob: e64ec6dbf7f3d6aacefde97b23a0e9aa7abfaa61 [file] [log] [blame]
Kenny Rootcf0b38c2011-03-22 14:17:59 -07001/*
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 static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
20import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
21import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
22
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -070023import android.content.pm.ApplicationInfo;
Dianne Hackborn7767eac2012-08-23 18:25:40 -070024import android.content.pm.PackageUserState;
25import android.content.pm.UserInfo;
Amith Yamasani483f3b02012-03-13 16:08:00 -070026import android.util.SparseArray;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070027
28import java.io.File;
29import java.util.HashSet;
Dianne Hackborn7767eac2012-08-23 18:25:40 -070030import java.util.List;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070031
32/**
33 * Settings base class for pending and resolved classes.
34 */
35class PackageSettingBase extends GrantedPermissions {
36 /**
37 * Indicates the state of installation. Used by PackageManager to figure out
38 * incomplete installations. Say a package is being installed (the state is
39 * set to PKG_INSTALL_INCOMPLETE) and remains so till the package
40 * installation is successful or unsuccessful in which case the
41 * PackageManager will no longer maintain state information associated with
42 * the package. If some exception(like device freeze or battery being pulled
43 * out) occurs during installation of a package, the PackageManager needs
44 * this information to clean up the previously failed installation.
45 */
46 static final int PKG_INSTALL_COMPLETE = 1;
47 static final int PKG_INSTALL_INCOMPLETE = 0;
48
49 final String name;
50 final String realName;
51 File codePath;
52 String codePathString;
53 File resourcePath;
54 String resourcePathString;
55 String nativeLibraryPathString;
56 long timeStamp;
57 long firstInstallTime;
58 long lastUpdateTime;
59 int versionCode;
60
61 boolean uidError;
62
63 PackageSignatures signatures = new PackageSignatures();
64
65 boolean permissionsFixed;
66 boolean haveGids;
67
Dianne Hackborn4a9f0712012-10-02 15:29:06 -070068 private static final PackageUserState DEFAULT_USER_STATE = new PackageUserState();
Dianne Hackborn7767eac2012-08-23 18:25:40 -070069
Kenny Rootcf0b38c2011-03-22 14:17:59 -070070 // Whether this package is currently stopped, thus can not be
71 // started until explicitly launched by the user.
Dianne Hackborn7767eac2012-08-23 18:25:40 -070072 private final SparseArray<PackageUserState> userState = new SparseArray<PackageUserState>();
Amith Yamasani483f3b02012-03-13 16:08:00 -070073
Kenny Rootcf0b38c2011-03-22 14:17:59 -070074 int installStatus = PKG_INSTALL_COMPLETE;
75
76 PackageSettingBase origPackage;
Amith Yamasani483f3b02012-03-13 16:08:00 -070077
Kenny Rootcf0b38c2011-03-22 14:17:59 -070078 /* package name of the app that installed this package */
79 String installerPackageName;
80 PackageSettingBase(String name, String realName, File codePath, File resourcePath,
81 String nativeLibraryPathString, int pVersionCode, int pkgFlags) {
82 super(pkgFlags);
83 this.name = name;
84 this.realName = realName;
85 init(codePath, resourcePath, nativeLibraryPathString, pVersionCode);
86 }
87
88 /**
89 * New instance of PackageSetting with one-level-deep cloning.
90 */
91 @SuppressWarnings("unchecked")
92 PackageSettingBase(PackageSettingBase base) {
93 super(base);
94
95 name = base.name;
96 realName = base.realName;
97 codePath = base.codePath;
98 codePathString = base.codePathString;
99 resourcePath = base.resourcePath;
100 resourcePathString = base.resourcePathString;
101 nativeLibraryPathString = base.nativeLibraryPathString;
102 timeStamp = base.timeStamp;
103 firstInstallTime = base.firstInstallTime;
104 lastUpdateTime = base.lastUpdateTime;
105 versionCode = base.versionCode;
106
107 uidError = base.uidError;
108
109 signatures = new PackageSignatures(base.signatures);
110
111 permissionsFixed = base.permissionsFixed;
112 haveGids = base.haveGids;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700113 userState.clear();
114 for (int i=0; i<base.userState.size(); i++) {
115 userState.put(base.userState.keyAt(i),
116 new PackageUserState(base.userState.valueAt(i)));
117 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700118 installStatus = base.installStatus;
119
120 origPackage = base.origPackage;
121
122 installerPackageName = base.installerPackageName;
123 }
124
125 void init(File codePath, File resourcePath, String nativeLibraryPathString,
126 int pVersionCode) {
127 this.codePath = codePath;
128 this.codePathString = codePath.toString();
129 this.resourcePath = resourcePath;
130 this.resourcePathString = resourcePath.toString();
131 this.nativeLibraryPathString = nativeLibraryPathString;
132 this.versionCode = pVersionCode;
133 }
134
135 public void setInstallerPackageName(String packageName) {
136 installerPackageName = packageName;
137 }
138
139 String getInstallerPackageName() {
140 return installerPackageName;
141 }
142
143 public void setInstallStatus(int newStatus) {
144 installStatus = newStatus;
145 }
146
147 public int getInstallStatus() {
148 return installStatus;
149 }
150
151 public void setTimeStamp(long newStamp) {
152 timeStamp = newStamp;
153 }
154
155 /**
156 * Make a shallow copy of this package settings.
157 */
158 public void copyFrom(PackageSettingBase base) {
159 grantedPermissions = base.grantedPermissions;
160 gids = base.gids;
161
162 timeStamp = base.timeStamp;
163 firstInstallTime = base.firstInstallTime;
164 lastUpdateTime = base.lastUpdateTime;
165 signatures = base.signatures;
166 permissionsFixed = base.permissionsFixed;
167 haveGids = base.haveGids;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700168 userState.clear();
169 for (int i=0; i<base.userState.size(); i++) {
170 userState.put(base.userState.keyAt(i), base.userState.valueAt(i));
171 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700172 installStatus = base.installStatus;
173 }
174
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700175 private PackageUserState modifyUserState(int userId) {
176 PackageUserState state = userState.get(userId);
177 if (state == null) {
Dianne Hackborn4a9f0712012-10-02 15:29:06 -0700178 state = new PackageUserState();
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700179 userState.put(userId, state);
180 }
181 return state;
182 }
183
184 public PackageUserState readUserState(int userId) {
185 PackageUserState state = userState.get(userId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700186 if (state != null) {
187 return state;
188 }
Dianne Hackborn4a9f0712012-10-02 15:29:06 -0700189 return DEFAULT_USER_STATE;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700190 }
191
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -0700192 void setEnabled(int state, int userId, String callingPackage) {
193 PackageUserState st = modifyUserState(userId);
194 st.enabled = state;
195 st.lastDisableAppCaller = callingPackage;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700196 }
197
198 int getEnabled(int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700199 return readUserState(userId).enabled;
200 }
201
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -0700202 String getLastDisabledAppCaller(int userId) {
203 return readUserState(userId).lastDisableAppCaller;
204 }
205
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700206 void setInstalled(boolean inst, int userId) {
207 modifyUserState(userId).installed = inst;
208 }
209
210 boolean getInstalled(int userId) {
211 return readUserState(userId).installed;
212 }
213
214 boolean isAnyInstalled(int[] users) {
215 for (int user: users) {
216 if (readUserState(user).installed) {
217 return true;
218 }
219 }
220 return false;
221 }
222
Dianne Hackborn786b4402012-08-27 15:14:02 -0700223 int[] queryInstalledUsers(int[] users, boolean installed) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700224 int num = 0;
225 for (int user : users) {
Dianne Hackborn786b4402012-08-27 15:14:02 -0700226 if (getInstalled(user) == installed) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700227 num++;
228 }
229 }
230 int[] res = new int[num];
231 num = 0;
232 for (int user : users) {
Dianne Hackborn786b4402012-08-27 15:14:02 -0700233 if (getInstalled(user) == installed) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700234 res[num] = user;
235 num++;
236 }
237 }
238 return res;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700239 }
240
241 boolean getStopped(int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700242 return readUserState(userId).stopped;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700243 }
244
245 void setStopped(boolean stop, int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700246 modifyUserState(userId).stopped = stop;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700247 }
248
249 boolean getNotLaunched(int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700250 return readUserState(userId).notLaunched;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700251 }
252
253 void setNotLaunched(boolean stop, int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700254 modifyUserState(userId).notLaunched = stop;
255 }
256
257 void setUserState(int userId, int enabled, boolean installed, boolean stopped,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -0700258 boolean notLaunched, String lastDisableAppCaller, HashSet<String> enabledComponents,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700259 HashSet<String> disabledComponents) {
260 PackageUserState state = modifyUserState(userId);
261 state.enabled = enabled;
262 state.installed = installed;
263 state.stopped = stopped;
264 state.notLaunched = notLaunched;
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -0700265 state.lastDisableAppCaller = lastDisableAppCaller;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700266 state.enabledComponents = enabledComponents;
267 state.disabledComponents = disabledComponents;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700268 }
269
270 HashSet<String> getEnabledComponents(int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700271 return readUserState(userId).enabledComponents;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700272 }
273
274 HashSet<String> getDisabledComponents(int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700275 return readUserState(userId).disabledComponents;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700276 }
277
278 void setEnabledComponents(HashSet<String> components, int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700279 modifyUserState(userId).enabledComponents = components;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700280 }
281
282 void setDisabledComponents(HashSet<String> components, int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700283 modifyUserState(userId).disabledComponents = components;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700284 }
285
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700286 void setEnabledComponentsCopy(HashSet<String> components, int userId) {
287 modifyUserState(userId).enabledComponents = components != null
288 ? new HashSet<String>(components) : null;
289 }
290
291 void setDisabledComponentsCopy(HashSet<String> components, int userId) {
292 modifyUserState(userId).disabledComponents = components != null
293 ? new HashSet<String>(components) : null;
294 }
295
296 PackageUserState modifyUserStateComponents(int userId, boolean disabled, boolean enabled) {
297 PackageUserState state = modifyUserState(userId);
298 if (disabled && state.disabledComponents == null) {
299 state.disabledComponents = new HashSet<String>(1);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700300 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700301 if (enabled && state.enabledComponents == null) {
302 state.enabledComponents = new HashSet<String>(1);
303 }
304 return state;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700305 }
306
307 void addDisabledComponent(String componentClassName, int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700308 modifyUserStateComponents(userId, true, false).disabledComponents.add(componentClassName);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700309 }
310
311 void addEnabledComponent(String componentClassName, int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700312 modifyUserStateComponents(userId, false, true).enabledComponents.add(componentClassName);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700313 }
314
315 boolean enableComponentLPw(String componentClassName, int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700316 PackageUserState state = modifyUserStateComponents(userId, false, true);
317 boolean changed = state.disabledComponents != null
318 ? state.disabledComponents.remove(componentClassName) : false;
319 changed |= state.enabledComponents.add(componentClassName);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700320 return changed;
321 }
322
Amith Yamasani483f3b02012-03-13 16:08:00 -0700323 boolean disableComponentLPw(String componentClassName, int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700324 PackageUserState state = modifyUserStateComponents(userId, true, false);
325 boolean changed = state.enabledComponents != null
326 ? state.enabledComponents.remove(componentClassName) : false;
327 changed |= state.disabledComponents.add(componentClassName);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700328 return changed;
329 }
330
Amith Yamasani483f3b02012-03-13 16:08:00 -0700331 boolean restoreComponentLPw(String componentClassName, int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700332 PackageUserState state = modifyUserStateComponents(userId, true, true);
333 boolean changed = state.disabledComponents != null
334 ? state.disabledComponents.remove(componentClassName) : false;
335 changed |= state.enabledComponents != null
336 ? state.enabledComponents.remove(componentClassName) : false;
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700337 return changed;
338 }
339
Amith Yamasani483f3b02012-03-13 16:08:00 -0700340 int getCurrentEnabledStateLPr(String componentName, int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700341 PackageUserState state = readUserState(userId);
342 if (state.enabledComponents != null && state.enabledComponents.contains(componentName)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700343 return COMPONENT_ENABLED_STATE_ENABLED;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700344 } else if (state.disabledComponents != null
345 && state.disabledComponents.contains(componentName)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700346 return COMPONENT_ENABLED_STATE_DISABLED;
347 } else {
348 return COMPONENT_ENABLED_STATE_DEFAULT;
349 }
350 }
Amith Yamasani13593602012-03-22 16:16:17 -0700351
352 void removeUser(int userId) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700353 userState.delete(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700354 }
Amith Yamasani483f3b02012-03-13 16:08:00 -0700355}