Loading core/java/android/content/pm/IShortcutService.aidl +4 −0 Original line number Original line Diff line number Diff line Loading @@ -47,4 +47,8 @@ interface IShortcutService { int getIconMaxDimensions(String packageName, int userId); int getIconMaxDimensions(String packageName, int userId); void resetThrottling(); // system only API for developer opsions void resetThrottling(); // system only API for developer opsions byte[] getBackupPayload(int user); void applyRestore(in byte[] payload, int user); } } No newline at end of file services/core/java/com/android/server/pm/ShortcutLauncher.java +62 −51 Original line number Original line Diff line number Diff line Loading @@ -32,7 +32,7 @@ import java.util.List; /** /** * Launcher information used by {@link ShortcutService}. * Launcher information used by {@link ShortcutService}. */ */ class ShortcutLauncher implements ShortcutPackageItem { class ShortcutLauncher extends ShortcutPackageItem { private static final String TAG = ShortcutService.TAG; private static final String TAG = ShortcutService.TAG; static final String TAG_ROOT = "launcher-pins"; static final String TAG_ROOT = "launcher-pins"; Loading @@ -44,44 +44,34 @@ class ShortcutLauncher implements ShortcutPackageItem { private static final String ATTR_VALUE = "value"; private static final String ATTR_VALUE = "value"; private static final String ATTR_PACKAGE_NAME = "package-name"; private static final String ATTR_PACKAGE_NAME = "package-name"; @UserIdInt private final int mOwnerUserId; private final int mUserId; @NonNull private final String mPackageName; @UserIdInt private final int mLauncherUserId; /** /** * Package name -> IDs. * Package name -> IDs. */ */ final private ArrayMap<String, ArraySet<String>> mPinnedShortcuts = new ArrayMap<>(); final private ArrayMap<String, ArraySet<String>> mPinnedShortcuts = new ArrayMap<>(); ShortcutLauncher(@UserIdInt int userId, @NonNull String packageName, public ShortcutLauncher(@UserIdInt int ownerUserId, @NonNull String packageName, @UserIdInt int launcherUserId) { @UserIdInt int launcherUserId, ShortcutPackageInfo spi) { mUserId = userId; super(launcherUserId, packageName, spi != null ? spi : ShortcutPackageInfo.newEmpty()); mPackageName = packageName; mOwnerUserId = ownerUserId; mLauncherUserId = launcherUserId; } } @UserIdInt public ShortcutLauncher(@UserIdInt int ownerUserId, @NonNull String packageName, public int getUserId() { @UserIdInt int launcherUserId) { return mUserId; this(launcherUserId, packageName, launcherUserId, null); } } @UserIdInt @Override public int getLauncherUserId() { public int getOwnerUserId() { return mLauncherUserId; return mOwnerUserId; } } @NonNull public void pinShortcuts(@NonNull ShortcutService s, @UserIdInt int packageUserId, public String getPackageName() { @NonNull String packageName, @NonNull List<String> ids) { return mPackageName; final ShortcutPackage packageShortcuts = } s.getPackageShortcutsLocked(packageName, packageUserId); public void pinShortcuts(@NonNull ShortcutService s, @NonNull String packageName, @NonNull List<String> ids) { final int idSize = ids.size(); final int idSize = ids.size(); if (idSize == 0) { if (idSize == 0) { mPinnedShortcuts.remove(packageName); mPinnedShortcuts.remove(packageName); Loading @@ -91,8 +81,6 @@ class ShortcutLauncher implements ShortcutPackageItem { // Pin shortcuts. Make sure only pin the ones that were visible to the caller. // Pin shortcuts. Make sure only pin the ones that were visible to the caller. // i.e. a non-dynamic, pinned shortcut by *other launchers* shouldn't be pinned here. // i.e. a non-dynamic, pinned shortcut by *other launchers* shouldn't be pinned here. final ShortcutPackage packageShortcuts = s.getPackageShortcutsLocked(packageName, mUserId); final ArraySet<String> newSet = new ArraySet<>(); final ArraySet<String> newSet = new ArraySet<>(); for (int i = 0; i < idSize; i++) { for (int i = 0; i < idSize; i++) { Loading @@ -107,7 +95,7 @@ class ShortcutLauncher implements ShortcutPackageItem { } } mPinnedShortcuts.put(packageName, newSet); mPinnedShortcuts.put(packageName, newSet); } } s.getPackageShortcutsLocked(packageName, mUserId).refreshPinnedFlags(s); packageShortcuts.refreshPinnedFlags(s); } } /** /** Loading @@ -124,15 +112,18 @@ class ShortcutLauncher implements ShortcutPackageItem { /** /** * Persist. * Persist. */ */ public void saveToXml(XmlSerializer out, boolean forBackup) throws IOException { @Override public void saveToXml(XmlSerializer out, boolean forBackup) throws IOException { final int size = mPinnedShortcuts.size(); final int size = mPinnedShortcuts.size(); if (size == 0) { if (size == 0) { return; // Nothing to write. return; // Nothing to write. } } out.startTag(null, TAG_ROOT); out.startTag(null, TAG_ROOT); ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, mPackageName); ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, getPackageName()); ShortcutService.writeAttr(out, ATTR_LAUNCHER_USER_ID, mLauncherUserId); ShortcutService.writeAttr(out, ATTR_LAUNCHER_USER_ID, getPackageUserId()); getPackageInfo().saveToXml(out); for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) { out.startTag(null, TAG_PACKAGE); out.startTag(null, TAG_PACKAGE); Loading @@ -153,16 +144,21 @@ class ShortcutLauncher implements ShortcutPackageItem { /** /** * Load. * Load. */ */ public static ShortcutLauncher loadFromXml(XmlPullParser parser, int ownerUserId) public static ShortcutLauncher loadFromXml(XmlPullParser parser, int ownerUserId, throws IOException, XmlPullParserException { boolean fromBackup) throws IOException, XmlPullParserException { final String launcherPackageName = ShortcutService.parseStringAttribute(parser, final String launcherPackageName = ShortcutService.parseStringAttribute(parser, ATTR_PACKAGE_NAME); ATTR_PACKAGE_NAME); final int launcherUserId = ShortcutService.parseIntAttribute(parser, ATTR_LAUNCHER_USER_ID, ownerUserId); // If restoring, just use the real user ID. final int launcherUserId = fromBackup ? ownerUserId : ShortcutService.parseIntAttribute(parser, ATTR_LAUNCHER_USER_ID, ownerUserId); final ShortcutLauncher ret = new ShortcutLauncher(launcherUserId, launcherPackageName, final ShortcutLauncher ret = new ShortcutLauncher(launcherUserId, launcherPackageName, launcherUserId); launcherUserId); ShortcutPackageInfo spi = null; ArraySet<String> ids = null; ArraySet<String> ids = null; final int outerDepth = parser.getDepth(); final int outerDepth = parser.getDepth(); int type; int type; Loading @@ -173,7 +169,11 @@ class ShortcutLauncher implements ShortcutPackageItem { } } final int depth = parser.getDepth(); final int depth = parser.getDepth(); final String tag = parser.getName(); final String tag = parser.getName(); if (depth == outerDepth + 1) { switch (tag) { switch (tag) { case ShortcutPackageInfo.TAG_ROOT: spi = ShortcutPackageInfo.loadFromXml(parser); continue; case TAG_PACKAGE: { case TAG_PACKAGE: { final String packageName = ShortcutService.parseStringAttribute(parser, final String packageName = ShortcutService.parseStringAttribute(parser, ATTR_PACKAGE_NAME); ATTR_PACKAGE_NAME); Loading @@ -181,13 +181,21 @@ class ShortcutLauncher implements ShortcutPackageItem { ret.mPinnedShortcuts.put(packageName, ids); ret.mPinnedShortcuts.put(packageName, ids); continue; continue; } } } } if (depth == outerDepth + 2) { switch (tag) { case TAG_PIN: { case TAG_PIN: { ids.add(ShortcutService.parseStringAttribute(parser, ids.add(ShortcutService.parseStringAttribute(parser, ATTR_VALUE)); ATTR_VALUE)); continue; continue; } } } } throw ShortcutService.throwForInvalidTag(depth, tag); } ShortcutService.warnForInvalidTag(depth, tag); } if (spi != null) { ret.replacePackageInfo(spi); } } return ret; return ret; } } Loading @@ -197,9 +205,12 @@ class ShortcutLauncher implements ShortcutPackageItem { pw.print(prefix); pw.print(prefix); pw.print("Launcher: "); pw.print("Launcher: "); pw.print(mPackageName); pw.print(getPackageName()); pw.print(" UserId: "); pw.print(" Package user: "); pw.print(mLauncherUserId); pw.print(getPackageUserId()); pw.println(); getPackageInfo().dump(s, pw, prefix + " "); pw.println(); pw.println(); final int size = mPinnedShortcuts.size(); final int size = mPinnedShortcuts.size(); Loading services/core/java/com/android/server/pm/ShortcutPackage.java +44 −37 Original line number Original line Diff line number Diff line Loading @@ -17,7 +17,6 @@ package com.android.server.pm; import android.annotation.NonNull; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.Nullable; import android.annotation.UserIdInt; import android.content.ComponentName; import android.content.ComponentName; import android.content.Intent; import android.content.Intent; import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutInfo; Loading @@ -41,7 +40,7 @@ import java.util.function.Predicate; /** /** * Package information used by {@link ShortcutService}. * Package information used by {@link ShortcutService}. */ */ class ShortcutPackage implements ShortcutPackageItem { class ShortcutPackage extends ShortcutPackageItem { private static final String TAG = ShortcutService.TAG; private static final String TAG = ShortcutService.TAG; static final String TAG_ROOT = "package"; static final String TAG_ROOT = "package"; Loading @@ -63,12 +62,6 @@ class ShortcutPackage implements ShortcutPackageItem { private static final String ATTR_ICON_RES = "icon-res"; private static final String ATTR_ICON_RES = "icon-res"; private static final String ATTR_BITMAP_PATH = "bitmap-path"; private static final String ATTR_BITMAP_PATH = "bitmap-path"; @UserIdInt private final int mUserId; @NonNull private final String mPackageName; /** /** * All the shortcuts from the package, keyed on IDs. * All the shortcuts from the package, keyed on IDs. */ */ Loading @@ -89,19 +82,18 @@ class ShortcutPackage implements ShortcutPackageItem { */ */ private long mLastResetTime; private long mLastResetTime; ShortcutPackage(int userId, String packageName) { public ShortcutPackage(int packageUserId, String packageName, ShortcutPackageInfo spi) { mUserId = userId; super(packageUserId, packageName, spi != null ? spi : ShortcutPackageInfo.newEmpty()); mPackageName = packageName; } } @UserIdInt public ShortcutPackage(int packageUserId, String packageName) { public int getUserId() { this(packageUserId, packageName, null); return mUserId; } } @NonNull @Override public String getPackageName() { public int getOwnerUserId() { return mPackageName; // For packages, always owner user == package user. return getPackageUserId(); } } /** /** Loading @@ -116,7 +108,7 @@ class ShortcutPackage implements ShortcutPackageItem { @NonNull String id) { @NonNull String id) { final ShortcutInfo shortcut = mShortcuts.remove(id); final ShortcutInfo shortcut = mShortcuts.remove(id); if (shortcut != null) { if (shortcut != null) { s.removeIcon(mUserId, shortcut); s.removeIcon(getPackageUserId(), shortcut); shortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_PINNED); shortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_PINNED); } } return shortcut; return shortcut; Loading @@ -124,7 +116,7 @@ class ShortcutPackage implements ShortcutPackageItem { void addShortcut(@NonNull ShortcutService s, @NonNull ShortcutInfo newShortcut) { void addShortcut(@NonNull ShortcutService s, @NonNull ShortcutInfo newShortcut) { deleteShortcut(s, newShortcut.getId()); deleteShortcut(s, newShortcut.getId()); s.saveIconAndFixUpShortcut(mUserId, newShortcut); s.saveIconAndFixUpShortcut(getPackageUserId(), newShortcut); mShortcuts.put(newShortcut.getId(), newShortcut); mShortcuts.put(newShortcut.getId(), newShortcut); } } Loading Loading @@ -233,11 +225,12 @@ class ShortcutPackage implements ShortcutPackageItem { // Then, for the pinned set for each launcher, set the pin flag one by one. // Then, for the pinned set for each launcher, set the pin flag one by one. final ArrayMap<ShortcutUser.PackageWithUser, ShortcutLauncher> launchers = final ArrayMap<ShortcutUser.PackageWithUser, ShortcutLauncher> launchers = s.getUserShortcutsLocked(mUserId).getAllLaunchers(); s.getUserShortcutsLocked(getPackageUserId()).getAllLaunchers(); for (int l = launchers.size() - 1; l >= 0; l--) { for (int l = launchers.size() - 1; l >= 0; l--) { final ShortcutLauncher launcherShortcuts = launchers.valueAt(l); final ShortcutLauncher launcherShortcuts = launchers.valueAt(l); final ArraySet<String> pinned = launcherShortcuts.getPinnedShortcutIds(mPackageName); final ArraySet<String> pinned = launcherShortcuts.getPinnedShortcutIds( getPackageName()); if (pinned == null || pinned.size() == 0) { if (pinned == null || pinned.size() == 0) { continue; continue; Loading Loading @@ -321,8 +314,8 @@ class ShortcutPackage implements ShortcutPackageItem { // Set of pinned shortcuts by the calling launcher. // Set of pinned shortcuts by the calling launcher. final ArraySet<String> pinnedByCallerSet = (callingLauncher == null) ? null final ArraySet<String> pinnedByCallerSet = (callingLauncher == null) ? null : s.getLauncherShortcuts(callingLauncher, mUserId, launcherUserId) : s.getLauncherShortcuts(callingLauncher, getPackageUserId(), launcherUserId) .getPinnedShortcutIds(mPackageName); .getPinnedShortcutIds(getPackageName()); for (int i = 0; i < mShortcuts.size(); i++) { for (int i = 0; i < mShortcuts.size(); i++) { final ShortcutInfo si = mShortcuts.valueAt(i); final ShortcutInfo si = mShortcuts.valueAt(i); Loading Loading @@ -362,7 +355,7 @@ class ShortcutPackage implements ShortcutPackageItem { pw.print(prefix); pw.print(prefix); pw.print("Package: "); pw.print("Package: "); pw.print(mPackageName); pw.print(getPackageName()); pw.println(); pw.println(); pw.print(prefix); pw.print(prefix); Loading @@ -380,6 +373,9 @@ class ShortcutPackage implements ShortcutPackageItem { pw.print(s.formatTime(mLastResetTime)); pw.print(s.formatTime(mLastResetTime)); pw.println(); pw.println(); getPackageInfo().dump(s, pw, prefix + " "); pw.println(); pw.println(" Shortcuts:"); pw.println(" Shortcuts:"); long totalBitmapSize = 0; long totalBitmapSize = 0; final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts; final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts; Loading @@ -406,6 +402,7 @@ class ShortcutPackage implements ShortcutPackageItem { pw.println(")"); pw.println(")"); } } @Override public void saveToXml(@NonNull XmlSerializer out, boolean forBackup) public void saveToXml(@NonNull XmlSerializer out, boolean forBackup) throws IOException, XmlPullParserException { throws IOException, XmlPullParserException { final int size = mShortcuts.size(); final int size = mShortcuts.size(); Loading @@ -416,10 +413,11 @@ class ShortcutPackage implements ShortcutPackageItem { out.startTag(null, TAG_ROOT); out.startTag(null, TAG_ROOT); ShortcutService.writeAttr(out, ATTR_NAME, mPackageName); ShortcutService.writeAttr(out, ATTR_NAME, getPackageName()); ShortcutService.writeAttr(out, ATTR_DYNAMIC_COUNT, mDynamicShortcutCount); ShortcutService.writeAttr(out, ATTR_DYNAMIC_COUNT, mDynamicShortcutCount); ShortcutService.writeAttr(out, ATTR_CALL_COUNT, mApiCallCount); ShortcutService.writeAttr(out, ATTR_CALL_COUNT, mApiCallCount); ShortcutService.writeAttr(out, ATTR_LAST_RESET, mLastResetTime); ShortcutService.writeAttr(out, ATTR_LAST_RESET, mLastResetTime); getPackageInfo().saveToXml(out); for (int j = 0; j < size; j++) { for (int j = 0; j < size; j++) { saveShortcut(out, mShortcuts.valueAt(j), forBackup); saveShortcut(out, mShortcuts.valueAt(j), forBackup); Loading Loading @@ -464,13 +462,14 @@ class ShortcutPackage implements ShortcutPackageItem { out.endTag(null, TAG_SHORTCUT); out.endTag(null, TAG_SHORTCUT); } } public static ShortcutPackage loadFromXml(XmlPullParser parser, int userId) public static ShortcutPackage loadFromXml(ShortcutService s, XmlPullParser parser, int ownerUserId, boolean fromBackup) throws IOException, XmlPullParserException { throws IOException, XmlPullParserException { final String packageName = ShortcutService.parseStringAttribute(parser, final String packageName = ShortcutService.parseStringAttribute(parser, ATTR_NAME); ATTR_NAME); final ShortcutPackage ret = new ShortcutPackage(userId, packageName); final ShortcutPackage ret = new ShortcutPackage(ownerUserId, packageName); ret.mDynamicShortcutCount = ret.mDynamicShortcutCount = ShortcutService.parseIntAttribute(parser, ATTR_DYNAMIC_COUNT); ShortcutService.parseIntAttribute(parser, ATTR_DYNAMIC_COUNT); Loading @@ -478,6 +477,7 @@ class ShortcutPackage implements ShortcutPackageItem { ShortcutService.parseIntAttribute(parser, ATTR_CALL_COUNT); ShortcutService.parseIntAttribute(parser, ATTR_CALL_COUNT); ret.mLastResetTime = ret.mLastResetTime = ShortcutService.parseLongAttribute(parser, ATTR_LAST_RESET); ShortcutService.parseLongAttribute(parser, ATTR_LAST_RESET); ShortcutPackageInfo spi = null; final int outerDepth = parser.getDepth(); final int outerDepth = parser.getDepth(); int type; int type; Loading @@ -488,7 +488,11 @@ class ShortcutPackage implements ShortcutPackageItem { } } final int depth = parser.getDepth(); final int depth = parser.getDepth(); final String tag = parser.getName(); final String tag = parser.getName(); if (depth == outerDepth + 1) { switch (tag) { switch (tag) { case ShortcutPackageInfo.TAG_ROOT: spi = ShortcutPackageInfo.loadFromXml(parser); continue; case TAG_SHORTCUT: case TAG_SHORTCUT: final ShortcutInfo si = parseShortcut(parser, packageName); final ShortcutInfo si = parseShortcut(parser, packageName); Loading @@ -496,7 +500,11 @@ class ShortcutPackage implements ShortcutPackageItem { ret.mShortcuts.put(si.getId(), si); ret.mShortcuts.put(si.getId(), si); continue; continue; } } throw ShortcutService.throwForInvalidTag(depth, tag); } ShortcutService.warnForInvalidTag(depth, tag); } if (spi != null) { ret.replacePackageInfo(spi); } } return ret; return ret; } } Loading @@ -522,8 +530,7 @@ class ShortcutPackage implements ShortcutPackageItem { title = ShortcutService.parseStringAttribute(parser, ATTR_TITLE); title = ShortcutService.parseStringAttribute(parser, ATTR_TITLE); intent = ShortcutService.parseIntentAttribute(parser, ATTR_INTENT); intent = ShortcutService.parseIntentAttribute(parser, ATTR_INTENT); weight = (int) ShortcutService.parseLongAttribute(parser, ATTR_WEIGHT); weight = (int) ShortcutService.parseLongAttribute(parser, ATTR_WEIGHT); lastChangedTimestamp = (int) ShortcutService.parseLongAttribute(parser, lastChangedTimestamp = ShortcutService.parseLongAttribute(parser, ATTR_TIMESTAMP); ATTR_TIMESTAMP); flags = (int) ShortcutService.parseLongAttribute(parser, ATTR_FLAGS); flags = (int) ShortcutService.parseLongAttribute(parser, ATTR_FLAGS); iconRes = (int) ShortcutService.parseLongAttribute(parser, ATTR_ICON_RES); iconRes = (int) ShortcutService.parseLongAttribute(parser, ATTR_ICON_RES); bitmapPath = ShortcutService.parseStringAttribute(parser, ATTR_BITMAP_PATH); bitmapPath = ShortcutService.parseStringAttribute(parser, ATTR_BITMAP_PATH); Loading services/core/java/com/android/server/pm/ShortcutPackageInfo.java +30 −140 Original line number Original line Diff line number Diff line Loading @@ -15,14 +15,11 @@ */ */ package com.android.server.pm; package com.android.server.pm; import android.annotation.NonNull; import android.annotation.UserIdInt; import android.annotation.UserIdInt; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageInfo; import android.content.pm.Signature; import android.util.Slog; import android.util.Slog; import com.android.internal.util.Preconditions; import com.android.server.backup.BackupUtils; import libcore.io.Base64; import libcore.io.Base64; import libcore.util.HexEncoding; import libcore.util.HexEncoding; Loading @@ -33,32 +30,21 @@ import org.xmlpull.v1.XmlSerializer; import java.io.IOException; import java.io.IOException; import java.io.PrintWriter; import java.io.PrintWriter; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.ArrayList; import java.util.Arrays; /** /** * Package information used by {@link android.content.pm.ShortcutManager} for backup / restore. * Package information used by {@link android.content.pm.ShortcutManager} for backup / restore. * * TODO: The methods about signature hashes are copied from BackupManagerService, which is not * visible here. Unify the code. */ */ class ShortcutPackageInfo implements ShortcutPackageItem { class ShortcutPackageInfo { private static final String TAG = ShortcutService.TAG; private static final String TAG = ShortcutService.TAG; static final String TAG_ROOT = "package-info"; static final String TAG_ROOT = "package-info"; private static final String ATTR_USER_ID = "user"; private static final String ATTR_NAME = "name"; private static final String ATTR_VERSION = "version"; private static final String ATTR_VERSION = "version"; private static final String ATTR_SHADOW = "shadow"; private static final String ATTR_SHADOW = "shadow"; private static final String TAG_SIGNATURE = "signature"; private static final String TAG_SIGNATURE = "signature"; private static final String ATTR_SIGNATURE_HASH = "hash"; private static final String ATTR_SIGNATURE_HASH = "hash"; private final String mPackageName; private final int mUserId; /** /** * When true, this package information was restored from the previous device, and the app hasn't * When true, this package information was restored from the previous device, and the app hasn't * been installed yet. * been installed yet. Loading @@ -67,22 +53,14 @@ class ShortcutPackageInfo implements ShortcutPackageItem { private int mVersionCode; private int mVersionCode; private ArrayList<byte[]> mSigHashes; private ArrayList<byte[]> mSigHashes; private ShortcutPackageInfo(String packageName, int userId, private ShortcutPackageInfo(int versionCode, ArrayList<byte[]> sigHashes, boolean isShadow) { int versionCode, ArrayList<byte[]> sigHashes, boolean isShadow) { mPackageName = Preconditions.checkNotNull(packageName); mUserId = userId; mVersionCode = versionCode; mVersionCode = versionCode; mIsShadow = isShadow; mIsShadow = isShadow; mSigHashes = sigHashes; mSigHashes = sigHashes; } } @NonNull public static ShortcutPackageInfo newEmpty() { public String getPackageName() { return new ShortcutPackageInfo(0, new ArrayList<>(0), /* isShadow */ false); return mPackageName; } public int getUserId() { return mUserId; } } public boolean isShadow() { public boolean isShadow() { Loading @@ -101,92 +79,13 @@ class ShortcutPackageInfo implements ShortcutPackageItem { return mVersionCode; return mVersionCode; } } private static byte[] hashSignature(Signature sig) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.update(sig.toByteArray()); return digest.digest(); } catch (NoSuchAlgorithmException e) { Slog.w(TAG, "No SHA-256 algorithm found!"); } return null; } private static ArrayList<byte[]> hashSignatureArray(Signature[] sigs) { if (sigs == null) { return null; } ArrayList<byte[]> hashes = new ArrayList<byte[]>(sigs.length); for (Signature s : sigs) { hashes.add(hashSignature(s)); } return hashes; } private static boolean signaturesMatch(ArrayList<byte[]> storedSigHashes, PackageInfo target) { if (target == null) { return false; } // If the target resides on the system partition, we allow it to restore // data from the like-named package in a restore set even if the signatures // do not match. (Unlike general applications, those flashed to the system // partition will be signed with the device's platform certificate, so on // different phones the same system app will have different signatures.) if ((target.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { return true; } // Allow unsigned apps, but not signed on one device and unsigned on the other // !!! TODO: is this the right policy? Signature[] deviceSigs = target.signatures; if ((storedSigHashes == null || storedSigHashes.size() == 0) && (deviceSigs == null || deviceSigs.length == 0)) { return true; } if (storedSigHashes == null || deviceSigs == null) { return false; } // !!! TODO: this demands that every stored signature match one // that is present on device, and does not demand the converse. // Is this this right policy? final int nStored = storedSigHashes.size(); final int nDevice = deviceSigs.length; // hash each on-device signature ArrayList<byte[]> deviceHashes = new ArrayList<byte[]>(nDevice); for (int i = 0; i < nDevice; i++) { deviceHashes.add(hashSignature(deviceSigs[i])); } // now ensure that each stored sig (hash) matches an on-device sig (hash) for (int n = 0; n < nStored; n++) { boolean match = false; final byte[] storedHash = storedSigHashes.get(n); for (int i = 0; i < nDevice; i++) { if (Arrays.equals(storedHash, deviceHashes.get(i))) { match = true; break; } } // match is false when no on-device sig matched one of the stored ones if (!match) { return false; } } return true; } public boolean canRestoreTo(PackageInfo target) { public boolean canRestoreTo(PackageInfo target) { if (target.versionCode < mVersionCode) { if (target.versionCode < mVersionCode) { Slog.w(TAG, String.format("Package current version %d < backed up version %d", Slog.w(TAG, String.format("Package current version %d < backed up version %d", target.versionCode, mVersionCode)); target.versionCode, mVersionCode)); return false; return false; } } if (!signaturesMatch(mSigHashes, target)) { if (!BackupUtils.signaturesMatch(mSigHashes, target)) { Slog.w(TAG, "Package signature mismtach"); Slog.w(TAG, "Package signature mismtach"); return false; return false; } } Loading @@ -194,37 +93,34 @@ class ShortcutPackageInfo implements ShortcutPackageItem { } } public static ShortcutPackageInfo generateForInstalledPackage( public static ShortcutPackageInfo generateForInstalledPackage( ShortcutService s, String packageName, @UserIdInt int userId) { ShortcutService s, String packageName, @UserIdInt int packageUserId) { final PackageInfo pi = s.getPackageInfoWithSignatures(packageName, userId); final PackageInfo pi = s.getPackageInfoWithSignatures(packageName, packageUserId); if (pi.signatures == null || pi.signatures.length == 0) { if (pi.signatures == null || pi.signatures.length == 0) { Slog.e(TAG, "Can't get signatures: package=" + packageName); Slog.e(TAG, "Can't get signatures: package=" + packageName); return null; return null; } } final ShortcutPackageInfo ret = new ShortcutPackageInfo(packageName, userId, pi.versionCode, final ShortcutPackageInfo ret = new ShortcutPackageInfo(pi.versionCode, hashSignatureArray(pi.signatures), /* shadow=*/ false); BackupUtils.hashSignatureArray(pi.signatures), /* shadow=*/ false); return ret; return ret; } } public void refreshAndSave(ShortcutService s, @UserIdInt int userId) { public void refresh(ShortcutService s, ShortcutPackageItem pkg) { final PackageInfo pi = s.getPackageInfoWithSignatures(mPackageName, userId); // Note use mUserId here, rather than userId. final PackageInfo pi = s.getPackageInfoWithSignatures( pkg.getPackageName(), pkg.getPackageUserId()); if (pi == null) { if (pi == null) { Slog.w(TAG, "Package not found: " + mPackageName); Slog.w(TAG, "Package not found: " + pkg.getPackageName()); return; return; } } mVersionCode = pi.versionCode; mVersionCode = pi.versionCode; mSigHashes = hashSignatureArray(pi.signatures); mSigHashes = BackupUtils.hashSignatureArray(pi.signatures); s.scheduleSaveUser(userId); } } public void saveToXml(XmlSerializer out, boolean forBackup) public void saveToXml(XmlSerializer out) throws IOException { throws IOException, XmlPullParserException { out.startTag(null, TAG_ROOT); out.startTag(null, TAG_ROOT); ShortcutService.writeAttr(out, ATTR_NAME, mPackageName); ShortcutService.writeAttr(out, ATTR_USER_ID, mUserId); ShortcutService.writeAttr(out, ATTR_VERSION, mVersionCode); ShortcutService.writeAttr(out, ATTR_VERSION, mVersionCode); ShortcutService.writeAttr(out, ATTR_SHADOW, mIsShadow); ShortcutService.writeAttr(out, ATTR_SHADOW, mIsShadow); Loading @@ -236,11 +132,9 @@ class ShortcutPackageInfo implements ShortcutPackageItem { out.endTag(null, TAG_ROOT); out.endTag(null, TAG_ROOT); } } public static ShortcutPackageInfo loadFromXml(XmlPullParser parser, int ownerUserId) public static ShortcutPackageInfo loadFromXml(XmlPullParser parser) throws IOException, XmlPullParserException { throws IOException, XmlPullParserException { final String packageName = ShortcutService.parseStringAttribute(parser, ATTR_NAME); final int userId = ShortcutService.parseIntAttribute(parser, ATTR_USER_ID, ownerUserId); final int versionCode = ShortcutService.parseIntAttribute(parser, ATTR_VERSION); final int versionCode = ShortcutService.parseIntAttribute(parser, ATTR_VERSION); final boolean shadow = ShortcutService.parseBooleanAttribute(parser, ATTR_SHADOW); final boolean shadow = ShortcutService.parseBooleanAttribute(parser, ATTR_SHADOW); Loading @@ -256,6 +150,8 @@ class ShortcutPackageInfo implements ShortcutPackageItem { } } final int depth = parser.getDepth(); final int depth = parser.getDepth(); final String tag = parser.getName(); final String tag = parser.getName(); if (depth == outerDepth + 1) { switch (tag) { switch (tag) { case TAG_SIGNATURE: { case TAG_SIGNATURE: { final String hash = ShortcutService.parseStringAttribute( final String hash = ShortcutService.parseStringAttribute( Loading @@ -264,23 +160,17 @@ class ShortcutPackageInfo implements ShortcutPackageItem { continue; continue; } } } } throw ShortcutService.throwForInvalidTag(depth, tag); } } return new ShortcutPackageInfo(packageName, userId, versionCode, hashes, shadow); ShortcutService.warnForInvalidTag(depth, tag); } return new ShortcutPackageInfo(versionCode, hashes, shadow); } } public void dump(ShortcutService s, PrintWriter pw, String prefix) { public void dump(ShortcutService s, PrintWriter pw, String prefix) { pw.println(); pw.println(); pw.print(prefix); pw.print(prefix); pw.print("PackageInfo: "); pw.println("PackageInfo:"); pw.print(mPackageName); pw.println(); pw.print(prefix); pw.print(" User: "); pw.print(mUserId); pw.println(); pw.print(prefix); pw.print(prefix); pw.print(" IsShadow: "); pw.print(" IsShadow: "); Loading services/core/java/com/android/server/pm/ShortcutPackageItem.java +57 −3 File changed.Preview size limit exceeded, changes collapsed. Show changes Loading
core/java/android/content/pm/IShortcutService.aidl +4 −0 Original line number Original line Diff line number Diff line Loading @@ -47,4 +47,8 @@ interface IShortcutService { int getIconMaxDimensions(String packageName, int userId); int getIconMaxDimensions(String packageName, int userId); void resetThrottling(); // system only API for developer opsions void resetThrottling(); // system only API for developer opsions byte[] getBackupPayload(int user); void applyRestore(in byte[] payload, int user); } } No newline at end of file
services/core/java/com/android/server/pm/ShortcutLauncher.java +62 −51 Original line number Original line Diff line number Diff line Loading @@ -32,7 +32,7 @@ import java.util.List; /** /** * Launcher information used by {@link ShortcutService}. * Launcher information used by {@link ShortcutService}. */ */ class ShortcutLauncher implements ShortcutPackageItem { class ShortcutLauncher extends ShortcutPackageItem { private static final String TAG = ShortcutService.TAG; private static final String TAG = ShortcutService.TAG; static final String TAG_ROOT = "launcher-pins"; static final String TAG_ROOT = "launcher-pins"; Loading @@ -44,44 +44,34 @@ class ShortcutLauncher implements ShortcutPackageItem { private static final String ATTR_VALUE = "value"; private static final String ATTR_VALUE = "value"; private static final String ATTR_PACKAGE_NAME = "package-name"; private static final String ATTR_PACKAGE_NAME = "package-name"; @UserIdInt private final int mOwnerUserId; private final int mUserId; @NonNull private final String mPackageName; @UserIdInt private final int mLauncherUserId; /** /** * Package name -> IDs. * Package name -> IDs. */ */ final private ArrayMap<String, ArraySet<String>> mPinnedShortcuts = new ArrayMap<>(); final private ArrayMap<String, ArraySet<String>> mPinnedShortcuts = new ArrayMap<>(); ShortcutLauncher(@UserIdInt int userId, @NonNull String packageName, public ShortcutLauncher(@UserIdInt int ownerUserId, @NonNull String packageName, @UserIdInt int launcherUserId) { @UserIdInt int launcherUserId, ShortcutPackageInfo spi) { mUserId = userId; super(launcherUserId, packageName, spi != null ? spi : ShortcutPackageInfo.newEmpty()); mPackageName = packageName; mOwnerUserId = ownerUserId; mLauncherUserId = launcherUserId; } } @UserIdInt public ShortcutLauncher(@UserIdInt int ownerUserId, @NonNull String packageName, public int getUserId() { @UserIdInt int launcherUserId) { return mUserId; this(launcherUserId, packageName, launcherUserId, null); } } @UserIdInt @Override public int getLauncherUserId() { public int getOwnerUserId() { return mLauncherUserId; return mOwnerUserId; } } @NonNull public void pinShortcuts(@NonNull ShortcutService s, @UserIdInt int packageUserId, public String getPackageName() { @NonNull String packageName, @NonNull List<String> ids) { return mPackageName; final ShortcutPackage packageShortcuts = } s.getPackageShortcutsLocked(packageName, packageUserId); public void pinShortcuts(@NonNull ShortcutService s, @NonNull String packageName, @NonNull List<String> ids) { final int idSize = ids.size(); final int idSize = ids.size(); if (idSize == 0) { if (idSize == 0) { mPinnedShortcuts.remove(packageName); mPinnedShortcuts.remove(packageName); Loading @@ -91,8 +81,6 @@ class ShortcutLauncher implements ShortcutPackageItem { // Pin shortcuts. Make sure only pin the ones that were visible to the caller. // Pin shortcuts. Make sure only pin the ones that were visible to the caller. // i.e. a non-dynamic, pinned shortcut by *other launchers* shouldn't be pinned here. // i.e. a non-dynamic, pinned shortcut by *other launchers* shouldn't be pinned here. final ShortcutPackage packageShortcuts = s.getPackageShortcutsLocked(packageName, mUserId); final ArraySet<String> newSet = new ArraySet<>(); final ArraySet<String> newSet = new ArraySet<>(); for (int i = 0; i < idSize; i++) { for (int i = 0; i < idSize; i++) { Loading @@ -107,7 +95,7 @@ class ShortcutLauncher implements ShortcutPackageItem { } } mPinnedShortcuts.put(packageName, newSet); mPinnedShortcuts.put(packageName, newSet); } } s.getPackageShortcutsLocked(packageName, mUserId).refreshPinnedFlags(s); packageShortcuts.refreshPinnedFlags(s); } } /** /** Loading @@ -124,15 +112,18 @@ class ShortcutLauncher implements ShortcutPackageItem { /** /** * Persist. * Persist. */ */ public void saveToXml(XmlSerializer out, boolean forBackup) throws IOException { @Override public void saveToXml(XmlSerializer out, boolean forBackup) throws IOException { final int size = mPinnedShortcuts.size(); final int size = mPinnedShortcuts.size(); if (size == 0) { if (size == 0) { return; // Nothing to write. return; // Nothing to write. } } out.startTag(null, TAG_ROOT); out.startTag(null, TAG_ROOT); ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, mPackageName); ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, getPackageName()); ShortcutService.writeAttr(out, ATTR_LAUNCHER_USER_ID, mLauncherUserId); ShortcutService.writeAttr(out, ATTR_LAUNCHER_USER_ID, getPackageUserId()); getPackageInfo().saveToXml(out); for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) { out.startTag(null, TAG_PACKAGE); out.startTag(null, TAG_PACKAGE); Loading @@ -153,16 +144,21 @@ class ShortcutLauncher implements ShortcutPackageItem { /** /** * Load. * Load. */ */ public static ShortcutLauncher loadFromXml(XmlPullParser parser, int ownerUserId) public static ShortcutLauncher loadFromXml(XmlPullParser parser, int ownerUserId, throws IOException, XmlPullParserException { boolean fromBackup) throws IOException, XmlPullParserException { final String launcherPackageName = ShortcutService.parseStringAttribute(parser, final String launcherPackageName = ShortcutService.parseStringAttribute(parser, ATTR_PACKAGE_NAME); ATTR_PACKAGE_NAME); final int launcherUserId = ShortcutService.parseIntAttribute(parser, ATTR_LAUNCHER_USER_ID, ownerUserId); // If restoring, just use the real user ID. final int launcherUserId = fromBackup ? ownerUserId : ShortcutService.parseIntAttribute(parser, ATTR_LAUNCHER_USER_ID, ownerUserId); final ShortcutLauncher ret = new ShortcutLauncher(launcherUserId, launcherPackageName, final ShortcutLauncher ret = new ShortcutLauncher(launcherUserId, launcherPackageName, launcherUserId); launcherUserId); ShortcutPackageInfo spi = null; ArraySet<String> ids = null; ArraySet<String> ids = null; final int outerDepth = parser.getDepth(); final int outerDepth = parser.getDepth(); int type; int type; Loading @@ -173,7 +169,11 @@ class ShortcutLauncher implements ShortcutPackageItem { } } final int depth = parser.getDepth(); final int depth = parser.getDepth(); final String tag = parser.getName(); final String tag = parser.getName(); if (depth == outerDepth + 1) { switch (tag) { switch (tag) { case ShortcutPackageInfo.TAG_ROOT: spi = ShortcutPackageInfo.loadFromXml(parser); continue; case TAG_PACKAGE: { case TAG_PACKAGE: { final String packageName = ShortcutService.parseStringAttribute(parser, final String packageName = ShortcutService.parseStringAttribute(parser, ATTR_PACKAGE_NAME); ATTR_PACKAGE_NAME); Loading @@ -181,13 +181,21 @@ class ShortcutLauncher implements ShortcutPackageItem { ret.mPinnedShortcuts.put(packageName, ids); ret.mPinnedShortcuts.put(packageName, ids); continue; continue; } } } } if (depth == outerDepth + 2) { switch (tag) { case TAG_PIN: { case TAG_PIN: { ids.add(ShortcutService.parseStringAttribute(parser, ids.add(ShortcutService.parseStringAttribute(parser, ATTR_VALUE)); ATTR_VALUE)); continue; continue; } } } } throw ShortcutService.throwForInvalidTag(depth, tag); } ShortcutService.warnForInvalidTag(depth, tag); } if (spi != null) { ret.replacePackageInfo(spi); } } return ret; return ret; } } Loading @@ -197,9 +205,12 @@ class ShortcutLauncher implements ShortcutPackageItem { pw.print(prefix); pw.print(prefix); pw.print("Launcher: "); pw.print("Launcher: "); pw.print(mPackageName); pw.print(getPackageName()); pw.print(" UserId: "); pw.print(" Package user: "); pw.print(mLauncherUserId); pw.print(getPackageUserId()); pw.println(); getPackageInfo().dump(s, pw, prefix + " "); pw.println(); pw.println(); final int size = mPinnedShortcuts.size(); final int size = mPinnedShortcuts.size(); Loading
services/core/java/com/android/server/pm/ShortcutPackage.java +44 −37 Original line number Original line Diff line number Diff line Loading @@ -17,7 +17,6 @@ package com.android.server.pm; import android.annotation.NonNull; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.Nullable; import android.annotation.UserIdInt; import android.content.ComponentName; import android.content.ComponentName; import android.content.Intent; import android.content.Intent; import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutInfo; Loading @@ -41,7 +40,7 @@ import java.util.function.Predicate; /** /** * Package information used by {@link ShortcutService}. * Package information used by {@link ShortcutService}. */ */ class ShortcutPackage implements ShortcutPackageItem { class ShortcutPackage extends ShortcutPackageItem { private static final String TAG = ShortcutService.TAG; private static final String TAG = ShortcutService.TAG; static final String TAG_ROOT = "package"; static final String TAG_ROOT = "package"; Loading @@ -63,12 +62,6 @@ class ShortcutPackage implements ShortcutPackageItem { private static final String ATTR_ICON_RES = "icon-res"; private static final String ATTR_ICON_RES = "icon-res"; private static final String ATTR_BITMAP_PATH = "bitmap-path"; private static final String ATTR_BITMAP_PATH = "bitmap-path"; @UserIdInt private final int mUserId; @NonNull private final String mPackageName; /** /** * All the shortcuts from the package, keyed on IDs. * All the shortcuts from the package, keyed on IDs. */ */ Loading @@ -89,19 +82,18 @@ class ShortcutPackage implements ShortcutPackageItem { */ */ private long mLastResetTime; private long mLastResetTime; ShortcutPackage(int userId, String packageName) { public ShortcutPackage(int packageUserId, String packageName, ShortcutPackageInfo spi) { mUserId = userId; super(packageUserId, packageName, spi != null ? spi : ShortcutPackageInfo.newEmpty()); mPackageName = packageName; } } @UserIdInt public ShortcutPackage(int packageUserId, String packageName) { public int getUserId() { this(packageUserId, packageName, null); return mUserId; } } @NonNull @Override public String getPackageName() { public int getOwnerUserId() { return mPackageName; // For packages, always owner user == package user. return getPackageUserId(); } } /** /** Loading @@ -116,7 +108,7 @@ class ShortcutPackage implements ShortcutPackageItem { @NonNull String id) { @NonNull String id) { final ShortcutInfo shortcut = mShortcuts.remove(id); final ShortcutInfo shortcut = mShortcuts.remove(id); if (shortcut != null) { if (shortcut != null) { s.removeIcon(mUserId, shortcut); s.removeIcon(getPackageUserId(), shortcut); shortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_PINNED); shortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_PINNED); } } return shortcut; return shortcut; Loading @@ -124,7 +116,7 @@ class ShortcutPackage implements ShortcutPackageItem { void addShortcut(@NonNull ShortcutService s, @NonNull ShortcutInfo newShortcut) { void addShortcut(@NonNull ShortcutService s, @NonNull ShortcutInfo newShortcut) { deleteShortcut(s, newShortcut.getId()); deleteShortcut(s, newShortcut.getId()); s.saveIconAndFixUpShortcut(mUserId, newShortcut); s.saveIconAndFixUpShortcut(getPackageUserId(), newShortcut); mShortcuts.put(newShortcut.getId(), newShortcut); mShortcuts.put(newShortcut.getId(), newShortcut); } } Loading Loading @@ -233,11 +225,12 @@ class ShortcutPackage implements ShortcutPackageItem { // Then, for the pinned set for each launcher, set the pin flag one by one. // Then, for the pinned set for each launcher, set the pin flag one by one. final ArrayMap<ShortcutUser.PackageWithUser, ShortcutLauncher> launchers = final ArrayMap<ShortcutUser.PackageWithUser, ShortcutLauncher> launchers = s.getUserShortcutsLocked(mUserId).getAllLaunchers(); s.getUserShortcutsLocked(getPackageUserId()).getAllLaunchers(); for (int l = launchers.size() - 1; l >= 0; l--) { for (int l = launchers.size() - 1; l >= 0; l--) { final ShortcutLauncher launcherShortcuts = launchers.valueAt(l); final ShortcutLauncher launcherShortcuts = launchers.valueAt(l); final ArraySet<String> pinned = launcherShortcuts.getPinnedShortcutIds(mPackageName); final ArraySet<String> pinned = launcherShortcuts.getPinnedShortcutIds( getPackageName()); if (pinned == null || pinned.size() == 0) { if (pinned == null || pinned.size() == 0) { continue; continue; Loading Loading @@ -321,8 +314,8 @@ class ShortcutPackage implements ShortcutPackageItem { // Set of pinned shortcuts by the calling launcher. // Set of pinned shortcuts by the calling launcher. final ArraySet<String> pinnedByCallerSet = (callingLauncher == null) ? null final ArraySet<String> pinnedByCallerSet = (callingLauncher == null) ? null : s.getLauncherShortcuts(callingLauncher, mUserId, launcherUserId) : s.getLauncherShortcuts(callingLauncher, getPackageUserId(), launcherUserId) .getPinnedShortcutIds(mPackageName); .getPinnedShortcutIds(getPackageName()); for (int i = 0; i < mShortcuts.size(); i++) { for (int i = 0; i < mShortcuts.size(); i++) { final ShortcutInfo si = mShortcuts.valueAt(i); final ShortcutInfo si = mShortcuts.valueAt(i); Loading Loading @@ -362,7 +355,7 @@ class ShortcutPackage implements ShortcutPackageItem { pw.print(prefix); pw.print(prefix); pw.print("Package: "); pw.print("Package: "); pw.print(mPackageName); pw.print(getPackageName()); pw.println(); pw.println(); pw.print(prefix); pw.print(prefix); Loading @@ -380,6 +373,9 @@ class ShortcutPackage implements ShortcutPackageItem { pw.print(s.formatTime(mLastResetTime)); pw.print(s.formatTime(mLastResetTime)); pw.println(); pw.println(); getPackageInfo().dump(s, pw, prefix + " "); pw.println(); pw.println(" Shortcuts:"); pw.println(" Shortcuts:"); long totalBitmapSize = 0; long totalBitmapSize = 0; final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts; final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts; Loading @@ -406,6 +402,7 @@ class ShortcutPackage implements ShortcutPackageItem { pw.println(")"); pw.println(")"); } } @Override public void saveToXml(@NonNull XmlSerializer out, boolean forBackup) public void saveToXml(@NonNull XmlSerializer out, boolean forBackup) throws IOException, XmlPullParserException { throws IOException, XmlPullParserException { final int size = mShortcuts.size(); final int size = mShortcuts.size(); Loading @@ -416,10 +413,11 @@ class ShortcutPackage implements ShortcutPackageItem { out.startTag(null, TAG_ROOT); out.startTag(null, TAG_ROOT); ShortcutService.writeAttr(out, ATTR_NAME, mPackageName); ShortcutService.writeAttr(out, ATTR_NAME, getPackageName()); ShortcutService.writeAttr(out, ATTR_DYNAMIC_COUNT, mDynamicShortcutCount); ShortcutService.writeAttr(out, ATTR_DYNAMIC_COUNT, mDynamicShortcutCount); ShortcutService.writeAttr(out, ATTR_CALL_COUNT, mApiCallCount); ShortcutService.writeAttr(out, ATTR_CALL_COUNT, mApiCallCount); ShortcutService.writeAttr(out, ATTR_LAST_RESET, mLastResetTime); ShortcutService.writeAttr(out, ATTR_LAST_RESET, mLastResetTime); getPackageInfo().saveToXml(out); for (int j = 0; j < size; j++) { for (int j = 0; j < size; j++) { saveShortcut(out, mShortcuts.valueAt(j), forBackup); saveShortcut(out, mShortcuts.valueAt(j), forBackup); Loading Loading @@ -464,13 +462,14 @@ class ShortcutPackage implements ShortcutPackageItem { out.endTag(null, TAG_SHORTCUT); out.endTag(null, TAG_SHORTCUT); } } public static ShortcutPackage loadFromXml(XmlPullParser parser, int userId) public static ShortcutPackage loadFromXml(ShortcutService s, XmlPullParser parser, int ownerUserId, boolean fromBackup) throws IOException, XmlPullParserException { throws IOException, XmlPullParserException { final String packageName = ShortcutService.parseStringAttribute(parser, final String packageName = ShortcutService.parseStringAttribute(parser, ATTR_NAME); ATTR_NAME); final ShortcutPackage ret = new ShortcutPackage(userId, packageName); final ShortcutPackage ret = new ShortcutPackage(ownerUserId, packageName); ret.mDynamicShortcutCount = ret.mDynamicShortcutCount = ShortcutService.parseIntAttribute(parser, ATTR_DYNAMIC_COUNT); ShortcutService.parseIntAttribute(parser, ATTR_DYNAMIC_COUNT); Loading @@ -478,6 +477,7 @@ class ShortcutPackage implements ShortcutPackageItem { ShortcutService.parseIntAttribute(parser, ATTR_CALL_COUNT); ShortcutService.parseIntAttribute(parser, ATTR_CALL_COUNT); ret.mLastResetTime = ret.mLastResetTime = ShortcutService.parseLongAttribute(parser, ATTR_LAST_RESET); ShortcutService.parseLongAttribute(parser, ATTR_LAST_RESET); ShortcutPackageInfo spi = null; final int outerDepth = parser.getDepth(); final int outerDepth = parser.getDepth(); int type; int type; Loading @@ -488,7 +488,11 @@ class ShortcutPackage implements ShortcutPackageItem { } } final int depth = parser.getDepth(); final int depth = parser.getDepth(); final String tag = parser.getName(); final String tag = parser.getName(); if (depth == outerDepth + 1) { switch (tag) { switch (tag) { case ShortcutPackageInfo.TAG_ROOT: spi = ShortcutPackageInfo.loadFromXml(parser); continue; case TAG_SHORTCUT: case TAG_SHORTCUT: final ShortcutInfo si = parseShortcut(parser, packageName); final ShortcutInfo si = parseShortcut(parser, packageName); Loading @@ -496,7 +500,11 @@ class ShortcutPackage implements ShortcutPackageItem { ret.mShortcuts.put(si.getId(), si); ret.mShortcuts.put(si.getId(), si); continue; continue; } } throw ShortcutService.throwForInvalidTag(depth, tag); } ShortcutService.warnForInvalidTag(depth, tag); } if (spi != null) { ret.replacePackageInfo(spi); } } return ret; return ret; } } Loading @@ -522,8 +530,7 @@ class ShortcutPackage implements ShortcutPackageItem { title = ShortcutService.parseStringAttribute(parser, ATTR_TITLE); title = ShortcutService.parseStringAttribute(parser, ATTR_TITLE); intent = ShortcutService.parseIntentAttribute(parser, ATTR_INTENT); intent = ShortcutService.parseIntentAttribute(parser, ATTR_INTENT); weight = (int) ShortcutService.parseLongAttribute(parser, ATTR_WEIGHT); weight = (int) ShortcutService.parseLongAttribute(parser, ATTR_WEIGHT); lastChangedTimestamp = (int) ShortcutService.parseLongAttribute(parser, lastChangedTimestamp = ShortcutService.parseLongAttribute(parser, ATTR_TIMESTAMP); ATTR_TIMESTAMP); flags = (int) ShortcutService.parseLongAttribute(parser, ATTR_FLAGS); flags = (int) ShortcutService.parseLongAttribute(parser, ATTR_FLAGS); iconRes = (int) ShortcutService.parseLongAttribute(parser, ATTR_ICON_RES); iconRes = (int) ShortcutService.parseLongAttribute(parser, ATTR_ICON_RES); bitmapPath = ShortcutService.parseStringAttribute(parser, ATTR_BITMAP_PATH); bitmapPath = ShortcutService.parseStringAttribute(parser, ATTR_BITMAP_PATH); Loading
services/core/java/com/android/server/pm/ShortcutPackageInfo.java +30 −140 Original line number Original line Diff line number Diff line Loading @@ -15,14 +15,11 @@ */ */ package com.android.server.pm; package com.android.server.pm; import android.annotation.NonNull; import android.annotation.UserIdInt; import android.annotation.UserIdInt; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageInfo; import android.content.pm.Signature; import android.util.Slog; import android.util.Slog; import com.android.internal.util.Preconditions; import com.android.server.backup.BackupUtils; import libcore.io.Base64; import libcore.io.Base64; import libcore.util.HexEncoding; import libcore.util.HexEncoding; Loading @@ -33,32 +30,21 @@ import org.xmlpull.v1.XmlSerializer; import java.io.IOException; import java.io.IOException; import java.io.PrintWriter; import java.io.PrintWriter; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.ArrayList; import java.util.Arrays; /** /** * Package information used by {@link android.content.pm.ShortcutManager} for backup / restore. * Package information used by {@link android.content.pm.ShortcutManager} for backup / restore. * * TODO: The methods about signature hashes are copied from BackupManagerService, which is not * visible here. Unify the code. */ */ class ShortcutPackageInfo implements ShortcutPackageItem { class ShortcutPackageInfo { private static final String TAG = ShortcutService.TAG; private static final String TAG = ShortcutService.TAG; static final String TAG_ROOT = "package-info"; static final String TAG_ROOT = "package-info"; private static final String ATTR_USER_ID = "user"; private static final String ATTR_NAME = "name"; private static final String ATTR_VERSION = "version"; private static final String ATTR_VERSION = "version"; private static final String ATTR_SHADOW = "shadow"; private static final String ATTR_SHADOW = "shadow"; private static final String TAG_SIGNATURE = "signature"; private static final String TAG_SIGNATURE = "signature"; private static final String ATTR_SIGNATURE_HASH = "hash"; private static final String ATTR_SIGNATURE_HASH = "hash"; private final String mPackageName; private final int mUserId; /** /** * When true, this package information was restored from the previous device, and the app hasn't * When true, this package information was restored from the previous device, and the app hasn't * been installed yet. * been installed yet. Loading @@ -67,22 +53,14 @@ class ShortcutPackageInfo implements ShortcutPackageItem { private int mVersionCode; private int mVersionCode; private ArrayList<byte[]> mSigHashes; private ArrayList<byte[]> mSigHashes; private ShortcutPackageInfo(String packageName, int userId, private ShortcutPackageInfo(int versionCode, ArrayList<byte[]> sigHashes, boolean isShadow) { int versionCode, ArrayList<byte[]> sigHashes, boolean isShadow) { mPackageName = Preconditions.checkNotNull(packageName); mUserId = userId; mVersionCode = versionCode; mVersionCode = versionCode; mIsShadow = isShadow; mIsShadow = isShadow; mSigHashes = sigHashes; mSigHashes = sigHashes; } } @NonNull public static ShortcutPackageInfo newEmpty() { public String getPackageName() { return new ShortcutPackageInfo(0, new ArrayList<>(0), /* isShadow */ false); return mPackageName; } public int getUserId() { return mUserId; } } public boolean isShadow() { public boolean isShadow() { Loading @@ -101,92 +79,13 @@ class ShortcutPackageInfo implements ShortcutPackageItem { return mVersionCode; return mVersionCode; } } private static byte[] hashSignature(Signature sig) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.update(sig.toByteArray()); return digest.digest(); } catch (NoSuchAlgorithmException e) { Slog.w(TAG, "No SHA-256 algorithm found!"); } return null; } private static ArrayList<byte[]> hashSignatureArray(Signature[] sigs) { if (sigs == null) { return null; } ArrayList<byte[]> hashes = new ArrayList<byte[]>(sigs.length); for (Signature s : sigs) { hashes.add(hashSignature(s)); } return hashes; } private static boolean signaturesMatch(ArrayList<byte[]> storedSigHashes, PackageInfo target) { if (target == null) { return false; } // If the target resides on the system partition, we allow it to restore // data from the like-named package in a restore set even if the signatures // do not match. (Unlike general applications, those flashed to the system // partition will be signed with the device's platform certificate, so on // different phones the same system app will have different signatures.) if ((target.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { return true; } // Allow unsigned apps, but not signed on one device and unsigned on the other // !!! TODO: is this the right policy? Signature[] deviceSigs = target.signatures; if ((storedSigHashes == null || storedSigHashes.size() == 0) && (deviceSigs == null || deviceSigs.length == 0)) { return true; } if (storedSigHashes == null || deviceSigs == null) { return false; } // !!! TODO: this demands that every stored signature match one // that is present on device, and does not demand the converse. // Is this this right policy? final int nStored = storedSigHashes.size(); final int nDevice = deviceSigs.length; // hash each on-device signature ArrayList<byte[]> deviceHashes = new ArrayList<byte[]>(nDevice); for (int i = 0; i < nDevice; i++) { deviceHashes.add(hashSignature(deviceSigs[i])); } // now ensure that each stored sig (hash) matches an on-device sig (hash) for (int n = 0; n < nStored; n++) { boolean match = false; final byte[] storedHash = storedSigHashes.get(n); for (int i = 0; i < nDevice; i++) { if (Arrays.equals(storedHash, deviceHashes.get(i))) { match = true; break; } } // match is false when no on-device sig matched one of the stored ones if (!match) { return false; } } return true; } public boolean canRestoreTo(PackageInfo target) { public boolean canRestoreTo(PackageInfo target) { if (target.versionCode < mVersionCode) { if (target.versionCode < mVersionCode) { Slog.w(TAG, String.format("Package current version %d < backed up version %d", Slog.w(TAG, String.format("Package current version %d < backed up version %d", target.versionCode, mVersionCode)); target.versionCode, mVersionCode)); return false; return false; } } if (!signaturesMatch(mSigHashes, target)) { if (!BackupUtils.signaturesMatch(mSigHashes, target)) { Slog.w(TAG, "Package signature mismtach"); Slog.w(TAG, "Package signature mismtach"); return false; return false; } } Loading @@ -194,37 +93,34 @@ class ShortcutPackageInfo implements ShortcutPackageItem { } } public static ShortcutPackageInfo generateForInstalledPackage( public static ShortcutPackageInfo generateForInstalledPackage( ShortcutService s, String packageName, @UserIdInt int userId) { ShortcutService s, String packageName, @UserIdInt int packageUserId) { final PackageInfo pi = s.getPackageInfoWithSignatures(packageName, userId); final PackageInfo pi = s.getPackageInfoWithSignatures(packageName, packageUserId); if (pi.signatures == null || pi.signatures.length == 0) { if (pi.signatures == null || pi.signatures.length == 0) { Slog.e(TAG, "Can't get signatures: package=" + packageName); Slog.e(TAG, "Can't get signatures: package=" + packageName); return null; return null; } } final ShortcutPackageInfo ret = new ShortcutPackageInfo(packageName, userId, pi.versionCode, final ShortcutPackageInfo ret = new ShortcutPackageInfo(pi.versionCode, hashSignatureArray(pi.signatures), /* shadow=*/ false); BackupUtils.hashSignatureArray(pi.signatures), /* shadow=*/ false); return ret; return ret; } } public void refreshAndSave(ShortcutService s, @UserIdInt int userId) { public void refresh(ShortcutService s, ShortcutPackageItem pkg) { final PackageInfo pi = s.getPackageInfoWithSignatures(mPackageName, userId); // Note use mUserId here, rather than userId. final PackageInfo pi = s.getPackageInfoWithSignatures( pkg.getPackageName(), pkg.getPackageUserId()); if (pi == null) { if (pi == null) { Slog.w(TAG, "Package not found: " + mPackageName); Slog.w(TAG, "Package not found: " + pkg.getPackageName()); return; return; } } mVersionCode = pi.versionCode; mVersionCode = pi.versionCode; mSigHashes = hashSignatureArray(pi.signatures); mSigHashes = BackupUtils.hashSignatureArray(pi.signatures); s.scheduleSaveUser(userId); } } public void saveToXml(XmlSerializer out, boolean forBackup) public void saveToXml(XmlSerializer out) throws IOException { throws IOException, XmlPullParserException { out.startTag(null, TAG_ROOT); out.startTag(null, TAG_ROOT); ShortcutService.writeAttr(out, ATTR_NAME, mPackageName); ShortcutService.writeAttr(out, ATTR_USER_ID, mUserId); ShortcutService.writeAttr(out, ATTR_VERSION, mVersionCode); ShortcutService.writeAttr(out, ATTR_VERSION, mVersionCode); ShortcutService.writeAttr(out, ATTR_SHADOW, mIsShadow); ShortcutService.writeAttr(out, ATTR_SHADOW, mIsShadow); Loading @@ -236,11 +132,9 @@ class ShortcutPackageInfo implements ShortcutPackageItem { out.endTag(null, TAG_ROOT); out.endTag(null, TAG_ROOT); } } public static ShortcutPackageInfo loadFromXml(XmlPullParser parser, int ownerUserId) public static ShortcutPackageInfo loadFromXml(XmlPullParser parser) throws IOException, XmlPullParserException { throws IOException, XmlPullParserException { final String packageName = ShortcutService.parseStringAttribute(parser, ATTR_NAME); final int userId = ShortcutService.parseIntAttribute(parser, ATTR_USER_ID, ownerUserId); final int versionCode = ShortcutService.parseIntAttribute(parser, ATTR_VERSION); final int versionCode = ShortcutService.parseIntAttribute(parser, ATTR_VERSION); final boolean shadow = ShortcutService.parseBooleanAttribute(parser, ATTR_SHADOW); final boolean shadow = ShortcutService.parseBooleanAttribute(parser, ATTR_SHADOW); Loading @@ -256,6 +150,8 @@ class ShortcutPackageInfo implements ShortcutPackageItem { } } final int depth = parser.getDepth(); final int depth = parser.getDepth(); final String tag = parser.getName(); final String tag = parser.getName(); if (depth == outerDepth + 1) { switch (tag) { switch (tag) { case TAG_SIGNATURE: { case TAG_SIGNATURE: { final String hash = ShortcutService.parseStringAttribute( final String hash = ShortcutService.parseStringAttribute( Loading @@ -264,23 +160,17 @@ class ShortcutPackageInfo implements ShortcutPackageItem { continue; continue; } } } } throw ShortcutService.throwForInvalidTag(depth, tag); } } return new ShortcutPackageInfo(packageName, userId, versionCode, hashes, shadow); ShortcutService.warnForInvalidTag(depth, tag); } return new ShortcutPackageInfo(versionCode, hashes, shadow); } } public void dump(ShortcutService s, PrintWriter pw, String prefix) { public void dump(ShortcutService s, PrintWriter pw, String prefix) { pw.println(); pw.println(); pw.print(prefix); pw.print(prefix); pw.print("PackageInfo: "); pw.println("PackageInfo:"); pw.print(mPackageName); pw.println(); pw.print(prefix); pw.print(" User: "); pw.print(mUserId); pw.println(); pw.print(prefix); pw.print(prefix); pw.print(" IsShadow: "); pw.print(" IsShadow: "); Loading
services/core/java/com/android/server/pm/ShortcutPackageItem.java +57 −3 File changed.Preview size limit exceeded, changes collapsed. Show changes