Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0be3a615 authored by Steven Ng's avatar Steven Ng
Browse files

Render preview layout in full widgets sheet and bottom widgets sheet

Video: https://drive.google.com/file/d/1gRE0axaq6tm3R_gjSon0RR5lC8HCmwee/view?usp=sharing

Test: Install a sample app which provides a preview layout.
      Go to full widgets sheet to observe widget previews are correctly
      displayed for bitmap previews, layout previews and placeholder
      previews. Repeat the same for bottom widgets picker.
      Pin a widget from a sample app which provide a custom RemoteViews
      preview, observe the custom RemoteViews is correctly rendered.

Bug: 179797520
Change-Id: I2ccd1f77b54a2430d842ee25349fa6e54b9159dc
parent 9199fd18
Loading
Loading
Loading
Loading
+28 −0
Original line number Original line Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2021 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<com.android.launcher3.dragndrop.LivePreviewWidgetCell
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:focusable="true"
    android:background="?android:attr/colorPrimaryDark"
    android:gravity="center_horizontal">

    <include layout="@layout/widget_cell_content"  />

</com.android.launcher3.dragndrop.LivePreviewWidgetCell>
 No newline at end of file
+23 −0
Original line number Original line Diff line number Diff line
@@ -10,7 +10,9 @@ import android.widget.RemoteViews;
import com.android.launcher3.BaseActivity;
import com.android.launcher3.BaseActivity;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.LauncherAppWidgetProviderInfo;
import com.android.launcher3.LauncherAppWidgetProviderInfo;
import com.android.launcher3.WidgetPreviewLoader;
import com.android.launcher3.icons.BitmapRenderer;
import com.android.launcher3.icons.BitmapRenderer;
import com.android.launcher3.model.WidgetItem;
import com.android.launcher3.widget.WidgetCell;
import com.android.launcher3.widget.WidgetCell;


