From 7fca8705713aca91ca0c7124b1cb94e79ab17a78 Mon Sep 17 00:00:00 2001 From: Pierre Barbier de Reuille Date: Fri, 4 Jun 2021 16:41:57 +0100 Subject: [PATCH] Improve local color extraction handling. Local color extraction doesn't happen if the wallpaper is not currently visible, so we don't need to add/remove the locations when the widget is visible/hidden. Also now compare the locations with some approximate measure, to account for possibly slightly different calculations during drag vs drop. Fix: 189827568 Test: Install Clock or test widgets and try to move them: the color are applied correcly. Change-Id: Ib2a08bb0552c75bd351868e4fd5b5df8b8b732b2 --- .../widget/LauncherAppWidgetHostView.java | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java b/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java index 8685aae737..fad9b0b3c5 100644 --- a/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java +++ b/src/com/android/launcher3/widget/LauncherAppWidgetHostView.java @@ -41,6 +41,7 @@ import android.widget.AdapterView; import android.widget.Advanceable; import android.widget.RemoteViews; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.UiThread; @@ -262,6 +263,10 @@ public class LauncherAppWidgetHostView extends NavigableAppWidgetHostView mIsAttachedToWindow = true; checkIfAutoAdvance(); + + if (mLastLocationRegistered != null) { + mColorExtractor.addLocation(List.of(mLastLocationRegistered)); + } } @Override @@ -366,7 +371,7 @@ public class LauncherAppWidgetHostView extends NavigableAppWidgetHostView if (mTempRectF.isEmpty()) { return; } - if (!mTempRectF.equals(mLastLocationRegistered)) { + if (!isSameLocation(mTempRectF, mLastLocationRegistered, /* epsilon= */ 1e-6f)) { if (mLastLocationRegistered != null) { mColorExtractor.removeLocations(); } @@ -375,6 +380,20 @@ public class LauncherAppWidgetHostView extends NavigableAppWidgetHostView } } + // Compare two location rectangles. Locations are always in the [0;1] range. + private static boolean isSameLocation(@NonNull RectF rect1, @Nullable RectF rect2, + float epsilon) { + if (rect2 == null) return false; + return isSameCoordinate(rect1.left, rect2.left, epsilon) + && isSameCoordinate(rect1.right, rect2.right, epsilon) + && isSameCoordinate(rect1.top, rect2.top, epsilon) + && isSameCoordinate(rect1.bottom, rect2.bottom, epsilon); + } + + private static boolean isSameCoordinate(float c1, float c2, float epsilon) { + return Math.abs(c1 - c2) < epsilon; + } + @Override public void onColorsChanged(RectF rectF, SparseIntArray colors) { // setColorResources will reapply the view, which must happen in the UI thread. @@ -391,14 +410,6 @@ public class LauncherAppWidgetHostView extends NavigableAppWidgetHostView protected void onWindowVisibilityChanged(int visibility) { super.onWindowVisibilityChanged(visibility); maybeRegisterAutoAdvance(); - - if (visibility == View.VISIBLE) { - if (mLastLocationRegistered != null) { - mColorExtractor.addLocation(List.of(mLastLocationRegistered)); - } - } else { - mColorExtractor.removeLocations(); - } } private void checkIfAutoAdvance() {