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