Skip to content

Commit f69c72f

Browse files
falhassenglide-copybara-robot
authored andcommitted
Add an experimental plugin system for decorating OnPreDrawListener in Glide.
- This change introduces `GlidePlugins` to allow external code to provide a decorator function for `ViewTreeObserver.OnPreDrawListener` instances used by `ViewTarget` and `CustomViewTarget`. PiperOrigin-RevId: 825163004
1 parent 777b8b5 commit f69c72f

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.bumptech.glide;
2+
3+
import android.view.ViewTreeObserver.OnPreDrawListener;
4+
import java.util.function.Function;
5+
6+
/**
7+
* A class for holding static methods that are used to globally modify the behavior of Glide. This
8+
* class is used to allow apps to globally modify the behavior of Glide.
9+
*/
10+
public final class GlidePlugins {
11+
12+
private static Function<OnPreDrawListener, OnPreDrawListener> onPreDrawListenerDecorator = null;
13+
14+
/**
15+
* Sets a decorator to be applied to all {@link OnPreDrawListener}s created by Glide.
16+
*
17+
* <p>This is intended to be used by apps that want to globally modify all {@link
18+
* OnPreDrawListener}s created by Glide.
19+
*
20+
* <p>This is an experimental method that may be removed without warning in a future version.
21+
*/
22+
public static void experimentalSetOnPreDrawListenerDecorator(
23+
Function<OnPreDrawListener, OnPreDrawListener> decorator) {
24+
onPreDrawListenerDecorator = decorator;
25+
}
26+
27+
/**
28+
* Returns the {@link OnPreDrawListener} provided, possibly after being decorated by {@link
29+
* #experimentalSetOnPreDrawListenerDecorator(Function)}.
30+
*/
31+
public static OnPreDrawListener decorateOnPreDrawListener(OnPreDrawListener listener) {
32+
if (onPreDrawListenerDecorator == null) {
33+
return listener;
34+
}
35+
return onPreDrawListenerDecorator.apply(listener);
36+
}
37+
38+
private GlidePlugins() {}
39+
}

library/src/main/java/com/bumptech/glide/request/target/CustomViewTarget.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import androidx.annotation.NonNull;
1515
import androidx.annotation.Nullable;
1616
import androidx.annotation.VisibleForTesting;
17+
import com.bumptech.glide.GlidePlugins;
1718
import com.bumptech.glide.R;
1819
import com.bumptech.glide.request.Request;
1920
import com.bumptech.glide.request.transition.Transition;
@@ -299,7 +300,7 @@ static final class SizeDeterminer {
299300
private final List<SizeReadyCallback> cbs = new ArrayList<>();
300301
@Synthetic boolean waitForLayout;
301302

302-
@Nullable private SizeDeterminerLayoutListener layoutListener;
303+
@Nullable private ViewTreeObserver.OnPreDrawListener layoutListener;
303304

304305
SizeDeterminer(@NonNull View view) {
305306
this.view = view;
@@ -359,7 +360,8 @@ void getSize(@NonNull SizeReadyCallback cb) {
359360
}
360361
if (layoutListener == null) {
361362
ViewTreeObserver observer = view.getViewTreeObserver();
362-
layoutListener = new SizeDeterminerLayoutListener(this);
363+
layoutListener =
364+
GlidePlugins.decorateOnPreDrawListener(new SizeDeterminerLayoutListener(this));
363365
observer.addOnPreDrawListener(layoutListener);
364366
}
365367
}

library/src/main/java/com/bumptech/glide/request/target/ViewTarget.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import androidx.annotation.NonNull;
1515
import androidx.annotation.Nullable;
1616
import androidx.annotation.VisibleForTesting;
17+
import com.bumptech.glide.GlidePlugins;
1718
import com.bumptech.glide.R;
1819
import com.bumptech.glide.request.Request;
1920
import com.bumptech.glide.util.Preconditions;
@@ -324,7 +325,7 @@ static final class SizeDeterminer {
324325
private final List<SizeReadyCallback> cbs = new ArrayList<>();
325326
@Synthetic boolean waitForLayout;
326327

327-
@Nullable private SizeDeterminerLayoutListener layoutListener;
328+
@Nullable private ViewTreeObserver.OnPreDrawListener layoutListener;
328329

329330
SizeDeterminer(@NonNull View view) {
330331
this.view = view;
@@ -384,7 +385,8 @@ void getSize(@NonNull SizeReadyCallback cb) {
384385
}
385386
if (layoutListener == null) {
386387
ViewTreeObserver observer = view.getViewTreeObserver();
387-
layoutListener = new SizeDeterminerLayoutListener(this);
388+
layoutListener =
389+
GlidePlugins.decorateOnPreDrawListener(new SizeDeterminerLayoutListener(this));
388390
observer.addOnPreDrawListener(layoutListener);
389391
}
390392
}

0 commit comments

Comments
 (0)