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; |
| 20 | import android.util.Slog; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 21 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 22 | import com.android.internal.util.Preconditions; |
| 23 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 24 | import org.xmlpull.v1.XmlPullParserException; |
| 25 | import org.xmlpull.v1.XmlSerializer; |
| 26 | |
| 27 | import java.io.IOException; |
| 28 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 29 | abstract class ShortcutPackageItem { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 30 | private static final String TAG = ShortcutService.TAG; |
| 31 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 32 | private final int mPackageUserId; |
| 33 | private final String mPackageName; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 34 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 35 | private final ShortcutPackageInfo mPackageInfo; |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 36 | |
| 37 | protected ShortcutPackageItem(int packageUserId, @NonNull String packageName, |
| 38 | @NonNull ShortcutPackageInfo packageInfo) { |
| 39 | mPackageUserId = packageUserId; |
| 40 | mPackageName = Preconditions.checkStringNotEmpty(packageName); |
| 41 | mPackageInfo = Preconditions.checkNotNull(packageInfo); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * ID of the user who actually has this package running on. For {@link ShortcutPackage}, |
| 46 | * this is the same thing as {@link #getOwnerUserId}, but if it's a {@link ShortcutLauncher} and |
| 47 | * {@link #getOwnerUserId} is of a work profile, then this ID could be the user who owns the |
| 48 | * profile. |
| 49 | */ |
| 50 | public int getPackageUserId() { |
| 51 | return mPackageUserId; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * ID of the user who sees the shortcuts from this instance. |
| 56 | */ |
| 57 | public abstract int getOwnerUserId(); |
| 58 | |
| 59 | @NonNull |
| 60 | public String getPackageName() { |
| 61 | return mPackageName; |
| 62 | } |
| 63 | |
| 64 | public ShortcutPackageInfo getPackageInfo() { |
| 65 | return mPackageInfo; |
| 66 | } |
| 67 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 68 | public void refreshPackageInfoAndSave(ShortcutService s) { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 69 | if (mPackageInfo.isShadow()) { |
| 70 | return; // Don't refresh for shadow user. |
| 71 | } |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 72 | mPackageInfo.refresh(s, this); |
| 73 | s.scheduleSaveUser(getOwnerUserId()); |
| 74 | } |
| 75 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 76 | public void attemptToRestoreIfNeededAndSave(ShortcutService s) { |
| 77 | if (!mPackageInfo.isShadow()) { |
| 78 | return; // Already installed, nothing to do. |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 79 | } |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 80 | if (!s.isPackageInstalled(mPackageName, mPackageUserId)) { |
| 81 | if (ShortcutService.DEBUG) { |
| 82 | Slog.d(TAG, String.format("Package still not installed: %s user=%d", |
| 83 | mPackageName, mPackageUserId)); |
| 84 | } |
| 85 | return; // Not installed, no need to restore yet. |
| 86 | } |
| 87 | if (!mPackageInfo.hasSignatures()) { |
| 88 | s.wtf("Attempted to restore package " + mPackageName + ", user=" + mPackageUserId |
| 89 | + " but signatures not found in the restore data."); |
| 90 | onRestoreBlocked(s); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | final PackageInfo pi = s.getPackageInfoWithSignatures(mPackageName, mPackageUserId); |
| 95 | if (!mPackageInfo.canRestoreTo(s, pi)) { |
| 96 | // Package is now installed, but can't restore. Let the subclass do the cleanup. |
| 97 | onRestoreBlocked(s); |
| 98 | return; |
| 99 | } |
| 100 | if (ShortcutService.DEBUG) { |
| 101 | Slog.d(TAG, String.format("Restored package: %s/%d on user %d", mPackageName, |
| 102 | mPackageUserId, getOwnerUserId())); |
| 103 | } |
| 104 | |
| 105 | onRestored(s); |
| 106 | |
| 107 | // Now the package is not shadow. |
| 108 | mPackageInfo.setShadow(false); |
| 109 | |
| 110 | s.scheduleSaveUser(mPackageUserId); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Called when the new package can't be restored because it has a lower version number |
| 115 | * or different signatures. |
| 116 | */ |
| 117 | protected abstract void onRestoreBlocked(ShortcutService s); |
| 118 | |
| 119 | /** |
| 120 | * Called when the new package is successfully restored. |
| 121 | */ |
| 122 | protected abstract void onRestored(ShortcutService s); |
| 123 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 124 | public abstract void saveToXml(@NonNull XmlSerializer out, boolean forBackup) |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 125 | throws IOException, XmlPullParserException; |
| 126 | } |