PackageSettingBase.java revision e31b820dad4c5f2b19ee10479a675a139ad3c61e
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 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
23import android.content.pm.IntentFilterVerificationInfo;
24import android.content.pm.PackageManager;
25import android.content.pm.PackageUserState;
26import android.os.storage.VolumeInfo;
27import android.text.TextUtils;
28import android.util.ArraySet;
29import android.util.SparseArray;
30
31import java.io.File;
32
33/**
34 * Settings base class for pending and resolved classes.
35 */
36abstract class PackageSettingBase extends SettingBase {
37    /**
38     * Indicates the state of installation. Used by PackageManager to figure out
39     * incomplete installations. Say a package is being installed (the state is
40     * set to PKG_INSTALL_INCOMPLETE) and remains so till the package
41     * installation is successful or unsuccessful in which case the
42     * PackageManager will no longer maintain state information associated with
43     * the package. If some exception(like device freeze or battery being pulled
44     * out) occurs during installation of a package, the PackageManager needs
45     * this information to clean up the previously failed installation.
46     */
47    static final int PKG_INSTALL_COMPLETE = 1;
48    static final int PKG_INSTALL_INCOMPLETE = 0;
49
50    final String name;
51    final String realName;
52
53    /**
54     * Path where this package was found on disk. For monolithic packages
55     * this is path to single base APK file; for cluster packages this is
56     * path to the cluster directory.
57     */
58    File codePath;
59    String codePathString;
60    File resourcePath;
61    String resourcePathString;
62
63    /**
64     * The path under which native libraries have been unpacked. This path is
65     * always derived at runtime, and is only stored here for cleanup when a
66     * package is uninstalled.
67     */
68    @Deprecated
69    String legacyNativeLibraryPathString;
70
71    /**
72     * The primary CPU abi for this package. This value is regenerated at every
73     * boot scan.
74     */
75    String primaryCpuAbiString;
76
77    /**
78     * The secondary CPU abi for this package. This value is regenerated at every
79     * boot scan.
80     */
81    String secondaryCpuAbiString;
82
83    /**
84     * The install time CPU override, if any. This value is written at install time
85     * and doesn't change during the life of an install. If non-null,
86     * {@code primaryCpuAbiString} will contain the same value.
87     */
88    String cpuAbiOverrideString;
89
90    long timeStamp;
91    long firstInstallTime;
92    long lastUpdateTime;
93    int versionCode;
94
95    boolean uidError;
96
97    PackageSignatures signatures = new PackageSignatures();
98
99    boolean installPermissionsFixed;
100
101    PackageKeySetData keySetData = new PackageKeySetData();
102
103    private static final PackageUserState DEFAULT_USER_STATE = new PackageUserState();
104
105    // Whether this package is currently stopped, thus can not be
106    // started until explicitly launched by the user.
107    private final SparseArray<PackageUserState> userState = new SparseArray<PackageUserState>();
108
109    int installStatus = PKG_INSTALL_COMPLETE;
110
111    /**
112     * Non-persisted value indicating this package has been temporarily frozen,
113     * usually during a critical section of the package update pipeline. The
114     * platform will refuse to launch packages in a frozen state.
115     */
116    boolean frozen = false;
117
118    PackageSettingBase origPackage;
119
120    /** Package name of the app that installed this package */
121    String installerPackageName;
122    /** UUID of {@link VolumeInfo} hosting this app */
123    String volumeUuid;
124
125    IntentFilterVerificationInfo verificationInfo;
126
127    PackageSettingBase(String name, String realName, File codePath, File resourcePath,
128            String legacyNativeLibraryPathString, String primaryCpuAbiString,
129            String secondaryCpuAbiString, String cpuAbiOverrideString,
130            int pVersionCode, int pkgFlags, int pkgPrivateFlags) {
131        super(pkgFlags, pkgPrivateFlags);
132        this.name = name;
133        this.realName = realName;
134        init(codePath, resourcePath, legacyNativeLibraryPathString, primaryCpuAbiString,
135                secondaryCpuAbiString, cpuAbiOverrideString, pVersionCode);
136    }
137
138    /**
139     * New instance of PackageSetting with one-level-deep cloning.
140     */
141    @SuppressWarnings("unchecked")
142    PackageSettingBase(PackageSettingBase base) {
143        super(base);
144
145        name = base.name;
146        realName = base.realName;
147        codePath = base.codePath;
148        codePathString = base.codePathString;
149        resourcePath = base.resourcePath;
150        resourcePathString = base.resourcePathString;
151        legacyNativeLibraryPathString = base.legacyNativeLibraryPathString;
152        primaryCpuAbiString = base.primaryCpuAbiString;
153        secondaryCpuAbiString = base.secondaryCpuAbiString;
154        cpuAbiOverrideString = base.cpuAbiOverrideString;
155        timeStamp = base.timeStamp;
156        firstInstallTime = base.firstInstallTime;
157        lastUpdateTime = base.lastUpdateTime;
158        versionCode = base.versionCode;
159
160        uidError = base.uidError;
161
162        signatures = new PackageSignatures(base.signatures);
163
164        installPermissionsFixed = base.installPermissionsFixed;
165        userState.clear();
166        for (int i=0; i<base.userState.size(); i++) {
167            userState.put(base.userState.keyAt(i),
168                    new PackageUserState(base.userState.valueAt(i)));
169        }
170        installStatus = base.installStatus;
171
172        origPackage = base.origPackage;
173
174        installerPackageName = base.installerPackageName;
175        volumeUuid = base.volumeUuid;
176
177        keySetData = new PackageKeySetData(base.keySetData);
178    }
179
180    void init(File codePath, File resourcePath, String legacyNativeLibraryPathString,
181              String primaryCpuAbiString, String secondaryCpuAbiString,
182              String cpuAbiOverrideString, int pVersionCode) {
183        this.codePath = codePath;
184        this.codePathString = codePath.toString();
185        this.resourcePath = resourcePath;
186        this.resourcePathString = resourcePath.toString();
187        this.legacyNativeLibraryPathString = legacyNativeLibraryPathString;
188        this.primaryCpuAbiString = primaryCpuAbiString;
189        this.secondaryCpuAbiString = secondaryCpuAbiString;
190        this.cpuAbiOverrideString = cpuAbiOverrideString;
191        this.versionCode = pVersionCode;
192    }
193
194    public void setInstallerPackageName(String packageName) {
195        installerPackageName = packageName;
196    }
197
198    public String getInstallerPackageName() {
199        return installerPackageName;
200    }
201
202    public void setVolumeUuid(String volumeUuid) {
203        this.volumeUuid = volumeUuid;
204    }
205
206    public String getVolumeUuid() {
207        return volumeUuid;
208    }
209
210    public void setInstallStatus(int newStatus) {
211        installStatus = newStatus;
212    }
213
214    public int getInstallStatus() {
215        return installStatus;
216    }
217
218    public void setTimeStamp(long newStamp) {
219        timeStamp = newStamp;
220    }
221
222    /**
223     * Make a shallow copy of this package settings.
224     */
225    public void copyFrom(PackageSettingBase base) {
226        setPermissionsUpdatedForUserIds(base.getPermissionsUpdatedForUserIds());
227        mPermissionsState.copyFrom(base.mPermissionsState);
228        primaryCpuAbiString = base.primaryCpuAbiString;
229        secondaryCpuAbiString = base.secondaryCpuAbiString;
230        cpuAbiOverrideString = base.cpuAbiOverrideString;
231        timeStamp = base.timeStamp;
232        firstInstallTime = base.firstInstallTime;
233        lastUpdateTime = base.lastUpdateTime;
234        signatures = base.signatures;
235        installPermissionsFixed = base.installPermissionsFixed;
236        userState.clear();
237        for (int i=0; i<base.userState.size(); i++) {
238            userState.put(base.userState.keyAt(i), base.userState.valueAt(i));
239        }
240        installStatus = base.installStatus;
241        keySetData = base.keySetData;
242        verificationInfo = base.verificationInfo;
243    }
244
245    private PackageUserState modifyUserState(int userId) {
246        PackageUserState state = userState.get(userId);
247        if (state == null) {
248            state = new PackageUserState();
249            userState.put(userId, state);
250        }
251        return state;
252    }
253
254    public PackageUserState readUserState(int userId) {
255        PackageUserState state = userState.get(userId);
256        if (state != null) {
257            return state;
258        }
259        return DEFAULT_USER_STATE;
260    }
261
262    void setEnabled(int state, int userId, String callingPackage) {
263        PackageUserState st = modifyUserState(userId);
264        st.enabled = state;
265        st.lastDisableAppCaller = callingPackage;
266    }
267
268    int getEnabled(int userId) {
269        return readUserState(userId).enabled;
270    }
271
272    String getLastDisabledAppCaller(int userId) {
273        return readUserState(userId).lastDisableAppCaller;
274    }
275
276    void setInstalled(boolean inst, int userId) {
277        modifyUserState(userId).installed = inst;
278    }
279
280    boolean getInstalled(int userId) {
281        return readUserState(userId).installed;
282    }
283
284    boolean isAnyInstalled(int[] users) {
285        for (int user: users) {
286            if (readUserState(user).installed) {
287                return true;
288            }
289        }
290        return false;
291    }
292
293    int[] queryInstalledUsers(int[] users, boolean installed) {
294        int num = 0;
295        for (int user : users) {
296            if (getInstalled(user) == installed) {
297                num++;
298            }
299        }
300        int[] res = new int[num];
301        num = 0;
302        for (int user : users) {
303            if (getInstalled(user) == installed) {
304                res[num] = user;
305                num++;
306            }
307        }
308        return res;
309    }
310
311    boolean getStopped(int userId) {
312        return readUserState(userId).stopped;
313    }
314
315    void setStopped(boolean stop, int userId) {
316        modifyUserState(userId).stopped = stop;
317    }
318
319    boolean getNotLaunched(int userId) {
320        return readUserState(userId).notLaunched;
321    }
322
323    void setNotLaunched(boolean stop, int userId) {
324        modifyUserState(userId).notLaunched = stop;
325    }
326
327    boolean getHidden(int userId) {
328        return readUserState(userId).hidden;
329    }
330
331    void setHidden(boolean hidden, int userId) {
332        modifyUserState(userId).hidden = hidden;
333    }
334
335    boolean getBlockUninstall(int userId) {
336        return readUserState(userId).blockUninstall;
337    }
338
339    void setBlockUninstall(boolean blockUninstall, int userId) {
340        modifyUserState(userId).blockUninstall = blockUninstall;
341    }
342
343    void setUserState(int userId, int enabled, boolean installed, boolean stopped,
344            boolean notLaunched, boolean hidden,
345            String lastDisableAppCaller, ArraySet<String> enabledComponents,
346            ArraySet<String> disabledComponents, boolean blockUninstall, int domainVerifState) {
347        PackageUserState state = modifyUserState(userId);
348        state.enabled = enabled;
349        state.installed = installed;
350        state.stopped = stopped;
351        state.notLaunched = notLaunched;
352        state.hidden = hidden;
353        state.lastDisableAppCaller = lastDisableAppCaller;
354        state.enabledComponents = enabledComponents;
355        state.disabledComponents = disabledComponents;
356        state.blockUninstall = blockUninstall;
357        state.domainVerificationStatus = domainVerifState;
358    }
359
360    ArraySet<String> getEnabledComponents(int userId) {
361        return readUserState(userId).enabledComponents;
362    }
363
364    ArraySet<String> getDisabledComponents(int userId) {
365        return readUserState(userId).disabledComponents;
366    }
367
368    void setEnabledComponents(ArraySet<String> components, int userId) {
369        modifyUserState(userId).enabledComponents = components;
370    }
371
372    void setDisabledComponents(ArraySet<String> components, int userId) {
373        modifyUserState(userId).disabledComponents = components;
374    }
375
376    void setEnabledComponentsCopy(ArraySet<String> components, int userId) {
377        modifyUserState(userId).enabledComponents = components != null
378                ? new ArraySet<String>(components) : null;
379    }
380
381    void setDisabledComponentsCopy(ArraySet<String> components, int userId) {
382        modifyUserState(userId).disabledComponents = components != null
383                ? new ArraySet<String>(components) : null;
384    }
385
386    PackageUserState modifyUserStateComponents(int userId, boolean disabled, boolean enabled) {
387        PackageUserState state = modifyUserState(userId);
388        if (disabled && state.disabledComponents == null) {
389            state.disabledComponents = new ArraySet<String>(1);
390        }
391        if (enabled && state.enabledComponents == null) {
392            state.enabledComponents = new ArraySet<String>(1);
393        }
394        return state;
395    }
396
397    void addDisabledComponent(String componentClassName, int userId) {
398        modifyUserStateComponents(userId, true, false).disabledComponents.add(componentClassName);
399    }
400
401    void addEnabledComponent(String componentClassName, int userId) {
402        modifyUserStateComponents(userId, false, true).enabledComponents.add(componentClassName);
403    }
404
405    boolean enableComponentLPw(String componentClassName, int userId) {
406        PackageUserState state = modifyUserStateComponents(userId, false, true);
407        boolean changed = state.disabledComponents != null
408                ? state.disabledComponents.remove(componentClassName) : false;
409        changed |= state.enabledComponents.add(componentClassName);
410        return changed;
411    }
412
413    boolean disableComponentLPw(String componentClassName, int userId) {
414        PackageUserState state = modifyUserStateComponents(userId, true, false);
415        boolean changed = state.enabledComponents != null
416                ? state.enabledComponents.remove(componentClassName) : false;
417        changed |= state.disabledComponents.add(componentClassName);
418        return changed;
419    }
420
421    boolean restoreComponentLPw(String componentClassName, int userId) {
422        PackageUserState state = modifyUserStateComponents(userId, true, true);
423        boolean changed = state.disabledComponents != null
424                ? state.disabledComponents.remove(componentClassName) : false;
425        changed |= state.enabledComponents != null
426                ? state.enabledComponents.remove(componentClassName) : false;
427        return changed;
428    }
429
430    int getCurrentEnabledStateLPr(String componentName, int userId) {
431        PackageUserState state = readUserState(userId);
432        if (state.enabledComponents != null && state.enabledComponents.contains(componentName)) {
433            return COMPONENT_ENABLED_STATE_ENABLED;
434        } else if (state.disabledComponents != null
435                && state.disabledComponents.contains(componentName)) {
436            return COMPONENT_ENABLED_STATE_DISABLED;
437        } else {
438            return COMPONENT_ENABLED_STATE_DEFAULT;
439        }
440    }
441
442    void removeUser(int userId) {
443        userState.delete(userId);
444    }
445
446    IntentFilterVerificationInfo getIntentFilterVerificationInfo() {
447        return verificationInfo;
448    }
449
450    void setIntentFilterVerificationInfo(IntentFilterVerificationInfo info) {
451        verificationInfo = info;
452    }
453
454    int getDomainVerificationStatusForUser(int userId) {
455        return readUserState(userId).domainVerificationStatus;
456    }
457
458    void setDomainVerificationStatusForUser(int status, int userId) {
459        modifyUserState(userId).domainVerificationStatus = status;
460    }
461
462    void clearDomainVerificationStatusForUser(int userId) {
463        modifyUserState(userId).domainVerificationStatus =
464                PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
465    }
466}
467