From 9f9ead209a9f341bc618980b44048fab804601bd Mon Sep 17 00:00:00 2001 From: MatthewTighe Date: Fri, 29 Jul 2022 15:44:00 -0700 Subject: [PATCH] Fixes: #26052 remove WallpaperManager::updateWallpaper --- .../org/mozilla/fenix/ui/HomeScreenTest.kt | 16 ------- .../org/mozilla/fenix/home/HomeFragment.kt | 23 +++++----- .../settings/wallpaper/WallpaperSettings.kt | 4 +- .../wallpaper/WallpaperSettingsFragment.kt | 4 +- .../fenix/wallpapers/WallpaperManager.kt | 43 ++++++------------- 5 files changed, 29 insertions(+), 61 deletions(-) diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/HomeScreenTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/HomeScreenTest.kt index c48b38be4a17..f2a1d46d728d 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/HomeScreenTest.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/HomeScreenTest.kt @@ -11,7 +11,6 @@ import androidx.test.uiautomator.Until import org.junit.Before import org.junit.Rule import org.junit.Test -import org.mozilla.fenix.customannotations.SmokeTest import org.mozilla.fenix.ext.settings import org.mozilla.fenix.helpers.HomeActivityTestRule import org.mozilla.fenix.helpers.RetryTestRule @@ -154,19 +153,4 @@ class HomeScreenTest { verifyWelcomeHeader() } } - - @SmokeTest - @Test - fun tapLogoToChangeWallpaperTest() { - homeScreen { - clickFirefoxLogo() - verifyWallpaperImageApplied(true) - clickFirefoxLogo() - verifyWallpaperImageApplied(true) - clickFirefoxLogo() - verifyWallpaperImageApplied(true) - clickFirefoxLogo() - verifyWallpaperImageApplied(false) - } - } } diff --git a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt index 66dd594acb04..f721edd1b050 100644 --- a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt @@ -120,7 +120,7 @@ import org.mozilla.fenix.tabstray.TabsTrayAccessPoint import org.mozilla.fenix.utils.Settings.Companion.TOP_SITES_PROVIDER_MAX_THRESHOLD import org.mozilla.fenix.utils.ToolbarPopupWindow import org.mozilla.fenix.utils.allowUndo -import org.mozilla.fenix.wallpapers.WallpaperManager +import org.mozilla.fenix.wallpapers.Wallpaper import java.lang.ref.WeakReference import kotlin.math.min @@ -754,10 +754,6 @@ class HomeFragment : Fragment() { themeCollection = newWallpaper::class.simpleName ) ) - manager.updateWallpaper( - wallpaperContainer = binding.wallpaperImageView, - newWallpaper = newWallpaper - ) } } } @@ -967,12 +963,17 @@ class HomeFragment : Fragment() { .onEach { state -> // We only want to update the wallpaper when it's different from the default one // as the default is applied already on xml by default. - val currentWallpaper = state.wallpaperState.currentWallpaper - if (currentWallpaper != WallpaperManager.defaultWallpaper) { - requireComponents.wallpaperManager.updateWallpaper( - binding.wallpaperImageView, - currentWallpaper - ) + when (val currentWallpaper = state.wallpaperState.currentWallpaper) { + is Wallpaper.Default -> { + binding.wallpaperImageView.visibility = View.GONE + } + else -> { + with(requireComponents.wallpaperManager) { + val bitmap = currentWallpaper.load(requireContext()) ?: return@onEach + bitmap.scaleBitmapToBottomOfView(binding.wallpaperImageView) + } + binding.wallpaperImageView.visibility = View.VISIBLE + } } } .launchIn(viewLifecycleOwner.lifecycleScope) diff --git a/app/src/main/java/org/mozilla/fenix/settings/wallpaper/WallpaperSettings.kt b/app/src/main/java/org/mozilla/fenix/settings/wallpaper/WallpaperSettings.kt index 9d0e8c7d77f0..36865ee2a302 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/wallpaper/WallpaperSettings.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/wallpaper/WallpaperSettings.kt @@ -297,8 +297,8 @@ private fun WallpaperThumbnailsPreview() { WallpaperSettings( defaultWallpaper = WallpaperManager.defaultWallpaper, - loadWallpaperResource = { - wallpaperManager.loadSavedWallpaper(context, it) + loadWallpaperResource = { wallpaper -> + with(wallpaperManager) { wallpaper.load(context) } }, wallpapers = wallpaperManager.wallpapers, selectedWallpaper = wallpaperManager.currentWallpaper, diff --git a/app/src/main/java/org/mozilla/fenix/settings/wallpaper/WallpaperSettingsFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/wallpaper/WallpaperSettingsFragment.kt index af911833279c..1e7fd4a5e107 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/wallpaper/WallpaperSettingsFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/wallpaper/WallpaperSettingsFragment.kt @@ -49,8 +49,8 @@ class WallpaperSettingsFragment : Fragment() { WallpaperSettings( wallpapers = wallpaperManager.wallpapers, defaultWallpaper = WallpaperManager.defaultWallpaper, - loadWallpaperResource = { - wallpaperManager.loadSavedWallpaper(requireContext(), it) + loadWallpaperResource = { wallpaper -> + with(wallpaperManager) { wallpaper.load(context) } }, selectedWallpaper = currentWallpaper, onSelectWallpaper = { selectedWallpaper: Wallpaper -> diff --git a/app/src/main/java/org/mozilla/fenix/wallpapers/WallpaperManager.kt b/app/src/main/java/org/mozilla/fenix/wallpapers/WallpaperManager.kt index e825a0cf682d..01026f1fa67e 100644 --- a/app/src/main/java/org/mozilla/fenix/wallpapers/WallpaperManager.kt +++ b/app/src/main/java/org/mozilla/fenix/wallpapers/WallpaperManager.kt @@ -58,30 +58,6 @@ class WallpaperManager( fileManager.clean(currentWallpaper, wallpapers.filterIsInstance()) } - /** - * Apply the [newWallpaper] into the [wallpaperContainer] and update the [currentWallpaper]. - */ - fun updateWallpaper(wallpaperContainer: ImageView, newWallpaper: Wallpaper) { - val context = wallpaperContainer.context - if (newWallpaper == defaultWallpaper) { - wallpaperContainer.visibility = View.GONE - logger.info("Wallpaper update to default background") - } else { - val bitmap = loadSavedWallpaper(context, newWallpaper) - if (bitmap == null) { - val message = "Could not load wallpaper bitmap. Resetting to default." - logger.error(message) - currentWallpaper = defaultWallpaper - wallpaperContainer.visibility = View.GONE - return - } else { - wallpaperContainer.visibility = View.VISIBLE - scaleBitmapToBottom(bitmap, wallpaperContainer) - } - } - currentWallpaper = newWallpaper - } - /** * Download all known remote wallpapers. */ @@ -104,7 +80,7 @@ class WallpaperManager( } else { values[index] }.also { - appStore.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(it)) + currentWallpaper = it } } @@ -139,10 +115,10 @@ class WallpaperManager( /** * Load a wallpaper that is saved locally. */ - fun loadSavedWallpaper(context: Context, wallpaper: Wallpaper): Bitmap? = - when (wallpaper) { - is Wallpaper.Local -> loadWallpaperFromDrawables(context, wallpaper) - is Wallpaper.Remote -> loadWallpaperFromDisk(context, wallpaper) + fun Wallpaper.load(context: Context): Bitmap? = + when (this) { + is Wallpaper.Local -> loadWallpaperFromDrawables(context, this) + is Wallpaper.Remote -> loadWallpaperFromDisk(context, this) else -> null } @@ -163,7 +139,14 @@ class WallpaperManager( } }.getOrNull() - private fun scaleBitmapToBottom(bitmap: Bitmap, view: ImageView) { + /** + * This will scale the received [Bitmap] to the size of the [view]. It retains the bitmap's + * original aspect ratio, but will shrink or enlarge it to fit the viewport. If bitmap does not + * correctly fit the aspect ratio of the view, it will be shifted to prioritize the bottom-left + * of the bitmap. + */ + fun Bitmap.scaleBitmapToBottomOfView(view: ImageView) { + val bitmap = this view.setImageBitmap(bitmap) view.scaleType = ImageView.ScaleType.MATRIX val matrix = Matrix()