ShortcutPackageItem.java revision c51b2876ec5c0af449469a0f76bb38c51cfcff04
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() { 73 if (mPackageInfo.isShadow()) { 74 return; // Don't refresh for shadow user. 75 } 76 final ShortcutService s = mShortcutUser.mService; 77 mPackageInfo.refresh(s, this); 78 s.scheduleSaveUser(getOwnerUserId()); 79 } 80 81 public void attemptToRestoreIfNeededAndSave() { 82 if (!mPackageInfo.isShadow()) { 83 return; // Already installed, nothing to do. 84 } 85 final ShortcutService s = mShortcutUser.mService; 86 if (!s.isPackageInstalled(mPackageName, mPackageUserId)) { 87 if (ShortcutService.DEBUG) { 88 Slog.d(TAG, String.format("Package still not installed: %s user=%d", 89 mPackageName, mPackageUserId)); 90 } 91 return; // Not installed, no need to restore yet. 92 } 93 if (!mPackageInfo.hasSignatures()) { 94 s.wtf("Attempted to restore package " + mPackageName + ", user=" + mPackageUserId 95 + " but signatures not found in the restore data."); 96 onRestoreBlocked(); 97 return; 98 } 99 100 final PackageInfo pi = s.getPackageInfoWithSignatures(mPackageName, mPackageUserId); 101 if (!mPackageInfo.canRestoreTo(s, pi)) { 102 // Package is now installed, but can't restore. Let the subclass do the cleanup. 103 onRestoreBlocked(); 104 return; 105 } 106 if (ShortcutService.DEBUG) { 107 Slog.d(TAG, String.format("Restored package: %s/%d on user %d", mPackageName, 108 mPackageUserId, getOwnerUserId())); 109 } 110 111 onRestored(); 112 113 // Now the package is not shadow. 114 mPackageInfo.setShadow(false); 115 116 s.scheduleSaveUser(mPackageUserId); 117 } 118 119 /** 120 * Called when the new package can't be restored because it has a lower version number 121 * or different signatures. 122 */ 123 protected abstract void onRestoreBlocked(); 124 125 /** 126 * Called when the new package is successfully restored. 127 */ 128 protected abstract void onRestored(); 129 130 public abstract void saveToXml(@NonNull XmlSerializer out, boolean forBackup) 131 throws IOException, XmlPullParserException; 132} 133