/**
/**
@@ -36,6 +38,15 @@ public class LivePreviewWidgetCell extends WidgetCell {
        mPreview = view;
        mPreview = view;
    }
    }


    public RemoteViews getPreview() {
        return mPreview;
    }

    /** Resets any resource. This should be called before recycling this view. */
    public void reset() {
        mPreview = null;
    }

    @Override
    @Override
    public void ensurePreview() {
    public void ensurePreview() {
        if (mPreview != null && mActiveRequest == null) {
        if (mPreview != null && mActiveRequest == null) {
@@ -49,6 +60,18 @@ public class LivePreviewWidgetCell extends WidgetCell {
        super.ensurePreview();
        super.ensurePreview();
    }
    }


    @Override
    public void applyFromCellItem(WidgetItem item, WidgetPreviewLoader loader) {
        if (mPreview == null
                && item.widgetInfo != null
                && item.widgetInfo.previewLayout != View.NO_ID) {
            mPreview = new RemoteViews(item.widgetInfo.provider.getPackageName(),
                    item.widgetInfo.previewLayout);
        }

        super.applyFromCellItem(item, loader);
    }

    /**
    /**
     * Generates a bitmap by inflating {@param views}.
     * Generates a bitmap by inflating {@param views}.
     * @see com.android.launcher3.WidgetPreviewLoader#generateWidgetPreview
     * @see com.android.launcher3.WidgetPreviewLoader#generateWidgetPreview
+8 −3
Original line number Original line Diff line number Diff line
@@ -30,6 +30,7 @@ import com.android.launcher3.DropTarget.DragObject;
import com.android.launcher3.R;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.Utilities;
import com.android.launcher3.dragndrop.DragOptions;
import com.android.launcher3.dragndrop.DragOptions;
import com.android.launcher3.dragndrop.LivePreviewWidgetCell;
import com.android.launcher3.popup.PopupDataProvider;
import com.android.launcher3.popup.PopupDataProvider;
import com.android.launcher3.testing.TestLogging;
import com.android.launcher3.testing.TestLogging;
import com.android.launcher3.testing.TestProtocol;
import com.android.launcher3.testing.TestProtocol;
@@ -99,12 +100,16 @@ public abstract class BaseWidgetSheet extends AbstractSlideInView
            return false;
            return false;
        }
        }


        PendingItemDragHelper dragHelper = new PendingItemDragHelper(v);
        if (v instanceof LivePreviewWidgetCell) {
            dragHelper.setPreview(((LivePreviewWidgetCell) v).getPreview());
        }

        int[] loc = new int[2];
        int[] loc = new int[2];
        getPopupContainer().getLocationInDragLayer(image, loc);
        getPopupContainer().getLocationInDragLayer(image, loc);


        new PendingItemDragHelper(v).startDrag(
        dragHelper.startDrag(image.getBitmapBounds(), image.getBitmap().getWidth(),
                image.getBitmapBounds(), image.getBitmap().getWidth(), image.getWidth(),
                image.getWidth(), new Point(loc[0], loc[1]), this, new DragOptions());
                new Point(loc[0], loc[1]), this, new DragOptions());
        close(true);
        close(true);
        return true;
        return true;
    }
    }
+4 −2
Original line number Original line Diff line number Diff line
@@ -25,6 +25,8 @@ import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.RemoteViews;


import androidx.annotation.Nullable;

import com.android.launcher3.DeviceProfile;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.DragSource;
import com.android.launcher3.DragSource;
import com.android.launcher3.Launcher;
import com.android.launcher3.Launcher;
@@ -48,14 +50,14 @@ public class PendingItemDragHelper extends DragPreviewProvider {
    private final PendingAddItemInfo mAddInfo;
    private final PendingAddItemInfo mAddInfo;
    private int[] mEstimatedCellSize;
    private int[] mEstimatedCellSize;


    private RemoteViews mPreview;
    @Nullable private RemoteViews mPreview;


    public PendingItemDragHelper(View view) {
    public PendingItemDragHelper(View view) {
        super(view);
        super(view);
        mAddInfo = (PendingAddItemInfo) view.getTag();
        mAddInfo = (PendingAddItemInfo) view.getTag();
    }
    }


    public void setPreview(RemoteViews preview) {
    public void setPreview(@Nullable RemoteViews preview) {
        mPreview = preview;
        mPreview = preview;
    }
    }


+3 −2
Original line number Original line Diff line number Diff line
@@ -36,6 +36,7 @@ import com.android.launcher3.LauncherAppState;
import com.android.launcher3.R;
import com.android.launcher3.R;
import com.android.launcher3.ResourceUtils;
import com.android.launcher3.ResourceUtils;
import com.android.launcher3.anim.PendingAnimation;
import com.android.launcher3.anim.PendingAnimation;
import com.android.launcher3.dragndrop.LivePreviewWidgetCell;
import com.android.launcher3.model.WidgetItem;
import com.android.launcher3.model.WidgetItem;
import com.android.launcher3.model.data.ItemInfo;
import com.android.launcher3.model.data.ItemInfo;
import com.android.launcher3.util.PackageUserKey;
import com.android.launcher3.util.PackageUserKey;
@@ -136,8 +137,8 @@ public class WidgetsBottomSheet extends BaseWidgetSheet implements Insettable {
    }
    }


    protected WidgetCell addItemCell(ViewGroup parent) {
    protected WidgetCell addItemCell(ViewGroup parent) {
        WidgetCell widget = (WidgetCell) LayoutInflater.from(getContext()).inflate(
        LivePreviewWidgetCell widget = (LivePreviewWidgetCell) LayoutInflater.from(
                R.layout.widget_cell, parent, false);
                getContext()).inflate(R.layout.live_preview_widget_cell, parent, false);


        widget.setOnClickListener(this);
        widget.setOnClickListener(this);
        widget.setOnLongClickListener(this);
        widget.setOnLongClickListener(this);
Loading