blob: 0629d9e45d649470833a425cf1f56e4478f14e74 [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;
Makoto Onukia4f89b12017-10-05 10:37:55 -070020import android.content.pm.ShortcutInfo;
Makoto Onuki2e210c42016-03-30 08:30:36 -070021import android.util.Slog;
Makoto Onuki0acbb142016-03-22 17:02:57 -070022
Makoto Onuki9da23fc2016-03-29 11:14:42 -070023import com.android.internal.util.Preconditions;
24
Makoto Onuki76269922016-07-15 14:58:54 -070025import org.json.JSONException;
26import org.json.JSONObject;
Makoto Onuki0acbb142016-03-22 17:02:57 -070027import org.xmlpull.v1.XmlPullParserException;
28import org.xmlpull.v1.XmlSerializer;
29
30import java.io.IOException;
31
Makoto Onuki22fcc682016-05-17 14:52:19 -070032/**
33 * All methods should be guarded by {@code #mShortcutUser.mService.mLock}.
34 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -070035abstract class ShortcutPackageItem {
Makoto Onuki2e210c42016-03-30 08:30:36 -070036 private static final String TAG = ShortcutService.TAG;
Makoto Onuki76269922016-07-15 14:58:54 -070037 private static final String KEY_NAME = "name";
Makoto Onuki2e210c42016-03-30 08:30:36 -070038
Makoto Onuki9da23fc2016-03-29 11:14:42 -070039 private final int mPackageUserId;
40 private final String mPackageName;
Makoto Onuki0acbb142016-03-22 17:02:57 -070041
Makoto Onuki2e210c42016-03-30 08:30:36 -070042 private final ShortcutPackageInfo mPackageInfo;
Makoto Onuki9da23fc2016-03-29 11:14:42 -070043
Makoto Onukifc4cf2d2016-08-24 11:10:26 -070044 protected ShortcutUser mShortcutUser;
Makoto Onuki4d36b3a2016-04-27 12:00:17 -070045
46 protected ShortcutPackageItem(@NonNull ShortcutUser shortcutUser,
47 int packageUserId, @NonNull String packageName,
Makoto Onuki9da23fc2016-03-29 11:14:42 -070048 @NonNull ShortcutPackageInfo packageInfo) {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -070049 mShortcutUser = shortcutUser;
Makoto Onuki9da23fc2016-03-29 11:14:42 -070050 mPackageUserId = packageUserId;
51 mPackageName = Preconditions.checkStringNotEmpty(packageName);
52 mPackageInfo = Preconditions.checkNotNull(packageInfo);
53 }
54
Makoto Onukifc4cf2d2016-08-24 11:10:26 -070055 /**
56 * Change the parent {@link ShortcutUser}. Need it in the restore code.
57 */
58 public void replaceUser(ShortcutUser user) {
59 mShortcutUser = user;
60 }
61
Makoto Onuki4e6cef42016-07-13 16:14:01 -070062 public ShortcutUser getUser() {
63 return mShortcutUser;
64 }
65
Makoto Onuki9da23fc2016-03-29 11:14:42 -070066 /**
67 * ID of the user who actually has this package running on. For {@link ShortcutPackage},
68 * this is the same thing as {@link #getOwnerUserId}, but if it's a {@link ShortcutLauncher} and
Makoto Onukic8c33292016-09-12 16:36:59 -070069 * {@link #getOwnerUserId} is of work profile, then this ID is of the primary user.
Makoto Onuki9da23fc2016-03-29 11:14:42 -070070 */
71 public int getPackageUserId() {
72 return mPackageUserId;
73 }
74
75 /**
76 * ID of the user who sees the shortcuts from this instance.
77 */
78 public abstract int getOwnerUserId();
79
80 @NonNull
81 public String getPackageName() {
82 return mPackageName;
83 }
84
85 public ShortcutPackageInfo getPackageInfo() {
86 return mPackageInfo;
87 }
88
Makoto Onukic8c33292016-09-12 16:36:59 -070089 public void refreshPackageSignatureAndSave() {
Makoto Onuki2e210c42016-03-30 08:30:36 -070090 if (mPackageInfo.isShadow()) {
91 return; // Don't refresh for shadow user.
92 }
Makoto Onukic51b2872016-05-04 15:24:50 -070093 final ShortcutService s = mShortcutUser.mService;
Makoto Onukic8c33292016-09-12 16:36:59 -070094 mPackageInfo.refreshSignature(s, this);
Makoto Onuki9da23fc2016-03-29 11:14:42 -070095 s.scheduleSaveUser(getOwnerUserId());
96 }
97
Makoto Onukic51b2872016-05-04 15:24:50 -070098 public void attemptToRestoreIfNeededAndSave() {
Makoto Onuki2e210c42016-03-30 08:30:36 -070099 if (!mPackageInfo.isShadow()) {
100 return; // Already installed, nothing to do.
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700101 }
Makoto Onukic51b2872016-05-04 15:24:50 -0700102 final ShortcutService s = mShortcutUser.mService;
Makoto Onuki2e210c42016-03-30 08:30:36 -0700103 if (!s.isPackageInstalled(mPackageName, mPackageUserId)) {
104 if (ShortcutService.DEBUG) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700105 Slog.d(TAG, String.format("Package still not installed: %s/u%d",
Makoto Onuki2e210c42016-03-30 08:30:36 -0700106 mPackageName, mPackageUserId));
107 }
108 return; // Not installed, no need to restore yet.
109 }
Makoto Onukia4f89b12017-10-05 10:37:55 -0700110 int restoreBlockReason;
Dianne Hackborn3accca02013-09-20 09:32:11 -0700111 long currentVersionCode = ShortcutInfo.VERSION_CODE_UNKNOWN;
Makoto Onukifac592f2016-11-21 13:41:32 -0800112
Makoto Onukia4f89b12017-10-05 10:37:55 -0700113 if (!mPackageInfo.hasSignatures()) {
114 s.wtf("Attempted to restore package " + mPackageName + "/u" + mPackageUserId
115 + " but signatures not found in the restore data.");
116 restoreBlockReason = ShortcutInfo.DISABLED_REASON_SIGNATURE_MISMATCH;
117 } else {
118 final PackageInfo pi = s.getPackageInfoWithSignatures(mPackageName, mPackageUserId);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700119 currentVersionCode = pi.getLongVersionCode();
Makoto Onukia4f89b12017-10-05 10:37:55 -0700120 restoreBlockReason = mPackageInfo.canRestoreTo(s, pi, canRestoreAnyVersion());
Makoto Onuki2e210c42016-03-30 08:30:36 -0700121 }
122
Makoto Onukia4f89b12017-10-05 10:37:55 -0700123 if (ShortcutService.DEBUG) {
124 Slog.d(TAG, String.format("Restoring package: %s/u%d (version=%d) %s for u%d",
125 mPackageName, mPackageUserId, currentVersionCode,
Makoto Onukib1588c02017-10-12 15:11:45 -0700126 ShortcutInfo.getDisabledReasonDebugString(restoreBlockReason),
Makoto Onukia4f89b12017-10-05 10:37:55 -0700127 getOwnerUserId()));
128 }
129
130 onRestored(restoreBlockReason);
131
Makoto Onukifac592f2016-11-21 13:41:32 -0800132 // Either way, it's no longer a shadow.
Makoto Onuki2e210c42016-03-30 08:30:36 -0700133 mPackageInfo.setShadow(false);
134
135 s.scheduleSaveUser(mPackageUserId);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700136 }
137
Makoto Onukia4f89b12017-10-05 10:37:55 -0700138 protected abstract boolean canRestoreAnyVersion();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700139
Makoto Onukia4f89b12017-10-05 10:37:55 -0700140 protected abstract void onRestored(int restoreBlockReason);
Makoto Onuki2e210c42016-03-30 08:30:36 -0700141
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700142 public abstract void saveToXml(@NonNull XmlSerializer out, boolean forBackup)
Makoto Onuki0acbb142016-03-22 17:02:57 -0700143 throws IOException, XmlPullParserException;
Makoto Onuki7001a612016-05-27 13:24:28 -0700144
Makoto Onuki76269922016-07-15 14:58:54 -0700145 public JSONObject dumpCheckin(boolean clear) throws JSONException {
146 final JSONObject result = new JSONObject();
147 result.put(KEY_NAME, mPackageName);
148 return result;
149 }
150
Makoto Onuki7001a612016-05-27 13:24:28 -0700151 /**
152 * Verify various internal states.
153 */
154 public void verifyStates() {
155 }
Makoto Onuki0acbb142016-03-22 17:02:57 -0700156}