blob: f31dd17d6288a96e842ea54eb3b44b6e158d8b47 [file] [log] [blame]
Makoto Onuki0acbb142016-03-22 17:02:57 -07001/*
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;
Makoto Onuki2e210c42016-03-30 08:30:36 -070019import android.content.pm.PackageInfo;
20import android.util.Slog;
Makoto Onuki0acbb142016-03-22 17:02:57 -070021
Makoto Onuki9da23fc2016-03-29 11:14:42 -070022import com.android.internal.util.Preconditions;
23
Makoto Onuki0acbb142016-03-22 17:02:57 -070024import org.xmlpull.v1.XmlPullParserException;
25import org.xmlpull.v1.XmlSerializer;
26
27import java.io.IOException;
28
Makoto Onuki9da23fc2016-03-29 11:14:42 -070029abstract class ShortcutPackageItem {
Makoto Onuki2e210c42016-03-30 08:30:36 -070030 private static final String TAG = ShortcutService.TAG;
31
Makoto Onuki9da23fc2016-03-29 11:14:42 -070032 private final int mPackageUserId;
33 private final String mPackageName;
Makoto Onuki0acbb142016-03-22 17:02:57 -070034
Makoto Onuki2e210c42016-03-30 08:30:36 -070035 private final ShortcutPackageInfo mPackageInfo;
Makoto Onuki9da23fc2016-03-29 11:14:42 -070036
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
Makoto Onuki9da23fc2016-03-29 11:14:42 -070068 public void refreshPackageInfoAndSave(ShortcutService s) {
Makoto Onuki2e210c42016-03-30 08:30:36 -070069 if (mPackageInfo.isShadow()) {
70 return; // Don't refresh for shadow user.
71 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -070072 mPackageInfo.refresh(s, this);
73 s.scheduleSaveUser(getOwnerUserId());
74 }
75
Makoto Onuki2e210c42016-03-30 08:30:36 -070076 public void attemptToRestoreIfNeededAndSave(ShortcutService s) {
77 if (!mPackageInfo.isShadow()) {
78 return; // Already installed, nothing to do.
Makoto Onuki9da23fc2016-03-29 11:14:42 -070079 }
Makoto Onuki2e210c42016-03-30 08:30:36 -070080 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);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700111 }
112
Makoto Onuki2e210c42016-03-30 08:30:36 -0700113 /**
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
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700124 public abstract void saveToXml(@NonNull XmlSerializer out, boolean forBackup)
Makoto Onuki0acbb142016-03-22 17:02:57 -0700125 throws IOException, XmlPullParserException;
126}