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

Commit c65a0085 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Adding support for showing the widget preview based on the provided RemoteViews

Bug: 35811129
Change-Id: I336e48cd00cfec2e617ac73bd8a81419b0944aa7
parent efd5cbf5
Loading
Loading
Loading
Loading
+12 −4
Original line number Diff line number Diff line
@@ -36,12 +36,20 @@
        android:background="?android:attr/colorPrimaryDark"
        android:layout_height="wrap_content">

        <include
            layout="@layout/widget_cell"
            android:layout_gravity="center_horizontal"
        <com.android.launcher3.dragndrop.LivePreviewWidgetCell
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/widget_cell" />
            android:layout_weight="1"
            android:orientation="vertical"
            android:focusable="true"
            android:background="?android:attr/colorPrimaryDark"
            android:gravity="center_horizontal"
            android:id="@+id/widget_cell"
            android:layout_gravity="center_horizontal" >

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

        </com.android.launcher3.dragndrop.LivePreviewWidgetCell>
    </FrameLayout>

    <LinearLayout
+1 −44
Original line number Diff line number Diff line
@@ -23,49 +23,6 @@
    android:background="?android:attr/colorPrimaryDark"
    android:gravity="center_horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/widget_preview_label_vertical_padding"
        android:paddingBottom="@dimen/widget_preview_label_vertical_padding"
        android:paddingLeft="@dimen/widget_preview_label_horizontal_padding"
        android:paddingRight="@dimen/widget_preview_label_horizontal_padding"
        android:orientation="horizontal">

        <!-- The name of the widget. -->
        <TextView
            android:id="@+id/widget_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
            android:fadingEdge="horizontal"
            android:fontFamily="sans-serif-condensed"
            android:gravity="start"
            android:singleLine="true"
            android:maxLines="1"
            android:textColor="?android:attr/textColorSecondary"
            android:textSize="14sp" />

        <!-- The original dimensions of the widget (can't be the same text as above due to different
             style. -->
        <TextView
            android:id="@+id/widget_dims"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:textColor="?android:attr/textColorSecondary"
            android:textSize="14sp"
            android:fontFamily="sans-serif-condensed"
            android:alpha="0.8" />
    </LinearLayout>
    <include layout="@layout/widget_cell_content"  />

    <!-- The image of the widget. This view does not support padding. Any placement adjustment
         should be done using margins. -->
    <com.android.launcher3.widget.WidgetImageView
        android:id="@+id/widget_preview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</com.android.launcher3.widget.WidgetCell>
 No newline at end of file
+65 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2017 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.
-->
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/widget_preview_label_vertical_padding"
        android:paddingBottom="@dimen/widget_preview_label_vertical_padding"
        android:paddingLeft="@dimen/widget_preview_label_horizontal_padding"
        android:paddingRight="@dimen/widget_preview_label_horizontal_padding"
        android:orientation="horizontal">

        <!-- The name of the widget. -->
        <TextView
            android:id="@+id/widget_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
            android:fadingEdge="horizontal"
            android:fontFamily="sans-serif-condensed"
            android:gravity="start"
            android:singleLine="true"
            android:maxLines="1"
            android:textColor="?android:attr/textColorSecondary"
            android:textSize="14sp" />

        <!-- The original dimensions of the widget (can't be the same text as above due to different
             style. -->
        <TextView
            android:id="@+id/widget_dims"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:textColor="?android:attr/textColorSecondary"
            android:textSize="14sp"
            android:fontFamily="sans-serif-condensed"
            android:alpha="0.8" />
    </LinearLayout>

    <!-- The image of the widget. This view does not support padding. Any placement adjustment
         should be done using margins. -->
    <com.android.launcher3.widget.WidgetImageView
        android:id="@+id/widget_preview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</merge>
 No newline at end of file
+30 −38
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.CancellationSignal;
import android.os.Handler;
import android.os.UserHandle;
import android.support.v4.graphics.ColorUtils;
@@ -89,14 +90,17 @@ public class WidgetPreviewLoader {
     *
     * @return a request id which can be used to cancel the request.
     */
    public PreviewLoadRequest getPreview(WidgetItem item, int previewWidth,
    public CancellationSignal getPreview(WidgetItem item, int previewWidth,
            int previewHeight, WidgetCell caller) {
        String size = previewWidth + "x" + previewHeight;
        WidgetCacheKey key = new WidgetCacheKey(item.componentName, item.user, size);

        PreviewLoadTask task = new PreviewLoadTask(key, item, previewWidth, previewHeight, caller);
        task.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
        return new PreviewLoadRequest(task);

        CancellationSignal signal = new CancellationSignal();
        signal.setOnCancelListener(task);
        return signal;
    }

    /**
@@ -510,42 +514,8 @@ public class WidgetPreviewLoader {
        }
    }

    /**
     * A request Id which can be used by the client to cancel any request.
     */
    public class PreviewLoadRequest {

        @Thunk final PreviewLoadTask mTask;

        public PreviewLoadRequest(PreviewLoadTask task) {
            mTask = task;
        }

        public void cleanup() {
            if (mTask != null) {
                mTask.cancel(true);
            }

            // This only handles the case where the PreviewLoadTask is cancelled after the task has
            // successfully completed (including having written to disk when necessary).  In the
            // other cases where it is cancelled while the task is running, it will be cleaned up
            // in the tasks's onCancelled() call, and if cancelled while the task is writing to
            // disk, it will be cancelled in the task's onPostExecute() call.
            if (mTask.mBitmapToRecycle != null) {
                mWorkerHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (mUnusedBitmaps) {
                            mUnusedBitmaps.add(mTask.mBitmapToRecycle);
                        }
                        mTask.mBitmapToRecycle = null;
                    }
                });
            }
        }
    }

    public class PreviewLoadTask extends AsyncTask<Void, Void, Bitmap> {
    public class PreviewLoadTask extends AsyncTask<Void, Void, Bitmap>
            implements CancellationSignal.OnCancelListener {
        @Thunk final WidgetCacheKey mKey;
        private final WidgetItem mInfo;
        private final int mPreviewHeight;
@@ -661,6 +631,28 @@ public class WidgetPreviewLoader {
                });
            }
        }

        @Override
        public void onCancel() {
            cancel(true);

            // This only handles the case where the PreviewLoadTask is cancelled after the task has
            // successfully completed (including having written to disk when necessary).  In the
            // other cases where it is cancelled while the task is running, it will be cleaned up
            // in the tasks's onCancelled() call, and if cancelled while the task is writing to
            // disk, it will be cancelled in the task's onPostExecute() call.
            if (mBitmapToRecycle != null) {
                mWorkerHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (mUnusedBitmaps) {
                            mUnusedBitmaps.add(mBitmapToRecycle);
                        }
                        mBitmapToRecycle = null;
                    }
                });
            }
        }
    }

    private static final class WidgetCacheKey extends ComponentKey {
+8 −0
Original line number Diff line number Diff line
@@ -76,6 +76,14 @@ public class PinItemRequestCompat implements Parcelable {
        }
    }

    public Bundle getExtras() {
        try {
            return (Bundle) mObject.getClass().getDeclaredMethod("getExtras").invoke(mObject);
        } catch (Exception e) {
            return null;
        }
    }

    private Object invokeMethod(String methodName) {
        try {
            return mObject.getClass().getDeclaredMethod(methodName).invoke(mObject);
Loading