1 /*
<lambda>null2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.android.wallpaper.weathereffects.provider
18 
19 import android.content.ContentProvider
20 import android.content.ContentValues
21 import android.content.UriMatcher
22 import android.database.Cursor
23 import android.database.MatrixCursor
24 import android.net.Uri
25 import com.google.android.wallpaper.weathereffects.WallpaperEffectsDebugApplication
26 import com.google.android.wallpaper.weathereffects.dagger.MainScope
27 import com.google.android.wallpaper.weathereffects.provider.WallpaperInfoContract.WallpaperGenerationData
28 import com.google.android.wallpaper.weathereffects.shared.model.WallpaperFileModel
29 import com.google.android.wallpaper.weathereffects.domain.WeatherEffectsInteractor
30 import javax.inject.Inject
31 import kotlinx.coroutines.CoroutineScope
32 import kotlinx.coroutines.launch
33 
34 class WeatherEffectsContentProvider: ContentProvider() {
35 
36     @Inject @MainScope lateinit var mainScope: CoroutineScope
37     @Inject lateinit var interactor: WeatherEffectsInteractor
38 
39     private val uriMatcher = UriMatcher(UriMatcher.NO_MATCH).apply {
40         addURI(
41             WallpaperInfoContract.AUTHORITY,
42             UPDATE_WALLPAPER,
43             UPDATE_WALLPAPER_ID
44         )
45     }
46 
47     override fun onCreate(): Boolean {
48         WallpaperEffectsDebugApplication.graph.inject(this)
49         return true
50     }
51 
52     override fun query(
53         uri: Uri,
54         projection: Array<out String>?,
55         selection: String?,
56         selectionArgs: Array<out String>?,
57         sortOrder: String?
58     ): Cursor {
59         return when (uriMatcher.match(uri)) {
60             UPDATE_WALLPAPER_ID -> updateWallpaper(uri)
61             // TODO(b/290939683): Add more URIs including save and load wallpapers.
62             else -> MatrixCursor(arrayOf())
63         }
64     }
65 
66     override fun getType(uri: Uri): String? = null
67 
68     override fun insert(uri: Uri, values: ContentValues?): Uri? = null
69 
70     override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0
71 
72     override fun update(
73         uri: Uri,
74         values: ContentValues?,
75         selection: String?,
76         selectionArgs: Array<out String>?
77     ): Int {
78         return 0
79     }
80 
81     private fun updateWallpaper(uri: Uri): MatrixCursor {
82         val foreground = uri.getQueryParameter(WallpaperInfoContract.FOREGROUND_TEXTURE_PARAM)
83         val background = uri.getQueryParameter(WallpaperInfoContract.BACKGROUND_TEXTURE_PARAM)
84         val weatherType = uri.getQueryParameter(WallpaperInfoContract.WEATHER_EFFECT_PARAM)
85 
86         val projection = WallpaperGenerationData.DEFAULT_PROJECTION
87         val cursor = MatrixCursor(projection)
88         cursor.addRow(projection.map { column ->
89             when (column) {
90                 WallpaperGenerationData.FOREGROUND_TEXTURE -> foreground
91                 WallpaperGenerationData.BACKGROUND_TEXTURE -> background
92                 WallpaperGenerationData.WEATHER_EFFECT -> weatherType
93                 else -> null
94             }
95         })
96 
97         mainScope.launch {
98             interactor.updateWallpaper(
99                 WallpaperFileModel(
100                     foreground,
101                     background,
102                     WallpaperInfoContract.WeatherEffect.fromStringValue(weatherType),
103                 )
104             )
105         }
106 
107         return cursor
108     }
109 
110     companion object {
111         const val UPDATE_WALLPAPER = "update_wallpaper"
112         const val UPDATE_WALLPAPER_ID = 0
113         const val TAG = "WeatherEffectsContentProvider"
114     }
115 }