Permalink
Browse files

Update to Kotlin Coroutines 0.26.1

Update all usages of launch / async / etc to take into account the new changes to Structured Concurrency: https://medium.com/@elizarov/structured-concurrency-722d765aa952
  • Loading branch information...
ianhanniballake committed Sep 21, 2018
1 parent e2ad3ec commit 247e9d6d9b54e5a3252e5952b641401df5ff34f9
Showing with 514 additions and 323 deletions.
  1. +1 −0 android-client-common/build.gradle
  2. +3 −3 android-client-common/src/main/java/com/google/android/apps/muzei/provider/Background.kt
  3. +2 −1 android-client-common/src/main/java/com/google/android/apps/muzei/provider/DirectBootCache.kt
  4. +2 −2 android-client-common/src/main/java/com/google/android/apps/muzei/render/ImageLoader.kt
  5. +6 −6 android-client-common/src/main/java/com/google/android/apps/muzei/room/ArtworkDao.kt
  6. +13 −20 android-client-common/src/main/java/com/google/android/apps/muzei/room/ArtworkExt.kt
  7. +5 −3 android-client-common/src/main/java/com/google/android/apps/muzei/room/InstalledProvidersLiveData.kt
  8. +2 −2 android-client-common/src/main/java/com/google/android/apps/muzei/room/ProviderDao.kt
  9. +5 −6 android-client-common/src/main/java/com/google/android/apps/muzei/room/SourceDao.kt
  10. +5 −4 android-client-common/src/main/java/com/google/android/apps/muzei/sync/ProviderManager.kt
  11. +4 −4 ...oid-client-common/src/main/java/com/google/android/apps/muzei/util/ContentProviderClientCompat.kt
  12. +1 −1 build.gradle
  13. +11 −0 extensions/build.gradle
  14. +29 −0 extensions/src/main/java/com/google/android/apps/muzei/util/BroadcastReceiverExt.kt
  15. +43 −0 extensions/src/main/java/com/google/android/apps/muzei/util/ContextExt.kt
  16. +46 −0 extensions/src/main/java/com/google/android/apps/muzei/util/LivecycleJob.kt
  17. +38 −0 extensions/src/main/java/com/google/android/apps/muzei/util/ScopedAndroidViewModel.kt
  18. +17 −10 main/src/main/java/com/google/android/apps/muzei/ArtDetailFragment.kt
  19. +4 −3 main/src/main/java/com/google/android/apps/muzei/ArtworkInfoRedirectActivity.kt
  20. +3 −2 main/src/main/java/com/google/android/apps/muzei/ChooseProviderFragment.kt
  21. +8 −5 main/src/main/java/com/google/android/apps/muzei/ChooseProviderViewModel.kt
  22. +10 −8 main/src/main/java/com/google/android/apps/muzei/MuzeiWallpaperService.kt
  23. +8 −4 main/src/main/java/com/google/android/apps/muzei/NextArtworkActivity.kt
  24. +12 −14 main/src/main/java/com/google/android/apps/muzei/PhotoSetAsTargetActivity.kt
  25. +12 −8 main/src/main/java/com/google/android/apps/muzei/browse/BrowseProviderFragment.kt
  26. +6 −4 main/src/main/java/com/google/android/apps/muzei/browse/BrowseProviderViewModel.kt
  27. +9 −11 main/src/main/java/com/google/android/apps/muzei/notifications/NewWallpaperNotificationReceiver.kt
  28. +2 −1 main/src/main/java/com/google/android/apps/muzei/notifications/NotificationUpdater.kt
  29. +5 −1 main/src/main/java/com/google/android/apps/muzei/quicksettings/NextArtworkTileService.kt
  30. +12 −9 main/src/main/java/com/google/android/apps/muzei/render/DemoRenderController.kt
  31. +1 −4 main/src/main/java/com/google/android/apps/muzei/render/RealRenderController.kt
  32. +7 −2 main/src/main/java/com/google/android/apps/muzei/render/RenderController.kt
  33. +4 −6 main/src/main/java/com/google/android/apps/muzei/settings/EffectsScreenFragment.kt
  34. +1 −4 main/src/main/java/com/google/android/apps/muzei/shortcuts/ArtworkInfoShortcutController.kt
  35. +2 −4 main/src/main/java/com/google/android/apps/muzei/sources/NetworkChangeObserver.kt
  36. +7 −11 main/src/main/java/com/google/android/apps/muzei/sources/SourceArtProvider.kt
  37. +14 −20 main/src/main/java/com/google/android/apps/muzei/sources/SourceManager.kt
  38. +3 −2 main/src/main/java/com/google/android/apps/muzei/sources/SourceSettingsActivity.kt
  39. +8 −10 main/src/main/java/com/google/android/apps/muzei/tasker/TaskerActionReceiver.kt
  40. +6 −3 main/src/main/java/com/google/android/apps/muzei/tasker/TaskerSettingViewModel.kt
  41. +2 −1 main/src/main/java/com/google/android/apps/muzei/wearable/WearableController.kt
  42. +6 −5 main/src/main/java/com/google/android/apps/muzei/widget/AppWidgetUpdateTask.kt
  43. +5 −5 main/src/main/java/com/google/android/apps/muzei/widget/MuzeiAppWidgetProvider.kt
  44. +2 −1 main/src/main/java/com/google/android/apps/muzei/widget/WidgetUpdater.kt
  45. +1 −0 source-featured-art/build.gradle
  46. +3 −9 source-featured-art/src/main/java/com/google/android/apps/muzei/featuredart/FeaturedArtSource.kt
  47. +50 −49 source-gallery/src/main/java/com/google/android/apps/muzei/gallery/ChosenPhotoDao.kt
  48. +6 −4 source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GalleryAddPhotosActivity.kt
  49. +2 −1 source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GalleryArtProvider.kt
  50. +2 −1 source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GalleryArtSource.kt
  51. +5 −4 source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GalleryProvider.kt
  52. +2 −1 source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GalleryScanWorker.kt
  53. +11 −12 source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GallerySettingsActivity.kt
  54. +2 −2 source-single/src/main/java/com/google/android/apps/muzei/single/SingleArtProvider.kt
  55. +2 −1 source-single/src/main/java/com/google/android/apps/muzei/single/SingleSettingsActivity.kt
  56. +3 −2 wearable/src/main/java/com/google/android/apps/muzei/ChooseProviderActivity.kt
  57. +3 −3 wearable/src/main/java/com/google/android/apps/muzei/ChooseProviderViewModel.kt
  58. +5 −3 wearable/src/main/java/com/google/android/apps/muzei/FullScreenActivity.kt
  59. +7 −4 wearable/src/main/java/com/google/android/apps/muzei/MuzeiActivity.kt
  60. +4 −2 wearable/src/main/java/com/google/android/apps/muzei/MuzeiWatchFace.kt
  61. +7 −2 ...e/src/main/java/com/google/android/apps/muzei/complications/ArtworkComplicationProviderService.kt
  62. +3 −6 wearable/src/main/java/com/google/android/apps/muzei/datalayer/ActivateMuzeiIntentService.kt
  63. +2 −1 wearable/src/main/java/com/google/android/apps/muzei/datalayer/CapabilityListenerService.kt
  64. +2 −6 wearable/src/main/java/com/google/android/apps/muzei/datalayer/DataLayerArtProvider.kt
