Search in sources :

Example 46 with LauncherAppWidgetProviderInfo

use of com.android.launcher3.LauncherAppWidgetProviderInfo in project Neo-Launcher by NeoApplications.

the class CustomWidgetParser method parseCustomWidgets.

private static void parseCustomWidgets(Context context) {
    ArrayList<LauncherAppWidgetProviderInfo> widgets = new ArrayList<>();
    SparseArray<ComponentName> idMap = new SparseArray<>();
    List<AppWidgetProviderInfo> providers = AppWidgetManager.getInstance(context).getInstalledProvidersForProfile(Process.myUserHandle());
    if (providers.isEmpty()) {
        sCustomWidgets = widgets;
        sWidgetsIdMap = idMap;
        return;
    }
    Parcel parcel = Parcel.obtain();
    providers.get(0).writeToParcel(parcel, 0);
    try (XmlResourceParser parser = context.getResources().getXml(R.xml.custom_widgets)) {
        final int depth = parser.getDepth();
        int type;
        while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
            if ((type == XmlPullParser.START_TAG) && "widget".equals(parser.getName())) {
                TypedArray a = context.obtainStyledAttributes(Xml.asAttributeSet(parser), R.styleable.CustomAppWidgetProviderInfo);
                parcel.setDataPosition(0);
                CustomAppWidgetProviderInfo info = newInfo(a, parcel, context);
                widgets.add(info);
                a.recycle();
                idMap.put(info.providerId, info.provider);
            }
        }
    } catch (IOException | XmlPullParserException e) {
        throw new RuntimeException(e);
    }
    parcel.recycle();
    sCustomWidgets = widgets;
    sWidgetsIdMap = idMap;
}
Also used : LauncherAppWidgetProviderInfo(com.android.launcher3.LauncherAppWidgetProviderInfo) XmlResourceParser(android.content.res.XmlResourceParser) Parcel(android.os.Parcel) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SparseArray(android.util.SparseArray) TypedArray(android.content.res.TypedArray) LauncherAppWidgetProviderInfo(com.android.launcher3.LauncherAppWidgetProviderInfo) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) ComponentName(android.content.ComponentName) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 47 with LauncherAppWidgetProviderInfo

use of com.android.launcher3.LauncherAppWidgetProviderInfo in project android_packages_apps_Launcher3 by ArrowOS.

the class Launcher method completeAddAppWidget.

/**
 * Add a widget to the workspace.
 *
 * @param appWidgetId The app widget id
 */
@Thunk
void completeAddAppWidget(int appWidgetId, ItemInfo itemInfo, AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
    if (appWidgetInfo == null) {
        appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
    }
    if (hostView == null) {
        // Perform actual inflation because we're live
        hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
    }
    LauncherAppWidgetInfo launcherInfo;
    launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider, appWidgetInfo, hostView);
    launcherInfo.spanX = itemInfo.spanX;
    launcherInfo.spanY = itemInfo.spanY;
    launcherInfo.minSpanX = itemInfo.minSpanX;
    launcherInfo.minSpanY = itemInfo.minSpanY;
    launcherInfo.user = appWidgetInfo.getProfile();
    if (itemInfo instanceof PendingAddWidgetInfo) {
        launcherInfo.sourceContainer = ((PendingAddWidgetInfo) itemInfo).sourceContainer;
    } else if (itemInfo instanceof PendingRequestArgs) {
        launcherInfo.sourceContainer = ((PendingRequestArgs) itemInfo).getWidgetSourceContainer();
    }
    getModelWriter().addItemToDatabase(launcherInfo, itemInfo.container, itemInfo.screenId, itemInfo.cellX, itemInfo.cellY);
    hostView.setVisibility(View.VISIBLE);
    prepareAppWidget(hostView, launcherInfo);
    mWorkspace.addInScreen(hostView, launcherInfo);
    announceForAccessibility(R.string.item_added_to_workspace);
    // Show the widget resize frame.
    if (hostView instanceof LauncherAppWidgetHostView) {
        final LauncherAppWidgetHostView launcherHostView = (LauncherAppWidgetHostView) hostView;
        CellLayout cellLayout = getCellLayout(launcherInfo.container, launcherInfo.screenId);
        if (mStateManager.getState() == NORMAL) {
            AppWidgetResizeFrame.showForWidget(launcherHostView, cellLayout);
        } else {
            mStateManager.addStateListener(new StateManager.StateListener<LauncherState>() {

                @Override
                public void onStateTransitionComplete(LauncherState finalState) {
                    if (mPrevLauncherState == SPRING_LOADED && finalState == NORMAL) {
                        AppWidgetResizeFrame.showForWidget(launcherHostView, cellLayout);
                        mStateManager.removeStateListener(this);
                    }
                }
            });
        }
    }
}
Also used : StateManager(com.android.launcher3.statemanager.StateManager) PendingAddWidgetInfo(com.android.launcher3.widget.PendingAddWidgetInfo) LauncherAppWidgetInfo(com.android.launcher3.model.data.LauncherAppWidgetInfo) LauncherAppWidgetHostView(com.android.launcher3.widget.LauncherAppWidgetHostView) PendingRequestArgs(com.android.launcher3.util.PendingRequestArgs) Thunk(com.android.launcher3.util.Thunk)

