07 · Launcher3 快捷方式与小组件 长按是 Launcher 的核心二级交互。本文拆两条主线:长按应用图标弹出的快捷菜单(Deep Shortcuts + System Shortcuts),以及添加小组件到桌面的端到端流程(Picker → 拖拽 → 绑定 → 调整大小)。两条线共用同一套 DragController 拖拽基础设施,差别只在数据来源与落位处理。
源码基线:packages/apps/Launcher3/src/com/android/launcher3/ 关键目录:popup/、shortcuts/、widget/、widget/picker/、widgetpicker/、AppWidgetResizeFrame.kt
Part 1 · 快捷方式(Deep Shortcuts) 一、整体参与角色 长按应用图标弹出的带箭头浮层,由一条职责清晰的链路协作完成:
角色
文件
职责
ItemLongClickListener
touch/ItemLongClickListener
全局长按入口,分发到 PopupController
PopupControllerForAppIcon
popup/PopupControllerAppIcons.kt
入口控制器:决定弹不弹、收集数据、建容器
PopupContainerWithArrow
popup/PopupContainerWithArrow.kt
浮层 UI 容器(LinearLayout + AbstractFloatingView),承载 deep + system shortcut
PopupPopulator
popup/PopupPopulator.java
后台线程查询/排序/过滤 deep shortcuts
ShortcutRequest
shortcuts/ShortcutRequest.java
封装 LauncherApps.getShortcuts() 的流式查询
PopupDataProvider
popup/PopupDataProvider.java
从 BgDataModel.deepShortcutMap 读 shortcut 数量、管理通知小红点
SystemShortcut
popup/SystemShortcut.java
系统级菜单项(抽象类 + 内部子类),既是数据又是点击行为
DeepShortcutView
shortcuts/DeepShortcutView.java
单条 deep shortcut 的 View(图标 + 文字 + 加号按钮)
PopupItemDragHandler
popup/PopupItemDragHandler.kt
从 popup 里长按拖出 shortcut 到桌面
长按的真正入口是图标的 OnLongClickListener。BubbleTextView 注册了 ItemLongClickListener,它判断当前是否处于可拖拽状态(非编辑锁定、无正在进行的拖拽),然后交给 PopupControllerForAppIcon.show()。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 override fun show (view: View ) : Popup? { val icon = view as BubbleTextView val launcher = Launcher.getLauncher(icon.context) if (PopupContainer.getOpen(launcher) != null ) { icon.clearFocus() return null } val item = icon.tag as ItemInfo if (!ShortcutUtil.supportsShortcuts(item)) return null val popupDataProvider = launcher.activityComponent.popupDataProvider val deepShortcutCount = popupDataProvider.getShortcutCountForItem(item) val systemShortcuts = launcher.getSupportedShortcuts(item) .map { s -> s.getShortcut(launcher, item, icon) } .filter { it != null } .collect(Collectors.toList()) val container = PopupContainerWithArrow.create<Launcher>(launcher, icon, item) container.configureForLauncher(launcher, item) container.populateAndShowRows( deepShortcutCount, if (view.showingMinimalPopup) emptyList() else systemShortcuts ) launcher.refreshAndBindWidgetsForPackageUser(PackageUserKey.fromItemInfo(item)) container.requestFocus() return container }
设计要点拆解:
重入保护 :PopupContainer.getOpen(launcher) != null 保证同一时刻只有一个浮层。已弹出的情况下,长按另一个图标只清焦点不弹新浮层。
数量来自缓存 :getShortcutCountForItem 读的是 BgDataModel.deepShortcutMap,这是 LauncherModel 在后台预建的缓存,避免每次长按都同步跨进程查询。
系统菜单项按容器过滤 :getSupportedShortcuts 根据 itemInfo.container 返回不同组合:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public Stream<SystemShortcut.Factory> getSupportedShortcuts(ItemInfo itemInfo) { int container = itemInfo.container; if (container == CONTAINER_DESKTOP || container == CONTAINER_HOTSEAT) { return Stream.of(APP_INFO, WIDGETS, INSTALL, REMOVE); } else if (container == CONTAINER_ALL_APPS || container == CONTAINER_ALL_APPS_PREDICTION) { boolean isPinnable = itemInfo instanceof ItemInfoWithIcon info && (info.runtimeStatusFlags & FLAG_NOT_PINNABLE) == 0 ; if (isPinnable) { return Stream.of(APP_INFO, WIDGETS, INSTALL, ADD_TO_HOME_SCREEN); } else { return Stream.of(APP_INFO, WIDGETS, INSTALL); } } return Stream.of(APP_INFO, WIDGETS, INSTALL); }
注意这里返回的是 Factory(工厂),不是实例。每个 Factory.getShortcut(context, itemInfo, originalView) 内部还会再做条件过滤(如 WIDGETS 工厂发现该包没有任何 widget 就返回 null)。
三、容器创建与布局折叠策略 PopupContainerWithArrow.create 构造一个竖直方向的 LinearLayout,加到 DragLayer 显示。ArrowPopup(父类)负责箭头指向、贴边翻转等定位逻辑。
3.1 populateAndShowRows:先占位后填充 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 private fun populateAndShowRows ( itemInfo: ItemInfo , deepShortcutCount: Int , systemShortcuts: List <SystemShortcut <*>>, ) { containerWidth = resources.getDimensionPixelSize(R.dimen.bg_popup_item_width) if (deepShortcutCount > 0 ) { addAllShortcuts(deepShortcutCount, systemShortcuts) } else if (systemShortcuts.isNotEmpty()) { addSystemShortcuts( systemShortcuts, R.layout.system_shortcut_rows_container, R.layout.system_shortcut, ) } show() loadAppShortcuts(itemInfo) }
show() 在数据还没回来时就执行,UI 先显示空行 + 系统菜单项,然后 loadAppShortcuts 把真实数据填充丢到后台。
3.2 折叠阈值 SHORTCUT_COLLAPSE_THRESHOLD = 6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 private fun addAllShortcuts (deepShortcutCount: Int , systemShortcuts: List <SystemShortcut <*>>) { if (deepShortcutCount + systemShortcuts.size <= SHORTCUT_COLLAPSE_THRESHOLD ) { addSystemShortcuts(systemShortcuts, R.layout.system_shortcut_rows_container, R.layout.system_shortcut) val startingHeight = ((shortcutHeight * systemShortcuts.size) + mChildContainerMargin) addDeepShortcuts(deepShortcutCount, startingHeight) return } currentHeight = shortcutHeight + mChildContainerMargin collapseEligibleSystemShortcutsIfOverThreshold(systemShortcuts) addDeepShortcuts(deepShortcutCount, currentHeight) }
折叠的核心是 mIsCollapsible 标记。看 SystemShortcut 的构造:
1 2 3 4 5 6 public SystemShortcut (int iconResId, int labelResId, T target, ItemInfo itemInfo, View originalView, boolean isCollapsible) { mIsCollapsible = isCollapsible; }
各子类在构造时传不同的 isCollapsible:
子类
isCollapsible
说明
Widgets
false
“小组件”入口,永远占整行(最重要)
AppInfo
true(默认)
应用信息
Install
true
安装
RemoveApp
false
删除
AddToHomeScreen
false
加到桌面
BubbleShortcut
true
气泡
折叠逻辑:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 private fun collapseEligibleSystemShortcutsIfOverThreshold (systemShortcuts: List <SystemShortcut <*>>) { val collapsibleSystemShortcuts = getCollapsibleSystemShortcuts(systemShortcuts) addSystemShortcutsIconsOnly(collapsibleSystemShortcuts) containerWidth = max(containerWidth, collapsibleSystemShortcuts.size * resources.getDimensionPixelSize(R.dimen.system_shortcut_header_icon_touch_size)) val nonCollapsibleSystemShortcuts = systemShortcuts .filter { shortcut: SystemShortcut<*> -> !shortcut.mIsCollapsible } if (nonCollapsibleSystemShortcuts.isNotEmpty()) { addSystemShortcuts(nonCollapsibleSystemShortcuts, R.layout.system_shortcut_rows_container, R.layout.system_shortcut) currentHeight += ((shortcutHeight * nonCollapsibleSystemShortcuts.size) + mChildContainerMargin) } }
设计意图:总数超 6 时,把次要的(AppInfo、Install 等)压成图标行,把重要的(Widgets 入口、RemoveApp)保留为整行带文字,保证用户最常用的入口不被吞掉。
3.3 addDeepShortcuts:按 count 预占行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 private fun addDeepShortcuts (deepShortcutCount: Int , startingHeight: Float ) { var height = startingHeight deepShortcutContainer = inflateAndAdd(R.layout.deep_shortcut_container, this ) for (i in deepShortcutCount downTo 1 ) { height += shortcutHeight if (height >= (mActivityContext?.deviceProfile?.deviceProperties?.availableHeightPx ?: 0 )) break val v = inflateAndAdd<DeepShortcutView>(R.layout.deep_shortcut, deepShortcutContainer) v.layoutParams.width = containerWidth deepShortcuts.add(v) } updateHiddenShortcuts() } private fun updateHiddenShortcuts () { val total = deepShortcuts.size for (i in 0. .<total) { val view = deepShortcuts[i] view.visibility = if (i >= PopupPopulator.MAX_SHORTCUTS) GONE else VISIBLE } }
注意:deepShortcutCount 可能大于 4(缓存里是 app 发布的全部 shortcut 数),但实际只 inflate 4 个 View,多出来的在 updateHiddenShortcuts 里被 GONE 掉。这里用缓存 count 决定预占行数,但渲染时硬限制在 MAX_SHORTCUTS = 4。
面试深问 Q1:为什么 deep shortcut 数量用缓存而不是实时查询? A:实时查询要走 LauncherApps.getShortcuts(),这是跨进程 binder 调用,主线程直接查会卡顿。LauncherModel 在后台遍历所有 app 的 shortcuts 预建 BgDataModel.deepShortcutMap(ComponentKey → count),长按时只读这个内存 map,零阻塞。真实数据(标题/图标/rank)才在 PopupPopulator 里异步加载。
Q2:折叠阈值 6 是怎么定的? A:经验值。手机竖屏下 popup 大约能舒适显示 6 行(系统项 + deep 项合计)。超过就压缩,把可折叠的系统项变成图标行,省出垂直空间给 deep shortcut。不可折叠项(Widgets、RemoveApp)强制占整行,因为它们是高频入口。
Q3:deepShortcutCount 大于 4 时会发生什么? A:addDeepShortcuts 会 inflate 出 count 个 View,但 updateHiddenShortcuts 把索引 ≥ MAX_SHORTCUTS(4) 的设为 GONE。同时 PopupPopulator.sortAndFilterShortcuts 在后台查询后也只返回排序后的前 4 条。所以用户最多看到 4 条,多余的 View 是浪费但有上限保护。
loadAppShortcuts 把真正耗时的查询丢到 MODEL_EXECUTOR(后台线程池):
1 2 3 4 5 6 7 8 9 10 11 12 13 private fun loadAppShortcuts (originalItemInfo: ItemInfo ) { accessibilityPaneTitle = context.getString(R.string.action_deep_shortcut) originalIcon.forceHideDot = true layoutTransition = LayoutTransition() Executors.MODEL_EXECUTOR.handler.postAtFrontOfQueue( PopupPopulator.createUpdateRunnable( mActivityContext, originalItemInfo, Handler(Looper.getMainLooper()), this , deepShortcuts, ) ) }
设计意图:为什么异步? LauncherApps.getShortcuts() 是跨进程调用,需要请求系统服务 system_server 查询目标 app 发布的 shortcuts,耗时不可控(可能几十毫秒)。主线程执行会卡住长按的手指反馈。所以先 show() 显示空行,后台查完再逐条回填。
4.1 createUpdateRunnable:查询 + 排序 + 回填 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public static <T extends Context & ActivityContext> Runnable createUpdateRunnable ( final T context, final ItemInfo originalInfo, final Handler uiHandler, final PopupContainerWithArrow container, final List<DeepShortcutView> shortcutViews) { final ComponentName activity = originalInfo.getTargetComponent(); final UserHandle user = originalInfo.user; final String targetPackage = originalInfo.getTargetPackage(); return () -> { ApplicationInfoWrapper infoWrapper = new ApplicationInfoWrapper (context, targetPackage, user); List<ShortcutInfo> shortcuts = new ShortcutRequest (context, user) .withContainer(activity) .query(ShortcutRequest.PUBLISHED); shortcuts = PopupPopulator.sortAndFilterShortcuts(shortcuts); IconCache cache = LauncherAppState.getInstance(context).getIconCache(); for (int i = 0 ; i < shortcuts.size() && i < shortcutViews.size(); i++) { final ShortcutInfo shortcut = shortcuts.get(i); final WorkspaceItemInfo si = new WorkspaceItemInfo (shortcut, context); cache.getShortcutIcon(si, shortcut, infoWrapper); si.rank = i; si.container = CONTAINER_SHORTCUTS; final DeepShortcutView view = shortcutViews.get(i); uiHandler.post(() -> view.applyShortcutInfo(si, shortcut, container, context)); } }; }
关键点:
query(ShortcutRequest.PUBLISHED) 只查 DYNAMIC + MANIFEST,不查 PINNED(已固定到桌面的)。
WorkspaceItemInfo 是 Launcher 内部统一的数据模型,这里把系统的 ShortcutInfo 转成它。
si.container = CONTAINER_SHORTCUTS 标记这条数据”属于快捷菜单容器”,落库时用于区分。
每条独立 uiHandler.post,配合 LayoutTransition 实现逐条淡入。
4.2 排序与过滤规则:sortAndFilterShortcuts 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 public static final int MAX_SHORTCUTS = 4 ; @VisibleForTesting static final int NUM_DYNAMIC = 2 ; private static final Comparator<ShortcutInfo> SHORTCUT_RANK_COMPARATOR = (a, b) -> { if (a.isDeclaredInManifest() && !b.isDeclaredInManifest()) { return -1 ; } if (!a.isDeclaredInManifest() && b.isDeclaredInManifest()) { return 1 ; } return Integer.compare(a.getRank(), b.getRank()); }; public static List<ShortcutInfo> sortAndFilterShortcuts (List<ShortcutInfo> shortcuts) { shortcuts.sort(SHORTCUT_RANK_COMPARATOR); if (shortcuts.size() <= MAX_SHORTCUTS) { return shortcuts; } List<ShortcutInfo> filteredShortcuts = new ArrayList <>(MAX_SHORTCUTS); int numDynamic = 0 ; int size = shortcuts.size(); for (int i = 0 ; i < size; i++) { ShortcutInfo shortcut = shortcuts.get(i); int filteredSize = filteredShortcuts.size(); if (filteredSize < MAX_SHORTCUTS) { filteredShortcuts.add(shortcut); if (shortcut.isDynamic()) { numDynamic++; } continue ; } if (shortcut.isDynamic() && numDynamic < NUM_DYNAMIC) { numDynamic++; int lastStaticIndex = filteredSize - numDynamic; filteredShortcuts.remove(lastStaticIndex); filteredShortcuts.add(shortcut); } } return filteredShortcuts; }
规则记忆:最多 4 条;manifest 静态优先在前(稳定,如”新标签页”);但保证至少 2 条是 dynamic(新鲜,如”继续播放”) 。
为什么这么排?manifest shortcut 是 app 在 AndroidManifest.xml 里静态声明的,稳定不变,适合放前面当默认入口。dynamic shortcut 是 app 运行时通过 ShortcutManager.pushDynamicShortcut 发布的,反映用户最近行为(如”继续看这个视频”),新鲜度高但可能随时变。强制保留 2 条 dynamic 避免用户看到的永远是同一组静态项。
4.3 ShortcutRequest:查询的流式封装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public static final int ALL = ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_MANIFEST | ShortcutQuery.FLAG_MATCH_PINNED; public static final int PUBLISHED = ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_MANIFEST; public static final int PINNED = ShortcutQuery.FLAG_MATCH_PINNED; private final ShortcutQuery mQuery = !WIDGETS_ENABLED ? null : new ShortcutQuery (); public ShortcutRequest withContainer (@Nullable ComponentName activity) { if (WIDGETS_ENABLED) { if (activity == null ) { mFailed = true ; } else { mQuery.setActivity(activity); } } return this ; } public QueryResult query (int flags) { if (!WIDGETS_ENABLED || mFailed) { return QueryResult.DEFAULT; } mQuery.setQueryFlags(flags); try { return new QueryResult ( mContext.getSystemService(LauncherApps.class) .getShortcuts(mQuery, mUserHandle)); } catch (SecurityException | IllegalStateException e) { FileLog.e(TAG, "Failed to query for shortcuts" , e); return QueryResult.DEFAULT; } }
QueryResult 继承 ArrayList<ShortcutInfo>,额外带 mWasSuccess 标记,区分”真的没 shortcut”和”查询失败”。
面试深问 Q1:为什么 manifest shortcut 优先于 dynamic? A:manifest 是 app 在 AndroidManifest.xml 静态声明的,稳定不变(如浏览器的”新标签页”),适合作为默认高频入口放前面。dynamic 是运行时推送的(如”继续播放某视频”),变化频繁。静态优先保证用户看到的菜单有稳定的骨架,dynamic 作为补充。
Q2:sortAndFilterShortcuts 为什么不直接取前 4 条? A:因为排序后前 4 条可能全是 static。这样用户永远看不到 dynamic(最新的)shortcut。所以加了一条约束:遍历剩余的,如果发现有 dynamic 且当前 dynamic 数 < 2,就挤掉 filtered 列表尾部的一个 static,把 dynamic 加进去。保证至少 2 条新鲜内容。
Q3:postAtFrontOfQueue 为什么不用普通 post? A:长按是高优先级交互,用户在等反馈。普通 post 排在队列尾部,如果后台有其他 Model 任务(如批量绑定)会延迟 shortcut 查询。postAtFrontOfQueue 把查询任务插到队首,尽快拿到数据回填。
五、SystemShortcut 子类体系 SystemShortcut<T> 是抽象类,**同时 extends ItemInfo 并 implements View.OnClickListener**——每条菜单项既是数据(图标 resId / label resId)又是点击行为。这种”数据即行为”的设计让菜单项可以作为一个完整的可点击 View 直接塞进容器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public abstract class SystemShortcut <T extends ActivityContext > extends ItemInfo implements View .OnClickListener { private final int mIconResId; protected final int mLabelResId; protected int mAccessibilityActionId; protected final T mTarget; protected final ItemInfo mItemInfo; protected final View mOriginalView; protected final boolean mIsCollapsible; public interface Factory <T extends ActivityContext > { @Nullable SystemShortcut<T> getShortcut (T context, ItemInfo itemInfo, @NonNull View originalView) ; } }
5.1 子类清单与职责 1 2 3 4 5 6 7 8 9 10 public static class Widgets <T> extends SystemShortcut <T> public static class AppInfo <T> extends SystemShortcut <T> public static class RemoveApp <T> extends SystemShortcut <T> public static class AddToHomeScreen <T> extends SystemShortcut <T>public static class Install <T> extends SystemShortcut <T> class InstallToPrivateProfile <T> extends SystemShortcut <T> class DontSuggestApp <T> extends SystemShortcut <T> class UninstallApp <T> extends SystemShortcut <T> public static class BubbleShortcut <T> extends SystemShortcut <T>
5.2 Factory 的过滤逻辑 每个子类配一个 Factory,决定该 item 是否应该显示这条:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public static final Factory<ActivityContext> WIDGETS = (context, itemInfo, originalView) -> { final PackageUserKey packageUserKey = PackageUserKey.fromItemInfo(itemInfo); if (packageUserKey == null ) return null ; final WidgetPickerData data = context.getWidgetPickerDataProvider().get(); if (findAllWidgetsForPackageUser(data, packageUserKey).isEmpty()) { return null ; } return new Widgets (context, itemInfo, originalView); }; public static final Factory<ActivityContext> ADD_TO_HOME_SCREEN = (activity, itemInfo, originalView) -> { if (itemInfo.container != CONTAINER_ALL_APPS && itemInfo.container != CONTAINER_ALL_APPS_PREDICTION) { return null ; } return new AddToHomeScreen <>(activity, itemInfo, originalView); }; public static final Factory<ActivityContext> PRIVATE_PROFILE_INSTALL = (context, itemInfo, originalView) -> { return new InstallToPrivateProfile <>(context, itemInfo, originalView, privateProfileUser); };
5.3 点击行为示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public static class Widgets <T extends ActivityContext > extends SystemShortcut <T> { public Widgets (T target, ItemInfo itemInfo, @NonNull View originalView) { super (getDrawableId(), R.string.widget_button_text, target, itemInfo, originalView, false ); } @Override public void onClick (View view) { AbstractFloatingView.closeAllOpenViews(mTarget); WidgetsBottomSheet widgetsBottomSheet = (WidgetsBottomSheet) mTarget.getLayoutInflater().inflate( R.layout.widgets_bottom_sheet, mTarget.getDragLayer(), false ); widgetsBottomSheet.populateAndShow(mItemInfo); mTarget.getStatsLogManager().logger().withItemInfo(mItemInfo) .log(LAUNCHER_SYSTEM_SHORTCUT_WIDGETS_TAP); } } public static class AppInfo <T extends ActivityContext > extends SystemShortcut <T> { @Override public void onClick (View view) { Rect sourceBounds = Utilities.getViewBounds(view); ActivityOptionsWrapper options = mTarget.getActivityLaunchOptions(view, mItemInfo); options.onEndCallback.add(this ::dismissTaskMenuView); PackageManagerHelper.startDetailsActivityForInfo(view.getContext(), mItemInfo, sourceBounds, options.toBundle()); mTarget.getStatsLogManager().logger().withItemInfo(mItemInfo) .log(LAUNCHER_SYSTEM_SHORTCUT_APP_INFO_TAP); } }
initializeSystemShortcut 把 SystemShortcut 实例塞进 View 并设为 OnClickListener:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 private fun initializeSystemShortcut ( resId: Int , container: ViewGroup ?, info: SystemShortcut <*>, shouldAppendSpacer: Boolean , ) : View { val view = inflateAndAdd<View>(resId, container) if (view is DeepShortcutView) { info.setIconAndLabelFor(shortcutView.iconView, shortcutView.bubbleText) } else if (view is ImageView) { info.setIconAndContentDescriptionFor(view) if (shouldAppendSpacer) inflateAndAdd<View>(R.layout.system_shortcut_spacer, container) view.setTooltipText(view.getContentDescription()) } view.tag = info view.setOnClickListener(info) return view }
view.setOnClickListener(info) 直接把 SystemShortcut 实例当 OnClickListener——因为它 implements 了该接口。点击时调它的 onClick,执行子类的具体逻辑。
面试深问 Q1:SystemShortcut 为什么同时继承 ItemInfo 和实现 OnClickListener? A:为了”数据即行为”。它既需要被当作 ItemInfo(带图标/label/container,可参与 ItemInfo 体系如埋点、accessibility),又需要在被点击时执行特定逻辑。合二为一避免再写一层 adapter。view.setOnClickListener(info) 直接把数据对象当监听器,简洁。
Q2:Widgets 菜单项什么时候不显示? A:WIDGETS 工厂会查 WidgetPickerData,如果该 app 包没有任何 widget provider,返回 null,菜单项不出现。所以纯 activity 的 app 长按不会看到”小组件”入口。
Q3:Factory 模式在这里解决了什么问题? A:解耦”是否显示”和”如何实例化”。getSupportedShortcuts 返回的是候选 Factory 列表,每个 Factory 内部根据 item 状态、设备能力、flag 开关等决定是否实例化。Launcher 主类只负责提供候选集,不关心具体过滤逻辑,新增系统菜单项只需加 Factory 不改 Launcher。
浮层里的 deep shortcut 可以长按拖到桌面创建快捷方式。PopupContainerWithArrow 实现了 DragSource 和 DragController.DragListener,并配一个 PopupItemDragHandler(Launcher 下是 LauncherPopupItemDragHandler)。
6.1 DeepShortcutView.applyShortcutInfo:绑定数据 + 注册拖拽 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public void applyShortcutInfo (WorkspaceItemInfo info, ShortcutInfo detail, PopupContainerWithArrow container, ActivityContext ac) { mInfo = info; mDetail = detail; mBubbleText.applyFromWorkspaceItem(info); mIconView.setBackground(mBubbleText.getIcon()); CharSequence longLabel = mDetail.getLongLabel(); int availableWidth = mBubbleText.getWidth() - mBubbleText.getTotalPaddingLeft() - mBubbleText.getTotalPaddingRight(); boolean usingLongLabel = !TextUtils.isEmpty(longLabel) && mBubbleText.getPaint().measureText(longLabel.toString()) <= availableWidth; mBubbleText.setText(usingLongLabel ? longLabel : mDetail.getShortLabel()); mBubbleText.setOnClickListener(container.getItemClickListener()); mBubbleText.setOnLongClickListener(container.getItemDragHandler()); mBubbleText.setOnTouchListener(container.getItemDragHandler()); if (ac instanceof Launcher launcher && isPinnable(container)) { setupAddButton(); setAddButtonClickListener(launcher, info, container); } }
6.2 LauncherPopupItemDragHandler:长按启动拖拽 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 class LauncherPopupItemDragHandler ( private val mLauncher: Launcher, private val mContainer: PopupContainerWithArrow<*>, ) : PopupItemDragHandler { private val mIconLastTouchPos: Point = Point() override fun onTouch (v: View , ev: MotionEvent ) : Boolean { when (ev.action) { MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> mIconLastTouchPos[ev.x.toInt()] = ev.y.toInt() } return false } override fun onLongClick (v: View ) : Boolean { if (!ItemLongClickListener.canStartDrag(mLauncher)) return false if (v.parent !is DeepShortcutView) return false val sv = v.parent as DeepShortcutView sv.setWillDrawIcon(false ) val iconShift = Point() iconShift.x = mIconLastTouchPos.x - sv.iconCenter.x iconShift.y = mIconLastTouchPos.y - mLauncher.deviceProfile.workspaceIconProfile.iconSizePx val draggableView = DraggableView.ofType(DraggableView.DRAGGABLE_ICON) val itemInfo = sv.finalInfo itemInfo.container = LauncherSettings.Favorites.CONTAINER_SHORTCUTS val dv = mLauncher.workspace.beginDragShared( sv.iconView, draggableView, mContainer, itemInfo, ShortcutDragPreviewProvider(sv.iconView, iconShift), DragOptions(), ) dv.animateShift(-iconShift.x, -iconShift.y) closeOpenContainer(mLauncher, TYPE_FOLDER) return false } }
6.3 WorkspaceItemInfo 的创建与 addToWorkspace DeepShortcutView.getFinalInfo() 造一个新的 WorkspaceItemInfo:
1 2 3 4 5 6 7 8 public WorkspaceItemInfo getFinalInfo () { final WorkspaceItemInfo badged = new WorkspaceItemInfo (mInfo); Launcher.getLauncher(getContext()).getModel() .updateAndBindWorkspaceItem(badged, mDetail); return badged; }
拖到桌面 drop 后,DragController 回调 Workspace.onDrop,最终走 Launcher.addPendingItem → 落到 CellLayout 并在 Model 层创建 WorkspaceItemInfo 持久化到数据库。
还有一条”一键添加”路径:DeepShortcutView 右侧的”+”按钮:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 private void setAddButtonClickListener (Launcher launcher, WorkspaceItemInfo info, PopupContainerWithArrow<Launcher> container) { LauncherAccessibilityDelegate launcherAccessibilityDelegate = launcher.getAccessibilityDelegate(); mAddButton.setOnClickListener(v -> { launcherAccessibilityDelegate.addToWorkspace(info, false , (success) -> { launcher.getStatsLogManager().logger().withItemInfo(info) .log(StatsLogManager.LauncherEvent.LAUNCHER_TAP_TO_ADD_DEEP_SHORTCUT); }); Folder folder = AbstractFloatingView.getOpenView(launcher, TYPE_FOLDER); container.close(folder == null ); if (folder != null ) folder.close(true ); }); }
6.4 延迟拖拽:createPreDragCondition PopupContainerWithArrow.createPreDragCondition 定义”手指移动超过阈值才真正开始拖”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 override fun createPreDragCondition () : PreDragCondition { return object : PreDragCondition { override fun shouldStartDrag (distanceDragged: Double ) : Boolean { return distanceDragged > startDragThreshold } override fun onPreDragStart (dragObject: DragObject ) { if (!updateIconUi) return if (mIsAboveIcon) { originalIcon.setIconVisible(false ) originalIcon.visibility = VISIBLE } else { originalIcon.visibility = INVISIBLE } } override fun onPreDragEnd (dragObject: DragObject , dragStarted: Boolean ) { if (!updateIconUi) return originalIcon.setIconVisible(true ) if (dragStarted) { originalIcon.visibility = INVISIBLE } else { if (!mIsAboveIcon) { originalIcon.visibility = VISIBLE originalIcon.setTextVisibility(false ) } } } } }
startDragThreshold 来自 R.dimen.deep_shortcuts_start_drag_threshold。设计意图:避免手指轻微抖动误触发拖拽,给用户”长按看菜单”和”长按拖走”两种意图的区分空间。
面试深问 Q1:为什么拖拽期间要隐藏原图标? A:视觉一致性。拖拽时 DragView 已经携带了图标副本在手指下方,如果原图标还显示,会看到两个一样的图标(一个固定在 popup,一个跟手指),造成混乱。隐藏原图标让用户只关注跟随手指的 DragView。
Q2:getFinalInfo 为什么要 new 一个新的 WorkspaceItemInfo 而不是用 mInfo? A:mInfo 是 popup 内部复用的显示数据(容器是 CONTAINER_SHORTCUTS)。拖到桌面要创建一个独立的、可持久化的副本,container 要改成目标 CellLayout。直接改 mInfo 会污染 popup 的显示状态。
Q3:PreDragCondition 解决了什么问题? A:触摸事件在 ACTION_DOWN 就可能触发 DragController 的拖拽流程,但用户可能只是想长按看菜单而不想拖。shouldStartDrag 在手指移动距离 < 阈值时返回 false,拖拽不真正开始,popup 保持显示;超过阈值才启动拖拽。这给了用户一个”反悔窗口”。
七、数据来源:DeepShortcutMap PopupDataProvider.getShortcutCountForItem 不是实时查询,而是从 BgDataModel 的缓存读:
1 2 3 4 5 6 7 8 9 10 11 12 public int getShortcutCountForItem (ItemInfo info) { if (!ShortcutUtil.supportsDeepShortcuts(info)) { return 0 ; } ComponentName component = info.getTargetComponent(); if (component == null ) { return 0 ; } return mBgDataModel.getDeepShortcutMap() .getOrDefault(new ComponentKey (component, info.user), 0 ); }
BgDataModel.deepShortcutMap 由 LauncherModel 在后台遍历所有 app 的 shortcuts 预先建立(ComponentKey → Integer count)。这个 map 在 PackageUpdatedTask / BgDataModel 重建时刷新。
长按时的流程是两段式:
同步阶段 :从缓存 map 拿 count,预占 N 个 View 行(零阻塞)。
异步阶段 :PopupPopulator 在后台用 ShortcutRequest 查真实数据(标题/图标/rank),排序过滤后回主线程逐条 applyShortcutInfo 填充。
PopupDataProvider 还负责通知小红点(NotificationRepository 的更新流 → updateNotificationDots 遍历所有 BubbleTextView 刷 dot 状态),但那是通知体系,不在本文展开。
面试深问 Q1:deepShortcutMap 什么时候更新? A:LauncherModel 在 PackageUpdatedTask(包安装/更新/卸载)、ShortcutAvailabilityTask(shortcut 变更广播)、BgDataModel 重建时会重新遍历所有 app 的 shortcuts 刷新这个 map。app 调 ShortcutManager.pushDynamicShortcut 后,系统会通知 Launcher,触发 map 更新。
Q2:如果缓存 count 和实际查询数量不一致会怎样? A:以实际查询为准。addDeepShortcuts 按 count 预占 View,但 updateHiddenShortcuts 硬限制只显示前 MAX_SHORTCUTS=4 个。PopupPopulator 查到的真实数据也只取前 4 条。如果 count=6 但实际查到 3 条,多占的 3 个 View 会是空的(但通常 LayoutTransition 不会特别处理空 View,视觉上可能有空隙,不过实际很少发生因为缓存和查询是同一数据源)。
Q3:PopupDataProvider 还管什么? A:除了 shortcut count,还管通知小红点。它订阅 NotificationRepository.getUpdateStream(),每当通知变更,updateNotificationDots 遍历 Workspace/Hotseat/AllApps/Folder 里所有 BubbleTextView,调 applyDotState 刷新角标。这是把通知和图标状态绑定的中枢。
八、整体架构与核心类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 WidgetsFullSheet / WidgetsTwoPaneSheet / WidgetPickerActivity │ (选择 widget) ▼ WidgetCell / WidgetImageView (预览) │ 长按开始拖 ▼ PendingItemDragHelper ──startDrag──▶ DragController │ │ (拖拽中预加载) │ WidgetHostViewLoader │ │ ▼ ▼ Workspace (drop) WidgetAddFlowHandler │ │ startBindFlow │ 添加完成 ▼ ▼ LauncherWidgetHolder ←── ListenableAppWidgetHost(系统) │ createView ▼ LauncherAppWidgetHostView (展示 RemoteViews) │ 长按 → AppWidgetResizeFrame (调整大小)
角色
文件
职责
LauncherWidgetHolder
widget/LauncherWidgetHolder.java
AppWidgetHost 的封装,管理 id 分配/监听/创建 View
LauncherAppWidgetHost
widget/LauncherAppWidgetHost.java
具体 AppWidgetHost 子类,onCreateView 造 LauncherAppWidgetHostView
LauncherAppWidgetHostView
widget/LauncherAppWidgetHostView.java
widget 展示容器,承载 RemoteViews
WidgetAddFlowHandler
widget/WidgetAddFlowHandler.java
单个 widget 的添加流程(绑定 + 配置)
PendingAddWidgetInfo
widget/PendingAddWidgetInfo.java
“待添加”widget 的元数据
PendingItemDragHelper
widget/PendingItemDragHelper.java
从 picker 拖 widget 时的拖拽预览提供者
WidgetHostViewLoader
widget/WidgetHostViewLoader.java
拖拽期间预加载(预绑定 + 预 inflate)
AppWidgetResizeFrame
AppWidgetResizeFrame.kt
widget 调整大小浮层(4 个拖拽点)
WidgetManagerHelper
widget/WidgetManagerHelper.java
封装 AppWidgetManager 的绑定调用
LauncherAppWidgetProviderInfo
widget/LauncherAppWidgetProviderInfo.java
AppWidgetProviderInfo 的 Launcher 包装(含 min/max span)
Launcher3 有三种 widget 选择入口:
9.1 三种入口
**WidgetsFullSheet / WidgetsTwoPaneSheet**(widget/picker/)—— Launcher 内的全屏/双栏面板。长按桌面空白或点 popup 的”小组件”项触发 Launcher.openWidgetPicker:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public boolean openWidgetPicker () { if (getPackageManager().isSafeMode()) { Toast.makeText(this , R.string.safemode_widget_error, Toast.LENGTH_SHORT).show(); return false ; } if (Flags.enableWidgetPickerRefactor() && ComposeFacade.INSTANCE.isComposeAvailable()) { Intent intent = new Intent (Intent.ACTION_PICK); intent.setPackage(asContext().getPackageName()); asContext().startActivity(intent); return true ; } openWidgetsFullSheet(); return true ; }
**AddItemWidgetsBottomSheet**(widget/AddItemWidgetsBottomSheet.java)—— 外部 app 通过 ACTION_APPWIDGET_BIND(pin widget 流程,如输入法 pin 一个搜索框 widget)拉起时的底部确认 sheet。
**WidgetPickerActivity**(widgetpicker/WidgetPickerActivity.kt)—— 新的独立 Activity,受 enableWidgetPickerRefactor flag 控制,用 Compose 实现:
1 2 3 4 5 6 7 8 override fun onCreate (savedInstanceState: Bundle ?) { super .onCreate(savedInstanceState) if (Flags.enableWidgetPickerRefactor() && isComposeAvailable()) { component.widgetPickerComposeWrapper.showAllWidgets(this , widgetPickerConfig) } }
无论哪种入口,最终都是:用户长按某个 widget 预览 → 进入拖拽 。
9.2 列表加载:PackageManager.getInstalledProviders Picker 的数据源是系统的 AppWidgetManager.getInstalledProvidersForProfile,由 WidgetManagerHelper 封装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public List<AppWidgetProviderInfo> getAllProviders (@Nullable PackageUserKey packageUser) { if (!WIDGETS_ENABLED) { return Collections.emptyList(); } if (packageUser == null ) { return allWidgetsSteam(mContext).collect(Collectors.toList()); } try { return mAppWidgetManager.getInstalledProvidersForPackage( packageUser.mPackageName, packageUser.mUser); } catch (IllegalStateException e) { Log.e(TAG, "getAllProviders: Error getting installed providers for package=" + packageUser.mPackageName, e); return Collections.emptyList(); } } private static Stream<AppWidgetProviderInfo> allWidgetsSteam (Context context) { AppWidgetManager awm = context.getSystemService(AppWidgetManager.class); return Stream.concat( UserCache.INSTANCE.get(context).getUserProfiles().stream() .flatMap(u -> awm.getInstalledProvidersForProfile(u).stream()), CustomWidgetManager.INSTANCE.get(context).stream()); }
9.3 拖拽预览的实现 WidgetCell 的预览图有三层来源,PendingItemDragHelper.startDrag 里决定用哪个:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 public void startDrag (Rect previewBounds, int previewBitmapWidth, int previewViewWidth, Point screenPos, DragSource source, DragOptions options) { final Launcher launcher = Launcher.getLauncher(mView.getContext()); mEstimatedCellSize = launcher.getWorkspace().estimateItemSize(mAddInfo); if (mAddInfo instanceof PendingAddWidgetInfo) { PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) mAddInfo; if (mWidgetPreviewInfo != null ) { if (mWidgetPreviewInfo.previewBitmap != null ) { preview = new RoundDrawableWrapper (new FastBitmapDrawable (mWidgetPreviewInfo.previewBitmap), mEnforcedRoundedCornersForWidget); } else { mAppWidgetHostViewPreview = new LauncherAppWidgetHostView (launcher); mAppWidgetHostViewPreview.setAppWidget(INVALID_APPWIDGET_ID, mWidgetPreviewInfo.providerInfo); mAppWidgetHostViewPreview.updateAppWidget(mWidgetPreviewInfo.remoteViews); Size widgetSizes = getWidgetSizePx(deviceProfile, mAddInfo.spanX, mAddInfo.spanY); measureAndUpdateAppWidgetHostViewScale(widgetSizes); } } else if (mRemoteViewsPreview != null ) { mAppWidgetHostViewPreview = new LauncherAppWidgetHostView (launcher); mAppWidgetHostViewPreview.updateAppWidget(mRemoteViewsPreview); } if (preview == null && mAppWidgetHostViewPreview == null ) { Drawable p = new FastBitmapDrawable (new DatabaseWidgetPreviewLoader (launcher, ...) .generateWidgetPreview(createWidgetInfo.info, maxWidth, previewSizeBeforeScale)); preview = new RoundDrawableWrapper (p, mEnforcedRoundedCornersForWidget); } scale = previewBounds.width() / (float ) previewWidth; launcher.getDragController().addDragListener(new WidgetHostViewLoader (launcher, mView)); launcher.getDragController().startDrag( mAppWidgetHostViewPreview != null ? mAppWidgetHostViewPreview : preview, DraggableView.ofType(DraggableView.DRAGGABLE_WIDGET), dragLayerX, dragLayerY, source, mAddInfo, dragRegion, scale, scale, options); } else { } }
设计意图:预览图三层优先级 ——缓存的位图(最快)> RemoteViews 实时渲染(真实)> 数据库生成(兜底)。保证拖拽跟手不卡顿,同时预览尽可能接近真实 widget。
面试深问 Q1:为什么要三种 widget picker 入口? A:不同触发场景。FullSheet 是 Launcher 内部的传统面板(长按桌面空白触发)。AddItemBottomSheet 是外部 app 通过 pin widget 流程拉起的确认页(外部已有目标,只需用户确认)。WidgetPickerActivity 是新版 Compose 实现的独立 Activity,解耦了 picker 和 Launcher 进程,方便独立演进和测试。
Q2:预览图为什么优先用缓存位图? A:RemoteViews 实时渲染需要 inflate 跨进程的视图树,有开销。缓存位图(WidgetPreviewInfo.previewBitmap)是之前渲染好的 Bitmap,直接用零延迟,拖拽跟手。只有缓存失效或首次加载才走 RemoteViews 或数据库生成。
Q3:MAX_WIDGET_SCALE = 1.25f 是什么? A:预览图最大放大倍数。maxWidth = Math.min((int)(previewBitmapWidth * MAX_WIDGET_SCALE), mEstimatedCellSize[0])——预览图宽度不能超过”原始位图宽度的 1.25 倍”和”估算的落位 cell 宽度”中的较小者。防止预览图被放大到模糊或超过实际占位。
这是本文最核心的部分,分四步:拖拽预览 → 预加载绑定 → drop 落位 → 配置 。
10.1 步骤一:开始拖拽(PendingItemDragHelper) 见上文 9.3 节。关键产出:构造拖拽预览 + 注册 WidgetHostViewLoader + 启动 DragController.startDrag。
这是体验优化的精髓。WidgetHostViewLoader 是 DragController.DragListener,在 onDragStart 立刻开始预绑定 + 预 inflate ,把耗时操作藏进用户拖拽的时间:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 public class WidgetHostViewLoader implements DragController .DragListener { @Thunk int mWidgetLoadingId = -1 ; public WidgetHostViewLoader (Launcher launcher, View view) { mLauncher = launcher; mHandler = new Handler (); mView = view; mInfo = (PendingAddWidgetInfo) view.getTag(); } @Override public void onDragStart (DropTarget.DragObject dragObject, DragOptions options) { preloadWidget(); } private boolean preloadWidget () { final LauncherAppWidgetProviderInfo pInfo = mInfo.info; if (pInfo.isCustomWidget()) { return false ; } final Bundle options = mInfo.getDefaultSizeOptions(mLauncher); if (mInfo.getHandler().needsConfigure()) { mInfo.bindOptions = options; return false ; } mBindWidgetRunnable = new Runnable () { @Override public void run () { mWidgetLoadingId = mLauncher.getAppWidgetHolder().allocateAppWidgetId(); if (new WidgetManagerHelper (mLauncher).bindAppWidgetIdIfAllowed( mWidgetLoadingId, pInfo, options)) { mHandler.post(mInflateWidgetRunnable); } } }; mInflateWidgetRunnable = new Runnable () { @Override public void run () { if (mWidgetLoadingId == -1 ) { return ; } AppWidgetHostView hostView = mLauncher.getAppWidgetHolder().createView( mWidgetLoadingId, pInfo); mInfo.boundWidget = hostView; mWidgetLoadingId = -1 ; hostView.setVisibility(View.INVISIBLE); int [] unScaledSize = mLauncher.getWorkspace().estimateItemSize(mInfo); DragLayer.LayoutParams lp = new DragLayer .LayoutParams(unScaledSize[0 ], unScaledSize[1 ]); lp.x = lp.y = 0 ; lp.customPosition = true ; hostView.setLayoutParams(lp); mLauncher.getDragLayer().addView(hostView); mView.setTag(mInfo); } }; mHandler.post(mBindWidgetRunnable); return true ; } @Override public void onDragEnd () { mLauncher.getDragController().removeDragListener(this ); mHandler.removeCallbacks(mBindWidgetRunnable); mHandler.removeCallbacks(mInflateWidgetRunnable); if (mWidgetLoadingId != -1 ) { mLauncher.getAppWidgetHolder().deleteAppWidgetId(mWidgetLoadingId); mWidgetLoadingId = -1 ; } if (mInfo.boundWidget != null ) { mLauncher.getDragLayer().removeView(mInfo.boundWidget); mLauncher.getAppWidgetHolder().deleteAppWidgetId(mInfo.boundWidget.getAppWidgetId()); mInfo.boundWidget = null ; } } }
为什么预加载? widget 的”分配 id → 跨进程绑定 provider → inflate RemoteViews”涉及多次 binder 调用和跨进程视图渲染,总耗时可能上百毫秒。如果等用户松手 drop 后才开始,会看到明显白屏等待。WidgetHostViewLoader 利用用户拖拽这段时间并行完成这些操作,松手时 widget 已准备好,几乎瞬间显示。
为什么 needsConfigure 时不预加载? 配置 Activity 可能让用户改变 widget 的尺寸/内容(如时钟选城市),预绑定后用户再配置会导致已绑定的 RemoteViews 作废,浪费。所以需要配置的 widget 等 drop 后走标准流程。
取消时的回收 :onDragEnd 检查 mWidgetLoadingId 和 mInfo.boundWidget,如果有未消费的 id 或已 inflate 但没用的 HostView,调 deleteAppWidgetId 回收,避免资源泄漏(widget id 是系统级资源,不回收会占名额)。
10.3 步骤三:Drop 落位与数据绑定 松手 drop 到 Workspace 后,走 Launcher.addPendingItem → addAppWidgetFromDrop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 public void addPendingItem (PendingAddItemInfo info, int container, int screenId, int [] cell, int spanX, int spanY) { if (info instanceof PendingAddWidgetInfo) { addAppWidgetFromDrop((PendingAddWidgetInfo) info); } else { processShortcutFromDrop((PendingAddShortcutInfo) info); } } private void addAppWidgetFromDrop (PendingAddWidgetInfo info) { AppWidgetHostView hostView = info.boundWidget; final int appWidgetId; WidgetAddFlowHandler addFlowHandler = info.getHandler(); if (hostView != null ) { getDragLayer().removeView(hostView); appWidgetId = hostView.getAppWidgetId(); addAppWidgetFromDropImpl(appWidgetId, info, hostView, addFlowHandler); info.boundWidget = null ; } else { if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET) { appWidgetId = CustomWidgetManager.INSTANCE.get(this ).allocateCustomAppWidgetId(info.componentName); } else { appWidgetId = getAppWidgetHolder().allocateAppWidgetId(); } Bundle options = info.bindOptions; boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.info, options); if (success) { addAppWidgetFromDropImpl(appWidgetId, info, null , addFlowHandler); } else { addFlowHandler.startBindFlow(this , appWidgetId, info, REQUEST_BIND_APPWIDGET); } } }
addAppWidgetFromDropImpl → addAppWidgetImpl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 void addAppWidgetImpl (int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget, WidgetAddFlowHandler addFlowHandler, int delay) { final boolean isActivityStarted = addFlowHandler.startConfigActivity( this , appWidgetId, info, REQUEST_CREATE_APPWIDGET); completeAddAppWidget(appWidgetId, info, boundWidget, addFlowHandler.getProviderInfo(this ), addFlowHandler.needsConfigure(), false , widgetPreviewBitmap); if (!isActivityStarted) { mWorkspace.removeExtraEmptyScreenDelayed(delay, false , onComplete); } }
completeAddAppWidget(在 Launcher.java,较长)最终在 Model 层创建 LauncherAppWidgetInfo,持久化到数据库,并把 HostView add 到对应 CellLayout。
10.4 步骤四:绑定授权与配置 绑定授权 (startBindFlow):首次添加某 provider 的 widget 时,系统可能要求用户确认(取决于 app 是否有 BIND_APPWIDGET 权限):
1 2 3 4 5 6 7 8 9 10 11 12 13 public void startBindFlow (@NonNull BaseActivity activity, int appWidgetId, @NonNull AppWidgetProviderInfo info, int requestCode) { if (!WIDGETS_ENABLED) { sendActionCancelled(activity, requestCode); return ; } Intent intent = new Intent (AppWidgetManager.ACTION_APPWIDGET_BIND) .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId) .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.provider) .putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, info.getProfile()); activity.startActivityForResult(intent, requestCode); }
配置 Activity (startConfigActivity):如果 widget 声明了 configure 且非 optional:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public boolean needsConfigure () { int featureFlags = mProviderInfo.widgetFeatures; boolean configurationOptional = (featureFlags & WIDGET_FEATURE_CONFIGURATION_OPTIONAL) != 0 && (featureFlags & WIDGET_FEATURE_RECONFIGURABLE) != 0 ; return mProviderInfo.configure != null && !configurationOptional; } public boolean startConfigActivity (Launcher launcher, int appWidgetId, ItemInfo info, int requestCode) { if (!needsConfigure()) { return false ; } launchConfigActivity(launcher, appWidgetId, info, requestCode); return true ; } private void launchConfigActivity (Launcher launcher, int appWidgetId, ItemInfo info, int requestCode) { launcher.setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, this , info)); launcher.getAppWidgetHolder().startConfigActivity(launcher, appWidgetId, requestCode); }
配置完成后回调 Launcher.onActivityResult → completeTwoStageWidgetDrop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 void completeTwoStageWidgetDrop (final int resultCode, final int appWidgetId, final PendingRequestArgs requestArgs) { CellLayout cellLayout = mWorkspace.getScreenWithId( getCellPosMapper().mapModelToPresenter(requestArgs).screenId); Runnable onCompleteRunnable = null ; AppWidgetHostView boundWidget = null ; if (resultCode == RESULT_OK) { final AppWidgetHostView layout = getWorkspace().getWidgetForAppWidgetId(appWidgetId); boundWidget = layout; onCompleteRunnable = () -> { completeAddAppWidget(appWidgetId, requestArgs, layout, null , false , true , null ); if (!isInState(EDIT_MODE)) { mStateManager.goToState(NORMAL, SPRING_LOADED_EXIT_DELAY); } }; } else if (resultCode == RESULT_CANCELED) { mAppWidgetHolder.deleteAppWidgetId(appWidgetId); animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION; } }
面试深问 Q1:WidgetHostViewLoader 预加载的 HostView 怎么和 drop 关联? A:通过 PendingAddWidgetInfo.boundWidget 字段。预加载 inflate 出 HostView 后存到 mInfo.boundWidget,而 mInfo 就是拖拽携带的 PendingAddWidgetInfo(作为 DragObject 的 tag)。drop 时 addAppWidgetFromDrop 从 info.boundWidget 取出预加载的 HostView 复用,不用重新创建。如果 boundWidget == null(未预加载或取消过),才现场分配 id + 绑定。
Q2:为什么 needsConfigure 的 widget 不预加载? A:配置 Activity 可能改变 widget 的最终内容/尺寸(如天气 widget 选城市后布局不同)。预绑定后配置会导致已绑定的 RemoteViews 被覆盖,浪费一次跨进程绑定。所以需要配置的 widget 等 drop 后走标准流程:先 add 占位,配置完成回调里再绑定真实 RemoteViews。
Q3:drop 失败(拖到无效区域)时预加载的资源怎么回收? A:WidgetHostViewLoader.onDragEnd 负责。它检查 mWidgetLoadingId(未消费的 id)和 mInfo.boundWidget(已 inflate 未用的 HostView),分别调 deleteAppWidgetId 回收并 removeView。保证不泄漏系统 widget id 名额。
Launcher3 不直接操作 AppWidgetManager,而是经由自己的 holder 链:
1 2 3 4 5 6 7 LauncherWidgetHolder ──持有──▶ LauncherAppWidgetHost (extends ListenableAppWidgetHost extends AppWidgetHost) │ │ onCreateView() │ mViews (SparseArray<id→view>) ▼ │ LauncherAppWidgetHostView (extends BaseLauncherAppWidgetHostView) │ │ updateAppWidget(RemoteViews) ▼ ▼ 分配/删除 id 承载并渲染 RemoteViews
LauncherAppWidgetHost 是具体的 AppWidgetHost 子类,核心是 onCreateView:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class LauncherAppWidgetHost extends ListenableAppWidgetHost { @Nullable private ListenableHostView mViewToRecycle; @Override @NonNull public LauncherAppWidgetHostView onCreateView (Context context, int appWidgetId, AppWidgetProviderInfo appWidget) { ListenableHostView result = mViewToRecycle != null ? mViewToRecycle : new ListenableHostView (context); mViewToRecycle = null ; return result; } public void recycleViewForNextCreation (ListenableHostView viewToRecycle) { mViewToRecycle = viewToRecycle; } }
设计意图:onCreateView 是系统 AppWidgetHost 的回调,每次 createView 时触发。回收机制避免重复 inflate 新 View 对象,在配置 Activity 往返时复用已有 View。
LauncherWidgetHolder(widget/LauncherWidgetHolder.java)是 AppWidgetHost 的封装层,职责包括:
id 管理 :allocateAppWidgetId() / deleteAppWidgetId(id)
监听生命周期 :startListening() / stopListening()
创建 View :createView(appWidgetId, providerInfo)
绑定/配置入口 :startBindFlow / startConfigActivity
省电监听 :三 flag 组合决定是否监听
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 public static final int APPWIDGET_HOST_ID = 1024 ; protected static final int FLAG_LISTENING = 1 ; protected static final int FLAG_STATE_IS_NORMAL = 1 << 1 ; protected static final int FLAG_ACTIVITY_STARTED = 1 << 2 ; protected static final int FLAG_ACTIVITY_RESUMED = 1 << 3 ; private static final int FLAGS_SHOULD_LISTEN = FLAG_STATE_IS_NORMAL | FLAG_ACTIVITY_STARTED | FLAG_ACTIVITY_RESUMED; protected AtomicInteger mFlags = new AtomicInteger (FLAG_STATE_IS_NORMAL); public int allocateAppWidgetId () { if (!WIDGETS_ENABLED) { return INVALID_APPWIDGET_ID; } return mWidgetHost.allocateAppWidgetId(); } public AppWidgetHostView createView (int appWidgetId, LauncherAppWidgetProviderInfo appWidget) { if (appWidget.isCustomWidget()) { LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView (mContext); lahv.setAppWidget(INVALID_APPWIDGET_ID, appWidget); CustomWidgetManager.INSTANCE.get(mContext).onViewCreated(lahv); return lahv; } LauncherAppWidgetHostView view = createViewInternal(appWidgetId, appWidget); if (mOnViewCreationCallback != null ) mOnViewCreationCallback.accept(view); if (Looper.myLooper() == Looper.getMainLooper()) { mViews.put(appWidgetId, view); } return view; } protected LauncherAppWidgetHostView createViewInternal ( int appWidgetId, LauncherAppWidgetProviderInfo appWidget) { if ((mFlags.get() & FLAG_LISTENING) == 0 ) { return new PendingAppWidgetHostView (mContext, this , appWidgetId, appWidget); } else { if (Looper.myLooper() != Looper.getMainLooper()) { ListenableHostView hostView = new ListenableHostView (mContext); hostView.setAppWidget(appWidgetId, appWidget); return hostView; } try { return (LauncherAppWidgetHostView) mWidgetHost.createView( mContext, appWidgetId, appWidget); } catch (Exception e) { if (!Utilities.isBinderSizeError(e)) { throw new RuntimeException (e); } LauncherAppWidgetHostView view = mViews.get(appWidgetId); if (view == null ) { view = new ListenableHostView (mContext); } view.setAppWidget(appWidgetId, appWidget); view.switchToErrorView(); return view; } } }
11.3 监听时机(省电设计) setShouldListenFlag 用 AtomicInteger 维护三个 flag,只有全 on 才 startListening:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 @VisibleForTesting void setShouldListenFlag (int flag, boolean on) { if (on) { mFlags.updateAndGet(old -> old | flag); } else { mFlags.updateAndGet(old -> old & ~flag); } final boolean listening = isListening(); int currentFlag = mFlags.get(); if (!listening && shouldListen(currentFlag)) { startListening(); } else if (listening && (currentFlag & FLAG_ACTIVITY_STARTED) == 0 ) { stopListening(); } } protected boolean shouldListen (int flags) { return (flags & FLAGS_SHOULD_LISTEN) == FLAGS_SHOULD_LIST; } public void startListening () { if (!WIDGETS_ENABLED) { return ; } getWidgetHolderExecutor().execute(() -> { try { mWidgetHost.startListening(); } catch (Exception e) { if (!Utilities.isBinderSizeError(e)) { throw new RuntimeException (e); } } setListeningFlag(true ); MAIN_EXECUTOR.execute(this ::updateDeferredView); }); } protected void updateDeferredView () { for (int i = mViews.size() - 1 ; i >= 0 ; i--) { LauncherAppWidgetHostView view = mViews.valueAt(i); if (view instanceof PendingAppWidgetHostView pv) { pv.reInflate(); } } }
Launcher 在不同生命周期调对应方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @Override protected void onStart () { super .onStart(); mAppWidgetHolder.setActivityStarted(true ); } @Override protected void onStop () { super .onStop(); mAppWidgetHolder.setActivityStarted(false ); } @Override protected void onDeferredResumed () { mAppWidgetHolder.setActivityResumed(true ); }
设计意图:Launcher 不可见(onStop)、处于 AllApps/OVERVIEW(非 NORMAL)、或未 resume 时,widget host 不监听系统更新,省电省内存。恢复到 NORMAL + resumed + started 时才重新 startListening,并用 updateDeferredView 把期间用 PendingAppWidgetHostView 占位的 widget 重新 inflate。
LauncherAppWidgetProviderInfo 是 AppWidgetProviderInfo 的 Launcher 包装,多了 spanX/spanY/minSpanX/minSpanY/maxSpanX/maxSpanY(格数):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public void initSpans (Context context, InvariantDeviceProfile idp) { int minSpanX = 0 , minSpanY = 0 ; int maxSpanX = idp.numColumns; int maxSpanY = idp.numRows; int spanX = 0 , spanY = 0 ; Point cellSize = new Point (); for (DeviceProfile dp : idp.supportedProfiles) { cellSize = dp.getWorkspaceIconProfile().getCellSize(); Rect widgetPadding = dp.widgetPadding; minSpanX = Math.max(minSpanX, getSpanX(widgetPadding, minResizeWidth, borderSpace.x, cellSize.x)); minSpanY = Math.max(minSpanY, getSpanY(widgetPadding, minResizeHeight, borderSpace.y, cellSize.y)); if (maxResizeWidth > 0 ) { maxSpanX = Math.min(maxSpanX, getSpanX(widgetPadding, maxResizeWidth, borderSpace.x, cellSize.x)); } if (maxResizeHeight > 0 ) { maxSpanY = Math.min(maxSpanY, getSpanY(widgetPadding, maxResizeHeight, borderSpace.y, cellSize.y)); } spanX = Math.max(spanX, getSpanX(widgetPadding, minWidth, borderSpace.x, cellSize.x)); spanY = Math.max(spanY, getSpanY(widgetPadding, minHeight, borderSpace.y, cellSize.y)); } maxSpanX = Math.max(maxSpanX, minSpanX); maxSpanY = Math.max(maxSpanY, minSpanY); if (targetCellWidth >= minSpanX && targetCellWidth <= maxSpanX && targetCellHeight >= minSpanY && targetCellHeight <= maxSpanY) { spanX = targetCellWidth; spanY = targetCellHeight; } this .minSpanX = Math.min(spanX, minSpanX); this .minSpanY = Math.min(spanY, minSpanY); this .maxSpanX = maxSpanX; this .maxSpanY = maxSpanY; this .spanX = Math.min(spanX, idp.numColumns); this .spanY = Math.min(spanY, idp.numRows); } private int getSpan (int widgetPadding, int widgetSize, int cellSpacing, float cellSize) { return Math.max(1 , (int ) Math.ceil( (widgetSize + widgetPadding + cellSpacing) / (cellSize + cellSpacing))); }
getSpan 是核心换算公式:widget 的像素尺寸 widgetSize 加上 padding 和 spacing,除以单格尺寸加间距,向上取整得到格数。这和 AppWidgetResizeFrame 里的换算是同一套逻辑。
11.5 RemoteViews 更新流程 provider 进程调 AppWidgetManager.updateAppWidget 后,系统 system_server 把 RemoteViews 通过 binder 推给 host 进程。AppWidgetHost(系统类)接收后调 onUpdateWidget → 最终调到 LauncherAppWidgetHostView.updateAppWidget(remoteViews),它 inflate RemoteViews 成真实 View 树并替换显示。
ListenableHostView 提供了 addUpdateListener 机制,允许注册回调监听每次更新(用于 picker 预览的实时刷新等)。
面试深问 Q1:为什么 host id 固定是 1024? A:APPWIDGET_HOST_ID = 1024 是 Launcher 在系统的唯一标识。系统用这个 id 区分不同 host(Launcher、第三方桌面、taskbar 等)。固定值保证 Launcher 重启后能找回之前绑定的 widget id。如果每次启动用随机 id,之前的绑定关系会丢失。
Q2:为什么 createView 时要判断是否在主线程? A:mViews 是 SparseArray,非线程安全。后台线程调用 createView(如预加载)时不能直接 put 到 mViews,否则和主线程并发修改崩溃。所以后台线程返回占位 ListenableHostView,主线程再通过 attachViewToHostAndGetAttachedView 正式注册到 mViews。
Q3:监听状态的三 flag 为什么用 AtomicInteger? A:三个 flag(NORMAL/STARTED/RESUMED)可能从不同线程/回调设置(如状态机切换在主线程,Activity 生命周期可能在 binder 回调)。AtomicInteger 的 updateAndGet 保证读-改-写的原子性,避免并发下 flag 丢失。比 synchronized 轻量。
长按已放置的 widget 显示带四个拖拽圆点的浮层 AppWidgetResizeFrame,支持上下左右四个方向缩放。这是本文算法最密集的部分。
12.1 显示与初始化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 companion object { private const val SNAP_DURATION_MS = 150 private const val DIMMED_ALPHA = 0f private const val VISIBLE_ALPHA = 1f private const val RESIZE_THRESHOLD = 0.66f private const val CELL_LAYOUT_INVALID_RESIZE_MAX_ALPHA = 0.5f @JvmStatic fun showForWidget (widget: LauncherAppWidgetHostView ?, cellLayout: CellLayout ) { if (widget == null || widget.parent == null ) return val activityContext = cellLayout.mActivity val dragLayer = activityContext.dragLayer as DragLayer closeAllOpenViewsExcept(activityContext, TYPE_ACTION_POPUP) val frame = activityContext.layoutInflater.inflate( R.layout.app_widget_resize_frame, dragLayer, false ) as AppWidgetResizeFrame frame.apply { setupForWidget(widget, cellLayout, dragLayer) tag = widget.tag accessibilityDelegate = activityContext.accessibilityDelegate (layoutParams as BaseDragLayer.LayoutParams).customPosition = true } dragLayer.addView(frame) frame.mIsOpen = true frame.post { frame.snapToWidget(false ) } } private fun getSpanIncrement (deltaFrac: Float ) : Int { return if (abs(deltaFrac.toDouble()) > RESIZE_THRESHOLD) { Math.round(deltaFrac) } else { 0 } } private fun AppWidgetProviderInfo.hasHorizontalResizeModeEnabled () = resizeMode and AppWidgetProviderInfo.RESIZE_HORIZONTAL != 0 private fun AppWidgetProviderInfo.hasVerticalResizeModeEnabled () = resizeMode and AppWidgetProviderInfo.RESIZE_VERTICAL != 0 }
resizeMode 由 widget provider 在 AppWidgetProviderInfo 声明:RESIZE_HORIZONTAL / RESIZE_VERTICAL / RESIZE_BOTH / RESIZE_NONE。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 private fun setupForWidget ( widgetView: LauncherAppWidgetHostView , cellLayout: CellLayout , dragLayer: DragLayer , ) { this .cellLayout = cellLayout this .widgetView = widgetView val info = widgetView.appWidgetInfo as LauncherAppWidgetProviderInfo this .dragLayer = dragLayer minHSpan = info.minSpanX minVSpan = info.minSpanY maxHSpan = info.maxSpanX maxVSpan = info.maxSpanY val widgetInfoOnView = this .widgetView.tag as LauncherAppWidgetInfo val idp = getIDP(cellLayout.context) updateResizeHandlesForGrid( currentSpanX = widgetInfoOnView.spanX, currentSpanY = widgetInfoOnView.spanY, info = info, numRows = idp.numRows, numColumns = idp.numColumns, ) if (!Flags.homeScreenEditImprovements() && info.isReconfigurable) { initializeReconfigureButton() } initializeWidgetViewLayoutParams(widgetInfoOnView) this .cellLayout.markCellsAsUnoccupiedForView(this .widgetView) } private fun updateResizeHandlesForGrid ( currentSpanX: Int , currentSpanY: Int , info: LauncherAppWidgetProviderInfo , numRows: Int , numColumns: Int , ) { val isWidgetVSpanInvalid = currentSpanY < minVSpan val isWidgetHSpanInvalid = currentSpanX < minHSpan verticalResizeActive = info.hasVerticalResizeModeEnabled() && ((minVSpan < numRows && maxVSpan > 1 && minVSpan < maxVSpan) || isWidgetVSpanInvalid) if (!verticalResizeActive) { dragHandles.top.visibility = GONE dragHandles.bottom.visibility = GONE } horizontalResizeActive = info.hasHorizontalResizeModeEnabled() && ((minHSpan < numColumns && maxHSpan > 1 && minHSpan < maxHSpan) || isWidgetHSpanInvalid) if (!horizontalResizeActive) { dragHandles.left.visibility = GONE dragHandles.right.visibility = GONE } }
设计意图:为什么用格数不用像素? Launcher 的网格是抽象单位,不同设备密度/字号下单格像素不同。用格数(span)作为约束边界,widget 的 min/max span 在 initSpans 时已换算成当前设备的格数,resize 时直接比较格数,与屏幕密度无关,保证一致性。markCellsAsUnoccupiedForView 是关键——先把 widget 当前占的格子释放,resize 过程中 CellLayout.createAreaForResize 才能重新计算排布,让其他图标实时让位。
12.3 触摸 → 识别方向 → 计算增量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 override fun onControllerTouchEvent (ev: MotionEvent ) : Boolean { val action = ev.action val x = ev.x.toInt() val y = ev.y.toInt() when (action) { MotionEvent.ACTION_DOWN -> return handleTouchDown(ev) MotionEvent.ACTION_MOVE -> { closePopupIfOpen() visualizeResizeForDelta(deltaX = x - xDown, deltaY = y - yDown) } MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> { visualizeResizeForDelta(deltaX = x - xDown, deltaY = y - yDown) onTouchUp() xDown = 0 ; yDown = 0 } } return true } private fun handleTouchDown (ev: MotionEvent ) : Boolean { val hitRect = Rect() val x = ev.x.toInt() val y = ev.y.toInt() getHitRect(hitRect) if (hitRect.contains(x, y)) { if (beginResizeIfPointInRegion(x - left, y - top)) { xDown = x; yDown = y return true } } return false } private fun beginResizeIfPointInRegion (x: Int , y: Int ) : Boolean { isLeftBorderActive = (x < touchTargetWidth) && horizontalResizeActive isRightBorderActive = (x > width - touchTargetWidth) && horizontalResizeActive isTopBorderActive = (y < touchTargetWidth + topTouchRegionAdjustment) && verticalResizeActive isBottomBorderActive = (y > height - touchTargetWidth + bottomTouchRegionAdjustment) && verticalResizeActive val anyBordersActive = isLeftBorderActive || isRightBorderActive || isTopBorderActive || isBottomBorderActive if (anyBordersActive) { dragHandles.left.alpha = if (isLeftBorderActive) VISIBLE_ALPHA else DIMMED_ALPHA dragHandles.right.alpha = if (isRightBorderActive) VISIBLE_ALPHA else DIMMED_ALPHA dragHandles.top.alpha = if (isTopBorderActive) VISIBLE_ALPHA else DIMMED_ALPHA dragHandles.bottom.alpha = if (isBottomBorderActive) VISIBLE_ALPHA else DIMMED_ALPHA } when { isLeftBorderActive -> deltaXRange.set (start = -left, end = width - 2 * touchTargetWidth) isRightBorderActive -> deltaXRange.set (start = 2 * touchTargetWidth - width, end = dragLayer.width - right) else -> deltaXRange.reset() } baselineXRange.set (start = left, end = right) when { isTopBorderActive -> deltaYRange.set (start = -top, end = height - 2 * touchTargetWidth) isBottomBorderActive -> deltaYRange.set (start = 2 * touchTargetWidth - height, end = dragLayer.height - bottom) else -> deltaYRange.reset() } baselineYRange.set (start = top, end = bottom) return anyBordersActive }
touchTargetWidth = 2 * backgroundPadding,是 handle 的触摸热区宽度。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 private fun visualizeResizeForDelta (deltaX: Int , deltaY: Int ) { this .deltaX = deltaXRange.clamp(deltaX) this .deltaY = deltaYRange.clamp(deltaY) val lp = layoutParams as BaseDragLayer.LayoutParams baselineXRange.applyDelta( moveStart = isLeftBorderActive, moveEnd = isRightBorderActive, delta = this .deltaX, outputRange = tempRange1, ) lp.x = tempRange1.start lp.width = tempRange1.size() baselineYRange.applyDelta( moveStart = isTopBorderActive, moveEnd = isBottomBorderActive, delta = this .deltaY, outputRange = tempRange1, ) lp.y = tempRange1.start lp.height = tempRange1.size() resizeWidgetIfNeeded(onDismiss = false ) if (cellLayout.parent is Workspace<*>) { val workspace = cellLayout.parent as Workspace<*> val pairedCellLayout = workspace.getScreenPair(cellLayout) if (pairedCellLayout != null ) { handleInvalidResizeForTwoPanelUi(workspace, pairedCellLayout) } } requestLayout() } private fun resizeWidgetIfNeeded (onDismiss: Boolean ) { val wlp: ViewGroup.LayoutParams? = widgetView.layoutParams if (wlp == null || wlp !is CellLayoutLayoutParams) return val dp = launcher.deviceProfile val xThreshold = (cellLayout.cellWidth + dp.workspaceIconProfile.cellLayoutBorderSpacePx.x).toFloat() val yThreshold = (cellLayout.cellHeight + dp.workspaceIconProfile.cellLayoutBorderSpacePx.y).toFloat() val hSpanInc = getSpanIncrement((deltaX + deltaXAddOn) / xThreshold - runningHInc) val vSpanInc = getSpanIncrement((deltaY + deltaYAddOn) / yThreshold - runningVInc) if (!onDismiss && (hSpanInc == 0 && vSpanInc == 0 )) return directionVector[DIRECTION_HORIZONTAL_INDEX] = DIRECTION_NONE directionVector[DIRECTION_VERTICAL_INDEX] = DIRECTION_NONE var spanX = wlp.cellHSpan var spanY = wlp.cellVSpan var cellX = if (wlp.useTmpCoords) wlp.tmpCellX else wlp.cellX var cellY = if (wlp.useTmpCoords) wlp.tmpCellY else wlp.cellY tempRange1.set (cellX, spanX + cellX) val hSpanDelta = tempRange1.applyDeltaAndBound( moveStart = isLeftBorderActive, moveEnd = isRightBorderActive, delta = hSpanInc, minSize = minHSpan, maxSize = maxHSpan, maxEnd = cellLayout.countX, outputRange = tempRange2, ) cellX = tempRange2.start spanX = tempRange2.size() if (hSpanDelta != 0 ) { directionVector[DIRECTION_HORIZONTAL_INDEX] = if (isLeftBorderActive) DIRECTION_LEFT else DIRECTION_RIGHT } tempRange1.set (cellY, spanY + cellY) val vSpanDelta = tempRange1.applyDeltaAndBound( moveStart = isTopBorderActive, moveEnd = isBottomBorderActive, delta = vSpanInc, minSize = minVSpan, maxSize = maxVSpan, maxEnd = cellLayout.countY, outputRange = tempRange2, ) cellY = tempRange2.start spanY = tempRange2.size() if (vSpanDelta != 0 ) { directionVector[DIRECTION_VERTICAL_INDEX] = if (isTopBorderActive) DIRECTION_TOP else DIRECTION_BOTTOM } if (!onDismiss && vSpanDelta == 0 && hSpanDelta == 0 ) return if (onDismiss) { directionVector[DIRECTION_HORIZONTAL_INDEX] = lastDirectionVector[DIRECTION_HORIZONTAL_INDEX] directionVector[DIRECTION_VERTICAL_INDEX] = lastDirectionVector[DIRECTION_VERTICAL_INDEX] } else { lastDirectionVector[DIRECTION_HORIZONTAL_INDEX] = directionVector[DIRECTION_HORIZONTAL_INDEX] lastDirectionVector[DIRECTION_VERTICAL_INDEX] = directionVector[DIRECTION_VERTICAL_INDEX] } if (widgetView !is PendingAppWidgetHostView && cellLayout.createAreaForResize( cellX, cellY, spanX, spanY, widgetView, directionVector, onDismiss, ) ) { if (wlp.cellHSpan != spanX || wlp.cellVSpan != spanY) { stateAnnouncer?.announce(launcher.getString(R.string.widget_resized, spanX, spanY)) } wlp.tmpCellX = cellX wlp.tmpCellY = cellY wlp.cellHSpan = spanX wlp.cellVSpan = spanY runningVInc += vSpanDelta runningHInc += hSpanDelta if (!onDismiss) { widgetView.updateSizeRanges(spanX, spanY) } } widgetView.requestLayout() }
12.5 RESIZE_THRESHOLD = 0.66 的意义 1 2 3 4 5 6 7 private fun getSpanIncrement (deltaFrac: Float ) : Int { return if (abs(deltaFrac.toDouble()) > RESIZE_THRESHOLD) { Math.round(deltaFrac) } else { 0 } }
为什么是 0.66 而不是 0.5? 这是迟滞设计(hysteresis)。
如果用 0.5:拖过半格就 +1,拖回半格就 -1。手指在半格附近抖动会反复触发增减,widget 尺寸来回跳。
用 0.66:必须拖过 2/3 格才确认变化。这给了一个”确认区”——用户明确拖到下一格的 2/3 位置才生效,减少误触发。Math.round 配合 0.66 阈值:当 deltaFrac = 0.66 时 round(0.66) = 1,当 deltaFrac = -0.66 时 round(-0.66) = -1,当 deltaFrac = 0.4 时不进入 if 返回 0。
这是一个典型的”既不灵敏也不迟钝”的折中——比 0.5 稳,比 0.9 灵敏。
12.6 deltaXAddOn:精度补偿 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 private var deltaXAddOn = 0 private var deltaYAddOn = 0 private var runningHInc = 0 private var runningVInc = 0 val hSpanInc = getSpanIncrement((deltaX + deltaXAddOn) / xThreshold - runningHInc)private fun onTouchUp () { val dp = launcher.deviceProfile val xThreshold = cellLayout.cellWidth + dp.workspaceIconProfile.cellLayoutBorderSpacePx.x val yThreshold = cellLayout.cellHeight + dp.workspaceIconProfile.cellLayoutBorderSpacePx.y deltaXAddOn = runningHInc * xThreshold deltaYAddOn = runningVInc * yThreshold deltaX = 0 ; deltaY = 0 post { snapToWidget(true ) } }
为什么需要 deltaXAddOn? 浮点精度问题。
每次 resizeWidgetIfNeeded 计算增量时:(deltaX + deltaXAddOn) / xThreshold - runningHInc。这里 deltaX 是当前拖拽的像素,deltaXAddOn 是之前已确认应用的像素累计,runningHInc 是已应用的格数。
假设 xThreshold = 100px,用户拖了 deltaX = 70px:
第一次:(70 + 0) / 100 - 0 = 0.7 > 0.66 → hSpanInc = round(0.7) = 1,应用一格,runningHInc = 1。
松手 onTouchUp:deltaXAddOn = 1 * 100 = 100,deltaX = 0。
用户继续拖 deltaX = 50px:(50 + 100) / 100 - 1 = 0.5,不 > 0.66,不变。
如果没有 deltaXAddOn:50 / 100 - 1 = -0.5,会错误地减一格。
deltaXAddOn 把”之前应用的格数”换算回像素累计到当前 delta 上,保证每次计算的基准是连续的,不会因为松手重置 deltaX 而丢失进度。
12.7 IntRange.applyDeltaAndBound:边界约束算法 这是 resize 的数学核心——移动某条边,但不能违反 min/max/边界四重约束:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 @VisibleForTesting class IntRange { var start: Int = 0 var end: Int = 0 fun clamp (value: Int ) = Utilities.boundToRange(value, start, end) fun reset () = set (start = 0 , end = 0 ) fun set (start: Int , end: Int ) { this .start = start; this .end = end } fun size () : Int = end - start fun applyDelta (moveStart: Boolean , moveEnd: Boolean , delta: Int , outputRange: IntRange ) { outputRange.start = if (moveStart) start + delta else start outputRange.end = if (moveEnd) end + delta else end } fun applyDeltaAndBound ( moveStart: Boolean , moveEnd: Boolean , delta: Int , minSize: Int , maxSize: Int , maxEnd: Int , outputRange: IntRange , ) : Int { applyDelta(moveStart, moveEnd, delta, outputRange) outputRange.start = outputRange.start.coerceAtLeast(0 ) outputRange.end = outputRange.end.coerceAtMost(maxEnd) if (outputRange.size() < minSize) { if (moveStart) { outputRange.start = outputRange.end - minSize } else if (moveEnd) { outputRange.end = outputRange.start + minSize } } if (outputRange.size() > maxSize) { if (moveStart) { outputRange.start = outputRange.end - maxSize } else if (moveEnd) { outputRange.end = outputRange.start + maxSize } } return if (moveEnd) { outputRange.size() - size() } else { size() - outputRange.size() } } }
逻辑顺序:先按 delta 平移 → 夹到屏幕边界(0 和 maxEnd)→ 夹到 min span → 夹到 max span 。被夹住的那条边不动,另一条边自由。约束的优先级是:屏幕边界 > min span > max span。
举例:widget 当前 [cellX=2, spanX=3](即占 cell 2~5),minHSpan=2, maxHSpan=4, countX=8:
用户拖左边 handle,delta = -1(想往左扩一格)。
applyDelta:start = 2 + (-1) = 1, end = 5,新范围 [1, 5],size=4。
coerceAtLeast(0):1 ≥ 0,不变。
coerceAtMost(8):5 ≤ 8,不变。
size()=4 不小于 minSize=2,不大于 maxSize=4,不变。
返回 size() - outputRange.size() = 3 - 4 = -1(start 方向,旧 size - 新 size)。
结果:cellX=1, spanX=4,widget 往左扩了一格。
12.8 松手吸附与最终提交 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 private fun onTouchUp () { val dp = launcher.deviceProfile val xThreshold = cellLayout.cellWidth + dp.workspaceIconProfile.cellLayoutBorderSpacePx.x val yThreshold = cellLayout.cellHeight + dp.workspaceIconProfile.cellLayoutBorderSpacePx.y deltaXAddOn = runningHInc * xThreshold deltaYAddOn = runningVInc * yThreshold deltaX = 0 ; deltaY = 0 post { snapToWidget(true ) } } override fun onDetachedFromWindow () { super .onDetachedFromWindow() launcher.dragController.removeDragListener(this ) resizeWidgetIfNeeded(true ) launcher.statsLogManager.logger() .withInstanceId(logInstanceId) .withItemInfo(widgetView.tag as ItemInfo) .log(LauncherEvent.LAUNCHER_WIDGET_RESIZE_COMPLETED) }
onDismiss=true 的 resizeWidgetIfNeeded 把最后一次方向向量应用到 CellLayout.createAreaForResize(commit=true),最终通过 Model 层把新的 spanX/spanY/cellX/cellY 写入 LauncherAppWidgetInfo 持久化到数据库。
12.9 双面板(Two Panel)特例 双面板布局下,widget 不能跨两个 CellLayout。handleInvalidResizeForTwoPanelUi 检测越界:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 private fun handleInvalidResizeForTwoPanelUi ( workspace: Workspace <*>, pairedCellLayout: CellLayout , ) { val focusedCellLayoutBound = TempRect dragLayerRelativeCoordinateHelper.viewToRect(cellLayout, focusedCellLayoutBound) val resizeFrameBound = TempRect2 findViewById<View>(R.id.widget_resize_frame).getGlobalVisibleRect(resizeFrameBound) val progress = when { workspace.indexOfChild(pairedCellLayout) < workspace.indexOfChild(cellLayout) && this .deltaX < 0 && resizeFrameBound.left < focusedCellLayoutBound.left -> ((crossPanelInvalidDragMargin + this .deltaX) / crossPanelInvalidDragMargin) workspace.indexOfChild(pairedCellLayout) > workspace.indexOfChild(cellLayout) && this .deltaX > 0 && resizeFrameBound.right > focusedCellLayoutBound.right -> ((crossPanelInvalidDragMargin - this .deltaX) / crossPanelInvalidDragMargin) else -> SPRING_LOADED_PROGRESS_MAX } val alpha = max(CELL_LAYOUT_INVALID_RESIZE_MAX_ALPHA.toDouble(), progress.toDouble()).toFloat() val springLoadedProgress = min(SPRING_LOADED_PROGRESS_MAX, (SPRING_LOADED_PROGRESS_MAX - progress)) updateInvalidResizeEffect( cellLayout = cellLayout, pairedCellLayout = pairedCellLayout, alpha = alpha, springLoadedProgress = springLoadedProgress, animatorSet = null , ) }
设计意图:拖过头时给视觉反馈(兄弟面板渐变变暗 + spring loaded 进度降低),提示用户”这里不能跨”。松手后 snapToWidget 会把 frame 拉回合法范围。
面试深问 Q1:RESIZE_THRESHOLD=0.66 为什么不用 0.5? A:迟滞防抖。0.5 的话手指在半格附近抖动会反复增减 span,widget 尺寸跳变。0.66 要求拖到下一格的 2/3 才确认,给确认区减少误触。配合 Math.round:delta=0.66 时 round 得 1,delta=0.4 时不变。是灵敏度和稳定性的折中。
Q2:deltaXAddOn 解决什么问题? A:浮点进度连续性。每次 resize 计算增量是 (deltaX + deltaXAddOn) / threshold - runningHInc。松手时 deltaX 重置为 0,但之前应用的格数进度不能丢。deltaXAddOn = runningHInc * threshold 把已应用的格数换算回像素累计,保证下次拖拽的基准连续。没有它,松手后再拖会错误计算增量。
Q3:applyDeltaAndBound 的四重约束优先级是什么? A:屏幕边界(start≥0, end≤maxEnd)> min span > max span。先夹屏幕边界(不能拖出网格),再夹 min(不能小于最小尺寸),最后夹 max(不能超过最大尺寸)。被夹的边不动,另一边自由。返回值是实际变化的格数,用于 runningInc 累计和 deltaXAddOn 计算。
Part 3 · 设计精髓与横向对比 十三、六大设计精髓
**Deep Shortcut 的”先占位后填充”**:长按时从缓存的 deepShortcutMap 拿 count 预占 View 行数(零阻塞),真实 shortcut 数据在后台线程用 ShortcutRequest 异步查询、sortAndFilterShortcuts 排序过滤,回主线程逐条 applyShortcutInfo,配合 LayoutTransition 实现逐条淡入。用户感知不到查询延迟。
排序保证动态性 :sortAndFilterShortcuts 保证最多 4 条且至少 2 条 dynamic,manifest 优先在前。manifest 提供稳定骨架(”新标签页”),dynamic 提供新鲜内容(”继续播放”),兼顾稳定性和新鲜度。
Widget 预加载(WidgetHostViewLoader) :拖拽期间并行完成「分配 id → 绑定 provider → inflate HostView」,把跨进程耗时操作藏进用户拖拽的时间。松手 drop 时 widget 已准备好,几乎瞬间显示。取消时 onDragEnd 干净回收 id 和 HostView,不留资源泄漏。
监听时机省电 :LauncherWidgetHolder 只在 NORMAL + resumed + started 三条件全满足时 startListening,否则用 PendingAppWidgetHostView 占位。Launcher 不可见或处于 AllApps/OVERVIEW 时不接收 RemoteViews 推送,省电省内存。恢复后 updateDeferredView 重新 inflate 延迟的 View。
Resize 的纯像素→格数换算 :把手指的像素 delta 除以单格像素阈值(cell 宽 + 间距),超过 0.66 才算一整格(RESIZE_THRESHOLD),用 IntRange.applyDeltaAndBound 统一处理 min/max/边界四重约束。deltaXAddOn 累计偏移避免浮点精度丢格。
统一拖拽体系 :shortcut 和 widget 都复用 DragController + DragPreviewProvider,区别只在预览生成方式(shortcut 用 ShortcutDragPreviewProvider 缩放到图标尺寸;widget 用 PendingItemDragHelper 的三层预览源)和 drop 后的处理(shortcut 创建 WorkspaceItemInfo;widget 走 addAppWidgetFromDrop 创建 LauncherAppWidgetInfo)。
维度
Deep Shortcut
System Shortcut
Widget
数据来源
app 进程发布(ShortcutManager)
Launcher 内部(Factory)
app 进程发布(AppWidgetProviderInfo)
查询方式
LauncherApps.getShortcuts(跨进程)
内存实例化
AppWidgetManager.getInstalledProviders(跨进程)
缓存
BgDataModel.deepShortcutMap(count)
无(实时 Factory 过滤)
WidgetPickerData(Compose)/ 数据库预览
展示 View
DeepShortcutView(图标+文字+加号)
DeepShortcutView 或 ImageView
LauncherAppWidgetHostView(RemoteViews)
点击行为
启动 shortcut(Intent)
子类 onClick(打开设置/widget picker 等)
widget 本身交互
拖到桌面
创建 WorkspaceItemInfo(CONTAINER_SHORTCUTS)
通常不可拖(Widgets 项打开 picker)
创建 LauncherAppWidgetInfo(绑定 id)
预加载
无(数据轻量)
无
WidgetHostViewLoader(重量级跨进程绑定)
十五、完整生命周期对照 Deep Shortcut 弹出 → 拖到桌面 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 长按图标 → ItemLongClickListener → PopupControllerForAppIcon.show() → PopupDataProvider.getShortcutCountForItem()(读缓存 count) → Launcher.getSupportedShortcuts()(Factory 过滤 system 项) → PopupContainerWithArrow.create() → populateAndShowRows()(按 count 预占行 + show()) → loadAppShortcuts()(后台 MODEL_EXECUTOR) → PopupPopulator.createUpdateRunnable() → ShortcutRequest.withContainer().query(PUBLISHED) → sortAndFilterShortcuts()(最多4,至少2 dynamic) → 逐条 uiHandler.post(applyShortcutInfo) → 用户长按某条 shortcut → LauncherPopupItemDragHandler.onLongClick() → Workspace.beginDragShared(ShortcutDragPreviewProvider) → 拖拽(PreDragCondition 阈值检查) → drop 到桌面 → Launcher.addPendingItem() → 创建 WorkspaceItemInfo 持久化到数据库
Widget 添加全流程 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 打开 picker(FullSheet / WidgetPickerActivity / AddItemBottomSheet) → WidgetManagerHelper.getAllProviders()(getInstalledProvidersForProfile) → 用户长按 widget 预览 → PendingItemDragHelper.startDrag() → 构造预览(缓存位图 > RemoteViews > 数据库生成) → DragController.addDragListener(WidgetHostViewLoader) ★ → DragController.startDrag() → 拖拽开始 → WidgetHostViewLoader.onDragStart() → preloadWidget() → allocateAppWidgetId()(分配 id) → bindAppWidgetIdIfAllowed()(绑定 provider,需权限) → createView()(inflate HostView) → 加到 DragLayer(INVISIBLE) → drop 到 Workspace → Launcher.addAppWidgetFromDrop() → info.boundWidget != null(预加载成功)→ 复用 → 否则现场 allocateAppWidgetId + bindAppWidgetIdIfAllowed → 失败 → startBindFlow()(弹系统授权确认) → addAppWidgetImpl() → startConfigActivity()(如需配置) → completeAddAppWidget()(创建 LauncherAppWidgetInfo 写库) → 配置完成(如需) → onActivityResult() → completeTwoStageWidgetDrop() → Widget 显示
十六、面试高频问答 Q:长按 app 图标弹出的快捷菜单是怎么实现的? A:入口是 PopupControllerForAppIcon.show(),由图标的 OnLongClickListener 触发。它先从 PopupDataProvider 拿该 app 的 deep shortcut 数量(这个数量是 LauncherModel 后台预建在 BgDataModel.deepShortcutMap 里的,不是实时查),同时通过 Launcher.getSupportedShortcuts 收集适用的系统菜单项(根据 container 返回 APP_INFO/WIDGETS/INSTALL/REMOVE 等组合,每个 Factory 内部还会过滤如 WIDGETS 工厂发现没 widget 就返回 null)。然后创建 PopupContainerWithArrow(LinearLayout + AbstractFloatingView),按总数是否超 SHORTCUT_COLLAPSE_THRESHOLD=6 决定折叠策略。容器先按 count 预占行并 show(),再调 PopupPopulator.createUpdateRunnable 在 MODEL_EXECUTOR 后台线程用 ShortcutRequest(封装 LauncherApps.getShortcuts)查真实数据,sortAndFilterShortcuts 排序过滤(最多 4 条、至少 2 条 dynamic、manifest 优先)后回主线程逐条 applyShortcutInfo 填充图标和文字,配合 LayoutTransition 实现逐条淡入。
Q:Deep Shortcut 和 System Shortcut 有什么区别? A:Deep Shortcut 是 app 通过 ShortcutManager API 发布的动态/静态快捷方式(比如”继续播放””新标签页”),数据来自 app 进程,由 LauncherApps.getShortcuts 跨进程查询,用 DeepShortcutView(图标+文字+加号按钮)展示。System Shortcut 是 Launcher 自己提供的菜单项,是 SystemShortcut 抽象类的内部子类(Widgets、AppInfo、Install、RemoveApp 等),它同时 extends ItemInfo 和 implements OnClickListener,由 Launcher.getSupportedShortcuts 根据 container 返回候选 Factory,每个 Factory 内部过滤。两类共用同一个 popup 容器,当总数超 6 时,可折叠的(mIsCollapsible=true,如 AppInfo)被压成图标行,不可折叠的(Widgets、RemoveApp)保留整行带文字。
Q:添加 widget 到桌面的完整流程? A:四步。① 在 widget picker(FullSheet 或新的 WidgetPickerActivity)里长按某个 widget 预览,PendingItemDragHelper.startDrag 构造拖拽预览(三层优先级:缓存位图 > RemoteViews 实时渲染 > 数据库生成)并启动 DragController.startDrag,同时 addDragListener(new WidgetHostViewLoader)。② 拖拽期间 WidgetHostViewLoader.onDragStart 并行预加载:allocateAppWidgetId 分配 id → WidgetManagerHelper.bindAppWidgetIdIfAllowed 绑定 provider → createView inflate 出 HostView 暂时 INVISIBLE 加到 DragLayer,存到 PendingAddWidgetInfo.boundWidget。③ 松手 drop 到 Workspace,addAppWidgetFromDrop 检查 boundWidget:非空直接复用预加载的;否则现场分配 id+绑定,绑定失败走 startBindFlow 发 ACTION_APPWIDGET_BIND 让用户授权。落位后 completeAddAppWidget 在 Model 层创建 LauncherAppWidgetInfo 写库。④ 如果 widget 声明了 configure Activity 且非 optional(needsConfigure 判断),先启动配置页,完成后 completeTwoStageWidgetDrop 回调真正显示。
Q:widget 的数据是怎么更新到桌面的? A:基于 Android 的 AppWidget 框架。Launcher 持有一个 LauncherAppWidgetHost(host id=APPWIDGET_HOST_ID=1024),通过 LauncherWidgetHolder 封装。每个 widget 有个 id,createView(id, providerInfo) 调到 LauncherAppWidgetHost.onCreateView 造 LauncherAppWidgetHostView(有回收机制 mViewToRecycle),它接收 provider 进程通过 RemoteViews 推送过来的视图树并渲染。provider 调 AppWidgetManager.updateAppWidget 时,系统把 RemoteViews 通过 binder 推给 host。一个省电设计是:LauncherWidgetHolder 用 AtomicInteger 维护三个 flag(FLAG_STATE_IS_NORMAL/FLAG_ACTIVITY_STARTED/FLAG_ACTIVITY_RESUMED),只在三个全 on 时 startListening,否则用 PendingAppWidgetHostView 占位,恢复监听后 updateDeferredView 重新 inflate。
Q:widget 调整大小的算法是怎么实现的? A:核心在 AppWidgetResizeFrame.kt。长按 widget 弹出带四个圆点的浮层(DragHandles),根据 AppWidgetProviderInfo.resizeMode(HORIZONTAL/VERTICAL/BOTH)和 min/max span 决定显示哪些 handle。触摸时 beginResizeIfPointInRegion 判断抓的是哪条边并计算允许的 delta 范围(deltaXRange,不拖出屏幕/对边)。visualizeResizeForDelta 把像素 delta 限幅后移动浮层自身跟随手指,然后调 resizeWidgetIfNeeded:把像素 delta 除以单格像素阈值(cellWidth + borderSpacePx)换算成格数增量,getSpanIncrement 判断超过 RESIZE_THRESHOLD=0.66 才算一整格(迟滞防抖),用 IntRange.applyDeltaAndBound 把增量应用到 [cellX, cellX+spanX] 并依次夹到屏幕边界、min span、max span。变化后调 CellLayout.createAreaForResize 让其他图标让位,并 widgetView.updateSizeRanges 通知 widget 新尺寸。松手 onTouchUp 用 deltaXAddOn = runningHInc * threshold 累计偏移避免精度丢失,onDetachedFromWindow 时以 onDismiss=true 提交最终尺寸写库。
Q:widget 预加载为什么能提升体验? A:因为 widget 的”分配 id → 跨进程绑定 provider → inflate RemoteViews”这几步都涉及 binder 调用和跨进程视图渲染,总耗时可能上百毫秒。WidgetHostViewLoader 利用用户拖拽这段时间并行完成——onDragStart 立刻 post 绑定 Runnable,绑定成功立刻 inflate 出 HostView 加到 DragLayer(INVISIBLE)。这样用户松手 drop 时 widget 已经准备好了,几乎瞬间显示,而不是 drop 后才开始绑定导致白屏等待。如果用户拖到一半取消,onDragEnd 会 deleteAppWidgetId 回收预分配的 id 并 removeView 清理 HostView,不留资源泄漏。这是典型的”用并行时间换体验”——把不可避免的耗时操作藏进用户感知不到的拖拽时间段。需要注意:如果 widget needsConfigure(必须配置),不预加载,因为配置会改变最终内容,预绑定会浪费。
Q:sortAndFilterShortcuts 的”至少 2 条 dynamic”规则是怎么实现的? A:排序后先取前 MAX_SHORTCUTS=4 条放入 filtered 列表,同时统计其中 dynamic 数量 numDynamic。然后继续遍历剩余的 shortcut:如果遇到 dynamic 且 numDynamic < NUM_DYNAMIC(2),就 numDynamic++,计算 lastStaticIndex = filteredSize - numDynamic(从尾部往前找 static 的位置),filteredShortcuts.remove(lastStaticIndex) 移除一个 static,再 add 把 dynamic 加进去。这样保证最终结果里至少有 2 条 dynamic,挤掉了同等数量的 static。这反映了设计意图:manifest static 稳定但 stale,dynamic 新鲜但易变,强制保留 dynamic 让用户看到最新行为入口。