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

Commit 178535d7 authored by Michel Comin Escude's avatar Michel Comin Escude Committed by Android (Google) Code Review
Browse files

Merge changes I70770b26,I363c46ea into main

* changes:
  Fix Weather Engine loading
  Fixing weatherEngine asset loader
parents 007c2a5e 82965df8
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -100,10 +100,20 @@
            android:text="@string/set_wallpaper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintBottom_toBottomOf="@id/seekBar"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginBottom="30dp"
            android:layout_marginEnd="20dp" />

        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:progress="80"
            android:layout_marginBottom="50dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>
+35 −7
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.view.SurfaceView
import android.view.View
import android.widget.Button
import android.widget.FrameLayout
import android.widget.SeekBar
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.google.android.torus.core.activity.TorusViewerActivity
@@ -36,9 +37,10 @@ import com.google.android.torus.core.engine.TorusEngine
import com.google.android.torus.utils.extensions.setImmersiveFullScreen
import com.google.android.wallpaper.weathereffects.dagger.BackgroundScope
import com.google.android.wallpaper.weathereffects.dagger.MainScope
import com.google.android.wallpaper.weathereffects.data.repository.WallpaperFileUtils
import com.google.android.wallpaper.weathereffects.domain.WeatherEffectsInteractor
import com.google.android.wallpaper.weathereffects.provider.WallpaperInfoContract
import com.google.android.wallpaper.weathereffects.shared.model.WallpaperFileModel
import com.google.android.wallpaper.weathereffects.domain.WeatherEffectsInteractor
import java.io.File
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
@@ -66,17 +68,20 @@ class WallpaperEffectsDebugActivity : TorusViewerActivity() {
    private val fgCachedAssetPaths: ArrayList<String> = arrayListOf()
    private val bgCachedAssetPaths: ArrayList<String> = arrayListOf()

    /** It will be initialized on [onCreate]. */
    private var intensity: Float = 0.8f

    override fun getWallpaperEngine(context: Context, surfaceView: SurfaceView): TorusEngine {
        this.surfaceView = surfaceView
        val engine = WeatherEngine(surfaceView.holder, context)
        val engine = WeatherEngine(surfaceView.holder, mainScope, interactor, context)
        this.engine = engine
        return engine
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        WallpaperEffectsDebugApplication.graph.inject(this)
        super.onCreate(savedInstanceState)

        setContentView(R.layout.debug_activity)
        setImmersiveFullScreen()
@@ -139,8 +144,28 @@ class WallpaperEffectsDebugActivity : TorusViewerActivity() {
                view.onTouchEvent(event)
            }

        engine?.initialize(mainScope, interactor)
        setDebugText()
        val seekBar = rootView.requireViewById<SeekBar>(R.id.seekBar)
        seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                // Convert progress to a value between 0 and 1
                val value = progress.toFloat() / 100f
                engine?.setIntensity(value)
                intensity = value
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
                hideButtons()
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                showButtons()
            }
        })
        intensity = seekBar.progress.toFloat() / 100f

        // This avoids that the initial state after installing is showing a black screen.
        if (!WallpaperFileUtils.hasBitmapsInLocalStorage(applicationContext)) updateWallpaper()
    }

    private fun writeAssetsToCache() {
@@ -191,8 +216,11 @@ class WallpaperEffectsDebugActivity : TorusViewerActivity() {
                    weatherEffect,
                )
            )
            setDebugText("Wallpaper updated successfully.\n* Weather: " +
                    "$weatherEffect\n* Foreground: $fgPath\n* Background: $bgPath")
            engine?.setIntensity(intensity)
            setDebugText(
                "Wallpaper updated successfully.\n* Weather: " +
                        "$weatherEffect\n* Foreground: $fgPath\n* Background: $bgPath"
            )
        }
    }

