blob: 1f195a7a544b032669cc408fe3a3e6a373d1f276 [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 Onuki76269922016-07-15 14:58:54 -070024import org.json.JSONException;
25import org.json.JSONObject;
Makoto Onuki0acbb142016-03-22 17:02:57 -070026import org.xmlpull.v1.XmlPullParserException;
27import org.xmlpull.v1.XmlSerializer;
28
29import java.io.IOException;
30
Makoto Onuki22fcc682016-05-17 14:52:19 -070031/**
32 * All methods should be guarded by {@code #mShortcutUser.mService.mLock}.
33 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -070034abstract class ShortcutPackageItem {
Makoto Onuki2e210c42016-03-30 08:30:36 -070035 private static final String TAG = ShortcutService.TAG;
Makoto Onuki76269922016-07-15 14:58:54 -070036 private static final String KEY_NAME = "name";
Makoto Onuki2e210c42016-03-30 08:30:36 -070037
Makoto Onuki9da23fc2016-03-29 11:14:42 -070038 private final int mPackageUserId;
39 private final String mPackageName;
Makoto Onuki0acbb142016-03-22 17:02:57 -070040
Makoto Onuki2e210c42016-03-30 08:30:36 -070041 private final ShortcutPackageInfo mPackageInfo;
Makoto Onuki9da23fc2016-03-29 11:14:42 -070042
Makoto Onukifc4cf2d2016-08-24 11:10:26 -070043 protected ShortcutUser mShortcutUser;
Makoto Onuki4d36b3a2016-04-27 12:00:17 -070044
45 protected ShortcutPackageItem(@NonNull ShortcutUser shortcutUser,
46 int packageUserId, @NonNull String packageName,
Makoto Onuki9da23fc2016-03-29 11:14:42 -070047 @NonNull ShortcutPackageInfo packageInfo) {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -070048 mShortcutUser = shortcutUser;
Makoto Onuki9da23fc2016-03-29 11:14:42 -070049 mPackageUserId = packageUserId;
50 mPackageName = Preconditions.checkStringNotEmpty(packageName);
51 mPackageInfo = Preconditions.checkNotNull(packageInfo);
52 }
53
Makoto Onukifc4cf2d2016-08-24 11:10:26 -070054 /**
55 * Change the parent {@link ShortcutUser}. Need it in the restore code.
56 */
57 public void replaceUser(ShortcutUser user) {
58 mShortcutUser = user;
59 }
60
Makoto Onuki4e6cef42016-07-13 16:14:01 -070061 public ShortcutUser getUser() {
62 return mShortcutUser;
63 }
64
Makoto Onuki9da23fc2016-03-29 11:14:42 -070065 /**
66 * ID of the user who actually has this package running on. For {@link ShortcutPackage},
67 * this is the same thing as {@link #getOwnerUserId}, but if it's a {@link ShortcutLauncher} and
Makoto Onukic8c33292016-09-12 16:36:59 -070068 * {@link #getOwnerUserId} is of work profile, then this ID is of the primary user.
Makoto Onuki9da23fc2016-03-29 11:14:42 -070069 */
70 public int getPackageUserId() {
71 return mPackageUserId;
72 }
73
74 /**
75 * ID of the user who sees the shortcuts from this instance.
76 */
77 public abstract int getOwnerUserId();
78
79 @NonNull
80 public String getPackageName() {
81 return mPackageName;
82 }
83
84 public ShortcutPackageInfo getPackageInfo() {
85 return mPackageInfo;
86 }
87
Makoto Onukic8c33292016-09-12 16:36:59 -070088 public void refreshPackageSignatureAndSave() {
Makoto Onuki2e210c42016-03-30 08:30:36 -070089 if (mPackageInfo.isShadow()) {
90 return; // Don't refresh for shadow user.
91 }
Makoto Onukic51b2872016-05-04 15:24:50 -070092 final ShortcutService s = mShortcutUser.mService;
Makoto Onukic8c33292016-09-12 16:36:59 -070093 mPackageInfo.refreshSignature(s, this);
Makoto Onuki9da23fc2016-03-29 11:14:42 -070094 s.scheduleSaveUser(getOwnerUserId());
95 }
96
Makoto Onukic51b2872016-05-04 15:24:50 -070097 public void attemptToRestoreIfNeededAndSave() {
Makoto Onuki2e210c42016-03-30 08:30:36 -070098 if (!mPackageInfo.isShadow()) {
99 return; // Already installed, nothing to do.
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700100 }
Makoto Onukic51b2872016-05-04 15:24:50 -0700101 final ShortcutService s = mShortcutUser.mService;
Makoto Onuki2e210c42016-03-30 08:30:36 -0700102 if (!s.isPackageInstalled(mPackageName, mPackageUserId)) {
103 if (ShortcutService.DEBUG) {
104 Slog.d(TAG, String.format("Package still not installed: %s user=%d",
105 mPackageName, mPackageUserId));
106 }
107 return; // Not installed, no need to restore yet.
108 }
109 if (!mPackageInfo.hasSignatures()) {
110 s.wtf("Attempted to restore package " + mPackageName + ", user=" + mPackageUserId
111 + " but signatures not found in the restore data.");
Makoto Onukic51b2872016-05-04 15:24:50 -0700112 onRestoreBlocked();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700113 return;
114 }
115
116 final PackageInfo pi = s.getPackageInfoWithSignatures(mPackageName, mPackageUserId);
117 if (!mPackageInfo.canRestoreTo(s, pi)) {
118 // Package is now installed, but can't restore. Let the subclass do the cleanup.
Makoto Onukic51b2872016-05-04 15:24:50 -0700119 onRestoreBlocked();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700120 return;
121 }
122 if (ShortcutService.DEBUG) {
123 Slog.d(TAG, String.format("Restored package: %s/%d on user %d", mPackageName,
124 mPackageUserId, getOwnerUserId()));
125 }
126
Makoto Onukic51b2872016-05-04 15:24:50 -0700127 onRestored();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700128
129 // Now the package is not shadow.
130 mPackageInfo.setShadow(false);
131
132 s.scheduleSaveUser(mPackageUserId);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700133 }
134
Makoto Onuki2e210c42016-03-30 08:30:36 -0700135 /**
136 * Called when the new package can't be restored because it has a lower version number
137 * or different signatures.
138 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700139 protected abstract void onRestoreBlocked();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700140
141 /**
142 * Called when the new package is successfully restored.
143 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700144 protected abstract void onRestored();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700145
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700146 public abstract void saveToXml(@NonNull XmlSerializer out, boolean forBackup)
Makoto Onuki0acbb142016-03-22 17:02:57 -0700147 throws IOException, XmlPullParserException;
Makoto Onuki7001a612016-05-27 13:24:28 -0700148
Makoto Onuki76269922016-07-15 14:58:54 -0700149 public JSONObject dumpCheckin(boolean clear) throws JSONException {
150 final JSONObject result = new JSONObject();
151 result.put(KEY_NAME, mPackageName);
152 return result;
153 }
154
Makoto Onuki7001a612016-05-27 13:24:28 -0700155 /**
156 * Verify various internal states.
157 */
158 public void verifyStates() {
159 }
Makoto Onuki0acbb142016-03-22 17:02:57 -0700160}