ShortcutPackageItem.java revision fc4cf2da34335fd7e84c020f51eea1a341d3f134
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.json.JSONException; 25import org.json.JSONObject; 26import org.xmlpull.v1.XmlPullParserException; 27import org.xmlpull.v1.XmlSerializer; 28 29import java.io.IOException; 30 31/** 32 * All methods should be guarded by {@code #mShortcutUser.mService.mLock}. 33 */ 34abstract class ShortcutPackageItem { 35 private static final String TAG = ShortcutService.TAG; 36 private static final String KEY_NAME = "name"; 37 38 private final int mPackageUserId; 39 private final String mPackageName; 40 41 private final ShortcutPackageInfo mPackageInfo; 42 43 protected ShortcutUser mShortcutUser; 44 45 protected ShortcutPackageItem(@NonNull ShortcutUser shortcutUser, 46 int packageUserId, @NonNull String packageName, 47 @NonNull ShortcutPackageInfo packageInfo) { 48 mShortcutUser = shortcutUser; 49 mPackageUserId = packageUserId; 50 mPackageName = Preconditions.checkStringNotEmpty(packageName); 51 mPackageInfo = Preconditions.checkNotNull(packageInfo); 52 } 53 54 /** 55 * Change the parent {@link ShortcutUser}. Need it in the restore code. 56 */ 57 public void replaceUser(ShortcutUser user) { 58 mShortcutUser = user; 59 } 60 61 public ShortcutUser getUser() { 62 return mShortcutUser; 63 } 64 65 /** 66 * ID of the user who actually has this package running on. For {@link ShortcutPackage}, 67 * this is the same thing as {@link #getOwnerUserId}, but if it's a {@link ShortcutLauncher} and 68 * {@link #getOwnerUserId} is of a work profile, then this ID could be the user who owns the 69 * profile. 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 89 public void refreshPackageInfoAndSave() { 90 if (mPackageInfo.isShadow()) { 91 return; // Don't refresh for shadow user. 92 } 93 final ShortcutService s = mShortcutUser.mService; 94 mPackageInfo.refresh(s, this); 95 s.scheduleSaveUser(getOwnerUserId()); 96 } 97 98 public void attemptToRestoreIfNeededAndSave() { 99 if (!mPackageInfo.isShadow()) { 100 return; // Already installed, nothing to do. 101 } 102 final ShortcutService s = mShortcutUser.mService; 103 if (!s.isPackageInstalled(mPackageName, mPackageUserId)) { 104 if (ShortcutService.DEBUG) { 105 Slog.d(TAG, String.format("Package still not installed: %s user=%d", 106 mPackageName, mPackageUserId)); 107 } 108 return; // Not installed, no need to restore yet. 109 } 110 if (!mPackageInfo.hasSignatures()) { 111 s.wtf("Attempted to restore package " + mPackageName + ", user=" + mPackageUserId 112 + " but signatures not found in the restore data."); 113 onRestoreBlocked(); 114 return; 115 } 116 117 final PackageInfo pi = s.getPackageInfoWithSignatures(mPackageName, mPackageUserId); 118 if (!mPackageInfo.canRestoreTo(s, pi)) { 119 // Package is now installed, but can't restore. Let the subclass do the cleanup. 120 onRestoreBlocked(); 121 return; 122 } 123 if (ShortcutService.DEBUG) { 124 Slog.d(TAG, String.format("Restored package: %s/%d on user %d", mPackageName, 125 mPackageUserId, getOwnerUserId())); 126 } 127 128 onRestored(); 129 130 // Now the package is not shadow. 131 mPackageInfo.setShadow(false); 132 133 s.scheduleSaveUser(mPackageUserId); 134 } 135 136 /** 137 * Called when the new package can't be restored because it has a lower version number 138 * or different signatures. 139 */ 140 protected abstract void onRestoreBlocked(); 141 142 /** 143 * Called when the new package is successfully restored. 144 */ 145 protected abstract void onRestored(); 146 147 public abstract void saveToXml(@NonNull XmlSerializer out, boolean forBackup) 148 throws IOException, XmlPullParserException; 149 150 public JSONObject dumpCheckin(boolean clear) throws JSONException { 151 final JSONObject result = new JSONObject(); 152 result.put(KEY_NAME, mPackageName); 153 return result; 154 } 155 156 /** 157 * Verify various internal states. 158 */ 159 public void verifyStates() { 160 } 161} 162