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

Commit 2ba20603 authored by Robert Snoeberger's avatar Robert Snoeberger
Browse files

Add method to ClockPlugin to get preview image.

Bug: 125370285
Test: Checked that preview images still appear in picker app.
Test: Added ViewPreviewerTest
Change-Id: I819d58f621be7b0c4f5e3d0e56d7cb2604c8c770
parent 97de281b
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ import java.util.TimeZone;
public interface ClockPlugin extends Plugin {

    String ACTION = "com.android.systemui.action.PLUGIN_CLOCK";
    int VERSION = 2;
    int VERSION = 3;

    /**
     * Get the name of the clock face.
@@ -47,6 +47,17 @@ public interface ClockPlugin extends Plugin {
     */
    Bitmap getThumbnail();

    /**
     * Get preview images of clock face to be shown in the picker app.
     *
     * Preview image should be realistic and show what the clock face will look like on AOD and lock
     * screen.
     *
     * @param width width of the preview image, should be the same as device width in pixels.
     * @param height height of the preview image, should be the same as device height in pixels.
     */
    Bitmap getPreview(int width, int height);

    /**
     * Get clock view.
     * @return clock view from plugin.
+37 −2
Original line number Diff line number Diff line
@@ -15,15 +15,19 @@
 */
package com.android.keyguard.clock;

import android.app.WallpaperManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextClock;

import com.android.internal.colorextraction.ColorExtractor;
import com.android.keyguard.R;
import com.android.systemui.colorextraction.SysuiColorExtractor;
import com.android.systemui.plugins.ClockPlugin;

import java.util.TimeZone;
@@ -43,6 +47,16 @@ public class BubbleClockController implements ClockPlugin {
     */
    private final LayoutInflater mLayoutInflater;

    /**
     * Extracts accent color from wallpaper.
     */
    private final SysuiColorExtractor mColorExtractor;

    /**
     * Renders preview from clock view.
     */
    private final ViewPreviewer mRenderer = new ViewPreviewer();

    /**
     * Custom clock shown on AOD screen and behind stack scroller on lock.
     */
@@ -64,11 +78,15 @@ public class BubbleClockController implements ClockPlugin {
    /**
     * Create a BubbleClockController instance.
     *
     * @param layoutInflater Inflater used to inflate custom clock views.
     * @param res Resources contains title and thumbnail.
     * @param inflater Inflater used to inflate custom clock views.
     * @param colorExtractor Extracts accent color from wallpaper.
     */
    public BubbleClockController(Resources res, LayoutInflater inflater) {
    public BubbleClockController(Resources res, LayoutInflater inflater,
            SysuiColorExtractor colorExtractor) {
        mResources = res;
        mLayoutInflater = inflater;
        mColorExtractor = colorExtractor;
    }

    private void createViews() {
@@ -98,6 +116,23 @@ public class BubbleClockController implements ClockPlugin {
        return BitmapFactory.decodeResource(mResources, R.drawable.bubble_thumbnail);
    }

    @Override
    public Bitmap getPreview(int width, int height) {

        // Use the big clock view for the preview
        View view = getBigClockView();

        // Initialize state of plugin before generating preview.
        setDarkAmount(1f);
        setTextColor(Color.WHITE);
        ColorExtractor.GradientColors colors = mColorExtractor.getColors(
                WallpaperManager.FLAG_LOCK, true);
        setColorPalette(colors.supportsDarkText(), colors.getColorPalette());
        onTimeTick();

        return mRenderer.createPreview(view, width, height);
    }

    @Override
    public View getView() {
        if (mLockClockContainer == null) {
+5 −94
Original line number Diff line number Diff line
@@ -16,29 +16,19 @@
package com.android.keyguard.clock;

import android.annotation.Nullable;
import android.app.WallpaperManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.util.ArrayMap;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;

import androidx.annotation.VisibleForTesting;

import com.android.internal.colorextraction.ColorExtractor;
import com.android.systemui.SysUiServiceProvider;
import com.android.systemui.colorextraction.SysuiColorExtractor;
import com.android.systemui.dock.DockManager;
@@ -51,8 +41,6 @@ import com.android.systemui.util.InjectionInflationController;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import javax.inject.Inject;
import javax.inject.Singleton;
@@ -128,7 +116,6 @@ public final class ClockManager {

    private final List<ClockChangedListener> mListeners = new ArrayList<>();

    private final SysuiColorExtractor mColorExtractor;
    private final int mWidth;
    private final int mHeight;

@@ -144,17 +131,16 @@ public final class ClockManager {
            ContentResolver contentResolver, SettingsWrapper settingsWrapper) {
        mContext = context;
        mPluginManager = pluginManager;
        mColorExtractor = colorExtractor;
        mContentResolver = contentResolver;
        mSettingsWrapper = settingsWrapper;

        Resources res = context.getResources();
        LayoutInflater layoutInflater = injectionInflater.injectable(LayoutInflater.from(context));

        addClockPlugin(new DefaultClockController(res, layoutInflater));
        addClockPlugin(new BubbleClockController(res, layoutInflater));
        addClockPlugin(new StretchAnalogClockController(res, layoutInflater));
        addClockPlugin(new TypeClockController(res, layoutInflater));
        addClockPlugin(new DefaultClockController(res, layoutInflater, colorExtractor));
        addClockPlugin(new BubbleClockController(res, layoutInflater, colorExtractor));
        addClockPlugin(new StretchAnalogClockController(res, layoutInflater, colorExtractor));
        addClockPlugin(new TypeClockController(res, layoutInflater, colorExtractor));

        // Store the size of the display for generation of clock preview.
        DisplayMetrics dm = res.getDisplayMetrics();
@@ -217,7 +203,7 @@ public final class ClockManager {
                .setTitle(plugin.getTitle())
                .setId(id)
                .setThumbnail(() -> plugin.getThumbnail())
                .setPreview(() -> getClockPreview(id))
                .setPreview(() -> plugin.getPreview(mWidth, mHeight))
                .build());
    }

@@ -232,81 +218,6 @@ public final class ClockManager {
        }
    }

    /**
     * Generate a realistic preview of a clock face.
     * @param clockId ID of clock to use for preview, should be obtained from {@link getClockInfos}.
     *        Returns null if clockId is not found.
     */
    @Nullable
    private Bitmap getClockPreview(String clockId) {
        FutureTask<Bitmap> task = new FutureTask<>(new Callable<Bitmap>() {
            @Override
            public Bitmap call() {
                Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
                ClockPlugin plugin = mClocks.get(clockId);
                if (plugin == null) {
                    return null;
                }

                // Use the big clock view for the preview
                View clockView = plugin.getBigClockView();
                if (clockView == null) {
                    return null;
                }

                // Initialize state of plugin before generating preview.
                plugin.setDarkAmount(1f);
                plugin.setTextColor(Color.WHITE);

                ColorExtractor.GradientColors colors = mColorExtractor.getColors(
                        WallpaperManager.FLAG_LOCK, true);
                plugin.setColorPalette(colors.supportsDarkText(), colors.getColorPalette());
                plugin.onTimeTick();

                // Draw clock view hierarchy to canvas.
                Canvas canvas = new Canvas(bitmap);
                canvas.drawColor(Color.BLACK);
                dispatchVisibilityAggregated(clockView, true);
                clockView.measure(MeasureSpec.makeMeasureSpec(mWidth, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(mHeight, MeasureSpec.EXACTLY));
                clockView.layout(0, 0, mWidth, mHeight);
                clockView.draw(canvas);
                return bitmap;
            }
        });

        if (Looper.myLooper() == Looper.getMainLooper()) {
            task.run();
        } else {
            mMainHandler.post(task);
        }

        try {
            return task.get();
        } catch (Exception e) {
            Log.e(TAG, "Error completing task", e);
            return null;
        }
    }

    private void dispatchVisibilityAggregated(View view, boolean isVisible) {
        // Similar to View.dispatchVisibilityAggregated implementation.
        final boolean thisVisible = view.getVisibility() == View.VISIBLE;
        if (thisVisible || !isVisible) {
            view.onVisibilityAggregated(isVisible);
        }

        if (view instanceof ViewGroup) {
            isVisible = thisVisible && isVisible;
            ViewGroup vg = (ViewGroup) view;
            int count = vg.getChildCount();

            for (int i = 0; i < count; i++) {
                dispatchVisibilityAggregated(vg.getChildAt(i), isVisible);
            }
        }
    }

    private void notifyClockChanged(ClockPlugin plugin) {
        for (int i = 0; i < mListeners.size(); i++) {
            // It probably doesn't make sense to supply the same plugin instances to multiple
+36 −1
Original line number Diff line number Diff line
@@ -15,15 +15,19 @@
 */
package com.android.keyguard.clock;

import android.app.WallpaperManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.android.internal.colorextraction.ColorExtractor;
import com.android.keyguard.R;
import com.android.systemui.colorextraction.SysuiColorExtractor;
import com.android.systemui.plugins.ClockPlugin;

import java.util.TimeZone;
@@ -43,6 +47,16 @@ public class DefaultClockController implements ClockPlugin {
     */
    private final LayoutInflater mLayoutInflater;

    /**
     * Extracts accent color from wallpaper.
     */
    private final SysuiColorExtractor mColorExtractor;

    /**
     * Renders preview from clock view.
     */
    private final ViewPreviewer mRenderer = new ViewPreviewer();

    /**
     * Root view of preview.
     */
@@ -61,11 +75,15 @@ public class DefaultClockController implements ClockPlugin {
    /**
     * Create a DefaultClockController instance.
     *
     * @param res Resources contains title and thumbnail.
     * @param inflater Inflater used to inflate custom clock views.
     * @param colorExtractor Extracts accent color from wallpaper.
     */
    public DefaultClockController(Resources res, LayoutInflater inflater) {
    public DefaultClockController(Resources res, LayoutInflater inflater,
            SysuiColorExtractor colorExtractor) {
        mResources = res;
        mLayoutInflater = inflater;
        mColorExtractor = colorExtractor;
    }

    private void createViews() {
@@ -89,6 +107,23 @@ public class DefaultClockController implements ClockPlugin {
        return BitmapFactory.decodeResource(mResources, R.drawable.default_thumbnail);
    }

    @Override
    public Bitmap getPreview(int width, int height) {

        // Use the big clock view for the preview
        View view = getBigClockView();

        // Initialize state of plugin before generating preview.
        setDarkAmount(1f);
        setTextColor(Color.WHITE);
        ColorExtractor.GradientColors colors = mColorExtractor.getColors(
                WallpaperManager.FLAG_LOCK, true);
        setColorPalette(colors.supportsDarkText(), colors.getColorPalette());
        onTimeTick();

        return mRenderer.createPreview(view, width, height);
    }

    @Override
    public View getView() {
        return null;
+37 −2
Original line number Diff line number Diff line
@@ -15,15 +15,19 @@
 */
package com.android.keyguard.clock;

import android.app.WallpaperManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextClock;

import com.android.internal.colorextraction.ColorExtractor;
import com.android.keyguard.R;
import com.android.systemui.colorextraction.SysuiColorExtractor;
import com.android.systemui.plugins.ClockPlugin;

import java.util.TimeZone;
@@ -43,6 +47,16 @@ public class StretchAnalogClockController implements ClockPlugin {
     */
    private final LayoutInflater mLayoutInflater;

    /**
     * Extracts accent color from wallpaper.
     */
    private final SysuiColorExtractor mColorExtractor;

    /**
     * Renders preview from clock view.
     */
    private final ViewPreviewer mRenderer = new ViewPreviewer();

    /**
     * Custom clock shown on AOD screen and behind stack scroller on lock.
     */
@@ -64,11 +78,15 @@ public class StretchAnalogClockController implements ClockPlugin {
    /**
     * Create a BubbleClockController instance.
     *
     * @param layoutInflater Inflater used to inflate custom clock views.
     * @param res Resources contains title and thumbnail.
     * @param inflater Inflater used to inflate custom clock views.
     * @param colorExtractor Extracts accent color from wallpaper.
     */
    public StretchAnalogClockController(Resources res, LayoutInflater inflater) {
    public StretchAnalogClockController(Resources res, LayoutInflater inflater,
            SysuiColorExtractor colorExtractor) {
        mResources = res;
        mLayoutInflater = inflater;
        mColorExtractor = colorExtractor;
    }

    private void createViews() {
@@ -98,6 +116,23 @@ public class StretchAnalogClockController implements ClockPlugin {
        return BitmapFactory.decodeResource(mResources, R.drawable.stretch_thumbnail);
    }

    @Override
    public Bitmap getPreview(int width, int height) {

        // Use the big clock view for the preview
        View view = getBigClockView();

        // Initialize state of plugin before generating preview.
        setDarkAmount(1f);
        setTextColor(Color.WHITE);
        ColorExtractor.GradientColors colors = mColorExtractor.getColors(
                WallpaperManager.FLAG_LOCK, true);
        setColorPalette(colors.supportsDarkText(), colors.getColorPalette());
        onTimeTick();

        return mRenderer.createPreview(view, width, height);
    }

    @Override
    public View getView() {
        if (mView == null) {
Loading