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