/
ProviderManager.kt
264 lines (244 loc) · 10.2 KB
/
ProviderManager.kt
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Copyright 2018 Google Inc.
*
* 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.apps.muzei.sync
import android.annotation.SuppressLint
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.Observer
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.database.ContentObserver
import android.net.Uri
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.RemoteException
import android.preference.PreferenceManager
import android.util.Log
import androidx.core.content.edit
import com.google.android.apps.muzei.api.internal.ProtocolConstants
import com.google.android.apps.muzei.api.provider.ProviderContract
import com.google.android.apps.muzei.room.Artwork
import com.google.android.apps.muzei.room.MuzeiDatabase
import com.google.android.apps.muzei.room.Provider
import com.google.android.apps.muzei.util.ContentProviderClientCompat
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.newSingleThreadContext
import kotlinx.coroutines.experimental.withContext
import net.nurik.roman.muzei.androidclientcommon.BuildConfig
/**
* Single threaded coroutine context used for all sync operations
*/
internal val syncSingleThreadContext by lazy {
newSingleThreadContext("ProviderSync")
}
/**
* Manager which monitors the current Provider
*/
class ProviderManager private constructor(private val context: Context)
: MutableLiveData<Provider>(), Observer<Provider?> {
companion object {
private const val TAG = "ProviderManager"
private const val PREF_LOAD_FREQUENCY_SECONDS = "loadFrequencySeconds"
private const val DEFAULT_LOAD_FREQUENCY_SECONDS = 3600L
private const val PREF_LOAD_ON_WIFI = "loadOnWifi"
private const val DEFAULT_LOAD_ON_WIFI = false
@SuppressLint("StaticFieldLeak")
@Volatile
private var instance: ProviderManager? = null
fun getInstance(context: Context): ProviderManager =
instance ?: synchronized(this) {
instance ?: ProviderManager(context.applicationContext)
.also { instance = it }
}
suspend fun select(context: Context, authority: String) = withContext(CommonPool) {
val database = MuzeiDatabase.getInstance(context)
database.beginTransaction()
database.providerDao().deleteAll()
database.providerDao().insert(Provider(authority))
database.setTransactionSuccessful()
database.endTransaction()
}
suspend fun getDescription(context: Context, authority: String): String {
val contentUri = Uri.Builder().authority(authority).build()
return ContentProviderClientCompat.getClient(context, contentUri)?.use { client ->
return try {
val result = client.call(ProtocolConstants.METHOD_GET_DESCRIPTION)
result?.getString(ProtocolConstants.KEY_DESCRIPTION, "") ?: ""
} catch (e: RemoteException) {
Log.i(TAG, "Provider ${this} crashed while retrieving description", e)
""
}
} ?: ""
}
}
private val packageChangeReceiver : BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent?) {
val provider = value ?: return
val packageName = intent?.data?.schemeSpecificPart
val pm = context.packageManager
@SuppressLint("InlinedApi")
val providerInfo = pm.resolveContentProvider(provider.authority,
PackageManager.MATCH_DISABLED_COMPONENTS)
if (providerInfo == null || providerInfo.packageName == packageName) {
// The selected provider changed, so restart loading
startArtworkLoad()
}
}
}
private val contentObserver: ContentObserver
private val providerLiveData by lazy {
MuzeiDatabase.getInstance(context).providerDao().currentProvider
}
private val artworkLiveData by lazy {
MuzeiDatabase.getInstance(context).artworkDao().currentArtwork
}
private var nextArtworkJob: Job? = null
private val artworkObserver = Observer<Artwork?> { artwork ->
if (artwork == null) {
// Can't have no artwork at all,
// try loading the next artwork with a slight delay
nextArtworkJob?.cancel()
nextArtworkJob = launch {
delay(1000)
if (nextArtworkJob?.isCancelled == false) {
nextArtwork()
}
}
} else {
nextArtworkJob?.cancel()
}
}
var loadFrequencySeconds: Long
set(newLoadFrequency) {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putLong(PREF_LOAD_FREQUENCY_SECONDS, newLoadFrequency)
}
if (newLoadFrequency > 0) {
ArtworkLoadWorker.enqueuePeriodic(newLoadFrequency, loadOnWifi)
} else {
ArtworkLoadWorker.cancelPeriodic()
}
}
get() = PreferenceManager.getDefaultSharedPreferences(context)
.getLong(PREF_LOAD_FREQUENCY_SECONDS, DEFAULT_LOAD_FREQUENCY_SECONDS)
var loadOnWifi: Boolean
set(newLoadOnWifi) {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(PREF_LOAD_ON_WIFI, newLoadOnWifi)
}
if (loadFrequencySeconds > 0) {
ArtworkLoadWorker.enqueuePeriodic(loadFrequencySeconds, newLoadOnWifi)
}
}
get() = PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_LOAD_ON_WIFI, DEFAULT_LOAD_ON_WIFI)
init {
contentObserver = object : ContentObserver(Handler(Looper.getMainLooper())) {
override fun onChange(selfChange: Boolean, uri: Uri) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "onChange for $uri")
}
ProviderChangedWorker.enqueueChanged()
}
}
}
override fun onActive() {
if (BuildConfig.DEBUG) {
Log.d(TAG, "ProviderManager became active")
}
// Register for package change events
val packageChangeFilter = IntentFilter().apply {
addDataScheme("package")
addAction(Intent.ACTION_PACKAGE_ADDED)
addAction(Intent.ACTION_PACKAGE_CHANGED)
addAction(Intent.ACTION_PACKAGE_REPLACED)
addAction(Intent.ACTION_PACKAGE_REMOVED)
}
context.registerReceiver(packageChangeReceiver, packageChangeFilter)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
ProviderChangedWorker.activeListeningStateChanged(context, true)
}
providerLiveData.observeForever(this)
artworkLiveData.observeForever(artworkObserver)
startArtworkLoad()
}
private fun runIfValid(provider: Provider?, block: (provider: Provider) -> Unit) {
if (provider != null) {
val pm = context.packageManager
if (pm.resolveContentProvider(provider.authority, 0) != null) {
// resolveContentProvider succeeded, so it is a valid ContentProvider
block(provider)
} else {
// Invalid ContentProvider, remove it from the ProviderDao
launch {
if (BuildConfig.DEBUG) {
Log.w(TAG, "Invalid provider ${provider.authority}")
}
MuzeiDatabase.getInstance(context).providerDao().delete(provider)
}
}
}
}
private fun startArtworkLoad() {
if (hasActiveObservers()) {
runIfValid(value) { currentSource ->
if (BuildConfig.DEBUG) {
Log.d(TAG, "Starting artwork load")
}
// Listen for MuzeiArtProvider changes
val contentUri = ProviderContract.Artwork.getContentUri(currentSource.authority)
context.contentResolver.registerContentObserver(
contentUri, true, contentObserver)
ProviderChangedWorker.enqueueSelected()
}
}
}
override fun onChanged(newProvider: Provider?) {
val existingProvider = value
value = newProvider
runIfValid(newProvider) { provider ->
if (existingProvider == null || provider.authority != existingProvider.authority) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Provider changed to ${provider.authority}")
}
startArtworkLoad()
}
}
}
override fun onInactive() {
nextArtworkJob?.cancel()
artworkLiveData.removeObserver(artworkObserver)
providerLiveData.removeObserver(this)
context.contentResolver.unregisterContentObserver(contentObserver)
ArtworkLoadWorker.cancelPeriodic()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
ProviderChangedWorker.activeListeningStateChanged(context, false)
}
context.unregisterReceiver(packageChangeReceiver)
if (BuildConfig.DEBUG) {
Log.d(TAG, "ProviderManager is now inactive")
}
}
fun nextArtwork() {
ArtworkLoadWorker.enqueueNext()
}
}