Luuncher onCreate stupViews 初始化com.android.launcher3
.Workspace 控件,构造方法中实现setOnTouchListener(new WorkspaceTouchListener(mLauncher, this));
在 WorkspaceTouchListener的onLongPress中
弹出OptionsPopupView.showDefaultOptions(mLauncher, mTouchDownPoint.x, mTouchDownPoint.y);,长按桌面弹出小部件选择,选择小部件
/** Returns WidgetsFullSheet that was opened, or null if nothing was opened. /
@Nullable
public static WidgetsFullSheet openWidgets(Launcher launcher) {
if (launcher.getPackageManager().isSafeMode()) {
// 在安全模式下無法使用小工具
Toast.makeText(launcher, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
return null;
} else {
return WidgetsFullSheet.show(launcher, true / animated */);
}
}
WidgetsFullSheet 控件中加载小部件到 WidgetsRecyclerView中
/** Gets the {@link WidgetsRecyclerView} which shows all widgets in {@link WidgetsFullSheet}. */
@VisibleForTesting
public static WidgetsRecyclerView getWidgetsView(Launcher launcher) {
return launcher.findViewById(R.id.primary_widgets_list_view);
}
在BaseWidgetSheet的onLongClick中开发创建Widget, beginDraggingWidget方法具体创建,走到startDrag方法
private boolean beginDraggingWidget(WidgetCell v) {
// Get the widget preview as the drag representation
WidgetImageView image = v.getWidgetView();
// If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
// we abort the drag.
if (image.getDrawable() == null && v.getAppWidgetHostViewPreview() == null) {
return false;
}
PendingItemDragHelper dragHelper = new PendingItemDragHelper(v);
dragHelper.setRemoteViewsPreview(v.getRemoteViewsPreview());
dragHelper.setAppWidgetHostViewPreview(v.getAppWidgetHostViewPreview());
if (image.getDrawable() != null) {
int[] loc = new int[2];
getPopupContainer().getLocationInDragLayer(image, loc);
dragHelper.startDrag(image.getBitmapBounds(), image.getDrawable().getIntrinsicWidth(),
image.getWidth(), new Point(loc[0], loc[1]), this, new DragOptions());
} else {
View preview = v.getAppWidgetHostViewPreview();
int[] loc = new int[2];
getPopupContainer().getLocationInDragLayer(preview, loc);
Rect r = new Rect(0, 0, preview.getWidth(), preview.getHeight());
dragHelper.startDrag(r, preview.getMeasuredWidth(), preview.getMeasuredWidth(),
new Point(loc[0], loc[1]), this, new DragOptions());
}
close(true);
return true;
}
首先调用AppWidgetHostView.setAppWidget(int appWidgetId, AppWidgetProviderInfo info)
,设置AppWidget的显示View和padding,appWidgetId默认-1
拖动Widget ,DragController.DragListener的实现WidgetHostViewLoader.onDragStart
(),
/**
* Start preloading the widget.
*/
private boolean preloadWidget()