@@ -24,6 +24,7 @@ dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
implementation "androidx.core:core-ktx:$rootProject.ext.ktxVersion"
implementation project(':muzei-api')
implementation project(':extensions')
implementation "com.android.support:support-compat:$supportLibraryVersion"
implementation "com.android.support:exifinterface:$supportLibraryVersion"
api "android.arch.lifecycle:livedata:$lifecycleVersion"
@@ -1,7 +1,7 @@
package com.google.android.apps.muzei.provider
import android.os.Looper
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.runBlocking
import kotlinx.coroutines.experimental.withContext
@@ -12,13 +12,13 @@ import kotlinx.coroutines.experimental.withContext
* - You were already on a background thread and [block]
* is run directly.
* - The main thread is blocked until [block] completes on
* the [CommonPool] coroutine context.
* the [Dispatchers.Default] coroutine context.
*/
internal fun <T> ensureBackground(
block: () -> T
) : T = if (Looper.getMainLooper() == Looper.myLooper()) {
runBlocking {
withContext(CommonPool) {
withContext(Dispatchers.Default) {
block()
}
}
@@ -21,6 +21,7 @@ import android.os.Build
import android.support.v4.content.ContextCompat
import android.util.Log
import com.google.android.apps.muzei.api.MuzeiContract
import kotlinx.coroutines.experimental.GlobalScope
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.launch
@@ -48,7 +49,7 @@ object DirectBootCache {
return
}
cacheJob?.cancel()
cacheJob = launch {
cacheJob = GlobalScope.launch {
delay(DIRECT_BOOT_CACHE_DELAY_MILLIS)
if (cacheJob?.isCancelled == true) {
return@launch
@@ -25,7 +25,7 @@ import android.net.Uri
import android.os.Build
import android.support.media.ExifInterface
import android.util.Log
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.withContext
import net.nurik.roman.muzei.androidclientcommon.BuildConfig
import java.io.FileNotFoundException
@@ -58,7 +58,7 @@ sealed class ImageLoader {
uri: Uri,
targetWidth: Int = 0,
targetHeight: Int = targetWidth
) = withContext(CommonPool) {
) = withContext(Dispatchers.Default) {
ContentUriImageLoader(contentResolver, uri)
.decode(targetWidth, targetHeight)
}
@@ -20,7 +20,7 @@ import android.arch.lifecycle.LiveData
import android.arch.persistence.room.Dao
import android.arch.persistence.room.Insert
import android.arch.persistence.room.Query
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.withContext
/**
@@ -32,7 +32,7 @@ abstract class ArtworkDao {
@get:Query("SELECT * FROM artwork ORDER BY date_added DESC LIMIT 100")
internal abstract val artworkBlocking: List<Artwork>
suspend fun getArtwork() = withContext(CommonPool) {
suspend fun getArtwork() = withContext(Dispatchers.Default) {
artworkBlocking
}
@@ -46,7 +46,7 @@ abstract class ArtworkDao {
"ORDER BY date_added DESC")
internal abstract val currentArtworkBlocking: Artwork?
suspend fun getCurrentArtwork() = withContext(CommonPool) {
suspend fun getCurrentArtwork() = withContext(Dispatchers.Default) {
currentArtworkBlocking
}
@@ -60,7 +60,7 @@ abstract class ArtworkDao {
suspend fun getCurrentArtworkForProvider(
providerAuthority: String
) = withContext(CommonPool) {
) = withContext(Dispatchers.Default) {
getCurrentArtworkForProviderBlocking(providerAuthority)
}
@@ -72,14 +72,14 @@ abstract class ArtworkDao {
@Query("SELECT * FROM artwork WHERE _id=:id")
internal abstract fun getArtworkByIdBlocking(id: Long): Artwork?
suspend fun getArtworkById(id: Long) = withContext(CommonPool) {
suspend fun getArtworkById(id: Long) = withContext(Dispatchers.Default) {
getArtworkByIdBlocking(id)
}
@Query("SELECT * FROM artwork WHERE title LIKE :query OR byline LIKE :query OR attribution LIKE :query")
internal abstract fun searchArtworkBlocking(query: String): List<Artwork>
suspend fun searchArtwork(query: String) = withContext(CommonPool) {
suspend fun searchArtwork(query: String) = withContext(Dispatchers.Default) {
searchArtworkBlocking(query)
}
@@ -20,7 +20,6 @@ import android.content.Context
import android.os.Bundle
import android.os.RemoteException
import android.util.Log
import androidx.core.widget.toast
import com.google.android.apps.muzei.api.MuzeiContract
import com.google.android.apps.muzei.api.UserCommand
import com.google.android.apps.muzei.api.internal.ProtocolConstants.KEY_COMMAND
@@ -30,31 +29,25 @@ import com.google.android.apps.muzei.api.internal.ProtocolConstants.METHOD_GET_C
import com.google.android.apps.muzei.api.internal.ProtocolConstants.METHOD_OPEN_ARTWORK_INFO
import com.google.android.apps.muzei.api.internal.ProtocolConstants.METHOD_TRIGGER_COMMAND
import com.google.android.apps.muzei.util.ContentProviderClientCompat
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import com.google.android.apps.muzei.util.toastFromBackground
import net.nurik.roman.muzei.androidclientcommon.R
import java.util.ArrayList
private const val TAG = "Artwork"
fun Artwork.openArtworkInfo(context: Context) {
val applicationContext = context.applicationContext
launch {
val success = ContentProviderClientCompat.getClient(
applicationContext, imageUri)?.use { client ->
try {
val result = client.call(METHOD_OPEN_ARTWORK_INFO, imageUri.toString())
result?.getBoolean(KEY_OPEN_ARTWORK_INFO_SUCCESS)
} catch (e: RemoteException) {
Log.i(TAG, "Provider for $imageUri crashed while opening artwork info", e)
false
}
} ?: false
if (!success) {
launch(UI) {
applicationContext.toast(R.string.error_view_details)
}
suspend fun Artwork.openArtworkInfo(context: Context) {
val success = ContentProviderClientCompat.getClient(
context, imageUri)?.use { client ->
try {
val result = client.call(METHOD_OPEN_ARTWORK_INFO, imageUri.toString())
result?.getBoolean(KEY_OPEN_ARTWORK_INFO_SUCCESS)
} catch (e: RemoteException) {
Log.i(TAG, "Provider for $imageUri crashed while opening artwork info", e)
false
}
} ?: false
if (!success) {
context.toastFromBackground(R.string.error_view_details)
}
}
@@ -25,6 +25,7 @@ import android.content.IntentFilter
import android.content.pm.PackageManager
import android.content.pm.ProviderInfo
import com.google.android.apps.muzei.api.provider.MuzeiArtProvider
import kotlinx.coroutines.experimental.CoroutineScope
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.newSingleThreadContext
@@ -34,7 +35,8 @@ fun ProviderInfo.getComponentName() = ComponentName(packageName, name)
* LiveData that returns the list of currently installed [MuzeiArtProvider] instances.
*/
class InstalledProvidersLiveData(
private val context: Context
private val context: Context,
private val coroutineScope: CoroutineScope
) : MutableLiveData<List<ProviderInfo>>() {
private val currentProviders = HashMap<ComponentName, ProviderInfo>()
@@ -47,7 +49,7 @@ class InstalledProvidersLiveData(
if (intent?.data == null) {
return
}
launch(singleThreadContext) {
coroutineScope.launch(singleThreadContext) {
updateProviders(intent.data?.schemeSpecificPart)
}
}
@@ -100,7 +102,7 @@ class InstalledProvidersLiveData(
addAction(Intent.ACTION_PACKAGE_REMOVED)
}
context.registerReceiver(packageChangeReceiver, packageChangeFilter)
launch(singleThreadContext) {
coroutineScope.launch(singleThreadContext) {
updateProviders()
}
}
@@ -22,7 +22,7 @@ import android.arch.persistence.room.Delete
import android.arch.persistence.room.Insert
import android.arch.persistence.room.Query
import android.arch.persistence.room.Update
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.withContext
/**
@@ -36,7 +36,7 @@ abstract class ProviderDao {
@get:Query("SELECT * FROM provider")
internal abstract val currentProviderBlocking: Provider?
suspend fun getCurrentProvider() = withContext(CommonPool) {
suspend fun getCurrentProvider() = withContext(Dispatchers.Default) {
currentProviderBlocking
}
@@ -24,9 +24,8 @@ import android.arch.persistence.room.Query
import android.arch.persistence.room.TypeConverters
import android.arch.persistence.room.Update
import android.content.ComponentName
import com.google.android.apps.muzei.room.converter.ComponentNameTypeConverter
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.withContext
/**
@@ -41,7 +40,7 @@ abstract class SourceDao {
@get:Query("SELECT * FROM sources")
abstract val sourcesBlocking: List<Source>
suspend fun getSources() = withContext(CommonPool) {
suspend fun getSources() = withContext(Dispatchers.Default) {
sourcesBlocking
}
@@ -55,14 +54,14 @@ abstract class SourceDao {
@get:Query("SELECT * FROM sources WHERE selected=1 ORDER BY component_name")
abstract val currentSourceBlocking: Source?
suspend fun getCurrentSource() = withContext(CommonPool) {
suspend fun getCurrentSource() = withContext(Dispatchers.Default) {
currentSourceBlocking
}
@get:Query("SELECT * FROM sources WHERE selected=1 AND wantsNetworkAvailable=1")
internal abstract val currentSourcesThatWantNetworkBlocking: List<Source>
suspend fun getCurrentSourcesThatWantNetwork() = withContext(CommonPool) {
suspend fun getCurrentSourcesThatWantNetwork() = withContext(Dispatchers.Default) {
currentSourcesThatWantNetworkBlocking
}
@@ -79,7 +78,7 @@ abstract class SourceDao {
suspend fun getSourceByComponentName(
componentName: ComponentName
) = withContext(CommonPool) {
) = withContext(Dispatchers.Default) {
getSourceByComponentNameBlocking(componentName)
}
@Update
@@ -39,7 +39,8 @@ 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.Dispatchers
import kotlinx.coroutines.experimental.GlobalScope
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.launch
@@ -77,7 +78,7 @@ class ProviderManager private constructor(private val context: Context)
.also { instance = it }
}
suspend fun select(context: Context, authority: String) = withContext(CommonPool) {
suspend fun select(context: Context, authority: String) = withContext(Dispatchers.Default) {
val database = MuzeiDatabase.getInstance(context)
database.beginTransaction()
database.providerDao().deleteAll()
@@ -127,7 +128,7 @@ class ProviderManager private constructor(private val context: Context)
// Can't have no artwork at all,
// try loading the next artwork with a slight delay
nextArtworkJob?.cancel()
nextArtworkJob = launch {
nextArtworkJob = GlobalScope.launch {
delay(1000)
if (nextArtworkJob?.isCancelled == false) {
nextArtwork()
@@ -204,7 +205,7 @@ class ProviderManager private constructor(private val context: Context)
block(provider)
} else {
// Invalid ContentProvider, remove it from the ProviderDao
launch {
GlobalScope.launch {
if (BuildConfig.DEBUG) {
Log.w(TAG, "Invalid provider ${provider.authority}")
}
@@ -24,7 +24,7 @@ import android.os.Build
import android.os.Bundle
import android.os.ParcelFileDescriptor
import android.os.RemoteException
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.withContext
import java.io.FileNotFoundException
@@ -49,7 +49,7 @@ class ContentProviderClientCompat private constructor(
method: String,
arg: String? = null,
extras: Bundle? = null
): Bundle? = withContext(CommonPool) {
): Bundle? = withContext(Dispatchers.Default) {
mContentProviderClient.call(method, arg, extras)
}
@@ -60,15 +60,15 @@ class ContentProviderClientCompat private constructor(
selection: String? = null,
selectionArgs: Array<String>? = null,
sortOrder: String? = null
): Cursor? = withContext(CommonPool) {
): Cursor? = withContext(Dispatchers.Default) {
return@withContext mContentProviderClient.query(
url, projection, selection, selectionArgs, sortOrder)
}
@Throws(FileNotFoundException::class, RemoteException::class)
suspend fun openInputStream(
url: Uri
): InputStream? = withContext(CommonPool) {
): InputStream? = withContext(Dispatchers.Default) {
mContentProviderClient.openFile(url, "r")?.run {
ParcelFileDescriptor.AutoCloseInputStream(this)
}
View
@@ -20,7 +20,7 @@ buildscript {
targetSdkVersion = 25
kotlinVersion = "1.2.70"
coroutinesVersion = "0.25.3"
coroutinesVersion = "0.26.1"
ktxVersion = "0.3"
supportLibraryVersion = "28.0.0-rc02"
constraintLayoutVersion = "1.1.3"
View
@@ -17,9 +17,15 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
kotlin.experimental.coroutines "enable"
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
implementation "android.arch.lifecycle:common-java8:$lifecycleVersion"
api "android.arch.lifecycle:livedata-core:$lifecycleVersion"
api "android.arch.lifecycle:viewmodel:$lifecycleVersion"
}
android {
@@ -34,4 +40,9 @@ android {
publicBeta
publicDebug
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Oops, something went wrong.

0 comments on commit 247e9d6

Please sign in to comment.