Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.server.pm; |
| 17 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 18 | import android.annotation.NonNull; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 19 | import android.annotation.UserIdInt; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 20 | import android.content.pm.PackageInfo; |
Michal Karpinski | 528c3e5 | 2018-02-07 17:47:10 +0000 | [diff] [blame] | 21 | import android.content.pm.PackageManagerInternal; |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 22 | import android.content.pm.ShortcutInfo; |
Michal Karpinski | 528c3e5 | 2018-02-07 17:47:10 +0000 | [diff] [blame] | 23 | import android.content.pm.Signature; |
Dan Cashman | 5c9f527e | 2018-04-03 16:42:23 -0700 | [diff] [blame] | 24 | import android.content.pm.SigningInfo; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 25 | import android.util.Slog; |
| 26 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 27 | import com.android.internal.annotations.VisibleForTesting; |
Michal Karpinski | 528c3e5 | 2018-02-07 17:47:10 +0000 | [diff] [blame] | 28 | import com.android.server.LocalServices; |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 29 | import com.android.server.backup.BackupUtils; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 30 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 31 | import libcore.util.HexEncoding; |
| 32 | |
| 33 | import org.xmlpull.v1.XmlPullParser; |
| 34 | import org.xmlpull.v1.XmlPullParserException; |
| 35 | import org.xmlpull.v1.XmlSerializer; |
| 36 | |
| 37 | import java.io.IOException; |
| 38 | import java.io.PrintWriter; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 39 | import java.util.ArrayList; |
Tobias Thierer | 9f00d71 | 2016-12-01 23:04:36 +0000 | [diff] [blame] | 40 | import java.util.Base64; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Package information used by {@link android.content.pm.ShortcutManager} for backup / restore. |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 44 | * |
| 45 | * All methods should be guarded by {@code ShortcutService.mLock}. |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 46 | */ |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 47 | class ShortcutPackageInfo { |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 48 | private static final String TAG = ShortcutService.TAG; |
| 49 | |
| 50 | static final String TAG_ROOT = "package-info"; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 51 | private static final String ATTR_VERSION = "version"; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 52 | private static final String ATTR_LAST_UPDATE_TIME = "last_udpate_time"; |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 53 | private static final String ATTR_BACKUP_SOURCE_VERSION = "bk_src_version"; |
| 54 | private static final String ATTR_BACKUP_ALLOWED = "allow-backup"; |
Makoto Onuki | e3fffa9 | 2018-02-28 16:25:40 -0800 | [diff] [blame] | 55 | private static final String ATTR_BACKUP_ALLOWED_INITIALIZED = "allow-backup-initialized"; |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 56 | private static final String ATTR_BACKUP_SOURCE_BACKUP_ALLOWED = "bk_src_backup-allowed"; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 57 | private static final String ATTR_SHADOW = "shadow"; |
| 58 | |
| 59 | private static final String TAG_SIGNATURE = "signature"; |
| 60 | private static final String ATTR_SIGNATURE_HASH = "hash"; |
| 61 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 62 | /** |
| 63 | * When true, this package information was restored from the previous device, and the app hasn't |
| 64 | * been installed yet. |
| 65 | */ |
| 66 | private boolean mIsShadow; |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 67 | private long mVersionCode = ShortcutInfo.VERSION_CODE_UNKNOWN; |
| 68 | private long mBackupSourceVersionCode = ShortcutInfo.VERSION_CODE_UNKNOWN; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 69 | private long mLastUpdateTime; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 70 | private ArrayList<byte[]> mSigHashes; |
| 71 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 72 | // mBackupAllowed didn't used to be parsisted, so we don't restore it from a file. |
| 73 | // mBackupAllowed will always start with false, and will have been updated before making a |
| 74 | // backup next time, which works file. |
| 75 | // We just don't want to print an uninitialzied mBackupAlldowed value on dumpsys, so |
| 76 | // we use this boolean to control dumpsys. |
| 77 | private boolean mBackupAllowedInitialized; |
| 78 | private boolean mBackupAllowed; |
| 79 | private boolean mBackupSourceBackupAllowed; |
| 80 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 81 | private ShortcutPackageInfo(long versionCode, long lastUpdateTime, |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 82 | ArrayList<byte[]> sigHashes, boolean isShadow) { |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 83 | mVersionCode = versionCode; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 84 | mLastUpdateTime = lastUpdateTime; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 85 | mIsShadow = isShadow; |
| 86 | mSigHashes = sigHashes; |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 87 | mBackupAllowed = false; // By default, we assume false. |
| 88 | mBackupSourceBackupAllowed = false; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 91 | public static ShortcutPackageInfo newEmpty() { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 92 | return new ShortcutPackageInfo(ShortcutInfo.VERSION_CODE_UNKNOWN, /* last update time =*/ 0, |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 93 | new ArrayList<>(0), /* isShadow */ false); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 96 | public boolean isShadow() { |
| 97 | return mIsShadow; |
| 98 | } |
| 99 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 100 | public void setShadow(boolean shadow) { |
| 101 | mIsShadow = shadow; |
| 102 | } |
| 103 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 104 | public long getVersionCode() { |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 105 | return mVersionCode; |
| 106 | } |
| 107 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 108 | public long getBackupSourceVersionCode() { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 109 | return mBackupSourceVersionCode; |
| 110 | } |
| 111 | |
| 112 | @VisibleForTesting |
| 113 | public boolean isBackupSourceBackupAllowed() { |
| 114 | return mBackupSourceBackupAllowed; |
| 115 | } |
| 116 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 117 | public long getLastUpdateTime() { |
| 118 | return mLastUpdateTime; |
| 119 | } |
| 120 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 121 | public boolean isBackupAllowed() { |
| 122 | return mBackupAllowed; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Set {@link #mVersionCode}, {@link #mLastUpdateTime} and {@link #mBackupAllowed} |
| 127 | * from a {@link PackageInfo}. |
| 128 | */ |
| 129 | public void updateFromPackageInfo(@NonNull PackageInfo pi) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 130 | if (pi != null) { |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 131 | mVersionCode = pi.getLongVersionCode(); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 132 | mLastUpdateTime = pi.lastUpdateTime; |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 133 | mBackupAllowed = ShortcutService.shouldBackupApp(pi); |
| 134 | mBackupAllowedInitialized = true; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 135 | } |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 138 | public boolean hasSignatures() { |
| 139 | return mSigHashes.size() > 0; |
| 140 | } |
| 141 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 142 | //@DisabledReason |
| 143 | public int canRestoreTo(ShortcutService s, PackageInfo currentPackage, boolean anyVersionOkay) { |
Michal Karpinski | 528c3e5 | 2018-02-07 17:47:10 +0000 | [diff] [blame] | 144 | PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
| 145 | if (!BackupUtils.signaturesMatch(mSigHashes, currentPackage, pmi)) { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 146 | Slog.w(TAG, "Can't restore: Package signature mismatch"); |
| 147 | return ShortcutInfo.DISABLED_REASON_SIGNATURE_MISMATCH; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 148 | } |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 149 | if (!ShortcutService.shouldBackupApp(currentPackage) || !mBackupSourceBackupAllowed) { |
| 150 | // "allowBackup" was true when backed up, but now false. |
| 151 | Slog.w(TAG, "Can't restore: package didn't or doesn't allow backup"); |
| 152 | return ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED; |
| 153 | } |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 154 | if (!anyVersionOkay && (currentPackage.getLongVersionCode() < mBackupSourceVersionCode)) { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 155 | Slog.w(TAG, String.format( |
| 156 | "Can't restore: package current version %d < backed up version %d", |
Dianne Hackborn | 4494797 | 2017-12-11 16:44:08 -0800 | [diff] [blame] | 157 | currentPackage.getLongVersionCode(), mBackupSourceVersionCode)); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 158 | return ShortcutInfo.DISABLED_REASON_VERSION_LOWER; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 159 | } |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 160 | return ShortcutInfo.DISABLED_REASON_NOT_DISABLED; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 163 | @VisibleForTesting |
| 164 | public static ShortcutPackageInfo generateForInstalledPackageForTest( |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 165 | ShortcutService s, String packageName, @UserIdInt int packageUserId) { |
| 166 | final PackageInfo pi = s.getPackageInfoWithSignatures(packageName, packageUserId); |
Michal Karpinski | 528c3e5 | 2018-02-07 17:47:10 +0000 | [diff] [blame] | 167 | // retrieve the newest sigs |
Dan Cashman | 5c9f527e | 2018-04-03 16:42:23 -0700 | [diff] [blame] | 168 | SigningInfo signingInfo = pi.signingInfo; |
| 169 | if (signingInfo == null) { |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 170 | Slog.e(TAG, "Can't get signatures: package=" + packageName); |
| 171 | return null; |
| 172 | } |
Dan Cashman | 5c9f527e | 2018-04-03 16:42:23 -0700 | [diff] [blame] | 173 | // TODO (b/73988180) use entire signing history in case of rollbacks |
| 174 | Signature[] signatures = signingInfo.getApkContentsSigners(); |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 175 | final ShortcutPackageInfo ret = new ShortcutPackageInfo(pi.getLongVersionCode(), |
Michal Karpinski | 528c3e5 | 2018-02-07 17:47:10 +0000 | [diff] [blame] | 176 | pi.lastUpdateTime, BackupUtils.hashSignatureArray(signatures), /* shadow=*/ false); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 177 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 178 | ret.mBackupSourceBackupAllowed = s.shouldBackupApp(pi); |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 179 | ret.mBackupSourceVersionCode = pi.getLongVersionCode(); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 180 | return ret; |
| 181 | } |
| 182 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 183 | public void refreshSignature(ShortcutService s, ShortcutPackageItem pkg) { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 184 | if (mIsShadow) { |
| 185 | s.wtf("Attempted to refresh package info for shadow package " + pkg.getPackageName() |
| 186 | + ", user=" + pkg.getOwnerUserId()); |
| 187 | return; |
| 188 | } |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 189 | // Note use mUserId here, rather than userId. |
| 190 | final PackageInfo pi = s.getPackageInfoWithSignatures( |
| 191 | pkg.getPackageName(), pkg.getPackageUserId()); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 192 | if (pi == null) { |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 193 | Slog.w(TAG, "Package not found: " + pkg.getPackageName()); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 194 | return; |
| 195 | } |
Michal Karpinski | 528c3e5 | 2018-02-07 17:47:10 +0000 | [diff] [blame] | 196 | // retrieve the newest sigs |
Dan Cashman | 5c9f527e | 2018-04-03 16:42:23 -0700 | [diff] [blame] | 197 | SigningInfo signingInfo = pi.signingInfo; |
| 198 | if (signingInfo == null) { |
Michal Karpinski | 528c3e5 | 2018-02-07 17:47:10 +0000 | [diff] [blame] | 199 | Slog.w(TAG, "Not refreshing signature for " + pkg.getPackageName() |
Dan Cashman | 5c9f527e | 2018-04-03 16:42:23 -0700 | [diff] [blame] | 200 | + " since it appears to have no signing info."); |
Michal Karpinski | 528c3e5 | 2018-02-07 17:47:10 +0000 | [diff] [blame] | 201 | return; |
| 202 | } |
Dan Cashman | 5c9f527e | 2018-04-03 16:42:23 -0700 | [diff] [blame] | 203 | // TODO (b/73988180) use entire signing history in case of rollbacks |
| 204 | Signature[] signatures = signingInfo.getApkContentsSigners(); |
Michal Karpinski | 528c3e5 | 2018-02-07 17:47:10 +0000 | [diff] [blame] | 205 | mSigHashes = BackupUtils.hashSignatureArray(signatures); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Makoto Onuki | e3fffa9 | 2018-02-28 16:25:40 -0800 | [diff] [blame] | 208 | public void saveToXml(ShortcutService s, XmlSerializer out, boolean forBackup) |
| 209 | throws IOException { |
| 210 | if (forBackup && !mBackupAllowedInitialized) { |
| 211 | s.wtf("Backup happened before mBackupAllowed is initialized."); |
| 212 | } |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 213 | |
| 214 | out.startTag(null, TAG_ROOT); |
| 215 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 216 | ShortcutService.writeAttr(out, ATTR_VERSION, mVersionCode); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 217 | ShortcutService.writeAttr(out, ATTR_LAST_UPDATE_TIME, mLastUpdateTime); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 218 | ShortcutService.writeAttr(out, ATTR_SHADOW, mIsShadow); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 219 | ShortcutService.writeAttr(out, ATTR_BACKUP_ALLOWED, mBackupAllowed); |
| 220 | |
Makoto Onuki | e3fffa9 | 2018-02-28 16:25:40 -0800 | [diff] [blame] | 221 | // We don't need to save this field (we don't even read it back), but it'll show up |
| 222 | // in the dumpsys in the backup / restore payload. |
| 223 | ShortcutService.writeAttr(out, ATTR_BACKUP_ALLOWED_INITIALIZED, mBackupAllowedInitialized); |
| 224 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 225 | ShortcutService.writeAttr(out, ATTR_BACKUP_SOURCE_VERSION, mBackupSourceVersionCode); |
| 226 | ShortcutService.writeAttr(out, |
| 227 | ATTR_BACKUP_SOURCE_BACKUP_ALLOWED, mBackupSourceBackupAllowed); |
| 228 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 229 | |
| 230 | for (int i = 0; i < mSigHashes.size(); i++) { |
| 231 | out.startTag(null, TAG_SIGNATURE); |
Tobias Thierer | 9f00d71 | 2016-12-01 23:04:36 +0000 | [diff] [blame] | 232 | final String encoded = Base64.getEncoder().encodeToString(mSigHashes.get(i)); |
| 233 | ShortcutService.writeAttr(out, ATTR_SIGNATURE_HASH, encoded); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 234 | out.endTag(null, TAG_SIGNATURE); |
| 235 | } |
| 236 | out.endTag(null, TAG_ROOT); |
| 237 | } |
| 238 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 239 | public void loadFromXml(XmlPullParser parser, boolean fromBackup) |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 240 | throws IOException, XmlPullParserException { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 241 | // Don't use the version code from the backup file. |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 242 | final long versionCode = ShortcutService.parseLongAttribute(parser, ATTR_VERSION, |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 243 | ShortcutInfo.VERSION_CODE_UNKNOWN); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 244 | |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 245 | final long lastUpdateTime = ShortcutService.parseLongAttribute( |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 246 | parser, ATTR_LAST_UPDATE_TIME); |
| 247 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 248 | // When restoring from backup, it's always shadow. |
| 249 | final boolean shadow = |
| 250 | fromBackup || ShortcutService.parseBooleanAttribute(parser, ATTR_SHADOW); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 251 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 252 | // We didn't used to save these attributes, and all backed up shortcuts were from |
| 253 | // apps that support backups, so the default values take this fact into consideration. |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 254 | final long backupSourceVersion = ShortcutService.parseLongAttribute(parser, |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 255 | ATTR_BACKUP_SOURCE_VERSION, ShortcutInfo.VERSION_CODE_UNKNOWN); |
| 256 | |
| 257 | // Note the only time these "true" default value is used is when restoring from an old |
| 258 | // build that didn't save ATTR_BACKUP_ALLOWED, and that means all the data included in |
| 259 | // a backup file were from apps that support backup, so we can just use "true" as the |
| 260 | // default. |
| 261 | final boolean backupAllowed = ShortcutService.parseBooleanAttribute( |
| 262 | parser, ATTR_BACKUP_ALLOWED, true); |
| 263 | final boolean backupSourceBackupAllowed = ShortcutService.parseBooleanAttribute( |
| 264 | parser, ATTR_BACKUP_SOURCE_BACKUP_ALLOWED, true); |
| 265 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 266 | final ArrayList<byte[]> hashes = new ArrayList<>(); |
| 267 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 268 | final int outerDepth = parser.getDepth(); |
| 269 | int type; |
| 270 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 271 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 272 | if (type != XmlPullParser.START_TAG) { |
| 273 | continue; |
| 274 | } |
| 275 | final int depth = parser.getDepth(); |
| 276 | final String tag = parser.getName(); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 277 | |
| 278 | if (depth == outerDepth + 1) { |
| 279 | switch (tag) { |
| 280 | case TAG_SIGNATURE: { |
| 281 | final String hash = ShortcutService.parseStringAttribute( |
| 282 | parser, ATTR_SIGNATURE_HASH); |
Tobias Thierer | 9f00d71 | 2016-12-01 23:04:36 +0000 | [diff] [blame] | 283 | // Throws IllegalArgumentException if hash is invalid base64 data |
| 284 | final byte[] decoded = Base64.getDecoder().decode(hash); |
| 285 | hashes.add(decoded); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 286 | continue; |
| 287 | } |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 290 | ShortcutService.warnForInvalidTag(depth, tag); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 291 | } |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 292 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 293 | // Successfully loaded; replace the fields. |
| 294 | if (fromBackup) { |
| 295 | mVersionCode = ShortcutInfo.VERSION_CODE_UNKNOWN; |
| 296 | mBackupSourceVersionCode = versionCode; |
| 297 | mBackupSourceBackupAllowed = backupAllowed; |
| 298 | } else { |
| 299 | mVersionCode = versionCode; |
| 300 | mBackupSourceVersionCode = backupSourceVersion; |
| 301 | mBackupSourceBackupAllowed = backupSourceBackupAllowed; |
| 302 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 303 | mLastUpdateTime = lastUpdateTime; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 304 | mIsShadow = shadow; |
| 305 | mSigHashes = hashes; |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 306 | |
| 307 | // Note we don't restore it from the file because it didn't used to be saved. |
| 308 | // We always start by assuming backup is disabled for the current package, |
| 309 | // and this field will have been updated before we actually create a backup, at the same |
| 310 | // time when we update the version code. |
| 311 | // Until then, the value of mBackupAllowed shouldn't matter, but we don't want to print |
| 312 | // a false flag on dumpsys, so set mBackupAllowedInitialized to false. |
| 313 | mBackupAllowed = false; |
| 314 | mBackupAllowedInitialized = false; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 317 | public void dump(PrintWriter pw, String prefix) { |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 318 | pw.println(); |
| 319 | |
| 320 | pw.print(prefix); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 321 | pw.println("PackageInfo:"); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 322 | |
| 323 | pw.print(prefix); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 324 | pw.print(" IsShadow: "); |
| 325 | pw.print(mIsShadow); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 326 | pw.print(mIsShadow ? " (not installed)" : " (installed)"); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 327 | pw.println(); |
| 328 | |
| 329 | pw.print(prefix); |
| 330 | pw.print(" Version: "); |
| 331 | pw.print(mVersionCode); |
| 332 | pw.println(); |
| 333 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 334 | if (mBackupAllowedInitialized) { |
| 335 | pw.print(prefix); |
| 336 | pw.print(" Backup Allowed: "); |
| 337 | pw.print(mBackupAllowed); |
| 338 | pw.println(); |
| 339 | } |
| 340 | |
| 341 | if (mBackupSourceVersionCode != ShortcutInfo.VERSION_CODE_UNKNOWN) { |
| 342 | pw.print(prefix); |
| 343 | pw.print(" Backup source version: "); |
| 344 | pw.print(mBackupSourceVersionCode); |
| 345 | pw.println(); |
| 346 | |
| 347 | pw.print(prefix); |
| 348 | pw.print(" Backup source backup allowed: "); |
| 349 | pw.print(mBackupSourceBackupAllowed); |
| 350 | pw.println(); |
| 351 | } |
| 352 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 353 | pw.print(prefix); |
| 354 | pw.print(" Last package update time: "); |
| 355 | pw.print(mLastUpdateTime); |
| 356 | pw.println(); |
| 357 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 358 | for (int i = 0; i < mSigHashes.size(); i++) { |
| 359 | pw.print(prefix); |
| 360 | pw.print(" "); |
| 361 | pw.print("SigHash: "); |
| 362 | pw.println(HexEncoding.encode(mSigHashes.get(i))); |
| 363 | } |
| 364 | } |
| 365 | } |