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 | |
| 18 | import android.annotation.NonNull; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 19 | import android.content.pm.PackageInfo; |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 20 | import android.content.pm.ShortcutInfo; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 21 | import android.util.Slog; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 22 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 23 | import com.android.internal.util.Preconditions; |
| 24 | |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 25 | import org.json.JSONException; |
| 26 | import org.json.JSONObject; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 27 | import org.xmlpull.v1.XmlPullParserException; |
| 28 | import org.xmlpull.v1.XmlSerializer; |
| 29 | |
| 30 | import java.io.IOException; |
| 31 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 32 | /** |
| 33 | * All methods should be guarded by {@code #mShortcutUser.mService.mLock}. |
| 34 | */ |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 35 | abstract class ShortcutPackageItem { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 36 | private static final String TAG = ShortcutService.TAG; |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 37 | private static final String KEY_NAME = "name"; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 38 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 39 | private final int mPackageUserId; |
| 40 | private final String mPackageName; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 41 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 42 | private final ShortcutPackageInfo mPackageInfo; |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 43 | |
Makoto Onuki | fc4cf2d | 2016-08-24 11:10:26 -0700 | [diff] [blame] | 44 | protected ShortcutUser mShortcutUser; |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 45 | |
| 46 | protected ShortcutPackageItem(@NonNull ShortcutUser shortcutUser, |
| 47 | int packageUserId, @NonNull String packageName, |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 48 | @NonNull ShortcutPackageInfo packageInfo) { |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 49 | mShortcutUser = shortcutUser; |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 50 | mPackageUserId = packageUserId; |
| 51 | mPackageName = Preconditions.checkStringNotEmpty(packageName); |
| 52 | mPackageInfo = Preconditions.checkNotNull(packageInfo); |
| 53 | } |
| 54 | |
Makoto Onuki | fc4cf2d | 2016-08-24 11:10:26 -0700 | [diff] [blame] | 55 | /** |
| 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 Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 62 | public ShortcutUser getUser() { |
| 63 | return mShortcutUser; |
| 64 | } |
| 65 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 66 | /** |
| 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 Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 69 | * {@link #getOwnerUserId} is of work profile, then this ID is of the primary user. |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 70 | */ |
| 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 Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 89 | public void refreshPackageSignatureAndSave() { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 90 | if (mPackageInfo.isShadow()) { |
| 91 | return; // Don't refresh for shadow user. |
| 92 | } |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 93 | final ShortcutService s = mShortcutUser.mService; |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 94 | mPackageInfo.refreshSignature(s, this); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 95 | s.scheduleSaveUser(getOwnerUserId()); |
| 96 | } |
| 97 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 98 | public void attemptToRestoreIfNeededAndSave() { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 99 | if (!mPackageInfo.isShadow()) { |
| 100 | return; // Already installed, nothing to do. |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 101 | } |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 102 | final ShortcutService s = mShortcutUser.mService; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 103 | if (!s.isPackageInstalled(mPackageName, mPackageUserId)) { |
| 104 | if (ShortcutService.DEBUG) { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 105 | Slog.d(TAG, String.format("Package still not installed: %s/u%d", |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 106 | mPackageName, mPackageUserId)); |
| 107 | } |
| 108 | return; // Not installed, no need to restore yet. |
| 109 | } |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 110 | int restoreBlockReason; |
| 111 | int currentVersionCode = ShortcutInfo.VERSION_CODE_UNKNOWN; |
Makoto Onuki | fac592f | 2016-11-21 13:41:32 -0800 | [diff] [blame] | 112 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 113 | 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); |
| 119 | currentVersionCode = pi.versionCode; |
| 120 | restoreBlockReason = mPackageInfo.canRestoreTo(s, pi, canRestoreAnyVersion()); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 123 | 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 Onuki | b1588c0 | 2017-10-12 15:11:45 -0700 | [diff] [blame] | 126 | ShortcutInfo.getDisabledReasonDebugString(restoreBlockReason), |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 127 | getOwnerUserId())); |
| 128 | } |
| 129 | |
| 130 | onRestored(restoreBlockReason); |
| 131 | |
Makoto Onuki | fac592f | 2016-11-21 13:41:32 -0800 | [diff] [blame] | 132 | // Either way, it's no longer a shadow. |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 133 | mPackageInfo.setShadow(false); |
| 134 | |
| 135 | s.scheduleSaveUser(mPackageUserId); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 138 | protected abstract boolean canRestoreAnyVersion(); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 139 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 140 | protected abstract void onRestored(int restoreBlockReason); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 141 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 142 | public abstract void saveToXml(@NonNull XmlSerializer out, boolean forBackup) |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 143 | throws IOException, XmlPullParserException; |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 144 | |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 145 | 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 Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 151 | /** |
| 152 | * Verify various internal states. |
| 153 | */ |
| 154 | public void verifyStates() { |
| 155 | } |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 156 | } |