Example 48 with LauncherAppWidgetProviderInfo

use of com.android.launcher3.LauncherAppWidgetProviderInfo in project android_packages_apps_Launcher3 by ArrowOS.

the class DatabaseWidgetPreviewLoader method generateWidgetPreview.

/**
 * Generates the widget preview from either the {@link WidgetManagerHelper} or cache
 * and add badge at the bottom right corner.
 *
 * @param info                        information about the widget
 * @param maxPreviewWidth             width of the preview on either workspace or tray
 * @param preScaledWidthOut           return the width of the returned bitmap
 */
public Bitmap generateWidgetPreview(LauncherAppWidgetProviderInfo info, int maxPreviewWidth, int[] preScaledWidthOut) {
    // Load the preview image if possible
    if (maxPreviewWidth < 0)
        maxPreviewWidth = Integer.MAX_VALUE;
    Drawable drawable = null;
    if (info.previewImage != 0) {
        try {
            drawable = info.loadPreviewImage(mContext, 0);
        } catch (OutOfMemoryError e) {
            Log.w(TAG, "Error loading widget preview for: " + info.provider, e);
            // During OutOfMemoryError, the previous heap stack is not affected. Catching
            // an OOM error here should be safe & not affect other parts of launcher.
            drawable = null;
        }
        if (drawable != null) {
            drawable = mutateOnMainThread(drawable);
        } else {
            Log.w(TAG, "Can't load widget preview drawable 0x" + Integer.toHexString(info.previewImage) + " for provider: " + info.provider);
        }
    }
    final boolean widgetPreviewExists = (drawable != null);
    final int spanX = info.spanX;
    final int spanY = info.spanY;
    int previewWidth;
    int previewHeight;
    DeviceProfile dp = ActivityContext.lookupContext(mContext).getDeviceProfile();
    if (widgetPreviewExists && drawable.getIntrinsicWidth() > 0 && drawable.getIntrinsicHeight() > 0) {
        previewWidth = drawable.getIntrinsicWidth();
        previewHeight = drawable.getIntrinsicHeight();
    } else {
        Size widgetSize = WidgetSizes.getWidgetPaddedSizePx(mContext, info.provider, dp, spanX, spanY);
        previewWidth = widgetSize.getWidth();
        previewHeight = widgetSize.getHeight();
    }
    if (preScaledWidthOut != null) {
        preScaledWidthOut[0] = previewWidth;
    }
    // Scale to fit width only - let the widget preview be clipped in the
    // vertical dimension
    final float scale = previewWidth > maxPreviewWidth ? (maxPreviewWidth / (float) (previewWidth)) : 1f;
    if (scale != 1f) {
        previewWidth = Math.max((int) (scale * previewWidth), 1);
        previewHeight = Math.max((int) (scale * previewHeight), 1);
    }
    final int previewWidthF = previewWidth;
    final int previewHeightF = previewHeight;
    final Drawable drawableF = drawable;
    return BitmapRenderer.createHardwareBitmap(previewWidth, previewHeight, c -> {
        // Draw the scaled preview into the final bitmap
        if (widgetPreviewExists) {
            drawableF.setBounds(0, 0, previewWidthF, previewHeightF);
            drawableF.draw(c);
        } else {
            RectF boxRect;
            // Draw horizontal and vertical lines to represent individual columns.
            final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
            if (Utilities.ATLEAST_S) {
                boxRect = new RectF(/* left= */
                0, /* top= */
                0, /* right= */
                previewWidthF, /* bottom= */
                previewHeightF);
                p.setStyle(Paint.Style.FILL);
                p.setColor(Color.WHITE);
                float roundedCorner = mContext.getResources().getDimension(android.R.dimen.system_app_widget_background_radius);
                c.drawRoundRect(boxRect, roundedCorner, roundedCorner, p);
            } else {
                boxRect = drawBoxWithShadow(c, previewWidthF, previewHeightF);
            }
            p.setStyle(Paint.Style.STROKE);
            p.setStrokeWidth(mContext.getResources().getDimension(R.dimen.widget_preview_cell_divider_width));
            p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            float t = boxRect.left;
            float tileSize = boxRect.width() / spanX;
            for (int i = 1; i < spanX; i++) {
                t += tileSize;
                c.drawLine(t, 0, t, previewHeightF, p);
            }
            t = boxRect.top;
            tileSize = boxRect.height() / spanY;
            for (int i = 1; i < spanY; i++) {
                t += tileSize;
                c.drawLine(0, t, previewWidthF, t, p);
            }
            // Draw icon in the center.
            try {
                Drawable icon = LauncherAppState.getInstance(mContext).getIconCache().getFullResIcon(info.provider.getPackageName(), info.icon);
                if (icon != null) {
                    int appIconSize = dp.iconSizePx;
                    int iconSize = (int) Math.min(appIconSize * scale, Math.min(boxRect.width(), boxRect.height()));
                    icon = mutateOnMainThread(icon);
                    int hoffset = (previewWidthF - iconSize) / 2;
                    int yoffset = (previewHeightF - iconSize) / 2;
                    icon.setBounds(hoffset, yoffset, hoffset + iconSize, yoffset + iconSize);
                    icon.draw(c);
                }
            } catch (Resources.NotFoundException e) {
            }
        }
    });
}
Also used : RectF(android.graphics.RectF) DeviceProfile(com.android.launcher3.DeviceProfile) Size(android.util.Size) PorterDuffXfermode(android.graphics.PorterDuffXfermode) Drawable(android.graphics.drawable.Drawable) FastBitmapDrawable(com.android.launcher3.icons.FastBitmapDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Paint(android.graphics.Paint) Resources(android.content.res.Resources) Paint(android.graphics.Paint)

Example 49 with LauncherAppWidgetProviderInfo

use of com.android.launcher3.LauncherAppWidgetProviderInfo in project android_packages_apps_Launcher3 by ArrowOS.

the class WidgetCell method applyPreviewOnAppWidgetHostView.

private void applyPreviewOnAppWidgetHostView(WidgetItem item) {
    if (mRemoteViewsPreview != null) {
        mAppWidgetHostViewPreview = createAppWidgetHostView(getContext());
        setAppWidgetHostViewPreview(mAppWidgetHostViewPreview, item.widgetInfo, mRemoteViewsPreview);
        return;
    }
    if (!item.hasPreviewLayout())
        return;
    Context context = getContext();
    // If the context is a Launcher activity, DragView will show mAppWidgetHostViewPreview as
    // a preview during drag & drop. And thus, we should use LauncherAppWidgetHostView, which
    // supports applying local color extraction during drag & drop.
    mAppWidgetHostViewPreview = isLauncherContext(context) ? new LauncherAppWidgetHostView(context) : createAppWidgetHostView(context);
    LauncherAppWidgetProviderInfo launcherAppWidgetProviderInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(context, item.widgetInfo.clone());
    // A hack to force the initial layout to be the preview layout since there is no API for
    // rendering a preview layout for work profile apps yet. For non-work profile layout, a
    // proper solution is to use RemoteViews(PackageName, LayoutId).
    launcherAppWidgetProviderInfo.initialLayout = item.widgetInfo.previewLayout;
    setAppWidgetHostViewPreview(mAppWidgetHostViewPreview, launcherAppWidgetProviderInfo, /* remoteViews= */
    null);
}
Also used : Context(android.content.Context) ActivityContext(com.android.launcher3.views.ActivityContext)

Example 50 with LauncherAppWidgetProviderInfo

use of com.android.launcher3.LauncherAppWidgetProviderInfo in project android_packages_apps_Launcher3 by ArrowOS.

the class ItemClickHandler method onClickPendingWidget.

/**
 * Event handler for the app widget view which has not fully restored.
 */
private static void onClickPendingWidget(PendingAppWidgetHostView v, Launcher launcher) {
    if (launcher.getPackageManager().isSafeMode()) {
        Toast.makeText(launcher, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
        return;
    }
    final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
    if (v.isReadyForClickSetup()) {
        LauncherAppWidgetProviderInfo appWidgetInfo = new WidgetManagerHelper(launcher).findProvider(info.providerName, info.user);
        if (appWidgetInfo == null) {
            return;
        }
        WidgetAddFlowHandler addFlowHandler = new WidgetAddFlowHandler(appWidgetInfo);
        if (info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
            if (!info.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_ALLOCATED)) {
                // This should not happen, as we make sure that an Id is allocated during bind.
                return;
            }
            addFlowHandler.startBindFlow(launcher, info.appWidgetId, info, REQUEST_BIND_PENDING_APPWIDGET);
        } else {
            addFlowHandler.startConfigActivity(launcher, info, REQUEST_RECONFIGURE_APPWIDGET);
        }
    } else {
        final String packageName = info.providerName.getPackageName();
        onClickPendingAppItem(v, launcher, packageName, info.installProgress >= 0);
    }
}
Also used : LauncherAppWidgetProviderInfo(com.android.launcher3.widget.LauncherAppWidgetProviderInfo) WidgetManagerHelper(com.android.launcher3.widget.WidgetManagerHelper) LauncherAppWidgetInfo(com.android.launcher3.model.data.LauncherAppWidgetInfo) WidgetAddFlowHandler(com.android.launcher3.widget.WidgetAddFlowHandler)

Aggregations

Test (org.junit.Test)108 LauncherAppWidgetProviderInfo (com.android.launcher3.widget.LauncherAppWidgetProviderInfo)81 InvariantDeviceProfile (com.android.launcher3.InvariantDeviceProfile)77 LauncherAppWidgetInfo (com.android.launcher3.model.data.LauncherAppWidgetInfo)65 SmallTest (androidx.test.filters.SmallTest)52 LargeTest (androidx.test.filters.LargeTest)42 AbstractLauncherUiTest (com.android.launcher3.ui.AbstractLauncherUiTest)42 PendingAddWidgetInfo (com.android.launcher3.widget.PendingAddWidgetInfo)33 Bundle (android.os.Bundle)32 WidgetManagerHelper (com.android.launcher3.widget.WidgetManagerHelper)31 Point (android.graphics.Point)29 LauncherAppWidgetProviderInfo (com.android.launcher3.LauncherAppWidgetProviderInfo)28 AppWidgetProviderInfo (android.appwidget.AppWidgetProviderInfo)23 Context (android.content.Context)23 DeviceProfile (com.android.launcher3.DeviceProfile)23 ArrayList (java.util.ArrayList)21 AppWidgetHostView (android.appwidget.AppWidgetHostView)20 WidgetItem (com.android.launcher3.model.WidgetItem)17 LauncherAppWidgetHostView (com.android.launcher3.widget.LauncherAppWidgetHostView)14 WidgetAddFlowHandler (com.android.launcher3.widget.WidgetAddFlowHandler)14