blob: 520ed2526b1799b94b42a371f9b5365432d2fc89 [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
Makoto Onuki22fcc682016-05-17 14:52:19 -070018import android.annotation.NonNull;
Makoto Onuki0acbb142016-03-22 17:02:57 -070019import android.annotation.UserIdInt;
Makoto Onuki0acbb142016-03-22 17:02:57 -070020import android.content.pm.PackageInfo;
Makoto Onukia4f89b12017-10-05 10:37:55 -070021import android.content.pm.ShortcutInfo;
Makoto Onuki0acbb142016-03-22 17:02:57 -070022import android.util.Slog;
23
Makoto Onukic8c33292016-09-12 16:36:59 -070024import com.android.internal.annotations.VisibleForTesting;
Makoto Onuki9da23fc2016-03-29 11:14:42 -070025import com.android.server.backup.BackupUtils;
Makoto Onuki0acbb142016-03-22 17:02:57 -070026
Makoto Onuki0acbb142016-03-22 17:02:57 -070027import libcore.util.HexEncoding;
28
29import org.xmlpull.v1.XmlPullParser;
30import org.xmlpull.v1.XmlPullParserException;
31import org.xmlpull.v1.XmlSerializer;
32
33import java.io.IOException;
34import java.io.PrintWriter;
Makoto Onuki0acbb142016-03-22 17:02:57 -070035import java.util.ArrayList;
Tobias Thierer9f00d712016-12-01 23:04:36 +000036import java.util.Base64;
Makoto Onuki0acbb142016-03-22 17:02:57 -070037
38/**
39 * Package information used by {@link android.content.pm.ShortcutManager} for backup / restore.
Makoto Onuki22fcc682016-05-17 14:52:19 -070040 *
41 * All methods should be guarded by {@code ShortcutService.mLock}.
Makoto Onuki0acbb142016-03-22 17:02:57 -070042 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -070043class ShortcutPackageInfo {
Makoto Onuki0acbb142016-03-22 17:02:57 -070044 private static final String TAG = ShortcutService.TAG;
45
46 static final String TAG_ROOT = "package-info";
Makoto Onuki0acbb142016-03-22 17:02:57 -070047 private static final String ATTR_VERSION = "version";
Makoto Onuki22fcc682016-05-17 14:52:19 -070048 private static final String ATTR_LAST_UPDATE_TIME = "last_udpate_time";
Makoto Onukia4f89b12017-10-05 10:37:55 -070049 private static final String ATTR_BACKUP_SOURCE_VERSION = "bk_src_version";
50 private static final String ATTR_BACKUP_ALLOWED = "allow-backup";
Makoto Onukie3fffa92018-02-28 16:25:40 -080051 private static final String ATTR_BACKUP_ALLOWED_INITIALIZED = "allow-backup-initialized";
Makoto Onukia4f89b12017-10-05 10:37:55 -070052 private static final String ATTR_BACKUP_SOURCE_BACKUP_ALLOWED = "bk_src_backup-allowed";
Makoto Onuki0acbb142016-03-22 17:02:57 -070053 private static final String ATTR_SHADOW = "shadow";
54
55 private static final String TAG_SIGNATURE = "signature";
56 private static final String ATTR_SIGNATURE_HASH = "hash";
57
Makoto Onuki0acbb142016-03-22 17:02:57 -070058 /**
59 * When true, this package information was restored from the previous device, and the app hasn't
60 * been installed yet.
61 */
62 private boolean mIsShadow;
Dianne Hackborn3accca02013-09-20 09:32:11 -070063 private long mVersionCode = ShortcutInfo.VERSION_CODE_UNKNOWN;
64 private long mBackupSourceVersionCode = ShortcutInfo.VERSION_CODE_UNKNOWN;
Makoto Onuki22fcc682016-05-17 14:52:19 -070065 private long mLastUpdateTime;
Makoto Onuki0acbb142016-03-22 17:02:57 -070066 private ArrayList<byte[]> mSigHashes;
67
Makoto Onukia4f89b12017-10-05 10:37:55 -070068 // mBackupAllowed didn't used to be parsisted, so we don't restore it from a file.
69 // mBackupAllowed will always start with false, and will have been updated before making a
70 // backup next time, which works file.
71 // We just don't want to print an uninitialzied mBackupAlldowed value on dumpsys, so
72 // we use this boolean to control dumpsys.
73 private boolean mBackupAllowedInitialized;
74 private boolean mBackupAllowed;
75 private boolean mBackupSourceBackupAllowed;
76
Dianne Hackborn3accca02013-09-20 09:32:11 -070077 private ShortcutPackageInfo(long versionCode, long lastUpdateTime,
Makoto Onuki22fcc682016-05-17 14:52:19 -070078 ArrayList<byte[]> sigHashes, boolean isShadow) {
Makoto Onuki0acbb142016-03-22 17:02:57 -070079 mVersionCode = versionCode;
Makoto Onuki22fcc682016-05-17 14:52:19 -070080 mLastUpdateTime = lastUpdateTime;
Makoto Onuki0acbb142016-03-22 17:02:57 -070081 mIsShadow = isShadow;
82 mSigHashes = sigHashes;
Makoto Onukia4f89b12017-10-05 10:37:55 -070083 mBackupAllowed = false; // By default, we assume false.
84 mBackupSourceBackupAllowed = false;
Makoto Onuki0acbb142016-03-22 17:02:57 -070085 }
86
Makoto Onuki9da23fc2016-03-29 11:14:42 -070087 public static ShortcutPackageInfo newEmpty() {
Makoto Onukia4f89b12017-10-05 10:37:55 -070088 return new ShortcutPackageInfo(ShortcutInfo.VERSION_CODE_UNKNOWN, /* last update time =*/ 0,
Makoto Onuki22fcc682016-05-17 14:52:19 -070089 new ArrayList<>(0), /* isShadow */ false);
Makoto Onukid99c6f02016-03-28 11:02:54 -070090 }
91
Makoto Onuki0acbb142016-03-22 17:02:57 -070092 public boolean isShadow() {
93 return mIsShadow;
94 }
95
Makoto Onuki0acbb142016-03-22 17:02:57 -070096 public void setShadow(boolean shadow) {
97 mIsShadow = shadow;
98 }
99
Dianne Hackborn3accca02013-09-20 09:32:11 -0700100 public long getVersionCode() {
Makoto Onuki0acbb142016-03-22 17:02:57 -0700101 return mVersionCode;
102 }
103
Dianne Hackborn3accca02013-09-20 09:32:11 -0700104 public long getBackupSourceVersionCode() {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700105 return mBackupSourceVersionCode;
106 }
107
108 @VisibleForTesting
109 public boolean isBackupSourceBackupAllowed() {
110 return mBackupSourceBackupAllowed;
111 }
112
Makoto Onuki22fcc682016-05-17 14:52:19 -0700113 public long getLastUpdateTime() {
114 return mLastUpdateTime;
115 }
116
Makoto Onukia4f89b12017-10-05 10:37:55 -0700117 public boolean isBackupAllowed() {
118 return mBackupAllowed;
119 }
120
121 /**
122 * Set {@link #mVersionCode}, {@link #mLastUpdateTime} and {@link #mBackupAllowed}
123 * from a {@link PackageInfo}.
124 */
125 public void updateFromPackageInfo(@NonNull PackageInfo pi) {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700126 if (pi != null) {
Dianne Hackborn3accca02013-09-20 09:32:11 -0700127 mVersionCode = pi.getLongVersionCode();
Makoto Onuki22fcc682016-05-17 14:52:19 -0700128 mLastUpdateTime = pi.lastUpdateTime;
Makoto Onukia4f89b12017-10-05 10:37:55 -0700129 mBackupAllowed = ShortcutService.shouldBackupApp(pi);
130 mBackupAllowedInitialized = true;
Makoto Onuki22fcc682016-05-17 14:52:19 -0700131 }
Makoto Onuki39686e82016-04-13 18:03:00 -0700132 }
133
Makoto Onuki2e210c42016-03-30 08:30:36 -0700134 public boolean hasSignatures() {
135 return mSigHashes.size() > 0;
136 }
137
Makoto Onukia4f89b12017-10-05 10:37:55 -0700138 //@DisabledReason
139 public int canRestoreTo(ShortcutService s, PackageInfo currentPackage, boolean anyVersionOkay) {
Amith Yamasani14c716c2018-03-05 20:39:04 +0000140 if (!BackupUtils.signaturesMatch(mSigHashes, currentPackage)) {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700141 Slog.w(TAG, "Can't restore: Package signature mismatch");
142 return ShortcutInfo.DISABLED_REASON_SIGNATURE_MISMATCH;
Makoto Onuki2e210c42016-03-30 08:30:36 -0700143 }
Makoto Onukia4f89b12017-10-05 10:37:55 -0700144 if (!ShortcutService.shouldBackupApp(currentPackage) || !mBackupSourceBackupAllowed) {
145 // "allowBackup" was true when backed up, but now false.
146 Slog.w(TAG, "Can't restore: package didn't or doesn't allow backup");
147 return ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED;
148 }
Dianne Hackborn3accca02013-09-20 09:32:11 -0700149 if (!anyVersionOkay && (currentPackage.getLongVersionCode() < mBackupSourceVersionCode)) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700150 Slog.w(TAG, String.format(
151 "Can't restore: package current version %d < backed up version %d",
Dianne Hackborn44947972017-12-11 16:44:08 -0800152 currentPackage.getLongVersionCode(), mBackupSourceVersionCode));
Makoto Onukia4f89b12017-10-05 10:37:55 -0700153 return ShortcutInfo.DISABLED_REASON_VERSION_LOWER;
Makoto Onuki0acbb142016-03-22 17:02:57 -0700154 }
Makoto Onukia4f89b12017-10-05 10:37:55 -0700155 return ShortcutInfo.DISABLED_REASON_NOT_DISABLED;
Makoto Onuki0acbb142016-03-22 17:02:57 -0700156 }
157
Makoto Onukic8c33292016-09-12 16:36:59 -0700158 @VisibleForTesting
159 public static ShortcutPackageInfo generateForInstalledPackageForTest(
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700160 ShortcutService s, String packageName, @UserIdInt int packageUserId) {
161 final PackageInfo pi = s.getPackageInfoWithSignatures(packageName, packageUserId);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700162 if (pi.signatures == null || pi.signatures.length == 0) {
163 Slog.e(TAG, "Can't get signatures: package=" + packageName);
164 return null;
165 }
Dianne Hackborn3accca02013-09-20 09:32:11 -0700166 final ShortcutPackageInfo ret = new ShortcutPackageInfo(pi.getLongVersionCode(),
167 pi.lastUpdateTime, BackupUtils.hashSignatureArray(pi.signatures),
168 /* shadow=*/ false);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700169
Makoto Onukia4f89b12017-10-05 10:37:55 -0700170 ret.mBackupSourceBackupAllowed = s.shouldBackupApp(pi);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700171 ret.mBackupSourceVersionCode = pi.getLongVersionCode();
Makoto Onuki0acbb142016-03-22 17:02:57 -0700172 return ret;
173 }
174
Makoto Onukic8c33292016-09-12 16:36:59 -0700175 public void refreshSignature(ShortcutService s, ShortcutPackageItem pkg) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700176 if (mIsShadow) {
177 s.wtf("Attempted to refresh package info for shadow package " + pkg.getPackageName()
178 + ", user=" + pkg.getOwnerUserId());
179 return;
180 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700181 // Note use mUserId here, rather than userId.
182 final PackageInfo pi = s.getPackageInfoWithSignatures(
183 pkg.getPackageName(), pkg.getPackageUserId());
Makoto Onuki0acbb142016-03-22 17:02:57 -0700184 if (pi == null) {
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700185 Slog.w(TAG, "Package not found: " + pkg.getPackageName());
Makoto Onuki0acbb142016-03-22 17:02:57 -0700186 return;
187 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700188 mSigHashes = BackupUtils.hashSignatureArray(pi.signatures);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700189 }
190
Makoto Onukie3fffa92018-02-28 16:25:40 -0800191 public void saveToXml(ShortcutService s, XmlSerializer out, boolean forBackup)
192 throws IOException {
193 if (forBackup && !mBackupAllowedInitialized) {
194 s.wtf("Backup happened before mBackupAllowed is initialized.");
195 }
Makoto Onuki0acbb142016-03-22 17:02:57 -0700196
197 out.startTag(null, TAG_ROOT);
198
Makoto Onuki0acbb142016-03-22 17:02:57 -0700199 ShortcutService.writeAttr(out, ATTR_VERSION, mVersionCode);
Makoto Onuki22fcc682016-05-17 14:52:19 -0700200 ShortcutService.writeAttr(out, ATTR_LAST_UPDATE_TIME, mLastUpdateTime);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700201 ShortcutService.writeAttr(out, ATTR_SHADOW, mIsShadow);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700202 ShortcutService.writeAttr(out, ATTR_BACKUP_ALLOWED, mBackupAllowed);
203
Makoto Onukie3fffa92018-02-28 16:25:40 -0800204 // We don't need to save this field (we don't even read it back), but it'll show up
205 // in the dumpsys in the backup / restore payload.
206 ShortcutService.writeAttr(out, ATTR_BACKUP_ALLOWED_INITIALIZED, mBackupAllowedInitialized);
207
Makoto Onukia4f89b12017-10-05 10:37:55 -0700208 ShortcutService.writeAttr(out, ATTR_BACKUP_SOURCE_VERSION, mBackupSourceVersionCode);
209 ShortcutService.writeAttr(out,
210 ATTR_BACKUP_SOURCE_BACKUP_ALLOWED, mBackupSourceBackupAllowed);
211
Makoto Onuki0acbb142016-03-22 17:02:57 -0700212
213 for (int i = 0; i < mSigHashes.size(); i++) {
214 out.startTag(null, TAG_SIGNATURE);
Tobias Thierer9f00d712016-12-01 23:04:36 +0000215 final String encoded = Base64.getEncoder().encodeToString(mSigHashes.get(i));
216 ShortcutService.writeAttr(out, ATTR_SIGNATURE_HASH, encoded);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700217 out.endTag(null, TAG_SIGNATURE);
218 }
219 out.endTag(null, TAG_ROOT);
220 }
221
Makoto Onuki2e210c42016-03-30 08:30:36 -0700222 public void loadFromXml(XmlPullParser parser, boolean fromBackup)
Makoto Onuki0acbb142016-03-22 17:02:57 -0700223 throws IOException, XmlPullParserException {
224
Makoto Onukia4f89b12017-10-05 10:37:55 -0700225 // Don't use the version code from the backup file.
Dianne Hackborn3accca02013-09-20 09:32:11 -0700226 final long versionCode = ShortcutService.parseLongAttribute(parser, ATTR_VERSION,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700227 ShortcutInfo.VERSION_CODE_UNKNOWN);
Makoto Onuki2e210c42016-03-30 08:30:36 -0700228
Makoto Onuki440a1ea2016-07-20 14:21:18 -0700229 final long lastUpdateTime = ShortcutService.parseLongAttribute(
Makoto Onuki22fcc682016-05-17 14:52:19 -0700230 parser, ATTR_LAST_UPDATE_TIME);
231
Makoto Onuki2e210c42016-03-30 08:30:36 -0700232 // When restoring from backup, it's always shadow.
233 final boolean shadow =
234 fromBackup || ShortcutService.parseBooleanAttribute(parser, ATTR_SHADOW);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700235
Makoto Onukia4f89b12017-10-05 10:37:55 -0700236 // We didn't used to save these attributes, and all backed up shortcuts were from
237 // apps that support backups, so the default values take this fact into consideration.
Dianne Hackborn3accca02013-09-20 09:32:11 -0700238 final long backupSourceVersion = ShortcutService.parseLongAttribute(parser,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700239 ATTR_BACKUP_SOURCE_VERSION, ShortcutInfo.VERSION_CODE_UNKNOWN);
240
241 // Note the only time these "true" default value is used is when restoring from an old
242 // build that didn't save ATTR_BACKUP_ALLOWED, and that means all the data included in
243 // a backup file were from apps that support backup, so we can just use "true" as the
244 // default.
245 final boolean backupAllowed = ShortcutService.parseBooleanAttribute(
246 parser, ATTR_BACKUP_ALLOWED, true);
247 final boolean backupSourceBackupAllowed = ShortcutService.parseBooleanAttribute(
248 parser, ATTR_BACKUP_SOURCE_BACKUP_ALLOWED, true);
249
Makoto Onuki0acbb142016-03-22 17:02:57 -0700250 final ArrayList<byte[]> hashes = new ArrayList<>();
251
Makoto Onuki0acbb142016-03-22 17:02:57 -0700252 final int outerDepth = parser.getDepth();
253 int type;
254 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
255 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
256 if (type != XmlPullParser.START_TAG) {
257 continue;
258 }
259 final int depth = parser.getDepth();
260 final String tag = parser.getName();
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700261
262 if (depth == outerDepth + 1) {
263 switch (tag) {
264 case TAG_SIGNATURE: {
265 final String hash = ShortcutService.parseStringAttribute(
266 parser, ATTR_SIGNATURE_HASH);
Tobias Thierer9f00d712016-12-01 23:04:36 +0000267 // Throws IllegalArgumentException if hash is invalid base64 data
268 final byte[] decoded = Base64.getDecoder().decode(hash);
269 hashes.add(decoded);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700270 continue;
271 }
Makoto Onuki0acbb142016-03-22 17:02:57 -0700272 }
273 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700274 ShortcutService.warnForInvalidTag(depth, tag);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700275 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700276
Makoto Onukia4f89b12017-10-05 10:37:55 -0700277 // Successfully loaded; replace the fields.
278 if (fromBackup) {
279 mVersionCode = ShortcutInfo.VERSION_CODE_UNKNOWN;
280 mBackupSourceVersionCode = versionCode;
281 mBackupSourceBackupAllowed = backupAllowed;
282 } else {
283 mVersionCode = versionCode;
284 mBackupSourceVersionCode = backupSourceVersion;
285 mBackupSourceBackupAllowed = backupSourceBackupAllowed;
286 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700287 mLastUpdateTime = lastUpdateTime;
Makoto Onuki2e210c42016-03-30 08:30:36 -0700288 mIsShadow = shadow;
289 mSigHashes = hashes;
Makoto Onukia4f89b12017-10-05 10:37:55 -0700290
291 // Note we don't restore it from the file because it didn't used to be saved.
292 // We always start by assuming backup is disabled for the current package,
293 // and this field will have been updated before we actually create a backup, at the same
294 // time when we update the version code.
295 // Until then, the value of mBackupAllowed shouldn't matter, but we don't want to print
296 // a false flag on dumpsys, so set mBackupAllowedInitialized to false.
297 mBackupAllowed = false;
298 mBackupAllowedInitialized = false;
Makoto Onuki0acbb142016-03-22 17:02:57 -0700299 }
300
Makoto Onukic51b2872016-05-04 15:24:50 -0700301 public void dump(PrintWriter pw, String prefix) {
Makoto Onuki0acbb142016-03-22 17:02:57 -0700302 pw.println();
303
304 pw.print(prefix);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700305 pw.println("PackageInfo:");
Makoto Onukid99c6f02016-03-28 11:02:54 -0700306
307 pw.print(prefix);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700308 pw.print(" IsShadow: ");
309 pw.print(mIsShadow);
Makoto Onukia4f89b12017-10-05 10:37:55 -0700310 pw.print(mIsShadow ? " (not installed)" : " (installed)");
Makoto Onuki0acbb142016-03-22 17:02:57 -0700311 pw.println();
312
313 pw.print(prefix);
314 pw.print(" Version: ");
315 pw.print(mVersionCode);
316 pw.println();
317
Makoto Onukia4f89b12017-10-05 10:37:55 -0700318 if (mBackupAllowedInitialized) {
319 pw.print(prefix);
320 pw.print(" Backup Allowed: ");
321 pw.print(mBackupAllowed);
322 pw.println();
323 }
324
325 if (mBackupSourceVersionCode != ShortcutInfo.VERSION_CODE_UNKNOWN) {
326 pw.print(prefix);
327 pw.print(" Backup source version: ");
328 pw.print(mBackupSourceVersionCode);
329 pw.println();
330
331 pw.print(prefix);
332 pw.print(" Backup source backup allowed: ");
333 pw.print(mBackupSourceBackupAllowed);
334 pw.println();
335 }
336
Makoto Onuki22fcc682016-05-17 14:52:19 -0700337 pw.print(prefix);
338 pw.print(" Last package update time: ");
339 pw.print(mLastUpdateTime);
340 pw.println();
341
Makoto Onuki0acbb142016-03-22 17:02:57 -0700342 for (int i = 0; i < mSigHashes.size(); i++) {
343 pw.print(prefix);
344 pw.print(" ");
345 pw.print("SigHash: ");
346 pw.println(HexEncoding.encode(mSigHashes.get(i)));
347 }
348 }
349}