From aeff066a59d90601ebdf3080c55710dcc05235b9 Mon Sep 17 00:00:00 2001 From: Ian Lake Date: Tue, 3 Apr 2018 20:42:08 -0700 Subject: [PATCH] Use toast extension method from Android-ktx --- .../google/android/apps/muzei/ArtDetailFragment.kt | 5 ++--- .../com/google/android/apps/muzei/IntroFragment.kt | 4 ++-- .../android/apps/muzei/PhotoSetAsTargetActivity.kt | 4 ++-- .../muzei/quicksettings/NextArtworkTileService.kt | 4 ++-- .../apps/muzei/sources/ChooseSourceFragment.kt | 6 +++--- .../android/apps/muzei/sources/SourceManager.kt | 13 +++++++------ .../apps/muzei/featuredart/FeaturedArtSource.kt | 9 +++------ .../apps/muzei/gallery/GalleryAddPhotosActivity.kt | 7 +++---- .../apps/muzei/gallery/GallerySettingsActivity.kt | 8 +++----- .../muzei/datalayer/ActivateMuzeiIntentService.kt | 7 +++---- 10 files changed, 30 insertions(+), 37 deletions(-) diff --git a/main/src/main/java/com/google/android/apps/muzei/ArtDetailFragment.kt b/main/src/main/java/com/google/android/apps/muzei/ArtDetailFragment.kt index 7a8ffa358..6e620c030 100644 --- a/main/src/main/java/com/google/android/apps/muzei/ArtDetailFragment.kt +++ b/main/src/main/java/com/google/android/apps/muzei/ArtDetailFragment.kt @@ -34,9 +34,9 @@ import android.view.View import android.view.ViewGroup import android.widget.PopupMenu import android.widget.TextView -import android.widget.Toast import androidx.core.view.isGone import androidx.core.view.isVisible +import androidx.core.widget.toast import com.google.android.apps.muzei.api.MuzeiArtSource import com.google.android.apps.muzei.api.MuzeiContract import com.google.android.apps.muzei.notifications.NewWallpaperNotificationReceiver @@ -144,8 +144,7 @@ class ArtDetailFragment : Fragment(), (Boolean) -> Unit { } catch (e: RuntimeException) { // Catch ActivityNotFoundException, SecurityException, // and FileUriExposedException - Toast.makeText(context, R.string.error_view_details, - Toast.LENGTH_SHORT).show() + requireContext().toast(R.string.error_view_details) Log.e(TAG, "Error viewing artwork details.", e) } } diff --git a/main/src/main/java/com/google/android/apps/muzei/IntroFragment.kt b/main/src/main/java/com/google/android/apps/muzei/IntroFragment.kt index cdc2019a2..906b73163 100644 --- a/main/src/main/java/com/google/android/apps/muzei/IntroFragment.kt +++ b/main/src/main/java/com/google/android/apps/muzei/IntroFragment.kt @@ -26,6 +26,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast +import androidx.core.widget.toast import com.google.android.apps.muzei.util.AnimatedMuzeiLogoFragment import com.google.firebase.analytics.FirebaseAnalytics @@ -67,8 +68,7 @@ class IntroFragment : Fragment() { startActivity(Intent(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) } catch (e2: ActivityNotFoundException) { - Toast.makeText(context, R.string.error_wallpaper_chooser, - Toast.LENGTH_LONG).show() + requireContext().toast(R.string.error_wallpaper_chooser, Toast.LENGTH_LONG) } } } diff --git a/main/src/main/java/com/google/android/apps/muzei/PhotoSetAsTargetActivity.kt b/main/src/main/java/com/google/android/apps/muzei/PhotoSetAsTargetActivity.kt index c19895705..f0d65c492 100644 --- a/main/src/main/java/com/google/android/apps/muzei/PhotoSetAsTargetActivity.kt +++ b/main/src/main/java/com/google/android/apps/muzei/PhotoSetAsTargetActivity.kt @@ -21,8 +21,8 @@ import android.content.ComponentName import android.content.Intent import android.os.Bundle import android.util.Log -import android.widget.Toast import androidx.core.os.bundleOf +import androidx.core.widget.toast import com.google.android.apps.muzei.single.SingleArtSource import com.google.android.apps.muzei.sources.SourceManager import com.google.android.apps.muzei.util.observeOnce @@ -43,7 +43,7 @@ class PhotoSetAsTargetActivity : Activity() { SingleArtSource.setArtwork(context, this).observeOnce { success -> if (success == false) { Log.e(TAG, "Unable to insert artwork for ${this@apply}") - Toast.makeText(context, R.string.set_as_wallpaper_failed, Toast.LENGTH_SHORT).show() + context.toast(R.string.set_as_wallpaper_failed) finish() return@observeOnce } diff --git a/main/src/main/java/com/google/android/apps/muzei/quicksettings/NextArtworkTileService.kt b/main/src/main/java/com/google/android/apps/muzei/quicksettings/NextArtworkTileService.kt index 4031cbc21..f4865a959 100644 --- a/main/src/main/java/com/google/android/apps/muzei/quicksettings/NextArtworkTileService.kt +++ b/main/src/main/java/com/google/android/apps/muzei/quicksettings/NextArtworkTileService.kt @@ -30,6 +30,7 @@ import android.service.quicksettings.Tile import android.service.quicksettings.TileService import android.support.annotation.RequiresApi import android.widget.Toast +import androidx.core.widget.toast import com.google.android.apps.muzei.MuzeiWallpaperService import com.google.android.apps.muzei.api.MuzeiArtSource import com.google.android.apps.muzei.room.MuzeiDatabase @@ -123,8 +124,7 @@ class NextArtworkTileService : TileService(), LifecycleOwner { startActivityAndCollapse(Intent(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) } catch (e: ActivityNotFoundException) { - Toast.makeText(context, R.string.error_wallpaper_chooser, - Toast.LENGTH_LONG).show() + context.toast(R.string.error_wallpaper_chooser, Toast.LENGTH_LONG) } } } diff --git a/main/src/main/java/com/google/android/apps/muzei/sources/ChooseSourceFragment.kt b/main/src/main/java/com/google/android/apps/muzei/sources/ChooseSourceFragment.kt index e24942af5..d97e427e9 100644 --- a/main/src/main/java/com/google/android/apps/muzei/sources/ChooseSourceFragment.kt +++ b/main/src/main/java/com/google/android/apps/muzei/sources/ChooseSourceFragment.kt @@ -57,6 +57,7 @@ import android.view.ViewTreeObserver import android.widget.TextView import android.widget.Toast import androidx.core.os.bundleOf +import androidx.core.widget.toast import com.google.android.apps.muzei.api.MuzeiArtSource import com.google.android.apps.muzei.notifications.NotificationSettingsDialogFragment import com.google.android.apps.muzei.room.MuzeiDatabase @@ -188,10 +189,9 @@ class ChooseSourceFragment : Fragment() { preferPackageForIntent(playStoreIntent, PLAY_STORE_PACKAGE_NAME) startActivity(playStoreIntent) } catch (e: ActivityNotFoundException) { - Toast.makeText(context, - R.string.play_store_not_found, Toast.LENGTH_LONG).show() + requireContext().toast(R.string.play_store_not_found, Toast.LENGTH_LONG) } catch (e: SecurityException) { - Toast.makeText(context, R.string.play_store_not_found, Toast.LENGTH_LONG).show() + requireContext().toast(R.string.play_store_not_found, Toast.LENGTH_LONG) } return true diff --git a/main/src/main/java/com/google/android/apps/muzei/sources/SourceManager.kt b/main/src/main/java/com/google/android/apps/muzei/sources/SourceManager.kt index c198a0e61..c9ab92c12 100644 --- a/main/src/main/java/com/google/android/apps/muzei/sources/SourceManager.kt +++ b/main/src/main/java/com/google/android/apps/muzei/sources/SourceManager.kt @@ -36,6 +36,7 @@ import android.os.Build import android.text.TextUtils import android.util.Log import android.widget.Toast +import androidx.core.widget.toast import com.google.android.apps.muzei.api.MuzeiArtSource import com.google.android.apps.muzei.api.internal.ProtocolConstants.ACTION_HANDLE_COMMAND import com.google.android.apps.muzei.api.internal.ProtocolConstants.ACTION_SUBSCRIBE @@ -132,15 +133,15 @@ class SourceManager(private val context: Context) : DefaultLifecycleObserver, Li .putExtra(EXTRA_COMMAND_ID, id)) } catch (e: PackageManager.NameNotFoundException) { Log.i(TAG, "Sending action + $id to $selectedSource failed; switching to default.", e) - Toast.makeText(context, R.string.source_unavailable, Toast.LENGTH_LONG).show() + context.toast(R.string.source_unavailable, Toast.LENGTH_LONG) selectSource(context, FeaturedArtSource::class) } catch (e: IllegalStateException) { Log.i(TAG, "Sending action + $id to $selectedSource failed; switching to default.", e) - Toast.makeText(context, R.string.source_unavailable, Toast.LENGTH_LONG).show() + context.toast(R.string.source_unavailable, Toast.LENGTH_LONG) selectSource(context, FeaturedArtSource::class) } catch (e: SecurityException) { Log.i(TAG, "Sending action + $id to $selectedSource failed; switching to default.", e) - Toast.makeText(context, R.string.source_unavailable, Toast.LENGTH_LONG).show() + context.toast(R.string.source_unavailable, Toast.LENGTH_LONG) selectSource(context, FeaturedArtSource::class) } } @@ -379,15 +380,15 @@ class SourceManager(private val context: Context) : DefaultLifecycleObserver, Li .putExtra(EXTRA_TOKEN, selectedSource.flattenToShortString())) } catch (e: PackageManager.NameNotFoundException) { Log.i(TAG, "Selected source $selectedSource is no longer available; switching to default.", e) - Toast.makeText(context, R.string.source_unavailable, Toast.LENGTH_LONG).show() + context.toast(R.string.source_unavailable, Toast.LENGTH_LONG) selectSource(context, FeaturedArtSource::class) } catch (e: IllegalStateException) { Log.i(TAG, "Selected source $selectedSource is no longer available; switching to default.", e) - Toast.makeText(context, R.string.source_unavailable, Toast.LENGTH_LONG).show() + context.toast(R.string.source_unavailable, Toast.LENGTH_LONG) selectSource(context, FeaturedArtSource::class) } catch (e: SecurityException) { Log.i(TAG, "Selected source $selectedSource is no longer available; switching to default.", e) - Toast.makeText(context, R.string.source_unavailable, Toast.LENGTH_LONG).show() + context.toast(R.string.source_unavailable, Toast.LENGTH_LONG) selectSource(context, FeaturedArtSource::class) } } diff --git a/source-featured-art/src/main/java/com/google/android/apps/muzei/featuredart/FeaturedArtSource.kt b/source-featured-art/src/main/java/com/google/android/apps/muzei/featuredart/FeaturedArtSource.kt index 92ae7e469..be9259388 100644 --- a/source-featured-art/src/main/java/com/google/android/apps/muzei/featuredart/FeaturedArtSource.kt +++ b/source-featured-art/src/main/java/com/google/android/apps/muzei/featuredart/FeaturedArtSource.kt @@ -24,6 +24,7 @@ import android.support.customtabs.CustomTabsIntent import android.support.v4.content.ContextCompat import android.util.Log import android.widget.Toast +import androidx.core.widget.toast import com.google.android.apps.muzei.api.Artwork import com.google.android.apps.muzei.api.MuzeiContract import com.google.android.apps.muzei.api.RemoteMuzeiArtSource @@ -136,9 +137,7 @@ class FeaturedArtSource : RemoteMuzeiArtSource(SOURCE_NAME) { } ?: run { Log.w(TAG, "No current artwork, can't share.") Handler(Looper.getMainLooper()).post { - Toast.makeText(this, - R.string.featuredart_source_error_no_artwork_to_share, - Toast.LENGTH_SHORT).show() + toast(R.string.featuredart_source_error_no_artwork_to_share) } } } @@ -169,9 +168,7 @@ class FeaturedArtSource : RemoteMuzeiArtSource(SOURCE_NAME) { } Handler(Looper.getMainLooper()).post { - Toast.makeText(this@FeaturedArtSource, - "Next update time: $nextUpdateTime", - Toast.LENGTH_LONG).show() + toast("Next update time: $nextUpdateTime", Toast.LENGTH_LONG) } } } diff --git a/source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GalleryAddPhotosActivity.kt b/source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GalleryAddPhotosActivity.kt index 899fe7648..95143621f 100644 --- a/source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GalleryAddPhotosActivity.kt +++ b/source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GalleryAddPhotosActivity.kt @@ -6,7 +6,7 @@ import android.os.Bundle import android.support.v4.app.ActivityCompat import android.support.v4.app.ShareCompat import android.util.Log -import android.widget.Toast +import androidx.core.widget.toast import com.google.android.apps.muzei.util.observeOnce /** @@ -65,14 +65,13 @@ class GalleryAddPhotosActivity : Activity() { private fun updateCount() { if (successCount + failureCount == streamCount) { - val message = if (failureCount == 0) { + toast(if (failureCount == 0) { resources.getQuantityString(R.plurals.gallery_add_photos_success, successCount, successCount) } else { resources.getQuantityString(R.plurals.gallery_add_photos_failure, failureCount, failureCount, streamCount) - } - Toast.makeText(this, message, Toast.LENGTH_SHORT).show() + }) finish() } } diff --git a/source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GallerySettingsActivity.kt b/source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GallerySettingsActivity.kt index e79ef054f..55a00e211 100644 --- a/source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GallerySettingsActivity.kt +++ b/source-gallery/src/main/java/com/google/android/apps/muzei/gallery/GallerySettingsActivity.kt @@ -69,6 +69,7 @@ import android.widget.Toast import android.widget.ViewAnimator import androidx.core.content.edit import androidx.core.database.getStringOrNull +import androidx.core.widget.toast import com.google.android.apps.muzei.util.MultiSelectionController import com.google.android.apps.muzei.util.observe import com.google.android.apps.muzei.util.observeOnce @@ -268,8 +269,7 @@ class GallerySettingsActivity : AppCompatActivity(), Observer