Skip to content

Commit

Permalink
Mitigations for #628
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamantcheese committed Feb 18, 2020
1 parent a58cff6 commit bd5b314
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public int getCount() {
public void finishUpdate(ViewGroup container) {
for (ModeChange change : pendingModeChanges) {
MultiImageView view = find(change.postImage);
if (view == null) {
if (view == null || view.getWindowToken() == null) {
Logger.w(TAG, "finishUpdate setMode view still not found");
} else {
view.setMode(loadable, change.mode, change.center);
Expand All @@ -86,7 +86,7 @@ public void finishUpdate(ViewGroup container) {

public void setMode(final PostImage postImage, MultiImageView.Mode mode, boolean center) {
MultiImageView view = find(postImage);
if (view == null) {
if (view == null || view.getWindowToken() == null) {
pendingModeChanges.add(new ModeChange(mode, postImage, center));
} else {
view.setMode(loadable, mode, center);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,19 @@ public interface OnMeasuredCallback {
public static void waitForMeasure(final View view, final OnMeasuredCallback callback) {
if (view.getWindowToken() == null) {
// If you call getViewTreeObserver on a view when it's not attached to a window will result in the creation of a temporarily viewtreeobserver.
// This is almost always not what you want.
throw new IllegalArgumentException(
"The view given to waitForMeasure is not attached to the window and does not have a ViewTreeObserver.");
view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
waitForLayoutInternal(true, view.getViewTreeObserver(), view, callback);
view.removeOnAttachStateChangeListener(this);
}

@Override
public void onViewDetachedFromWindow(View v) {
view.removeOnAttachStateChangeListener(this);
}
});
return;
}

waitForLayoutInternal(true, view.getViewTreeObserver(), view, callback);
Expand All @@ -307,8 +317,19 @@ public static void waitForMeasure(final View view, final OnMeasuredCallback call
public static void waitForLayout(final View view, final OnMeasuredCallback callback) {
if (view.getWindowToken() == null) {
// See comment above
throw new IllegalArgumentException(
"The view given to waitForLayout is not attached to the window and does not have a ViewTreeObserver.");
view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
waitForLayoutInternal(true, view.getViewTreeObserver(), view, callback);
view.removeOnAttachStateChangeListener(this);
}

@Override
public void onViewDetachedFromWindow(View v) {
view.removeOnAttachStateChangeListener(this);
}
});
return;
}

waitForLayoutInternal(false, view.getViewTreeObserver(), view, callback);
Expand Down

0 comments on commit bd5b314

Please sign in to comment.