下面列出了android.content.pm.ShortcutInfo#isDynamic() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Remove all dynamic shortcuts.
*/
public void deleteAllDynamicShortcuts(boolean ignoreInvisible) {
final long now = mShortcutUser.mService.injectCurrentTimeMillis();
boolean changed = false;
for (int i = mShortcuts.size() - 1; i >= 0; i--) {
final ShortcutInfo si = mShortcuts.valueAt(i);
if (si.isDynamic() && (!ignoreInvisible || si.isVisibleToPublisher())) {
changed = true;
si.setTimestamp(now);
si.clearFlags(ShortcutInfo.FLAG_DYNAMIC);
si.setRank(0); // It may still be pinned, so clear the rank.
}
}
if (changed) {
removeOrphans();
}
}
private String getType(ShortcutInfo shortcut) {
final StringBuilder sb = new StringBuilder();
String sep = "";
if (shortcut.isDynamic()) {
sb.append(sep);
sb.append("Dynamic");
sep = ", ";
}
if (shortcut.isPinned()) {
sb.append(sep);
sb.append("Pinned");
sep = ", ";
}
if (!shortcut.isEnabled()) {
sb.append(sep);
sb.append("Disabled");
sep = ", ";
}
return sb.toString();
}
private String getType(ShortcutInfo shortcut) {
final StringBuilder sb = new StringBuilder();
String sep = "";
if (shortcut.isDynamic()) {
sb.append(sep);
sb.append("Dynamic");
sep = ", ";
}
if (shortcut.isPinned()) {
sb.append(sep);
sb.append("Pinned");
sep = ", ";
}
if (!shortcut.isEnabled()) {
sb.append(sep);
sb.append("Disabled");
sep = ", ";
}
return sb.toString();
}
@Override
public JSONObject dumpCheckin(boolean clear) throws JSONException {
final JSONObject result = super.dumpCheckin(clear);
int numDynamic = 0;
int numPinned = 0;
int numManifest = 0;
int numBitmaps = 0;
long totalBitmapSize = 0;
final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts;
final int size = shortcuts.size();
for (int i = 0; i < size; i++) {
final ShortcutInfo si = shortcuts.valueAt(i);
if (si.isDynamic()) numDynamic++;
if (si.isDeclaredInManifest()) numManifest++;
if (si.isPinned()) numPinned++;
if (si.getBitmapPath() != null) {
numBitmaps++;
totalBitmapSize += new File(si.getBitmapPath()).length();
}
}
result.put(KEY_DYNAMIC, numDynamic);
result.put(KEY_MANIFEST, numManifest);
result.put(KEY_PINNED, numPinned);
result.put(KEY_BITMAPS, numBitmaps);
result.put(KEY_BITMAP_BYTES, totalBitmapSize);
// TODO Log update frequency too.
return result;
}
/**
* Pin the given shortcuts, replacing the current pinned ones.
*/
public void pinShortcuts(@UserIdInt int packageUserId,
@NonNull String packageName, @NonNull List<String> ids, boolean forPinRequest) {
final ShortcutPackage packageShortcuts =
mShortcutUser.getPackageShortcutsIfExists(packageName);
if (packageShortcuts == null) {
return; // No need to instantiate.
}
final PackageWithUser pu = PackageWithUser.of(packageUserId, packageName);
final int idSize = ids.size();
if (idSize == 0) {
mPinnedShortcuts.remove(pu);
} else {
final ArraySet<String> prevSet = mPinnedShortcuts.get(pu);
// Actually pin shortcuts.
// This logic here is to make sure a launcher cannot pin a shortcut that is floating
// (i.e. not dynamic nor manifest but is pinned) and pinned by another launcher.
// In this case, technically the shortcut doesn't exist to this launcher, so it can't
// pin it.
// (Maybe unnecessarily strict...)
final ArraySet<String> newSet = new ArraySet<>();
for (int i = 0; i < idSize; i++) {
final String id = ids.get(i);
final ShortcutInfo si = packageShortcuts.findShortcutById(id);
if (si == null) {
continue;
}
if (si.isDynamic()
|| si.isManifestShortcut()
|| (prevSet != null && prevSet.contains(id))
|| forPinRequest) {
newSet.add(id);
}
}
mPinnedShortcuts.put(pu, newSet);
}
packageShortcuts.refreshPinnedFlags();
}
/**
* Called by
* {@link android.content.pm.ShortcutManager#setDynamicShortcuts},
* {@link android.content.pm.ShortcutManager#addDynamicShortcuts}, and
* {@link android.content.pm.ShortcutManager#updateShortcuts} before actually performing
* the operation to make sure the operation wouldn't result in the target activities having
* more than the allowed number of dynamic/manifest shortcuts.
*
* @param newList shortcut list passed to set, add or updateShortcuts().
* @param operation add, set or update.
* @throws IllegalArgumentException if the operation would result in going over the max
* shortcut count for any activity.
*/
public void enforceShortcutCountsBeforeOperation(List<ShortcutInfo> newList,
@ShortcutOperation int operation) {
final ShortcutService service = mShortcutUser.mService;
// Current # of dynamic / manifest shortcuts for each activity.
// (If it's for update, then don't count dynamic shortcuts, since they'll be replaced
// anyway.)
final ArrayMap<ComponentName, Integer> counts = new ArrayMap<>(4);
for (int i = mShortcuts.size() - 1; i >= 0; i--) {
final ShortcutInfo shortcut = mShortcuts.valueAt(i);
if (shortcut.isManifestShortcut()) {
incrementCountForActivity(counts, shortcut.getActivity(), 1);
} else if (shortcut.isDynamic() && (operation != ShortcutService.OPERATION_SET)) {
incrementCountForActivity(counts, shortcut.getActivity(), 1);
}
}
for (int i = newList.size() - 1; i >= 0; i--) {
final ShortcutInfo newShortcut = newList.get(i);
final ComponentName newActivity = newShortcut.getActivity();
if (newActivity == null) {
if (operation != ShortcutService.OPERATION_UPDATE) {
service.wtf("Activity must not be null at this point");
continue; // Just ignore this invalid case.
}
continue; // Activity can be null for update.
}
final ShortcutInfo original = mShortcuts.get(newShortcut.getId());
if (original == null) {
if (operation == ShortcutService.OPERATION_UPDATE) {
continue; // When updating, ignore if there's no target.
}
// Add() or set(), and there's no existing shortcut with the same ID. We're
// simply publishing (as opposed to updating) this shortcut, so just +1.
incrementCountForActivity(counts, newActivity, 1);
continue;
}
if (original.isFloating() && (operation == ShortcutService.OPERATION_UPDATE)) {
// Updating floating shortcuts doesn't affect the count, so ignore.
continue;
}
// If it's add() or update(), then need to decrement for the previous activity.
// Skip it for set() since it's already been taken care of by not counting the original
// dynamic shortcuts in the first loop.
if (operation != ShortcutService.OPERATION_SET) {
final ComponentName oldActivity = original.getActivity();
if (!original.isFloating()) {
incrementCountForActivity(counts, oldActivity, -1);
}
}
incrementCountForActivity(counts, newActivity, 1);
}
// Then make sure none of the activities have more than the max number of shortcuts.
for (int i = counts.size() - 1; i >= 0; i--) {
service.enforceMaxActivityShortcuts(counts.valueAt(i));
}
}