summaryrefslogtreecommitdiff
path: root/weathereffects/src/com/google/android/wallpaper/weathereffects/provider/WeatherEffectsContentProvider.kt
blob: cc5c0c36b73a86d02c6fb136ec73a2fe10550b88 (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
/*
 * 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.provider

import android.content.ContentProvider
import android.content.ContentValues
import android.content.UriMatcher
import android.database.Cursor
import android.database.MatrixCursor
import android.net.Uri
import com.google.android.wallpaper.weathereffects.WallpaperEffectsDebugApplication
import com.google.android.wallpaper.weathereffects.dagger.MainScope
import com.google.android.wallpaper.weathereffects.provider.WallpaperInfoContract.WallpaperGenerationData
import com.google.android.wallpaper.weathereffects.shared.model.WallpaperFileModel
import com.google.android.wallpaper.weathereffects.domain.WeatherEffectsInteractor
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

class WeatherEffectsContentProvider: ContentProvider() {

    @Inject @MainScope lateinit var mainScope: CoroutineScope
    @Inject lateinit var interactor: WeatherEffectsInteractor

    private val uriMatcher = UriMatcher(UriMatcher.NO_MATCH).apply {
        addURI(
            WallpaperInfoContract.AUTHORITY,
            UPDATE_WALLPAPER,
            UPDATE_WALLPAPER_ID
        )
    }

    override fun onCreate(): Boolean {
        WallpaperEffectsDebugApplication.graph.inject(this)
        return true
    }

    override fun query(
        uri: Uri,
        projection: Array<out String>?,
        selection: String?,
        selectionArgs: Array<out String>?,
        sortOrder: String?
    ): Cursor {
        return when (uriMatcher.match(uri)) {
            UPDATE_WALLPAPER_ID -> updateWallpaper(uri)
            // TODO(b/290939683): Add more URIs including save and load wallpapers.
            else -> MatrixCursor(arrayOf())
        }
    }

    override fun getType(uri: Uri): String? = null

    override fun insert(uri: Uri, values: ContentValues?): Uri? = null

    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0

    override fun update(
        uri: Uri,
        values: ContentValues?,
        selection: String?,
        selectionArgs: Array<out String>?
    ): Int {
        return 0
    }

    private fun updateWallpaper(uri: Uri): MatrixCursor {
        val foreground = uri.getQueryParameter(WallpaperInfoContract.FOREGROUND_TEXTURE_PARAM)
        val background = uri.getQueryParameter(WallpaperInfoContract.BACKGROUND_TEXTURE_PARAM)
        val weatherType = uri.getQueryParameter(WallpaperInfoContract.WEATHER_EFFECT_PARAM)

        val projection = WallpaperGenerationData.DEFAULT_PROJECTION
        val cursor = MatrixCursor(projection)
        cursor.addRow(projection.map { column ->
            when (column) {
                WallpaperGenerationData.FOREGROUND_TEXTURE -> foreground
                WallpaperGenerationData.BACKGROUND_TEXTURE -> background
                WallpaperGenerationData.WEATHER_EFFECT -> weatherType
                else -> null
            }
        })

        mainScope.launch {
            interactor.updateWallpaper(
                WallpaperFileModel(
                    foreground,
                    background,
                    WallpaperInfoContract.WeatherEffect.fromStringValue(weatherType),
                )
            )
        }

        return cursor
    }

    companion object {
        const val UPDATE_WALLPAPER = "update_wallpaper"
        const val UPDATE_WALLPAPER_ID = 0
        const val TAG = "WeatherEffectsContentProvider"
    }
}