2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.server.pm;
18 import android.annotation.NonNull;
19 import android.content.pm.PackageInfo;
20 import android.util.Slog;
22 import com.android.internal.util.Preconditions;
24 import org.xmlpull.v1.XmlPullParserException;
25 import org.xmlpull.v1.XmlSerializer;
27 import java.io.IOException;
29 abstract class ShortcutPackageItem {
30 private static final String TAG = ShortcutService.TAG;
32 private final int mPackageUserId;
33 private final String mPackageName;
35 private final ShortcutPackageInfo mPackageInfo;
37 protected final ShortcutUser mShortcutUser;
39 protected ShortcutPackageItem(@NonNull ShortcutUser shortcutUser,
40 int packageUserId, @NonNull String packageName,
41 @NonNull ShortcutPackageInfo packageInfo) {
42 mShortcutUser = shortcutUser;
43 mPackageUserId = packageUserId;
44 mPackageName = Preconditions.checkStringNotEmpty(packageName);
45 mPackageInfo = Preconditions.checkNotNull(packageInfo);
49 * ID of the user who actually has this package running on. For {@link ShortcutPackage},
50 * this is the same thing as {@link #getOwnerUserId}, but if it's a {@link ShortcutLauncher} and
51 * {@link #getOwnerUserId} is of a work profile, then this ID could be the user who owns the
54 public int getPackageUserId() {
55 return mPackageUserId;
59 * ID of the user who sees the shortcuts from this instance.
61 public abstract int getOwnerUserId();
64 public String getPackageName() {
68 public ShortcutPackageInfo getPackageInfo() {
72 public void refreshPackageInfoAndSave(ShortcutService s) {
73 if (mPackageInfo.isShadow()) {
74 return; // Don't refresh for shadow user.
76 mPackageInfo.refresh(s, this);
77 s.scheduleSaveUser(getOwnerUserId());
80 public void attemptToRestoreIfNeededAndSave(ShortcutService s) {
81 if (!mPackageInfo.isShadow()) {
82 return; // Already installed, nothing to do.
84 if (!s.isPackageInstalled(mPackageName, mPackageUserId)) {
85 if (ShortcutService.DEBUG) {
86 Slog.d(TAG, String.format("Package still not installed: %s user=%d",
87 mPackageName, mPackageUserId));
89 return; // Not installed, no need to restore yet.
91 if (!mPackageInfo.hasSignatures()) {
92 s.wtf("Attempted to restore package " + mPackageName + ", user=" + mPackageUserId
93 + " but signatures not found in the restore data.");
98 final PackageInfo pi = s.getPackageInfoWithSignatures(mPackageName, mPackageUserId);
99 if (!mPackageInfo.canRestoreTo(s, pi)) {
100 // Package is now installed, but can't restore. Let the subclass do the cleanup.
104 if (ShortcutService.DEBUG) {
105 Slog.d(TAG, String.format("Restored package: %s/%d on user %d", mPackageName,
106 mPackageUserId, getOwnerUserId()));
111 // Now the package is not shadow.
112 mPackageInfo.setShadow(false);
114 s.scheduleSaveUser(mPackageUserId);
118 * Called when the new package can't be restored because it has a lower version number
119 * or different signatures.
121 protected abstract void onRestoreBlocked(ShortcutService s);
124 * Called when the new package is successfully restored.
126 protected abstract void onRestored(ShortcutService s);
128 public abstract void saveToXml(@NonNull XmlSerializer out, boolean forBackup)
129 throws IOException, XmlPullParserException;