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