summaryrefslogtreecommitdiff
path: root/weathereffects/debug/src/com/google/android/wallpaper/weathereffects/WallpaperEffectsDebugActivity.kt
blob: afbd4e3974a5b2ab333746a99dc4917ce48dbd69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Copyright (C) 2023 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.
 */

package com.google.android.wallpaper.weathereffects

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.annotation.SuppressLint
import android.app.WallpaperManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MotionEvent
import android.view.SurfaceView
import android.view.View
import android.widget.Button
import android.widget.FrameLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.google.android.torus.core.activity.TorusViewerActivity
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.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
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class WallpaperEffectsDebugActivity : TorusViewerActivity() {

    @Inject
    @MainScope
    lateinit var mainScope: CoroutineScope
    @Inject
    @BackgroundScope
    lateinit var bgScope: CoroutineScope
    @Inject
    lateinit var context: Context
    @Inject
    lateinit var interactor: WeatherEffectsInteractor

    private lateinit var rootView: FrameLayout
    private lateinit var surfaceView: SurfaceView
    private var engine: WeatherEngine? = null
    private var weatherEffect: WallpaperInfoContract.WeatherEffect? = null
    private var assetIndex = 0
    private val fgCachedAssetPaths: ArrayList<String> = arrayListOf()
    private val bgCachedAssetPaths: ArrayList<String> = arrayListOf()

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

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

        setContentView(R.layout.debug_activity)
        setImmersiveFullScreen()

         writeAssetsToCache()

        rootView = requireViewById(R.id.main_layout)
        rootView.requireViewById<FrameLayout>(R.id.wallpaper_layout).addView(surfaceView)

        rootView.requireViewById<Button>(R.id.rain).setOnClickListener {
            weatherEffect = WallpaperInfoContract.WeatherEffect.RAIN
            updateWallpaper()
            setDebugText(context.getString(R.string.generating))
        }
        rootView.requireViewById<Button>(R.id.fog).setOnClickListener {
            weatherEffect = WallpaperInfoContract.WeatherEffect.FOG
            updateWallpaper()
            setDebugText(context.getString(R.string.generating))
        }
        rootView.requireViewById<Button>(R.id.snow).setOnClickListener {
            weatherEffect = WallpaperInfoContract.WeatherEffect.SNOW
            updateWallpaper()
            setDebugText(context.getString(R.string.generating))
        }
        rootView.requireViewById<Button>(R.id.clear).setOnClickListener {
            weatherEffect = null

            updateWallpaper()
        }
        rootView.requireViewById<Button>(R.id.change_asset).setOnClickListener {
            assetIndex = (assetIndex + 1) % fgCachedAssetPaths.size

            updateWallpaper()
        }

        rootView.requireViewById<Button>(R.id.set_wallpaper).setOnClickListener {
            val i = Intent()
            i.action = WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER
            i.putExtra(
                WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
                ComponentName(this, WeatherWallpaperService::class.java)
            )
            this.startActivityForResult(i, SET_WALLPAPER_REQUEST_CODE)
            saveWallpaper()
        }

        rootView.requireViewById<FrameLayout>(R.id.wallpaper_layout)
            .setOnTouchListener { view, event ->
                when (event?.action) {
                    MotionEvent.ACTION_DOWN -> {
                        if (rootView.requireViewById<ConstraintLayout>(R.id.buttons).visibility
                            == View.GONE) {
                            showButtons()
                        } else {
                            hideButtons()
                        }
                    }
                }

                view.onTouchEvent(event)
            }

        engine?.initialize(mainScope, interactor)
        setDebugText()
    }

    private fun writeAssetsToCache() {
        // Writes test files from assets to local cache dir
        // (on the main thread!.. only ok for the debug app)
        fgCachedAssetPaths.apply {
            clear()
            addAll(
                listOf(
                    /* TODO(b/300991599): Add debug assets. */
                    FOREGROUND_IMAGE_1
                ).map { getFileFromAssets(it).absolutePath })
        }
        bgCachedAssetPaths.apply {
            clear()
            addAll(
                listOf(
                    /* TODO(b/300991599): Add debug assets. */
                    BACKGROUND_IMAGE_1
                ).map { getFileFromAssets(it).absolutePath })
        }
    }

    private fun getFileFromAssets(fileName: String): File {
        return File(context.cacheDir, fileName).also {
            if (!it.exists()) {
                it.outputStream().use { cache ->
                    context.assets.open(fileName).use { inputStream ->
                        inputStream.copyTo(cache)
                    }
                }
            }
        }
    }

    private fun updateWallpaper() {
        mainScope.launch {
            val fgPath = fgCachedAssetPaths[assetIndex]
            val bgPath = bgCachedAssetPaths[assetIndex]
            interactor.updateWallpaper(
                WallpaperFileModel(
                    fgPath,
                    bgPath,
                    weatherEffect,
                )
            )
            setDebugText("Wallpaper updated successfully.\n* Weather: " +
                    "$weatherEffect\n* Foreground: $fgPath\n* Background: $bgPath")
        }
    }

    private fun saveWallpaper() {
        bgScope.launch {
            interactor.saveWallpaper()
        }
    }

    private fun setDebugText(text: String? = null) {
        val output = rootView.requireViewById<TextView>(R.id.output)
        output.text = text

        if (text.isNullOrEmpty()) {
            output.visibility = View.INVISIBLE
        } else {
            output.visibility = View.VISIBLE
            mainScope.launch {
                // Make the text disappear after 3 sec.
                delay(3000L)
                output.visibility = View.INVISIBLE
            }
        }
    }

    private fun showButtons() {
        val buttons = rootView.requireViewById<ConstraintLayout>(R.id.buttons)
        buttons.visibility = View.VISIBLE
        buttons.animate().alpha(1f).setDuration(400).setListener(null)
    }

    private fun hideButtons() {
        val buttons = rootView.requireViewById<ConstraintLayout>(R.id.buttons)
        buttons.animate()
            .alpha(0f)
            .setDuration(400)
            .setListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    buttons.visibility = View.GONE
                }
            })
    }

    private companion object {
        // TODO(b/300991599): Add debug assets.
        private const val FOREGROUND_IMAGE_1 = "test-foreground.png"
        private const val BACKGROUND_IMAGE_1 = "test-background.png"
        private const val SET_WALLPAPER_REQUEST_CODE = 2
    }
}