Skip to content

Commit

Permalink
feat : re-impliment AccentColorExtractor
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSluffy authored and nulldrf committed Feb 14, 2025
1 parent 151d10f commit aa1576b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 10 deletions.
20 changes: 16 additions & 4 deletions lawnchair/src/app/lawnchair/AccentColorExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@

import android.app.WallpaperColors;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.util.SparseIntArray;
import android.view.View;
import android.widget.RemoteViews;

import androidx.annotation.Keep;
Expand All @@ -42,12 +39,19 @@
public class AccentColorExtractor extends LocalColorExtractor implements ThemeProvider.ColorSchemeChangeListener {

private final ThemeProvider mThemeProvider;
private Listener mListener;

@Keep
public AccentColorExtractor(Context context) {
mThemeProvider = ThemeProvider.INSTANCE.get(context);
}

@Override
public void setListener(@Nullable Listener listener) {
mListener = listener;
notifyListener();
}

@Nullable
protected SparseIntArray generateColorsOverride(ColorScheme colorScheme) {
SparseIntArray colorRes = new SparseIntArray(5 * 13);
Expand All @@ -71,7 +75,15 @@ public void applyColorsOverride(Context base, WallpaperColors colors) {
}

@Override
public void onColorSchemeChanged() {}
public void onColorSchemeChanged() {
notifyListener();
}

protected void notifyListener() {
if (mListener != null) {
mListener.onColorsChanged(generateColorsOverride(mThemeProvider.getColorScheme()));
}
}

// Shade number -> color resource ID maps
private static final SparseIntArray ACCENT1_RES = new SparseIntArray(13);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,7 @@ private void inflateAndAddWidgets(
mContext, info.appWidgetId, providerInfo);

if (mWallpaperColorResources != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
view.setColorResources(mWallpaperColorResources);
}
view.setColorResources(mWallpaperColorResources);
}

view.setTag(info);
Expand Down Expand Up @@ -615,6 +613,11 @@ private LauncherPreviewAppWidgetHostView(Context context) {
protected boolean shouldAllowDirectClick() {
return false;
}

@Override
public void onColorsChanged(SparseIntArray colors) {
post(() -> setColorResources(colors));
}
}

/** Root layout for launcher preview that intercepts all touch events. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import android.view.SurfaceControlViewHost;
import android.view.SurfaceControlViewHost.SurfacePackage;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;

import androidx.annotation.NonNull;
Expand All @@ -48,6 +49,7 @@
import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherSettings;
import com.android.launcher3.Utilities;
import com.android.launcher3.Workspace;
import com.android.launcher3.graphics.LauncherPreviewRenderer.PreviewContext;
import com.android.launcher3.model.BaseLauncherBinder;
Expand All @@ -66,7 +68,6 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;

import app.lawnchair.AccentColorExtractor;

/** Render preview using surface view. */
@SuppressWarnings("NewApi")
Expand Down Expand Up @@ -218,7 +219,11 @@ private Context getPreviewContext() {
return new ContextThemeWrapper(context,
Themes.getActivityThemeRes(context));
}
AccentColorExtractor.newInstance(context)
if (Utilities.ATLEAST_R) {
context = context.createWindowContext(
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, null);
}
LocalColorExtractor.newInstance(mContext)
.applyColorsOverride(context, mWallpaperColors);
return new ContextThemeWrapper(context,
Themes.getActivityThemeRes(context, mWallpaperColors.getColorHints()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/**
* Launcher AppWidgetHostView with support for rounded corners and a fallback View.
*/
public abstract class BaseLauncherAppWidgetHostView extends NavigableAppWidgetHostView {
public abstract class BaseLauncherAppWidgetHostView extends NavigableAppWidgetHostView implements LocalColorExtractor.Listener {

private static final ViewOutlineProvider VIEW_OUTLINE_PROVIDER = new ViewOutlineProvider() {
@Override
Expand Down Expand Up @@ -61,6 +61,7 @@ public void getOutline(View view, Outline outline) {
};

private boolean mIsCornerRadiusEnforced;
private final LocalColorExtractor mColorExtractor;

public BaseLauncherAppWidgetHostView(Context context) {
super(context);
Expand All @@ -70,6 +71,7 @@ public BaseLauncherAppWidgetHostView(Context context) {

mInflater = LayoutInflater.from(context);
mEnforcedCornerRadius = RoundedCornerEnforcement.computeEnforcedRadius(getContext());
mColorExtractor = LocalColorExtractor.newInstance(getContext());
}

@Override
Expand All @@ -85,6 +87,18 @@ public void switchToErrorView() {
updateAppWidget(new RemoteViews(getAppWidgetInfo().provider.getPackageName(), 0));
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mColorExtractor.setListener(this);
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mColorExtractor.setListener(null);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ protected boolean shouldAllowDirectClick() {
return false;
}

@Override
public void onColorsChanged(SparseIntArray colors) {
post(() -> setColorResources(colors));
}

/**
* Listener interface to be called when {@code CellLayout} is about to layout this child view
*/
Expand Down
20 changes: 20 additions & 0 deletions src/com/android/launcher3/widget/LocalColorExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
package com.android.launcher3.widget;

import android.app.WallpaperColors;
import android.appwidget.AppWidgetHostView;
import android.content.Context;
import android.graphics.Rect;
import android.util.SparseIntArray;
import android.view.View;

import androidx.annotation.Nullable;

Expand All @@ -28,6 +31,18 @@
/** Extracts the colors we need from the wallpaper at given locations. */
public class LocalColorExtractor implements ResourceBasedOverride {

/** Listener for color changes on a screen location. */
public interface Listener {
/**
* Method called when the colors on a registered location has changed.
*
* {@code extractedColors} maps the color resources {@code android.R.colors.system_*} to
* their value, in a format that can be passed directly to
* {@link AppWidgetHostView#setColorResources(SparseIntArray)}.
*/
void onColorsChanged(SparseIntArray extractedColors);
}

/**
* Creates a new instance of LocalColorExtractor
*/
Expand All @@ -36,6 +51,11 @@ public static LocalColorExtractor newInstance(Context context) {
R.string.local_colors_extraction_class);
}

/** Sets the object that will receive the color changes. */
public void setListener(@Nullable Listener listener) {
// no-op
}

/**
* Updates the base context to contain the colors override
*/
Expand Down

0 comments on commit aa1576b

Please sign in to comment.