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

Commit cef3bef7 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Reformat LiveWallpaper.kt, LiveWallpaperEventListener.kt, WeatherEngine.kt" into main

parents 8dd21fd8 654c4e1a
Loading
Loading
Loading
Loading
+59 −62
Original line number Diff line number Diff line
@@ -36,11 +36,11 @@ import com.google.android.torus.core.wallpaper.listener.LiveWallpaperKeyguardEve
import java.lang.ref.WeakReference

/**
 * Implements [WallpaperService] using Filament to render the wallpaper.
 * An instance of this class should only implement [getWallpaperEngine]
 * Implements [WallpaperService] using Filament to render the wallpaper. An instance of this class
 * should only implement [getWallpaperEngine]
 *
 * Note: [LiveWallpaper] subclasses must include the following attribute/s
 * in the AndroidManifest.xml:
 * Note: [LiveWallpaper] subclasses must include the following attribute/s in the
 * AndroidManifest.xml:
 * - android:configChanges="uiMode"
 */
abstract class LiveWallpaper : WallpaperService() {
@@ -77,18 +77,19 @@ abstract class LiveWallpaper : WallpaperService() {
         * through WallpaperService.Engine.onCommand events that should be more accurate.
         */
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
            wakeStateReceiver = object : BroadcastReceiver() {
            wakeStateReceiver =
                object : BroadcastReceiver() {
                    override fun onReceive(context: Context, intent: Intent) {
                        val positionExtras = Bundle()
                        when (intent.action) {
                            Intent.ACTION_SCREEN_ON -> {
                                positionExtras.putInt(
                                    LiveWallpaperEventListener.WAKE_ACTION_LOCATION_X,
                                -1
                                    -1,
                                )
                                positionExtras.putInt(
                                    LiveWallpaperEventListener.WAKE_ACTION_LOCATION_Y,
                                -1
                                    -1,
                                )
                                wakeStateChangeListeners.forEach {
                                    it.get()?.onWake(positionExtras)
@@ -98,11 +99,11 @@ abstract class LiveWallpaper : WallpaperService() {
                            Intent.ACTION_SCREEN_OFF -> {
                                positionExtras.putInt(
                                    LiveWallpaperEventListener.SLEEP_ACTION_LOCATION_X,
                                -1
                                    -1,
                                )
                                positionExtras.putInt(
                                    LiveWallpaperEventListener.SLEEP_ACTION_LOCATION_Y,
                                -1
                                    -1,
                                )
                                wakeStateChangeListeners.forEach {
                                    it.get()?.onSleep(positionExtras)
@@ -121,22 +122,21 @@ abstract class LiveWallpaper : WallpaperService() {
    }

    /**
     * Must be implemented to return a new instance of [TorusEngine].
     * If you want it to subscribe to wallpaper interactions (offset, preview, zoom...) the engine
     * should also implement [LiveWallpaperEventListener]. If you want it to subscribe to touch
     * events, it should implement [TorusTouchListener].
     * Must be implemented to return a new instance of [TorusEngine]. If you want it to subscribe to
     * wallpaper interactions (offset, preview, zoom...) the engine should also implement
     * [LiveWallpaperEventListener]. If you want it to subscribe to touch events, it should
     * implement [TorusTouchListener].
     *
     * Note: You might have multiple Engines running at the same time (when the wallpaper is set as
     * the active wallpaper and the user is in the wallpaper picker viewing a preview of it
     * as well). You can track the lifecycle when *any* Engine is active using the
     * the active wallpaper and the user is in the wallpaper picker viewing a preview of it as
     * well). You can track the lifecycle when *any* Engine is active using the
     * is{First/Last}ActiveInstance parameters of the create/destroy methods.
     *
     */
    abstract fun getWallpaperEngine(context: Context, surfaceHolder: SurfaceHolder): TorusEngine

    /**
     * returns a new instance of [LiveWallpaperEngineWrapper].
     * Caution: This function should not be override when extending [LiveWallpaper] class.
     * returns a new instance of [LiveWallpaperEngineWrapper]. Caution: This function should not be
     * override when extending [LiveWallpaper] class.
     */
    override fun onCreateEngine(): Engine {
        val wrapper = LiveWallpaperEngineWrapper()
@@ -196,9 +196,7 @@ abstract class LiveWallpaper : WallpaperService() {
            return false
        }

        /**
         * Triggers the [WallpaperService] to recompute the Wallpaper Colors.
         */
        /** Triggers the [WallpaperService] to recompute the Wallpaper Colors. */
        fun notifyWallpaperColorsChanged() {
            this.wallpaperServiceEngine?.notifyColorsChanged()
        }
@@ -227,11 +225,11 @@ abstract class LiveWallpaper : WallpaperService() {

    /**
     * Implementation of [WallpaperService.Engine] that works as a wrapper. If we used a
     * [WallpaperService.Engine] instance as the framework engine, we would find the problem
     * that the engine will be created for preview, then destroyed and recreated again when the
     * wallpaper is set. This behavior may cause to load assets multiple time for every time the
     * Rendering engine is created. Also, wrapping our [TorusEngine] inside
     * [WallpaperService.Engine] allow us to reuse [TorusEngine] in other places, like Activities.
     * [WallpaperService.Engine] instance as the framework engine, we would find the problem that
     * the engine will be created for preview, then destroyed and recreated again when the wallpaper
     * is set. This behavior may cause to load assets multiple time for every time the Rendering
     * engine is created. Also, wrapping our [TorusEngine] inside [WallpaperService.Engine] allow us
     * to reuse [TorusEngine] in other places, like Activities.
     */
    private inner class LiveWallpaperEngineWrapper : WallpaperService.Engine() {
        private lateinit var wallpaperEngine: TorusEngine
@@ -245,7 +243,8 @@ abstract class LiveWallpaper : WallpaperService() {
             * For Android 10 (SDK 29).
             * This is needed for Foldables and multiple display devices.
             */
            val context = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            val context =
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    displayContext ?: this@LiveWallpaper
                } else {
                    this@LiveWallpaper
@@ -298,7 +297,7 @@ abstract class LiveWallpaper : WallpaperService() {
            holder: SurfaceHolder?,
            format: Int,
            width: Int,
            height: Int
            height: Int,
        ) {
            super.onSurfaceChanged(holder, format, width, height)
            wallpaperEngine.resize(width, height)
@@ -310,7 +309,7 @@ abstract class LiveWallpaper : WallpaperService() {
            xOffsetStep: Float,
            yOffsetStep: Float,
            xPixelOffset: Int,
            yPixelOffset: Int
            yPixelOffset: Int,
        ) {
            super.onOffsetsChanged(
                xOffset,
@@ -318,7 +317,7 @@ abstract class LiveWallpaper : WallpaperService() {
                xOffsetStep,
                yOffsetStep,
                xPixelOffset,
                yPixelOffset
                yPixelOffset,
            )

            if (wallpaperEngine is LiveWallpaperEventListener) {
@@ -328,7 +327,7 @@ abstract class LiveWallpaper : WallpaperService() {
                        1.0f
                    } else {
                        xOffsetStep
                    }
                    },
                )
            }
        }
@@ -368,7 +367,7 @@ abstract class LiveWallpaper : WallpaperService() {
            y: Int,
            z: Int,
            extras: Bundle?,
            resultRequested: Boolean
            resultRequested: Boolean,
        ): Bundle? {
            when (action) {
                COMMAND_REAPPLY -> onWallpaperReapplied()
@@ -406,9 +405,7 @@ abstract class LiveWallpaper : WallpaperService() {
            wallpaperEngine.onWallpaperFlagsChanged(which)
        }

        /**
         * This is overriding a hidden API [WallpaperService.shouldZoomOutWallpaper].
         */
        /** This is overriding a hidden API [WallpaperService.shouldZoomOutWallpaper]. */
        override fun shouldZoomOutWallpaper(): Boolean {
            if (wallpaperEngine is LiveWallpaperEventListener) {
                return (wallpaperEngine as LiveWallpaperEventListener).shouldZoomOutWallpaper()
+20 −19
Original line number Diff line number Diff line
@@ -36,8 +36,8 @@ interface LiveWallpaperEventListener {
     * the home of the launcher). This only tracts the horizontal scroll.
     *
     * @param xOffset The current offset of the scroll. The value is normalize between [0,1].
     * @param xOffsetStep How is stepped the scroll. If you invert [xOffsetStep] you get the
     * number of pages in the scrolling area.
     * @param xOffsetStep How is stepped the scroll. If you invert [xOffsetStep] you get the number
     *   of pages in the scrolling area.
     */
    fun onOffsetChanged(xOffset: Float, xOffsetStep: Float)

@@ -45,27 +45,28 @@ interface LiveWallpaperEventListener {
     * Called when the zoom level of the wallpaper is changing.
     *
     * @param zoomLevel A value between 0 and 1 that tells how much the wallpaper should be zoomed
     * out: if 0, the wallpaper should be in normal state; if 1 the wallpaper should be zoomed out.
     *   out: if 0, the wallpaper should be in normal state; if 1 the wallpaper should be zoomed
     *   out.
     */
    fun onZoomChanged(zoomLevel: Float)

    /**
     * Call when the wallpaper was set, and then is reapplied. This means that the wallpaper was
     * set and is being set again. This is useful to know if the wallpaper settings have to be
     * reapplied again (i.e. if the user enters the wallpaper picker and picks the same wallpaper,
     * changes the settings and sets the wallpaper again).
     * Call when the wallpaper was set, and then is reapplied. This means that the wallpaper was set
     * and is being set again. This is useful to know if the wallpaper settings have to be reapplied
     * again (i.e. if the user enters the wallpaper picker and picks the same wallpaper, changes the
     * settings and sets the wallpaper again).
     */
    fun onWallpaperReapplied()

    /**
     * Called when the Wallpaper colors need to be computed you can create a [WallpaperColors]
     * instance using the [WallpaperColors.fromBitmap] function and passing a bitmap that
     * represents the wallpaper (i.e. the gallery thumbnail) or use the [WallpaperColors]
     * constructor and pass the primary, secondary and tertiary colors. This method is specially
     * important since the UI will change their colors based on what is returned here.
     * instance using the [WallpaperColors.fromBitmap] function and passing a bitmap that represents
     * the wallpaper (i.e. the gallery thumbnail) or use the [WallpaperColors] constructor and pass
     * the primary, secondary and tertiary colors. This method is specially important since the UI
     * will change their colors based on what is returned here.
     *
     * @return The colors that represent the wallpaper; null if you want the System to take
     * care of the colors.
     * @return The colors that represent the wallpaper; null if you want the System to take care of
     *   the colors.
     */
    fun computeWallpaperColors(): WallpaperColors?

@@ -73,8 +74,8 @@ interface LiveWallpaperEventListener {
     * Called when the wallpaper receives the preview information (asynchronous call).
     *
     * @param extras the bundle of the preview information. The key "which_preview" can be used to
     * retrieve a string value (ex. main_preview_home) that specifies which preview the engine
     * is referring to.
     *   retrieve a string value (ex. main_preview_home) that specifies which preview the engine is
     *   referring to.
     */
    fun onPreviewInfoReceived(extras: Bundle?) {}

+4 −4
Original line number Diff line number Diff line
@@ -217,7 +217,7 @@ class WeatherEngine(
                        foreground,
                        background,
                        effectIntensity,
                        screenSize.toSizeF()
                        screenSize.toSizeF(),
                    )
            }
            WallpaperInfoContract.WeatherEffect.SNOW -> {
@@ -242,7 +242,7 @@ class WeatherEngine(
                        foreground,
                        background,
                        effectIntensity,
                        screenSize.toSizeF()
                        screenSize.toSizeF(),
                    )
            }
            else -> {
@@ -370,7 +370,7 @@ class WeatherEngine(
                    background,
                    256,
                    (background.width / background.height.toFloat() * 256).roundToInt(),
                    /* filter = */ true
                    /* filter = */ true,
                )
            )
    }
@@ -382,7 +382,7 @@ class WeatherEngine(
    private enum class AnimationType {
        UNLOCK,
        WAKE,
        NONE
        NONE,
    }

    private companion object {