+4 −2
Original line number Diff line number Diff line
@@ -86,6 +86,7 @@ class FrameBuffer(width: Int, height: Int, format: Int = HardwareBuffer.RGBA_888
        renderer.obtainRenderRequest().setColorSpace(colorSpace).draw(executor) { result ->
            if (result.status == HardwareBufferRenderer.RenderResult.SUCCESS) {
                result.fence.await(Duration.ofMillis(RESULT_FENCE_TIME_OUT))
                if (!buffer.isClosed) {
                    if (!buffer.isClosed) {
                        Bitmap.wrapHardwareBuffer(buffer, colorSpace)?.let {
                            callbackExecutor.execute { onImageReady.invoke(it) }
@@ -94,6 +95,7 @@ class FrameBuffer(width: Int, height: Int, format: Int = HardwareBuffer.RGBA_888
                }
            }
        }
    }

    /**
     * Configure the [FrameBuffer] to apply to this RenderNode. This will apply a visual effect to
+1 −0
Original line number Diff line number Diff line
@@ -18,4 +18,5 @@
    android:author="@string/google_author"
    android:description="@string/wallpaper_description"
    android:showMetadataInPreview="true"
    android:thumbnail="@drawable/ic_launcher_foreground"
    android:supportsAmbientMode="false"/>
+26 −27
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.util.Log
import android.util.Size
import android.util.SizeF
import android.view.SurfaceHolder
import androidx.annotation.FloatRange
import com.google.android.torus.canvas.engine.CanvasWallpaperEngine
import com.google.android.wallpaper.weathereffects.shared.model.WallpaperImageModel
import com.google.android.wallpaper.weathereffects.domain.WeatherEffectsInteractor
@@ -40,11 +41,13 @@ import kotlinx.coroutines.launch

class WeatherEngine(
    defaultHolder: SurfaceHolder,
    private val applicationScope: CoroutineScope,
    private val interactor: WeatherEffectsInteractor,
    private val context: Context,
    hardwareAccelerated: Boolean = true
) : CanvasWallpaperEngine(defaultHolder, hardwareAccelerated) {

    private val currentAssets: WallpaperImageModel? = null
    private var currentAssets: WallpaperImageModel? = null
    private var activeEffect: WeatherEffect? = null
        private set(value) {
            field = value
@@ -56,8 +59,16 @@ class WeatherEngine(
        }

    private var collectWallpaperImageJob: Job? = null
    private lateinit var interactor: WeatherEffectsInteractor
    private lateinit var applicationScope: CoroutineScope
    private var effectIntensity: Float = 1f

    init {
        /* Load assets. */
        if (interactor.wallpaperImageModel.value == null) {
            applicationScope.launch {
                interactor.loadWallpaper()
            }
        }
    }

    override fun onCreate(isFirstActiveInstance: Boolean) {
        Log.d(TAG, "Engine created.")
@@ -70,31 +81,22 @@ class WeatherEngine(
        }
    }

    fun initialize(
        applicationScope: CoroutineScope,
        interactor: WeatherEffectsInteractor,
    ) {
        this.interactor = interactor
        this.applicationScope = applicationScope

        if (interactor.wallpaperImageModel.value == null) {
            applicationScope.launch {
                interactor.loadWallpaper()
            }
        }
    }

    override fun onResume() {
        if (shouldTriggerUpdate()) {
            startUpdateLoop()
        }
        collectWallpaperImageJob = applicationScope.launch {
            interactor.wallpaperImageModel.collect { asset ->
                if (asset == null || asset == currentAssets) return@collect

                currentAssets = asset
                createWeatherEffect(asset.foreground, asset.background, asset.weatherEffect)
            }
        }
        if (shouldTriggerUpdate()) startUpdateLoop()
    }

    override fun onUpdate(deltaMillis: Long, frameTimeNanos: Long) {
        super.onUpdate(deltaMillis, frameTimeNanos)
        activeEffect?.update(deltaMillis, frameTimeNanos)

        renderWithFpsLimit(frameTimeNanos) { canvas -> activeEffect?.draw(canvas) }
    }

    override fun onPause() {
@@ -103,17 +105,14 @@ class WeatherEngine(
        collectWallpaperImageJob?.cancel()
    }


    override fun onDestroy(isLastActiveInstance: Boolean) {
        activeEffect?.release()
        activeEffect = null
    }

    override fun onUpdate(deltaMillis: Long, frameTimeNanos: Long) {
        super.onUpdate(deltaMillis, frameTimeNanos)
        activeEffect?.update(deltaMillis, frameTimeNanos)

        renderWithFpsLimit(frameTimeNanos) { canvas -> activeEffect?.draw(canvas) }
    fun setIntensity(@FloatRange(from = 0.0, to = 1.0) intensity: Float) {
        effectIntensity = intensity
        activeEffect?.setIntensity(intensity)
    }

    private fun createWeatherEffect(
Loading