@Implementation protected AppWidgetHostView createView( Context context, int appWidgetId, AppWidgetProviderInfo appWidget) { AppWidgetHostView hostView = new AppWidgetHostView(context); hostView.setAppWidget(appWidgetId, appWidget); ShadowAppWidgetHostView shadowAppWidgetHostView = Shadow.extract(hostView); shadowAppWidgetHostView.setHost(realAppWidgetHost); return hostView; } }
@Test public void shouldKnowItsWidgetId() throws Exception { appWidgetHostView.setAppWidget(789, null); assertThat(appWidgetHostView.getAppWidgetId()).isEqualTo(789); }
/** * Adds a widget to the widget area on the MainActivity * * @param appWidgetId id of widget to add */ private void addWidgetToLauncher(int appWidgetId) { // only add widgets if in minimal mode (may need launcher restart when turned on) if (prefs.getBoolean("history-hide", true)) { // remove empty list view when using widgets, this would block touches on the widget mainActivity.emptyListView.setVisibility(View.GONE); //add widget to view AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); if (appWidgetInfo == null) { removeAllWidgets(); return; } AppWidgetHostView hostView = mAppWidgetHost.createView(mainActivity, appWidgetId, appWidgetInfo); hostView.setMinimumHeight(appWidgetInfo.minHeight); hostView.setAppWidget(appWidgetId, appWidgetInfo); if (Build.VERSION.SDK_INT > 15) { hostView.updateAppWidgetSize(null, appWidgetInfo.minWidth, appWidgetInfo.minHeight, appWidgetInfo.minWidth, appWidgetInfo.minHeight); } widgetArea.addView(hostView); } // only one widget allowed so widgetUsed is true now, even if not added to view widgetUsed = true; }
@Test public void shouldKnowItsAppWidgetProviderInfo() throws Exception { AppWidgetProviderInfo providerInfo = new AppWidgetProviderInfo(); appWidgetHostView.setAppWidget(0, providerInfo); assertThat(appWidgetHostView.getAppWidgetInfo()).isSameAs(providerInfo); }
// APPWIDGET_HOST_ID is any number you like appWidgetManager = AppWidgetManager.getInstance(this); appWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID); AppWidgetProviderInfo newAppWidgetProviderInfo = new AppWidgetProviderInfo(); // Get an id int appWidgetId = appWidgetHost.allocateAppWidgetId(); // Get the list of installed widgets List<AppWidgetProviderInfo> appWidgetInfos = new ArrayList<AppWidgetProviderInfo>(); appWidgetInfos = appWidgetManager.getInstalledProviders(); for(int j = 0; j < appWidgetInfos.size(); j++) { if (appWidgetInfos.get(j).provider.getPackageName().equals("com.android.quicksearchbox") && appWidgetInfos.get(j).provider.getClassName().equals("com.android.quicksearchbox.SearchWidgetProvider")) { // Get the full info of the required widget newAppWidgetProviderInfo = appWidgetInfos.get(j); break; } } // Create Widget AppWidgetHostView hostView = appWidgetHost.createView(this, appWidgetId, newAppWidgetProviderInfo); hostView.setAppWidget(appWidgetId, newAppWidgetProviderInfo); // Add it to your layout LinearLayout ll = (LinearLayout) findViewById(R.id.ll); ll.addView(hostView);
@Test public void createView_shouldSetViewsAppWidgetId() throws Exception { AppWidgetHostView hostView = appWidgetHost.createView(context, 765, null); assertThat(hostView.getAppWidgetId()).isEqualTo(765); }
launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo); } else { launcherInfo.hostView.setTag(launcherInfo); launcherInfo.hostView.setVisibility(View.VISIBLE); launcherInfo.notifyWidgetSizeChanged(this);
static int[] getSpanForWidget(Context context, ComponentName component, int minWidth, int minHeight) { Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null); // We want to account for the extra amount of padding that we are adding to the widget // to ensure that it gets the full amount of space that it has requested int requiredWidth = minWidth + padding.left + padding.right; int requiredHeight = minHeight + padding.top + padding.bottom; return CellLayout.rectToCell(requiredWidth, requiredHeight, null); }
@Override public void run() { if (mWidgetLoadingId == -1) { return; } AppWidgetHostView hostView = mLauncher.getAppWidgetHost().createView( (Context) mLauncher, mWidgetLoadingId, pInfo); mInfo.boundWidget = hostView; // We used up the widget Id in binding the above view. mWidgetLoadingId = -1; hostView.setVisibility(View.INVISIBLE); int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(mInfo, false); // We want the first widget layout to be the correct size. This will be important // for width size reporting to the AppWidgetManager. 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); } };
this.mAppWidgetHostView.setAppWidget(appWidgetId, appWidgetInfo); wrapper.addView(this.mAppWidgetHostView); Util.setWidget(this, appWidgetId, appWidgetInfo.provider); ViewGroup parent = (ViewGroup) this.mAppWidgetHostView.getParent(); if (parent != wrapper) { if (parent != null) {
private void prepareAppWidget(AppWidgetHostView hostView, LauncherAppWidgetInfo item) { hostView.setTag(item); item.onBindAppWidget(this, hostView); hostView.setFocusable(true); hostView.setOnFocusChangeListener(mFocusHandler); }
view.updateIcon(mIconCache); item.hostView = view; item.hostView.updateAppWidget(null); item.hostView.setOnClickListener(this); item.hostView.setTag(item); item.onBindAppWidget(this);
public void createWidget(Intent data) { Bundle extras = data.getExtras(); int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(rl.getWidth(), rl.getHeight()/3); lp.topMargin = numWidgets * (rl.getHeight()/3); AppWidgetHostView hostView = mAppWidgetHost.createView(getActivity().getApplicationContext(), appWidgetId, appWidgetInfo); hostView.setAppWidget(widID,appWidgetInfo); rl.addView(hostView, lp); hostView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); numWidgets ++; }
static void updateWidgetSizeRanges(AppWidgetHostView widgetView, Launcher launcher, int spanX, int spanY) { getWidgetSizeRanges(launcher, spanX, spanY, sTmpRect); widgetView.updateAppWidgetSize(null, sTmpRect.left, sTmpRect.top, sTmpRect.right, sTmpRect.bottom); }
/** * Add a host view to the frame layout for a widget id. * @param appWidgetId the widget id */ public void addHostView(final int appWidgetId) { frWidget.removeAllViews(); if (hasAppWidgets(this)) { final AppWidgetProviderInfo appWidgetInfo = appWidgetManager.getAppWidgetInfo(appWidgetId); if (appWidgetInfo != null) { final AppWidgetHostView hostView = appWidgetHost.createView(this, appWidgetId, appWidgetInfo); hostView.setAppWidget(appWidgetId, appWidgetInfo); frWidget.addView(hostView); frWidget.requestLayout(); final Bundle options = BundleUtil.getWidgetOptionsBundle(frWidget.getMeasuredWidth(), frWidget.getMeasuredHeight(), frWidget.getMeasuredWidth(), frWidget.getMeasuredHeight()); hostView.updateAppWidgetOptions(options); } else { model.setAppWidgetId(-1); } } }
@Test public void createView_shouldSetViewsAppWidgetInfo() throws Exception { AppWidgetProviderInfo info = new AppWidgetProviderInfo(); AppWidgetHostView hostView = appWidgetHost.createView(context, 0, info); assertThat(hostView.getAppWidgetInfo()).isSameAs(info); }
@Override public void cancelLongPress() { super.cancelLongPress(); mLongPressHelper.cancelLongPress(); }
@Override protected void onAttachedToWindow() { super.onAttachedToWindow(); mSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); }
@Override public void run() { appwid.updateAppWidgetSize(null, wDp, hDpf, wDp, hDpf); if (appwid.getParent()!=null) { appwid.getParent().requestLayout(); } appwid.requestLayout(); appwid.postInvalidate(); } }, 1000);
AppWidgetProviderInfo pinfo = hostView.getAppWidgetInfo(); ViewGroup parent = (ViewGroup) hostView.getParent(); if (parent != null) { parent.removeView(hostView); hostView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { hostView.setOnDragListener(new View.OnDragListener() { @Override public boolean onDrag(View view, DragEvent dragEvent) {