ShortcutPackageItem.java revision 2e210c4d0f766e52ea4c087a1d54213c36a4e0ea
1/*
2 * Copyright (C) 2016 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 */
16package com.android.server.pm;
17
18import android.annotation.NonNull;
19import android.content.pm.PackageInfo;
20import android.util.Slog;
21
22import com.android.internal.util.Preconditions;
23
24import org.xmlpull.v1.XmlPullParserException;
25import org.xmlpull.v1.XmlSerializer;
26
27import java.io.IOException;
28
29abstract class ShortcutPackageItem {
30    private static final String TAG = ShortcutService.TAG;
31
32    private final int mPackageUserId;
33    private final String mPackageName;
34
35    private final ShortcutPackageInfo mPackageInfo;
36
37    protected ShortcutPackageItem(int packageUserId, @NonNull String packageName,
38            @NonNull ShortcutPackageInfo packageInfo) {
39        mPackageUserId = packageUserId;
40        mPackageName = Preconditions.checkStringNotEmpty(packageName);
41        mPackageInfo = Preconditions.checkNotNull(packageInfo);
42    }
43
44    /**
45     * ID of the user who actually has this package running on.  For {@link ShortcutPackage},
46     * this is the same thing as {@link #getOwnerUserId}, but if it's a {@link ShortcutLauncher} and
47     * {@link #getOwnerUserId} is of a work profile, then this ID could be the user who owns the
48     * profile.
49     */
50    public int getPackageUserId() {
51        return mPackageUserId;
52    }
53
54    /**
55     * ID of the user who sees the shortcuts from this instance.
56     */
57    public abstract int getOwnerUserId();
58
59    @NonNull
60    public String getPackageName() {
61        return mPackageName;
62    }
63
64    public ShortcutPackageInfo getPackageInfo() {
65        return mPackageInfo;
66    }
67
68    public void refreshPackageInfoAndSave(ShortcutService s) {
69        if (mPackageInfo.isShadow()) {
70            return; // Don't refresh for shadow user.
71        }
72        mPackageInfo.refresh(s, this);
73        s.scheduleSaveUser(getOwnerUserId());
74    }
75
76    public void attemptToRestoreIfNeededAndSave(ShortcutService s) {
77        if (!mPackageInfo.isShadow()) {
78            return; // Already installed, nothing to do.
79        }
80        if (!s.isPackageInstalled(mPackageName, mPackageUserId)) {
81            if (ShortcutService.DEBUG) {
82                Slog.d(TAG, String.format("Package still not installed: %s user=%d",
83                        mPackageName, mPackageUserId));
84            }
85            return; // Not installed, no need to restore yet.
86        }
87        if (!mPackageInfo.hasSignatures()) {
88            s.wtf("Attempted to restore package " + mPackageName + ", user=" + mPackageUserId
89                    + " but signatures not found in the restore data.");
90            onRestoreBlocked(s);
91            return;
92        }
93
94        final PackageInfo pi = s.getPackageInfoWithSignatures(mPackageName, mPackageUserId);
95        if (!mPackageInfo.canRestoreTo(s, pi)) {
96            // Package is now installed, but can't restore.  Let the subclass do the cleanup.
97            onRestoreBlocked(s);
98            return;
99        }
100        if (ShortcutService.DEBUG) {
101            Slog.d(TAG, String.format("Restored package: %s/%d on user %d", mPackageName,
102                    mPackageUserId, getOwnerUserId()));
103        }
104
105        onRestored(s);
106
107        // Now the package is not shadow.
108        mPackageInfo.setShadow(false);
109
110        s.scheduleSaveUser(mPackageUserId);
111    }
112
113    /**
114     * Called when the new package can't be restored because it has a lower version number
115     * or different signatures.
116     */
117    protected abstract void onRestoreBlocked(ShortcutService s);
118
119    /**
120     * Called when the new package is successfully restored.
121     */
122    protected abstract void onRestored(ShortcutService s);
123
124    public abstract void saveToXml(@NonNull XmlSerializer out, boolean forBackup)
125            throws IOException, XmlPullParserException;
126}
127