ShortcutService.java revision 3145924596ad0db9e8f1f5aead90fb50127243cb
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.annotation.Nullable; 20import android.annotation.UserIdInt; 21import android.app.ActivityManager; 22import android.content.ComponentName; 23import android.content.ContentProvider; 24import android.content.Context; 25import android.content.Intent; 26import android.content.pm.IShortcutService; 27import android.content.pm.LauncherApps; 28import android.content.pm.LauncherApps.ShortcutQuery; 29import android.content.pm.PackageManager; 30import android.content.pm.PackageManager.NameNotFoundException; 31import android.content.pm.PackageManagerInternal; 32import android.content.pm.ParceledListSlice; 33import android.content.pm.ResolveInfo; 34import android.content.pm.ShortcutInfo; 35import android.content.pm.ShortcutServiceInternal; 36import android.content.pm.ShortcutServiceInternal.ShortcutChangeListener; 37import android.graphics.Bitmap; 38import android.graphics.Bitmap.CompressFormat; 39import android.graphics.BitmapFactory; 40import android.graphics.Canvas; 41import android.graphics.RectF; 42import android.graphics.drawable.Icon; 43import android.net.Uri; 44import android.os.Binder; 45import android.os.Environment; 46import android.os.Handler; 47import android.os.Looper; 48import android.os.ParcelFileDescriptor; 49import android.os.PersistableBundle; 50import android.os.Process; 51import android.os.RemoteException; 52import android.os.ResultReceiver; 53import android.os.SELinux; 54import android.os.ShellCommand; 55import android.os.UserHandle; 56import android.os.UserManager; 57import android.text.TextUtils; 58import android.text.format.Formatter; 59import android.text.format.Time; 60import android.util.ArrayMap; 61import android.util.ArraySet; 62import android.util.AtomicFile; 63import android.util.KeyValueListParser; 64import android.util.Slog; 65import android.util.SparseArray; 66import android.util.TypedValue; 67import android.util.Xml; 68 69import com.android.internal.annotations.GuardedBy; 70import com.android.internal.annotations.VisibleForTesting; 71import com.android.internal.content.PackageMonitor; 72import com.android.internal.os.BackgroundThread; 73import com.android.internal.util.FastXmlSerializer; 74import com.android.internal.util.Preconditions; 75import com.android.server.LocalServices; 76import com.android.server.SystemService; 77 78import libcore.io.IoUtils; 79import libcore.util.Objects; 80 81import org.xmlpull.v1.XmlPullParser; 82import org.xmlpull.v1.XmlPullParserException; 83import org.xmlpull.v1.XmlSerializer; 84 85import java.io.File; 86import java.io.FileDescriptor; 87import java.io.FileInputStream; 88import java.io.FileNotFoundException; 89import java.io.FileOutputStream; 90import java.io.IOException; 91import java.io.InputStream; 92import java.io.PrintWriter; 93import java.net.URISyntaxException; 94import java.nio.charset.StandardCharsets; 95import java.util.ArrayList; 96import java.util.List; 97import java.util.function.Predicate; 98 99/** 100 * TODO: 101 * 102 * - Default launcher check does take a few ms. Worth caching. 103 * 104 * - Allow non-default launcher to start pinned shortcuts. (but not dynamic.) 105 * 106 * - Extract the user/package/launcher classes to their own files. Maybe rename so they all have 107 * the same "Shortcut" prefix. 108 * 109 * - Listen to PACKAGE_*, remove orphan info, update timestamp for icon res 110 * -> Need to scan all packages when a user starts too. 111 * -> Clear data -> remove all dynamic? but not the pinned? 112 * 113 * - Scan and remove orphan bitmaps (just in case). 114 * 115 * - Backup & restore 116 * 117 * - Detect when already registered instances are passed to APIs again, which might break 118 * internal bitmap handling. 119 */ 120public class ShortcutService extends IShortcutService.Stub { 121 static final String TAG = "ShortcutService"; 122 123 static final boolean DEBUG = false; // STOPSHIP if true 124 static final boolean DEBUG_LOAD = false; // STOPSHIP if true 125 126 @VisibleForTesting 127 static final long DEFAULT_RESET_INTERVAL_SEC = 24 * 60 * 60; // 1 day 128 129 @VisibleForTesting 130 static final int DEFAULT_MAX_DAILY_UPDATES = 10; 131 132 @VisibleForTesting 133 static final int DEFAULT_MAX_SHORTCUTS_PER_APP = 5; 134 135 @VisibleForTesting 136 static final int DEFAULT_MAX_ICON_DIMENSION_DP = 96; 137 138 @VisibleForTesting 139 static final int DEFAULT_MAX_ICON_DIMENSION_LOWRAM_DP = 48; 140 141 @VisibleForTesting 142 static final String DEFAULT_ICON_PERSIST_FORMAT = CompressFormat.PNG.name(); 143 144 @VisibleForTesting 145 static final int DEFAULT_ICON_PERSIST_QUALITY = 100; 146 147 @VisibleForTesting 148 static final int DEFAULT_SAVE_DELAY_MS = 3000; 149 150 @VisibleForTesting 151 static final String FILENAME_BASE_STATE = "shortcut_service.xml"; 152 153 @VisibleForTesting 154 static final String DIRECTORY_PER_USER = "shortcut_service"; 155 156 @VisibleForTesting 157 static final String FILENAME_USER_PACKAGES = "shortcuts.xml"; 158 159 static final String DIRECTORY_BITMAPS = "bitmaps"; 160 161 private static final String TAG_ROOT = "root"; 162 private static final String TAG_LAST_RESET_TIME = "last_reset_time"; 163 164 private static final String ATTR_VALUE = "value"; 165 166 @VisibleForTesting 167 interface ConfigConstants { 168 /** 169 * Key name for the save delay, in milliseconds. (int) 170 */ 171 String KEY_SAVE_DELAY_MILLIS = "save_delay_ms"; 172 173 /** 174 * Key name for the throttling reset interval, in seconds. (long) 175 */ 176 String KEY_RESET_INTERVAL_SEC = "reset_interval_sec"; 177 178 /** 179 * Key name for the max number of modifying API calls per app for every interval. (int) 180 */ 181 String KEY_MAX_DAILY_UPDATES = "max_daily_updates"; 182 183 /** 184 * Key name for the max icon dimensions in DP, for non-low-memory devices. 185 */ 186 String KEY_MAX_ICON_DIMENSION_DP = "max_icon_dimension_dp"; 187 188 /** 189 * Key name for the max icon dimensions in DP, for low-memory devices. 190 */ 191 String KEY_MAX_ICON_DIMENSION_DP_LOWRAM = "max_icon_dimension_dp_lowram"; 192 193 /** 194 * Key name for the max dynamic shortcuts per app. (int) 195 */ 196 String KEY_MAX_SHORTCUTS = "max_shortcuts"; 197 198 /** 199 * Key name for icon compression quality, 0-100. 200 */ 201 String KEY_ICON_QUALITY = "icon_quality"; 202 203 /** 204 * Key name for icon compression format: "PNG", "JPEG" or "WEBP" 205 */ 206 String KEY_ICON_FORMAT = "icon_format"; 207 } 208 209 final Context mContext; 210 211 private final Object mLock = new Object(); 212 213 private final Handler mHandler; 214 215 @GuardedBy("mLock") 216 private final ArrayList<ShortcutChangeListener> mListeners = new ArrayList<>(1); 217 218 @GuardedBy("mLock") 219 private long mRawLastResetTime; 220 221 /** 222 * User ID -> UserShortcuts 223 */ 224 @GuardedBy("mLock") 225 private final SparseArray<ShortcutUser> mUsers = new SparseArray<>(); 226 227 /** 228 * Max number of dynamic shortcuts that each application can have at a time. 229 */ 230 private int mMaxDynamicShortcuts; 231 232 /** 233 * Max number of updating API calls that each application can make a day. 234 */ 235 int mMaxDailyUpdates; 236 237 /** 238 * Actual throttling-reset interval. By default it's a day. 239 */ 240 private long mResetInterval; 241 242 /** 243 * Icon max width/height in pixels. 244 */ 245 private int mMaxIconDimension; 246 247 private CompressFormat mIconPersistFormat; 248 private int mIconPersistQuality; 249 250 private int mSaveDelayMillis; 251 252 private final PackageManagerInternal mPackageManagerInternal; 253 private final UserManager mUserManager; 254 255 @GuardedBy("mLock") 256 private List<Integer> mDirtyUserIds = new ArrayList<>(); 257 258 public ShortcutService(Context context) { 259 this(context, BackgroundThread.get().getLooper()); 260 } 261 262 @VisibleForTesting 263 ShortcutService(Context context, Looper looper) { 264 mContext = Preconditions.checkNotNull(context); 265 LocalServices.addService(ShortcutServiceInternal.class, new LocalService()); 266 mHandler = new Handler(looper); 267 mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class); 268 mUserManager = context.getSystemService(UserManager.class); 269 270 mPackageMonitor.register(context, looper, UserHandle.ALL, /* externalStorage= */ false); 271 } 272 273 /** 274 * System service lifecycle. 275 */ 276 public static final class Lifecycle extends SystemService { 277 final ShortcutService mService; 278 279 public Lifecycle(Context context) { 280 super(context); 281 mService = new ShortcutService(context); 282 } 283 284 @Override 285 public void onStart() { 286 publishBinderService(Context.SHORTCUT_SERVICE, mService); 287 } 288 289 @Override 290 public void onBootPhase(int phase) { 291 mService.onBootPhase(phase); 292 } 293 294 @Override 295 public void onCleanupUser(int userHandle) { 296 mService.handleCleanupUser(userHandle); 297 } 298 299 @Override 300 public void onUnlockUser(int userId) { 301 mService.handleUnlockUser(userId); 302 } 303 } 304 305 /** lifecycle event */ 306 void onBootPhase(int phase) { 307 if (DEBUG) { 308 Slog.d(TAG, "onBootPhase: " + phase); 309 } 310 switch (phase) { 311 case SystemService.PHASE_LOCK_SETTINGS_READY: 312 initialize(); 313 break; 314 } 315 } 316 317 /** lifecycle event */ 318 void handleUnlockUser(int userId) { 319 synchronized (mLock) { 320 // Preload 321 getUserShortcutsLocked(userId); 322 } 323 } 324 325 /** lifecycle event */ 326 void handleCleanupUser(int userId) { 327 synchronized (mLock) { 328 unloadUserLocked(userId); 329 } 330 } 331 332 private void unloadUserLocked(int userId) { 333 if (DEBUG) { 334 Slog.d(TAG, "unloadUserLocked: user=" + userId); 335 } 336 // Save all dirty information. 337 saveDirtyInfo(); 338 339 // Unload 340 mUsers.delete(userId); 341 } 342 343 /** Return the base state file name */ 344 private AtomicFile getBaseStateFile() { 345 final File path = new File(injectSystemDataPath(), FILENAME_BASE_STATE); 346 path.mkdirs(); 347 return new AtomicFile(path); 348 } 349 350 /** 351 * Init the instance. (load the state file, etc) 352 */ 353 private void initialize() { 354 synchronized (mLock) { 355 loadConfigurationLocked(); 356 loadBaseStateLocked(); 357 } 358 } 359 360 /** 361 * Load the configuration from Settings. 362 */ 363 private void loadConfigurationLocked() { 364 updateConfigurationLocked(injectShortcutManagerConstants()); 365 } 366 367 /** 368 * Load the configuration from Settings. 369 */ 370 @VisibleForTesting 371 boolean updateConfigurationLocked(String config) { 372 boolean result = true; 373 374 final KeyValueListParser parser = new KeyValueListParser(','); 375 try { 376 parser.setString(config); 377 } catch (IllegalArgumentException e) { 378 // Failed to parse the settings string, log this and move on 379 // with defaults. 380 Slog.e(TAG, "Bad shortcut manager settings", e); 381 result = false; 382 } 383 384 mSaveDelayMillis = (int) parser.getLong(ConfigConstants.KEY_SAVE_DELAY_MILLIS, 385 DEFAULT_SAVE_DELAY_MS); 386 387 mResetInterval = parser.getLong( 388 ConfigConstants.KEY_RESET_INTERVAL_SEC, DEFAULT_RESET_INTERVAL_SEC) 389 * 1000L; 390 391 mMaxDailyUpdates = (int) parser.getLong( 392 ConfigConstants.KEY_MAX_DAILY_UPDATES, DEFAULT_MAX_DAILY_UPDATES); 393 394 mMaxDynamicShortcuts = (int) parser.getLong( 395 ConfigConstants.KEY_MAX_SHORTCUTS, DEFAULT_MAX_SHORTCUTS_PER_APP); 396 397 final int iconDimensionDp = injectIsLowRamDevice() 398 ? (int) parser.getLong( 399 ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM, 400 DEFAULT_MAX_ICON_DIMENSION_LOWRAM_DP) 401 : (int) parser.getLong( 402 ConfigConstants.KEY_MAX_ICON_DIMENSION_DP, 403 DEFAULT_MAX_ICON_DIMENSION_DP); 404 405 mMaxIconDimension = injectDipToPixel(iconDimensionDp); 406 407 mIconPersistFormat = CompressFormat.valueOf( 408 parser.getString(ConfigConstants.KEY_ICON_FORMAT, DEFAULT_ICON_PERSIST_FORMAT)); 409 410 mIconPersistQuality = (int) parser.getLong( 411 ConfigConstants.KEY_ICON_QUALITY, 412 DEFAULT_ICON_PERSIST_QUALITY); 413 414 return result; 415 } 416 417 @VisibleForTesting 418 String injectShortcutManagerConstants() { 419 return android.provider.Settings.Global.getString( 420 mContext.getContentResolver(), 421 android.provider.Settings.Global.SHORTCUT_MANAGER_CONSTANTS); 422 } 423 424 @VisibleForTesting 425 int injectDipToPixel(int dip) { 426 return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, 427 mContext.getResources().getDisplayMetrics()); 428 } 429 430 // === Persisting === 431 432 @Nullable 433 static String parseStringAttribute(XmlPullParser parser, String attribute) { 434 return parser.getAttributeValue(null, attribute); 435 } 436 437 static int parseIntAttribute(XmlPullParser parser, String attribute) { 438 return (int) parseLongAttribute(parser, attribute); 439 } 440 441 static long parseLongAttribute(XmlPullParser parser, String attribute) { 442 final String value = parseStringAttribute(parser, attribute); 443 if (TextUtils.isEmpty(value)) { 444 return 0; 445 } 446 try { 447 return Long.parseLong(value); 448 } catch (NumberFormatException e) { 449 Slog.e(TAG, "Error parsing long " + value); 450 return 0; 451 } 452 } 453 454 @Nullable 455 static ComponentName parseComponentNameAttribute(XmlPullParser parser, String attribute) { 456 final String value = parseStringAttribute(parser, attribute); 457 if (TextUtils.isEmpty(value)) { 458 return null; 459 } 460 return ComponentName.unflattenFromString(value); 461 } 462 463 @Nullable 464 static Intent parseIntentAttribute(XmlPullParser parser, String attribute) { 465 final String value = parseStringAttribute(parser, attribute); 466 if (TextUtils.isEmpty(value)) { 467 return null; 468 } 469 try { 470 return Intent.parseUri(value, /* flags =*/ 0); 471 } catch (URISyntaxException e) { 472 Slog.e(TAG, "Error parsing intent", e); 473 return null; 474 } 475 } 476 477 static void writeTagValue(XmlSerializer out, String tag, String value) throws IOException { 478 if (TextUtils.isEmpty(value)) return; 479 480 out.startTag(null, tag); 481 out.attribute(null, ATTR_VALUE, value); 482 out.endTag(null, tag); 483 } 484 485 static void writeTagValue(XmlSerializer out, String tag, long value) throws IOException { 486 writeTagValue(out, tag, Long.toString(value)); 487 } 488 489 static void writeTagValue(XmlSerializer out, String tag, ComponentName name) throws IOException { 490 if (name == null) return; 491 writeTagValue(out, tag, name.flattenToString()); 492 } 493 494 static void writeTagExtra(XmlSerializer out, String tag, PersistableBundle bundle) 495 throws IOException, XmlPullParserException { 496 if (bundle == null) return; 497 498 out.startTag(null, tag); 499 bundle.saveToXml(out); 500 out.endTag(null, tag); 501 } 502 503 static void writeAttr(XmlSerializer out, String name, String value) throws IOException { 504 if (TextUtils.isEmpty(value)) return; 505 506 out.attribute(null, name, value); 507 } 508 509 static void writeAttr(XmlSerializer out, String name, long value) throws IOException { 510 writeAttr(out, name, String.valueOf(value)); 511 } 512 513 static void writeAttr(XmlSerializer out, String name, ComponentName comp) throws IOException { 514 if (comp == null) return; 515 writeAttr(out, name, comp.flattenToString()); 516 } 517 518 static void writeAttr(XmlSerializer out, String name, Intent intent) throws IOException { 519 if (intent == null) return; 520 521 writeAttr(out, name, intent.toUri(/* flags =*/ 0)); 522 } 523 524 @VisibleForTesting 525 void saveBaseStateLocked() { 526 final AtomicFile file = getBaseStateFile(); 527 if (DEBUG) { 528 Slog.d(TAG, "Saving to " + file.getBaseFile()); 529 } 530 531 FileOutputStream outs = null; 532 try { 533 outs = file.startWrite(); 534 535 // Write to XML 536 XmlSerializer out = new FastXmlSerializer(); 537 out.setOutput(outs, StandardCharsets.UTF_8.name()); 538 out.startDocument(null, true); 539 out.startTag(null, TAG_ROOT); 540 541 // Body. 542 writeTagValue(out, TAG_LAST_RESET_TIME, mRawLastResetTime); 543 544 // Epilogue. 545 out.endTag(null, TAG_ROOT); 546 out.endDocument(); 547 548 // Close. 549 file.finishWrite(outs); 550 } catch (IOException e) { 551 Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e); 552 file.failWrite(outs); 553 } 554 } 555 556 private void loadBaseStateLocked() { 557 mRawLastResetTime = 0; 558 559 final AtomicFile file = getBaseStateFile(); 560 if (DEBUG) { 561 Slog.d(TAG, "Loading from " + file.getBaseFile()); 562 } 563 try (FileInputStream in = file.openRead()) { 564 XmlPullParser parser = Xml.newPullParser(); 565 parser.setInput(in, StandardCharsets.UTF_8.name()); 566 567 int type; 568 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { 569 if (type != XmlPullParser.START_TAG) { 570 continue; 571 } 572 final int depth = parser.getDepth(); 573 // Check the root tag 574 final String tag = parser.getName(); 575 if (depth == 1) { 576 if (!TAG_ROOT.equals(tag)) { 577 Slog.e(TAG, "Invalid root tag: " + tag); 578 return; 579 } 580 continue; 581 } 582 // Assume depth == 2 583 switch (tag) { 584 case TAG_LAST_RESET_TIME: 585 mRawLastResetTime = parseLongAttribute(parser, ATTR_VALUE); 586 break; 587 default: 588 Slog.e(TAG, "Invalid tag: " + tag); 589 break; 590 } 591 } 592 } catch (FileNotFoundException e) { 593 // Use the default 594 } catch (IOException|XmlPullParserException e) { 595 Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e); 596 597 mRawLastResetTime = 0; 598 } 599 // Adjust the last reset time. 600 getLastResetTimeLocked(); 601 } 602 603 private void saveUserLocked(@UserIdInt int userId) { 604 final File path = new File(injectUserDataPath(userId), FILENAME_USER_PACKAGES); 605 if (DEBUG) { 606 Slog.d(TAG, "Saving to " + path); 607 } 608 path.mkdirs(); 609 final AtomicFile file = new AtomicFile(path); 610 FileOutputStream outs = null; 611 try { 612 outs = file.startWrite(); 613 614 // Write to XML 615 XmlSerializer out = new FastXmlSerializer(); 616 out.setOutput(outs, StandardCharsets.UTF_8.name()); 617 out.startDocument(null, true); 618 619 getUserShortcutsLocked(userId).saveToXml(out); 620 621 out.endDocument(); 622 623 // Close. 624 file.finishWrite(outs); 625 } catch (IOException|XmlPullParserException e) { 626 Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e); 627 file.failWrite(outs); 628 } 629 } 630 631 static IOException throwForInvalidTag(int depth, String tag) throws IOException { 632 throw new IOException(String.format("Invalid tag '%s' found at depth %d", tag, depth)); 633 } 634 635 @Nullable 636 private ShortcutUser loadUserLocked(@UserIdInt int userId) { 637 final File path = new File(injectUserDataPath(userId), FILENAME_USER_PACKAGES); 638 if (DEBUG) { 639 Slog.d(TAG, "Loading from " + path); 640 } 641 final AtomicFile file = new AtomicFile(path); 642 643 final FileInputStream in; 644 try { 645 in = file.openRead(); 646 } catch (FileNotFoundException e) { 647 if (DEBUG) { 648 Slog.d(TAG, "Not found " + path); 649 } 650 return null; 651 } 652 ShortcutUser ret = null; 653 try { 654 XmlPullParser parser = Xml.newPullParser(); 655 parser.setInput(in, StandardCharsets.UTF_8.name()); 656 657 int type; 658 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { 659 if (type != XmlPullParser.START_TAG) { 660 continue; 661 } 662 final int depth = parser.getDepth(); 663 664 final String tag = parser.getName(); 665 if (DEBUG_LOAD) { 666 Slog.d(TAG, String.format("depth=%d type=%d name=%s", 667 depth, type, tag)); 668 } 669 if ((depth == 1) && ShortcutUser.TAG_ROOT.equals(tag)) { 670 ret = ShortcutUser.loadFromXml(parser, userId); 671 continue; 672 } 673 throwForInvalidTag(depth, tag); 674 } 675 return ret; 676 } catch (IOException|XmlPullParserException e) { 677 Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e); 678 return null; 679 } finally { 680 IoUtils.closeQuietly(in); 681 } 682 } 683 684 private void scheduleSaveBaseState() { 685 scheduleSave(UserHandle.USER_NULL); // Special case -- use USER_NULL for base state. 686 } 687 688 void scheduleSaveUser(@UserIdInt int userId) { 689 scheduleSave(userId); 690 } 691 692 // In order to re-schedule, we need to reuse the same instance, so keep it in final. 693 private final Runnable mSaveDirtyInfoRunner = this::saveDirtyInfo; 694 695 private void scheduleSave(@UserIdInt int userId) { 696 if (DEBUG) { 697 Slog.d(TAG, "Scheduling to save for " + userId); 698 } 699 synchronized (mLock) { 700 if (!mDirtyUserIds.contains(userId)) { 701 mDirtyUserIds.add(userId); 702 } 703 } 704 // If already scheduled, remove that and re-schedule in N seconds. 705 mHandler.removeCallbacks(mSaveDirtyInfoRunner); 706 mHandler.postDelayed(mSaveDirtyInfoRunner, mSaveDelayMillis); 707 } 708 709 @VisibleForTesting 710 void saveDirtyInfo() { 711 if (DEBUG) { 712 Slog.d(TAG, "saveDirtyInfo"); 713 } 714 synchronized (mLock) { 715 for (int i = mDirtyUserIds.size() - 1; i >= 0; i--) { 716 final int userId = mDirtyUserIds.get(i); 717 if (userId == UserHandle.USER_NULL) { // USER_NULL for base state. 718 saveBaseStateLocked(); 719 } else { 720 saveUserLocked(userId); 721 } 722 } 723 mDirtyUserIds.clear(); 724 } 725 } 726 727 /** Return the last reset time. */ 728 long getLastResetTimeLocked() { 729 updateTimesLocked(); 730 return mRawLastResetTime; 731 } 732 733 /** Return the next reset time. */ 734 long getNextResetTimeLocked() { 735 updateTimesLocked(); 736 return mRawLastResetTime + mResetInterval; 737 } 738 739 static boolean isClockValid(long time) { 740 return time >= 1420070400; // Thu, 01 Jan 2015 00:00:00 GMT 741 } 742 743 /** 744 * Update the last reset time. 745 */ 746 private void updateTimesLocked() { 747 748 final long now = injectCurrentTimeMillis(); 749 750 final long prevLastResetTime = mRawLastResetTime; 751 752 if (mRawLastResetTime == 0) { // first launch. 753 // TODO Randomize?? 754 mRawLastResetTime = now; 755 } else if (now < mRawLastResetTime) { 756 // Clock rewound. 757 if (isClockValid(now)) { 758 Slog.w(TAG, "Clock rewound"); 759 // TODO Randomize?? 760 mRawLastResetTime = now; 761 } 762 } else { 763 if ((mRawLastResetTime + mResetInterval) <= now) { 764 final long offset = mRawLastResetTime % mResetInterval; 765 mRawLastResetTime = ((now / mResetInterval) * mResetInterval) + offset; 766 } 767 } 768 if (prevLastResetTime != mRawLastResetTime) { 769 scheduleSaveBaseState(); 770 } 771 } 772 773 @GuardedBy("mLock") 774 @NonNull 775 boolean isUserLoadedLocked(@UserIdInt int userId) { 776 return mUsers.get(userId) != null; 777 } 778 779 /** Return the per-user state. */ 780 @GuardedBy("mLock") 781 @NonNull 782 ShortcutUser getUserShortcutsLocked(@UserIdInt int userId) { 783 ShortcutUser userPackages = mUsers.get(userId); 784 if (userPackages == null) { 785 userPackages = loadUserLocked(userId); 786 if (userPackages == null) { 787 userPackages = new ShortcutUser(userId); 788 } 789 mUsers.put(userId, userPackages); 790 } 791 return userPackages; 792 } 793 794 /** Return the per-user per-package state. */ 795 @GuardedBy("mLock") 796 @NonNull 797 ShortcutPackage getPackageShortcutsLocked( 798 @NonNull String packageName, @UserIdInt int userId) { 799 return getUserShortcutsLocked(userId).getPackageShortcuts(packageName); 800 } 801 802 @GuardedBy("mLock") 803 @NonNull 804 ShortcutLauncher getLauncherShortcuts( 805 @NonNull String packageName, @UserIdInt int userId) { 806 return getUserShortcutsLocked(userId).getLauncherShortcuts(packageName); 807 } 808 809 // === Caller validation === 810 811 void removeIcon(@UserIdInt int userId, ShortcutInfo shortcut) { 812 if (shortcut.getBitmapPath() != null) { 813 if (DEBUG) { 814 Slog.d(TAG, "Removing " + shortcut.getBitmapPath()); 815 } 816 new File(shortcut.getBitmapPath()).delete(); 817 818 shortcut.setBitmapPath(null); 819 shortcut.setIconResourceId(0); 820 shortcut.clearFlags(ShortcutInfo.FLAG_HAS_ICON_FILE | ShortcutInfo.FLAG_HAS_ICON_RES); 821 } 822 } 823 824 @VisibleForTesting 825 static class FileOutputStreamWithPath extends FileOutputStream { 826 private final File mFile; 827 828 public FileOutputStreamWithPath(File file) throws FileNotFoundException { 829 super(file); 830 mFile = file; 831 } 832 833 public File getFile() { 834 return mFile; 835 } 836 } 837 838 /** 839 * Build the cached bitmap filename for a shortcut icon. 840 * 841 * The filename will be based on the ID, except certain characters will be escaped. 842 */ 843 @VisibleForTesting 844 FileOutputStreamWithPath openIconFileForWrite(@UserIdInt int userId, ShortcutInfo shortcut) 845 throws IOException { 846 final File packagePath = new File(getUserBitmapFilePath(userId), 847 shortcut.getPackageName()); 848 if (!packagePath.isDirectory()) { 849 packagePath.mkdirs(); 850 if (!packagePath.isDirectory()) { 851 throw new IOException("Unable to create directory " + packagePath); 852 } 853 SELinux.restorecon(packagePath); 854 } 855 856 final String baseName = String.valueOf(injectCurrentTimeMillis()); 857 for (int suffix = 0;; suffix++) { 858 final String filename = (suffix == 0 ? baseName : baseName + "_" + suffix) + ".png"; 859 final File file = new File(packagePath, filename); 860 if (!file.exists()) { 861 if (DEBUG) { 862 Slog.d(TAG, "Saving icon to " + file.getAbsolutePath()); 863 } 864 return new FileOutputStreamWithPath(file); 865 } 866 } 867 } 868 869 void saveIconAndFixUpShortcut(@UserIdInt int userId, ShortcutInfo shortcut) { 870 if (shortcut.hasIconFile() || shortcut.hasIconResource()) { 871 return; 872 } 873 874 final long token = injectClearCallingIdentity(); 875 try { 876 // Clear icon info on the shortcut. 877 shortcut.setIconResourceId(0); 878 shortcut.setBitmapPath(null); 879 880 final Icon icon = shortcut.getIcon(); 881 if (icon == null) { 882 return; // has no icon 883 } 884 885 Bitmap bitmap = null; 886 try { 887 switch (icon.getType()) { 888 case Icon.TYPE_RESOURCE: { 889 injectValidateIconResPackage(shortcut, icon); 890 891 shortcut.setIconResourceId(icon.getResId()); 892 shortcut.addFlags(ShortcutInfo.FLAG_HAS_ICON_RES); 893 return; 894 } 895 case Icon.TYPE_BITMAP: { 896 bitmap = icon.getBitmap(); 897 break; 898 } 899 case Icon.TYPE_URI: { 900 final Uri uri = ContentProvider.maybeAddUserId(icon.getUri(), userId); 901 902 try (InputStream is = mContext.getContentResolver().openInputStream(uri)) { 903 904 bitmap = BitmapFactory.decodeStream(is); 905 906 } catch (IOException e) { 907 Slog.e(TAG, "Unable to load icon from " + uri); 908 return; 909 } 910 break; 911 } 912 default: 913 // This shouldn't happen because we've already validated the icon, but 914 // just in case. 915 throw ShortcutInfo.getInvalidIconException(); 916 } 917 if (bitmap == null) { 918 Slog.e(TAG, "Null bitmap detected"); 919 return; 920 } 921 // Shrink and write to the file. 922 File path = null; 923 try { 924 final FileOutputStreamWithPath out = openIconFileForWrite(userId, shortcut); 925 try { 926 path = out.getFile(); 927 928 shrinkBitmap(bitmap, mMaxIconDimension) 929 .compress(mIconPersistFormat, mIconPersistQuality, out); 930 931 shortcut.setBitmapPath(out.getFile().getAbsolutePath()); 932 shortcut.addFlags(ShortcutInfo.FLAG_HAS_ICON_FILE); 933 } finally { 934 IoUtils.closeQuietly(out); 935 } 936 } catch (IOException|RuntimeException e) { 937 // STOPSHIP Change wtf to e 938 Slog.wtf(ShortcutService.TAG, "Unable to write bitmap to file", e); 939 if (path != null && path.exists()) { 940 path.delete(); 941 } 942 } 943 } finally { 944 if (bitmap != null) { 945 bitmap.recycle(); 946 } 947 // Once saved, we won't use the original icon information, so null it out. 948 shortcut.clearIcon(); 949 } 950 } finally { 951 injectRestoreCallingIdentity(token); 952 } 953 } 954 955 // Unfortunately we can't do this check in unit tests because we fake creator package names, 956 // so override in unit tests. 957 // TODO CTS this case. 958 void injectValidateIconResPackage(ShortcutInfo shortcut, Icon icon) { 959 if (!shortcut.getPackageName().equals(icon.getResPackage())) { 960 throw new IllegalArgumentException( 961 "Icon resource must reside in shortcut owner package"); 962 } 963 } 964 965 @VisibleForTesting 966 static Bitmap shrinkBitmap(Bitmap in, int maxSize) { 967 // Original width/height. 968 final int ow = in.getWidth(); 969 final int oh = in.getHeight(); 970 if ((ow <= maxSize) && (oh <= maxSize)) { 971 if (DEBUG) { 972 Slog.d(TAG, String.format("Icon size %dx%d, no need to shrink", ow, oh)); 973 } 974 return in; 975 } 976 final int longerDimension = Math.max(ow, oh); 977 978 // New width and height. 979 final int nw = ow * maxSize / longerDimension; 980 final int nh = oh * maxSize / longerDimension; 981 if (DEBUG) { 982 Slog.d(TAG, String.format("Icon size %dx%d, shrinking to %dx%d", 983 ow, oh, nw, nh)); 984 } 985 986 final Bitmap scaledBitmap = Bitmap.createBitmap(nw, nh, Bitmap.Config.ARGB_8888); 987 final Canvas c = new Canvas(scaledBitmap); 988 989 final RectF dst = new RectF(0, 0, nw, nh); 990 991 c.drawBitmap(in, /*src=*/ null, dst, /* paint =*/ null); 992 993 in.recycle(); 994 995 return scaledBitmap; 996 } 997 998 // === Caller validation === 999 1000 private boolean isCallerSystem() { 1001 final int callingUid = injectBinderCallingUid(); 1002 return UserHandle.isSameApp(callingUid, Process.SYSTEM_UID); 1003 } 1004 1005 private boolean isCallerShell() { 1006 final int callingUid = injectBinderCallingUid(); 1007 return callingUid == Process.SHELL_UID || callingUid == Process.ROOT_UID; 1008 } 1009 1010 private void enforceSystemOrShell() { 1011 Preconditions.checkState(isCallerSystem() || isCallerShell(), 1012 "Caller must be system or shell"); 1013 } 1014 1015 private void enforceShell() { 1016 Preconditions.checkState(isCallerShell(), "Caller must be shell"); 1017 } 1018 1019 private void verifyCaller(@NonNull String packageName, @UserIdInt int userId) { 1020 Preconditions.checkStringNotEmpty(packageName, "packageName"); 1021 1022 if (isCallerSystem()) { 1023 return; // no check 1024 } 1025 1026 final int callingUid = injectBinderCallingUid(); 1027 1028 // Otherwise, make sure the arguments are valid. 1029 if (UserHandle.getUserId(callingUid) != userId) { 1030 throw new SecurityException("Invalid user-ID"); 1031 } 1032 if (injectGetPackageUid(packageName, userId) == injectBinderCallingUid()) { 1033 return; // Caller is valid. 1034 } 1035 throw new SecurityException("Caller UID= doesn't own " + packageName); 1036 } 1037 1038 // Test overrides it. 1039 int injectGetPackageUid(@NonNull String packageName, @UserIdInt int userId) { 1040 try { 1041 return mContext.getPackageManager().getPackageUidAsUser(packageName, 1042 PackageManager.MATCH_DIRECT_BOOT_AWARE 1043 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE 1044 | PackageManager.MATCH_UNINSTALLED_PACKAGES, 1045 userId); 1046 } catch (NameNotFoundException e) { 1047 return -1; 1048 } 1049 } 1050 1051 void postToHandler(Runnable r) { 1052 mHandler.post(r); 1053 } 1054 1055 /** 1056 * Throw if {@code numShortcuts} is bigger than {@link #mMaxDynamicShortcuts}. 1057 */ 1058 void enforceMaxDynamicShortcuts(int numShortcuts) { 1059 if (numShortcuts > mMaxDynamicShortcuts) { 1060 throw new IllegalArgumentException("Max number of dynamic shortcuts exceeded"); 1061 } 1062 } 1063 1064 /** 1065 * - Sends a notification to LauncherApps 1066 * - Write to file 1067 */ 1068 private void userPackageChanged(@NonNull String packageName, @UserIdInt int userId) { 1069 notifyListeners(packageName, userId); 1070 scheduleSaveUser(userId); 1071 } 1072 1073 private void notifyListeners(@NonNull String packageName, @UserIdInt int userId) { 1074 if (!mUserManager.isUserRunning(userId)) { 1075 return; 1076 } 1077 postToHandler(() -> { 1078 final ArrayList<ShortcutChangeListener> copy; 1079 synchronized (mLock) { 1080 copy = new ArrayList<>(mListeners); 1081 } 1082 // Note onShortcutChanged() needs to be called with the system service permissions. 1083 for (int i = copy.size() - 1; i >= 0; i--) { 1084 copy.get(i).onShortcutChanged(packageName, userId); 1085 } 1086 }); 1087 } 1088 1089 /** 1090 * Clean up / validate an incoming shortcut. 1091 * - Make sure all mandatory fields are set. 1092 * - Make sure the intent's extras are persistable, and them to set 1093 * {@link ShortcutInfo#mIntentPersistableExtras}. Also clear its extras. 1094 * - Clear flags. 1095 * 1096 * TODO Detailed unit tests 1097 */ 1098 private void fixUpIncomingShortcutInfo(@NonNull ShortcutInfo shortcut, boolean forUpdate) { 1099 Preconditions.checkNotNull(shortcut, "Null shortcut detected"); 1100 if (shortcut.getActivityComponent() != null) { 1101 Preconditions.checkState( 1102 shortcut.getPackageName().equals( 1103 shortcut.getActivityComponent().getPackageName()), 1104 "Activity package name mismatch"); 1105 } 1106 1107 if (!forUpdate) { 1108 shortcut.enforceMandatoryFields(); 1109 } 1110 if (shortcut.getIcon() != null) { 1111 ShortcutInfo.validateIcon(shortcut.getIcon()); 1112 } 1113 1114 validateForXml(shortcut.getId()); 1115 validateForXml(shortcut.getTitle()); 1116 validatePersistableBundleForXml(shortcut.getIntentPersistableExtras()); 1117 validatePersistableBundleForXml(shortcut.getExtras()); 1118 1119 shortcut.replaceFlags(0); 1120 } 1121 1122 // KXmlSerializer is strict and doesn't allow certain characters, so we disallow those 1123 // characters. 1124 1125 private static void validatePersistableBundleForXml(PersistableBundle b) { 1126 if (b == null || b.size() == 0) { 1127 return; 1128 } 1129 for (String key : b.keySet()) { 1130 validateForXml(key); 1131 final Object value = b.get(key); 1132 if (value == null) { 1133 continue; 1134 } else if (value instanceof String) { 1135 validateForXml((String) value); 1136 } else if (value instanceof String[]) { 1137 for (String v : (String[]) value) { 1138 validateForXml(v); 1139 } 1140 } else if (value instanceof PersistableBundle) { 1141 validatePersistableBundleForXml((PersistableBundle) value); 1142 } 1143 } 1144 } 1145 1146 private static void validateForXml(String s) { 1147 if (TextUtils.isEmpty(s)) { 1148 return; 1149 } 1150 for (int i = s.length() - 1; i >= 0; i--) { 1151 if (!isAllowedInXml(s.charAt(i))) { 1152 throw new IllegalArgumentException("Unsupported character detected in: " + s); 1153 } 1154 } 1155 } 1156 1157 private static boolean isAllowedInXml(char c) { 1158 return (c >= 0x20 && c <= 0xd7ff) || (c >= 0xe000 && c <= 0xfffd); 1159 } 1160 1161 // === APIs === 1162 1163 @Override 1164 public boolean setDynamicShortcuts(String packageName, ParceledListSlice shortcutInfoList, 1165 @UserIdInt int userId) { 1166 verifyCaller(packageName, userId); 1167 1168 final List<ShortcutInfo> newShortcuts = (List<ShortcutInfo>) shortcutInfoList.getList(); 1169 final int size = newShortcuts.size(); 1170 1171 synchronized (mLock) { 1172 final ShortcutPackage ps = getPackageShortcutsLocked(packageName, userId); 1173 1174 // Throttling. 1175 if (!ps.tryApiCall(this)) { 1176 return false; 1177 } 1178 enforceMaxDynamicShortcuts(size); 1179 1180 // Validate the shortcuts. 1181 for (int i = 0; i < size; i++) { 1182 fixUpIncomingShortcutInfo(newShortcuts.get(i), /* forUpdate= */ false); 1183 } 1184 1185 // First, remove all un-pinned; dynamic shortcuts 1186 ps.deleteAllDynamicShortcuts(this); 1187 1188 // Then, add/update all. We need to make sure to take over "pinned" flag. 1189 for (int i = 0; i < size; i++) { 1190 final ShortcutInfo newShortcut = newShortcuts.get(i); 1191 ps.addDynamicShortcut(this, newShortcut); 1192 } 1193 } 1194 userPackageChanged(packageName, userId); 1195 return true; 1196 } 1197 1198 @Override 1199 public boolean updateShortcuts(String packageName, ParceledListSlice shortcutInfoList, 1200 @UserIdInt int userId) { 1201 verifyCaller(packageName, userId); 1202 1203 final List<ShortcutInfo> newShortcuts = (List<ShortcutInfo>) shortcutInfoList.getList(); 1204 final int size = newShortcuts.size(); 1205 1206 synchronized (mLock) { 1207 final ShortcutPackage ps = getPackageShortcutsLocked(packageName, userId); 1208 1209 // Throttling. 1210 if (!ps.tryApiCall(this)) { 1211 return false; 1212 } 1213 1214 for (int i = 0; i < size; i++) { 1215 final ShortcutInfo source = newShortcuts.get(i); 1216 fixUpIncomingShortcutInfo(source, /* forUpdate= */ true); 1217 1218 final ShortcutInfo target = ps.findShortcutById(source.getId()); 1219 if (target != null) { 1220 final boolean replacingIcon = (source.getIcon() != null); 1221 if (replacingIcon) { 1222 removeIcon(userId, target); 1223 } 1224 1225 target.copyNonNullFieldsFrom(source); 1226 1227 if (replacingIcon) { 1228 saveIconAndFixUpShortcut(userId, target); 1229 } 1230 } 1231 } 1232 } 1233 userPackageChanged(packageName, userId); 1234 1235 return true; 1236 } 1237 1238 @Override 1239 public boolean addDynamicShortcut(String packageName, ShortcutInfo newShortcut, 1240 @UserIdInt int userId) { 1241 verifyCaller(packageName, userId); 1242 1243 synchronized (mLock) { 1244 final ShortcutPackage ps = getPackageShortcutsLocked(packageName, userId); 1245 1246 // Throttling. 1247 if (!ps.tryApiCall(this)) { 1248 return false; 1249 } 1250 1251 // Validate the shortcut. 1252 fixUpIncomingShortcutInfo(newShortcut, /* forUpdate= */ false); 1253 1254 // Add it. 1255 ps.addDynamicShortcut(this, newShortcut); 1256 } 1257 userPackageChanged(packageName, userId); 1258 1259 return true; 1260 } 1261 1262 @Override 1263 public void deleteDynamicShortcut(String packageName, String shortcutId, 1264 @UserIdInt int userId) { 1265 verifyCaller(packageName, userId); 1266 Preconditions.checkStringNotEmpty(shortcutId, "shortcutId must be provided"); 1267 1268 synchronized (mLock) { 1269 getPackageShortcutsLocked(packageName, userId).deleteDynamicWithId(this, shortcutId); 1270 } 1271 userPackageChanged(packageName, userId); 1272 } 1273 1274 @Override 1275 public void deleteAllDynamicShortcuts(String packageName, @UserIdInt int userId) { 1276 verifyCaller(packageName, userId); 1277 1278 synchronized (mLock) { 1279 getPackageShortcutsLocked(packageName, userId).deleteAllDynamicShortcuts(this); 1280 } 1281 userPackageChanged(packageName, userId); 1282 } 1283 1284 @Override 1285 public ParceledListSlice<ShortcutInfo> getDynamicShortcuts(String packageName, 1286 @UserIdInt int userId) { 1287 verifyCaller(packageName, userId); 1288 synchronized (mLock) { 1289 return getShortcutsWithQueryLocked( 1290 packageName, userId, ShortcutInfo.CLONE_REMOVE_FOR_CREATOR, 1291 ShortcutInfo::isDynamic); 1292 } 1293 } 1294 1295 @Override 1296 public ParceledListSlice<ShortcutInfo> getPinnedShortcuts(String packageName, 1297 @UserIdInt int userId) { 1298 verifyCaller(packageName, userId); 1299 synchronized (mLock) { 1300 return getShortcutsWithQueryLocked( 1301 packageName, userId, ShortcutInfo.CLONE_REMOVE_FOR_CREATOR, 1302 ShortcutInfo::isPinned); 1303 } 1304 } 1305 1306 private ParceledListSlice<ShortcutInfo> getShortcutsWithQueryLocked(@NonNull String packageName, 1307 @UserIdInt int userId, int cloneFlags, @NonNull Predicate<ShortcutInfo> query) { 1308 1309 final ArrayList<ShortcutInfo> ret = new ArrayList<>(); 1310 1311 getPackageShortcutsLocked(packageName, userId).findAll(this, ret, query, cloneFlags, 1312 /* callingLauncher= */ null); 1313 1314 return new ParceledListSlice<>(ret); 1315 } 1316 1317 @Override 1318 public int getMaxDynamicShortcutCount(String packageName, @UserIdInt int userId) 1319 throws RemoteException { 1320 verifyCaller(packageName, userId); 1321 1322 return mMaxDynamicShortcuts; 1323 } 1324 1325 @Override 1326 public int getRemainingCallCount(String packageName, @UserIdInt int userId) { 1327 verifyCaller(packageName, userId); 1328 1329 synchronized (mLock) { 1330 return mMaxDailyUpdates 1331 - getPackageShortcutsLocked(packageName, userId).getApiCallCount(this); 1332 } 1333 } 1334 1335 @Override 1336 public long getRateLimitResetTime(String packageName, @UserIdInt int userId) { 1337 verifyCaller(packageName, userId); 1338 1339 synchronized (mLock) { 1340 return getNextResetTimeLocked(); 1341 } 1342 } 1343 1344 @Override 1345 public int getIconMaxDimensions(String packageName, int userId) throws RemoteException { 1346 synchronized (mLock) { 1347 return mMaxIconDimension; 1348 } 1349 } 1350 1351 /** 1352 * Reset all throttling, for developer options and command line. Only system/shell can call it. 1353 */ 1354 @Override 1355 public void resetThrottling() { 1356 enforceSystemOrShell(); 1357 1358 resetThrottlingInner(getCallingUserId()); 1359 } 1360 1361 void resetThrottlingInner(@UserIdInt int userId) { 1362 synchronized (mLock) { 1363 getUserShortcutsLocked(userId).resetThrottling(); 1364 } 1365 scheduleSaveUser(userId); 1366 Slog.i(TAG, "ShortcutManager: throttling counter reset"); 1367 } 1368 1369 // We override this method in unit tests to do a simpler check. 1370 boolean hasShortcutHostPermission(@NonNull String callingPackage, int userId) { 1371 return hasShortcutHostPermissionInner(callingPackage, userId); 1372 } 1373 1374 // This method is extracted so we can directly call this method from unit tests, 1375 // even when hasShortcutPermission() is overridden. 1376 @VisibleForTesting 1377 boolean hasShortcutHostPermissionInner(@NonNull String callingPackage, int userId) { 1378 synchronized (mLock) { 1379 long start = 0; 1380 if (DEBUG) { 1381 start = System.currentTimeMillis(); 1382 } 1383 1384 final ShortcutUser user = getUserShortcutsLocked(userId); 1385 1386 final List<ResolveInfo> allHomeCandidates = new ArrayList<>(); 1387 1388 // Default launcher from package manager. 1389 final ComponentName defaultLauncher = injectPackageManagerInternal() 1390 .getHomeActivitiesAsUser(allHomeCandidates, userId); 1391 1392 ComponentName detected; 1393 if (defaultLauncher != null) { 1394 detected = defaultLauncher; 1395 if (DEBUG) { 1396 Slog.v(TAG, "Default launcher from PM: " + detected); 1397 } 1398 } else { 1399 detected = user.getLauncherComponent(); 1400 1401 // TODO: Make sure it's still enabled. 1402 if (DEBUG) { 1403 Slog.v(TAG, "Cached launcher: " + detected); 1404 } 1405 } 1406 1407 if (detected == null) { 1408 // If we reach here, that means it's the first check since the user was created, 1409 // and there's already multiple launchers and there's no default set. 1410 // Find the system one with the highest priority. 1411 // (We need to check the priority too because of FallbackHome in Settings.) 1412 // If there's no system launcher yet, then no one can access shortcuts, until 1413 // the user explicitly 1414 final int size = allHomeCandidates.size(); 1415 1416 int lastPriority = Integer.MIN_VALUE; 1417 for (int i = 0; i < size; i++) { 1418 final ResolveInfo ri = allHomeCandidates.get(i); 1419 if (!ri.activityInfo.applicationInfo.isSystemApp()) { 1420 continue; 1421 } 1422 if (DEBUG) { 1423 Slog.d(TAG, String.format("hasShortcutPermissionInner: pkg=%s prio=%d", 1424 ri.activityInfo.getComponentName(), ri.priority)); 1425 } 1426 if (ri.priority < lastPriority) { 1427 continue; 1428 } 1429 detected = ri.activityInfo.getComponentName(); 1430 lastPriority = ri.priority; 1431 } 1432 } 1433 if (DEBUG) { 1434 long end = System.currentTimeMillis(); 1435 Slog.v(TAG, String.format("hasShortcutPermission took %d ms", end - start)); 1436 } 1437 if (detected != null) { 1438 if (DEBUG) { 1439 Slog.v(TAG, "Detected launcher: " + detected); 1440 } 1441 user.setLauncherComponent(this, detected); 1442 return detected.getPackageName().equals(callingPackage); 1443 } else { 1444 // Default launcher not found. 1445 return false; 1446 } 1447 } 1448 } 1449 1450 // === House keeping === 1451 1452 @VisibleForTesting 1453 void cleanUpPackageLocked(String packageName, int userId) { 1454 final boolean wasUserLoaded = isUserLoadedLocked(userId); 1455 1456 final ShortcutUser mUser = getUserShortcutsLocked(userId); 1457 boolean doNotify = false; 1458 1459 // First, remove the package from the package list (if the package is a publisher). 1460 if (mUser.getPackages().remove(packageName) != null) { 1461 doNotify = true; 1462 } 1463 // Also remove from the launcher list (if the package is a launcher). 1464 mUser.getLaunchers().remove(packageName); 1465 1466 // Then remove pinned shortcuts from all launchers. 1467 for (int i = mUser.getLaunchers().size() - 1; i >= 0; i--) { 1468 mUser.getLaunchers().valueAt(i).cleanUpPackage(packageName); 1469 } 1470 // Now there may be orphan shortcuts because we removed pinned shortucts at the previous 1471 // step. Remove them too. 1472 for (int i = mUser.getPackages().size() - 1; i >= 0; i--) { 1473 mUser.getPackages().valueAt(i).refreshPinnedFlags(this); 1474 } 1475 1476 scheduleSaveUser(userId); 1477 1478 if (doNotify) { 1479 notifyListeners(packageName, userId); 1480 } 1481 1482 if (!wasUserLoaded) { 1483 // Note this will execute the scheduled save. 1484 unloadUserLocked(userId); 1485 } 1486 } 1487 1488 /** 1489 * Entry point from {@link LauncherApps}. 1490 */ 1491 private class LocalService extends ShortcutServiceInternal { 1492 @Override 1493 public List<ShortcutInfo> getShortcuts( 1494 @NonNull String callingPackage, long changedSince, 1495 @Nullable String packageName, @Nullable ComponentName componentName, 1496 int queryFlags, int userId) { 1497 final ArrayList<ShortcutInfo> ret = new ArrayList<>(); 1498 final int cloneFlag = 1499 ((queryFlags & ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY) == 0) 1500 ? ShortcutInfo.CLONE_REMOVE_FOR_LAUNCHER 1501 : ShortcutInfo.CLONE_REMOVE_NON_KEY_INFO; 1502 1503 synchronized (mLock) { 1504 if (packageName != null) { 1505 getShortcutsInnerLocked( 1506 callingPackage, packageName, changedSince, 1507 componentName, queryFlags, userId, ret, cloneFlag); 1508 } else { 1509 final ArrayMap<String, ShortcutPackage> packages = 1510 getUserShortcutsLocked(userId).getPackages(); 1511 for (int i = packages.size() - 1; i >= 0; i--) { 1512 getShortcutsInnerLocked( 1513 callingPackage, packages.keyAt(i), changedSince, 1514 componentName, queryFlags, userId, ret, cloneFlag); 1515 } 1516 } 1517 } 1518 return ret; 1519 } 1520 1521 private void getShortcutsInnerLocked(@NonNull String callingPackage, 1522 @Nullable String packageName,long changedSince, 1523 @Nullable ComponentName componentName, int queryFlags, 1524 int userId, ArrayList<ShortcutInfo> ret, int cloneFlag) { 1525 getPackageShortcutsLocked(packageName, userId).findAll(ShortcutService.this, ret, 1526 (ShortcutInfo si) -> { 1527 if (si.getLastChangedTimestamp() < changedSince) { 1528 return false; 1529 } 1530 if (componentName != null 1531 && !componentName.equals(si.getActivityComponent())) { 1532 return false; 1533 } 1534 final boolean matchDynamic = 1535 ((queryFlags & ShortcutQuery.FLAG_GET_DYNAMIC) != 0) 1536 && si.isDynamic(); 1537 final boolean matchPinned = 1538 ((queryFlags & ShortcutQuery.FLAG_GET_PINNED) != 0) 1539 && si.isPinned(); 1540 return matchDynamic || matchPinned; 1541 }, cloneFlag, callingPackage); 1542 } 1543 1544 @Override 1545 public List<ShortcutInfo> getShortcutInfo( 1546 @NonNull String callingPackage, 1547 @NonNull String packageName, @Nullable List<String> ids, int userId) { 1548 // Calling permission must be checked by LauncherAppsImpl. 1549 Preconditions.checkStringNotEmpty(packageName, "packageName"); 1550 1551 final ArrayList<ShortcutInfo> ret = new ArrayList<>(ids.size()); 1552 final ArraySet<String> idSet = new ArraySet<>(ids); 1553 synchronized (mLock) { 1554 getPackageShortcutsLocked(packageName, userId).findAll( 1555 ShortcutService.this, ret, 1556 (ShortcutInfo si) -> idSet.contains(si.getId()), 1557 ShortcutInfo.CLONE_REMOVE_FOR_LAUNCHER, callingPackage); 1558 } 1559 return ret; 1560 } 1561 1562 @Override 1563 public void pinShortcuts(@NonNull String callingPackage, @NonNull String packageName, 1564 @NonNull List<String> shortcutIds, int userId) { 1565 // Calling permission must be checked by LauncherAppsImpl. 1566 Preconditions.checkStringNotEmpty(packageName, "packageName"); 1567 Preconditions.checkNotNull(shortcutIds, "shortcutIds"); 1568 1569 synchronized (mLock) { 1570 getLauncherShortcuts(callingPackage, userId).pinShortcuts( 1571 ShortcutService.this, packageName, shortcutIds); 1572 } 1573 userPackageChanged(packageName, userId); 1574 } 1575 1576 @Override 1577 public Intent createShortcutIntent(@NonNull String callingPackage, 1578 @NonNull String packageName, @NonNull String shortcutId, int userId) { 1579 // Calling permission must be checked by LauncherAppsImpl. 1580 Preconditions.checkStringNotEmpty(packageName, "packageName can't be empty"); 1581 Preconditions.checkStringNotEmpty(shortcutId, "shortcutId can't be empty"); 1582 1583 synchronized (mLock) { 1584 final ShortcutInfo fullShortcut = 1585 getPackageShortcutsLocked(packageName, userId) 1586 .findShortcutById(shortcutId); 1587 return fullShortcut == null ? null : fullShortcut.getIntent(); 1588 } 1589 } 1590 1591 @Override 1592 public void addListener(@NonNull ShortcutChangeListener listener) { 1593 synchronized (mLock) { 1594 mListeners.add(Preconditions.checkNotNull(listener)); 1595 } 1596 } 1597 1598 @Override 1599 public int getShortcutIconResId(@NonNull String callingPackage, 1600 @NonNull ShortcutInfo shortcut, int userId) { 1601 Preconditions.checkNotNull(shortcut, "shortcut"); 1602 1603 synchronized (mLock) { 1604 final ShortcutInfo shortcutInfo = getPackageShortcutsLocked( 1605 shortcut.getPackageName(), userId).findShortcutById(shortcut.getId()); 1606 return (shortcutInfo != null && shortcutInfo.hasIconResource()) 1607 ? shortcutInfo.getIconResourceId() : 0; 1608 } 1609 } 1610 1611 @Override 1612 public ParcelFileDescriptor getShortcutIconFd(@NonNull String callingPackage, 1613 @NonNull ShortcutInfo shortcutIn, int userId) { 1614 Preconditions.checkNotNull(shortcutIn, "shortcut"); 1615 1616 synchronized (mLock) { 1617 final ShortcutInfo shortcutInfo = getPackageShortcutsLocked( 1618 shortcutIn.getPackageName(), userId).findShortcutById(shortcutIn.getId()); 1619 if (shortcutInfo == null || !shortcutInfo.hasIconFile()) { 1620 return null; 1621 } 1622 try { 1623 if (shortcutInfo.getBitmapPath() == null) { 1624 Slog.w(TAG, "null bitmap detected in getShortcutIconFd()"); 1625 return null; 1626 } 1627 return ParcelFileDescriptor.open( 1628 new File(shortcutInfo.getBitmapPath()), 1629 ParcelFileDescriptor.MODE_READ_ONLY); 1630 } catch (FileNotFoundException e) { 1631 Slog.e(TAG, "Icon file not found: " + shortcutInfo.getBitmapPath()); 1632 return null; 1633 } 1634 } 1635 } 1636 1637 @Override 1638 public boolean hasShortcutHostPermission(@NonNull String callingPackage, int userId) { 1639 return ShortcutService.this.hasShortcutHostPermission(callingPackage, userId); 1640 } 1641 } 1642 1643 private PackageMonitor mPackageMonitor = new PackageMonitor() { 1644 @Override 1645 public void onPackageUpdateFinished(String packageName, int uid) { 1646 handlePackageUpdateFinished(packageName, getChangingUserId()); 1647 } 1648 1649 @Override 1650 public void onPackageRemoved(String packageName, int uid) { 1651 handlePackageRemoved(packageName, getChangingUserId()); 1652 } 1653 1654 @Override 1655 public void onPackageRemovedAllUsers(String packageName, int uid) { 1656 handlePackageRemovedAllUsers(packageName, getChangingUserId()); 1657 } 1658 }; 1659 1660 void handlePackageUpdateFinished(String packageName, @UserIdInt int userId) { 1661 if (DEBUG) { 1662 Slog.d(TAG, "onPackageUpdateFinished() userId=" + userId); 1663 } 1664 // TODO Update the version. 1665 } 1666 1667 void handlePackageRemoved(String packageName, @UserIdInt int userId) { 1668 if (DEBUG) { 1669 Slog.d(TAG, "onPackageRemoved() userId=" + userId); 1670 } 1671 synchronized (mLock) { 1672 cleanUpPackageLocked(packageName, userId); 1673 } 1674 } 1675 1676 void handlePackageRemovedAllUsers(String packageName, @UserIdInt int userId) { 1677 if (DEBUG) { 1678 Slog.d(TAG, "onPackageRemovedAllUsers() userId=" + userId); 1679 } 1680 synchronized (mLock) { 1681 cleanUpPackageLocked(packageName, userId); 1682 } 1683 1684 // TODO Remove from all users, which we can't if the user is locked. 1685 } 1686 1687 // === Dump === 1688 1689 @Override 1690 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 1691 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 1692 != PackageManager.PERMISSION_GRANTED) { 1693 pw.println("Permission Denial: can't dump UserManager from from pid=" 1694 + Binder.getCallingPid() 1695 + ", uid=" + Binder.getCallingUid() 1696 + " without permission " 1697 + android.Manifest.permission.DUMP); 1698 return; 1699 } 1700 dumpInner(pw); 1701 } 1702 1703 @VisibleForTesting 1704 void dumpInner(PrintWriter pw) { 1705 synchronized (mLock) { 1706 final long now = injectCurrentTimeMillis(); 1707 pw.print("Now: ["); 1708 pw.print(now); 1709 pw.print("] "); 1710 pw.print(formatTime(now)); 1711 1712 pw.print(" Raw last reset: ["); 1713 pw.print(mRawLastResetTime); 1714 pw.print("] "); 1715 pw.print(formatTime(mRawLastResetTime)); 1716 1717 final long last = getLastResetTimeLocked(); 1718 pw.print(" Last reset: ["); 1719 pw.print(last); 1720 pw.print("] "); 1721 pw.print(formatTime(last)); 1722 1723 final long next = getNextResetTimeLocked(); 1724 pw.print(" Next reset: ["); 1725 pw.print(next); 1726 pw.print("] "); 1727 pw.print(formatTime(next)); 1728 pw.println(); 1729 1730 pw.print(" Max icon dim: "); 1731 pw.print(mMaxIconDimension); 1732 pw.print(" Icon format: "); 1733 pw.print(mIconPersistFormat); 1734 pw.print(" Icon quality: "); 1735 pw.print(mIconPersistQuality); 1736 pw.println(); 1737 1738 1739 for (int i = 0; i < mUsers.size(); i++) { 1740 pw.println(); 1741 mUsers.valueAt(i).dump(this, pw, " "); 1742 } 1743 } 1744 } 1745 1746 static String formatTime(long time) { 1747 Time tobj = new Time(); 1748 tobj.set(time); 1749 return tobj.format("%Y-%m-%d %H:%M:%S"); 1750 } 1751 1752 // === Shell support === 1753 1754 @Override 1755 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, 1756 String[] args, ResultReceiver resultReceiver) throws RemoteException { 1757 1758 enforceShell(); 1759 1760 (new MyShellCommand()).exec(this, in, out, err, args, resultReceiver); 1761 } 1762 1763 static class CommandException extends Exception { 1764 public CommandException(String message) { 1765 super(message); 1766 } 1767 } 1768 1769 /** 1770 * Handle "adb shell cmd". 1771 */ 1772 private class MyShellCommand extends ShellCommand { 1773 1774 private int mUserId = UserHandle.USER_SYSTEM; 1775 1776 private void parseOptions(boolean takeUser) 1777 throws CommandException { 1778 String opt; 1779 while ((opt = getNextOption()) != null) { 1780 switch (opt) { 1781 case "--user": 1782 if (takeUser) { 1783 mUserId = UserHandle.parseUserArg(getNextArgRequired()); 1784 break; 1785 } 1786 // fallthrough 1787 default: 1788 throw new CommandException("Unknown option: " + opt); 1789 } 1790 } 1791 } 1792 1793 @Override 1794 public int onCommand(String cmd) { 1795 if (cmd == null) { 1796 return handleDefaultCommands(cmd); 1797 } 1798 final PrintWriter pw = getOutPrintWriter(); 1799 try { 1800 switch (cmd) { 1801 case "reset-package-throttling": 1802 handleResetPackageThrottling(); 1803 break; 1804 case "reset-throttling": 1805 handleResetThrottling(); 1806 break; 1807 case "override-config": 1808 handleOverrideConfig(); 1809 break; 1810 case "reset-config": 1811 handleResetConfig(); 1812 break; 1813 case "clear-default-launcher": 1814 handleClearDefaultLauncher(); 1815 break; 1816 case "get-default-launcher": 1817 handleGetDefaultLauncher(); 1818 break; 1819 case "refresh-default-launcher": 1820 handleRefreshDefaultLauncher(); 1821 break; 1822 default: 1823 return handleDefaultCommands(cmd); 1824 } 1825 } catch (CommandException e) { 1826 pw.println("Error: " + e.getMessage()); 1827 return 1; 1828 } 1829 pw.println("Success"); 1830 return 0; 1831 } 1832 1833 @Override 1834 public void onHelp() { 1835 final PrintWriter pw = getOutPrintWriter(); 1836 pw.println("Usage: cmd shortcut COMMAND [options ...]"); 1837 pw.println(); 1838 pw.println("cmd shortcut reset-package-throttling [--user USER_ID] PACKAGE"); 1839 pw.println(" Reset throttling for a package"); 1840 pw.println(); 1841 pw.println("cmd shortcut reset-throttling"); 1842 pw.println(" Reset throttling for all packages and users"); 1843 pw.println(); 1844 pw.println("cmd shortcut override-config CONFIG"); 1845 pw.println(" Override the configuration for testing (will last until reboot)"); 1846 pw.println(); 1847 pw.println("cmd shortcut reset-config"); 1848 pw.println(" Reset the configuration set with \"update-config\""); 1849 pw.println(); 1850 pw.println("cmd shortcut clear-default-launcher [--user USER_ID]"); 1851 pw.println(" Clear the cached default launcher"); 1852 pw.println(); 1853 pw.println("cmd shortcut get-default-launcher [--user USER_ID]"); 1854 pw.println(" Show the cached default launcher"); 1855 pw.println(); 1856 pw.println("cmd shortcut refresh-default-launcher [--user USER_ID]"); 1857 pw.println(" Refresh the cached default launcher"); 1858 pw.println(); 1859 } 1860 1861 private int handleResetThrottling() throws CommandException { 1862 parseOptions(/* takeUser =*/ true); 1863 1864 resetThrottlingInner(mUserId); 1865 return 0; 1866 } 1867 1868 private void handleResetPackageThrottling() throws CommandException { 1869 parseOptions(/* takeUser =*/ true); 1870 1871 final String packageName = getNextArgRequired(); 1872 1873 synchronized (mLock) { 1874 getPackageShortcutsLocked(packageName, mUserId).resetRateLimitingForCommandLine(); 1875 saveUserLocked(mUserId); 1876 } 1877 } 1878 1879 private void handleOverrideConfig() throws CommandException { 1880 final String config = getNextArgRequired(); 1881 1882 synchronized (mLock) { 1883 if (!updateConfigurationLocked(config)) { 1884 throw new CommandException("override-config failed. See logcat for details."); 1885 } 1886 } 1887 } 1888 1889 private void handleResetConfig() { 1890 synchronized (mLock) { 1891 loadConfigurationLocked(); 1892 } 1893 } 1894 1895 private void clearLauncher() { 1896 synchronized (mLock) { 1897 getUserShortcutsLocked(mUserId).setLauncherComponent( 1898 ShortcutService.this, null); 1899 } 1900 } 1901 1902 private void showLauncher() { 1903 synchronized (mLock) { 1904 // This ensures to set the cached launcher. Package name doesn't matter. 1905 hasShortcutHostPermissionInner("-", mUserId); 1906 1907 getOutPrintWriter().println("Launcher: " 1908 + getUserShortcutsLocked(mUserId).getLauncherComponent()); 1909 } 1910 } 1911 1912 private void handleClearDefaultLauncher() throws CommandException { 1913 parseOptions(/* takeUser =*/ true); 1914 1915 clearLauncher(); 1916 } 1917 1918 private void handleGetDefaultLauncher() throws CommandException { 1919 parseOptions(/* takeUser =*/ true); 1920 1921 showLauncher(); 1922 } 1923 1924 private void handleRefreshDefaultLauncher() throws CommandException { 1925 parseOptions(/* takeUser =*/ true); 1926 1927 clearLauncher(); 1928 showLauncher(); 1929 } 1930 } 1931 1932 // === Unit test support === 1933 1934 // Injection point. 1935 @VisibleForTesting 1936 long injectCurrentTimeMillis() { 1937 return System.currentTimeMillis(); 1938 } 1939 1940 // Injection point. 1941 @VisibleForTesting 1942 int injectBinderCallingUid() { 1943 return getCallingUid(); 1944 } 1945 1946 private int getCallingUserId() { 1947 return UserHandle.getUserId(injectBinderCallingUid()); 1948 } 1949 1950 // Injection point. 1951 @VisibleForTesting 1952 long injectClearCallingIdentity() { 1953 return Binder.clearCallingIdentity(); 1954 } 1955 1956 // Injection point. 1957 @VisibleForTesting 1958 void injectRestoreCallingIdentity(long token) { 1959 Binder.restoreCallingIdentity(token); 1960 } 1961 1962 final void wtf(String message) { 1963 Slog.wtf(TAG, message, /* exception= */ null); 1964 } 1965 1966 void wtf(String message, Exception e) { 1967 Slog.wtf(TAG, message, e); 1968 } 1969 1970 @VisibleForTesting 1971 File injectSystemDataPath() { 1972 return Environment.getDataSystemDirectory(); 1973 } 1974 1975 @VisibleForTesting 1976 File injectUserDataPath(@UserIdInt int userId) { 1977 return new File(Environment.getDataSystemCeDirectory(userId), DIRECTORY_PER_USER); 1978 } 1979 1980 @VisibleForTesting 1981 boolean injectIsLowRamDevice() { 1982 return ActivityManager.isLowRamDeviceStatic(); 1983 } 1984 1985 @VisibleForTesting 1986 PackageManagerInternal injectPackageManagerInternal() { 1987 return mPackageManagerInternal; 1988 } 1989 1990 @VisibleForTesting 1991 File getUserBitmapFilePath(@UserIdInt int userId) { 1992 return new File(injectUserDataPath(userId), DIRECTORY_BITMAPS); 1993 } 1994 1995 @VisibleForTesting 1996 SparseArray<ShortcutUser> getShortcutsForTest() { 1997 return mUsers; 1998 } 1999 2000 @VisibleForTesting 2001 int getMaxDynamicShortcutsForTest() { 2002 return mMaxDynamicShortcuts; 2003 } 2004 2005 @VisibleForTesting 2006 int getMaxDailyUpdatesForTest() { 2007 return mMaxDailyUpdates; 2008 } 2009 2010 @VisibleForTesting 2011 long getResetIntervalForTest() { 2012 return mResetInterval; 2013 } 2014 2015 @VisibleForTesting 2016 int getMaxIconDimensionForTest() { 2017 return mMaxIconDimension; 2018 } 2019 2020 @VisibleForTesting 2021 CompressFormat getIconPersistFormatForTest() { 2022 return mIconPersistFormat; 2023 } 2024 2025 @VisibleForTesting 2026 int getIconPersistQualityForTest() { 2027 return mIconPersistQuality; 2028 } 2029 2030 @VisibleForTesting 2031 ShortcutInfo getPackageShortcutForTest(String packageName, String shortcutId, int userId) { 2032 synchronized (mLock) { 2033 final ShortcutUser user = mUsers.get(userId); 2034 if (user == null) return null; 2035 2036 final ShortcutPackage pkg = user.getPackages().get(packageName); 2037 if (pkg == null) return null; 2038 2039 return pkg.findShortcutById(shortcutId); 2040 } 2041 } 2042} 2043