Skip to content

Commit 61c5a6d

Browse files
pubiqqdsn5ft
authored andcommitted
[Internal] Restore binary compatibility (ViewOverlay)
Resolves #4821 Resolves #4822 - 88fa671 by pubiqq <[email protected]> PiperOrigin-RevId: 776589862 (cherry picked from commit a17e208)
1 parent e722464 commit 61c5a6d

File tree

3 files changed

+105
-11
lines changed

3 files changed

+105
-11
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C) 2016 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.material.internal;
18+
19+
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20+
21+
import android.graphics.drawable.Drawable;
22+
import androidx.annotation.NonNull;
23+
import androidx.annotation.RestrictTo;
24+
25+
/**
26+
* @deprecated Use {@link android.view.ViewOverlay} instead.
27+
* @hide
28+
*/
29+
@Deprecated
30+
@RestrictTo(LIBRARY_GROUP)
31+
public interface ViewOverlayImpl {
32+
33+
/**
34+
* Adds a Drawable to the overlay. The bounds of the drawable should be relative to the host view.
35+
* Any drawable added to the overlay should be removed when it is no longer needed or no longer
36+
* visible.
37+
*
38+
* @param drawable The Drawable to be added to the overlay. This drawable will be drawn when the
39+
* view redraws its overlay.
40+
* @see #remove(Drawable)
41+
*/
42+
void add(@NonNull Drawable drawable);
43+
44+
/**
45+
* Removes the specified Drawable from the overlay.
46+
*
47+
* @param drawable The Drawable to be removed from the overlay.
48+
* @see #add(Drawable)
49+
*/
50+
void remove(@NonNull Drawable drawable);
51+
}

lib/java/com/google/android/material/internal/ViewUtils.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
import android.content.res.TypedArray;
2828
import android.graphics.PorterDuff;
2929
import android.graphics.Rect;
30+
import android.graphics.drawable.Drawable;
3031
import android.util.AttributeSet;
3132
import android.util.TypedValue;
3233
import android.view.View;
3334
import android.view.View.OnAttachStateChangeListener;
3435
import android.view.ViewGroup;
35-
import android.view.ViewOverlay;
3636
import android.view.ViewParent;
3737
import android.view.ViewTreeObserver;
3838
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
@@ -361,6 +361,29 @@ public static float getParentAbsoluteElevation(@NonNull View view) {
361361
return absoluteElevation;
362362
}
363363

364+
/**
365+
* @deprecated Use {@link View#getOverlay()} instead.
366+
*/
367+
@Deprecated
368+
@Nullable
369+
public static ViewOverlayImpl getOverlay(@Nullable View view) {
370+
if (view == null) {
371+
return null;
372+
}
373+
374+
return new ViewOverlayImpl() {
375+
@Override
376+
public void add(@NonNull Drawable drawable) {
377+
view.getOverlay().add(drawable);
378+
}
379+
380+
@Override
381+
public void remove(@NonNull Drawable drawable) {
382+
view.getOverlay().remove(drawable);
383+
}
384+
};
385+
}
386+
364387
/** Returns the content view that is the parent of the provided view. */
365388
@Nullable
366389
public static ViewGroup getContentView(@Nullable View view) {
@@ -387,11 +410,14 @@ public static ViewGroup getContentView(@Nullable View view) {
387410

388411
/**
389412
* Returns the content view overlay that can be used to add drawables on top of all other views.
413+
*
414+
* @deprecated Use {@link View#getOverlay()} on the result of {@link
415+
* ViewUtils#getContentView(View)} instead.
390416
*/
417+
@Deprecated
391418
@Nullable
392-
public static ViewOverlay getContentViewOverlay(@NonNull View view) {
393-
ViewGroup contentView = getContentView(view);
394-
return contentView != null ? contentView.getOverlay() : null;
419+
public static ViewOverlayImpl getContentViewOverlay(@NonNull View view) {
420+
return getOverlay(getContentView(view));
395421
}
396422

397423
public static void addOnGlobalLayoutListener(

lib/java/com/google/android/material/slider/BaseSlider.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,7 +2370,7 @@ protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
23702370
// When the visibility is set to VISIBLE, onDraw() is called again which adds or removes labels
23712371
// according to the setting.
23722372
if (visibility != VISIBLE) {
2373-
ViewOverlay contentViewOverlay = ViewUtils.getContentViewOverlay(this);
2373+
final ViewOverlay contentViewOverlay = getContentViewOverlay();
23742374
if (contentViewOverlay == null) {
23752375
return;
23762376
}
@@ -2380,6 +2380,12 @@ protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
23802380
}
23812381
}
23822382

2383+
@Nullable
2384+
private ViewOverlay getContentViewOverlay() {
2385+
final View contentView = ViewUtils.getContentView(this);
2386+
return contentView == null ? null : contentView.getOverlay();
2387+
}
2388+
23832389
@Override
23842390
public void setEnabled(boolean enabled) {
23852391
super.setEnabled(enabled);
@@ -2455,11 +2461,13 @@ protected void onDetachedFromWindow() {
24552461
}
24562462

24572463
private void detachLabelFromContentView(TooltipDrawable label) {
2458-
ViewOverlay contentViewOverlay = ViewUtils.getContentViewOverlay(this);
2459-
if (contentViewOverlay != null) {
2460-
contentViewOverlay.remove(label);
2461-
label.detachView(ViewUtils.getContentView(this));
2464+
final View contentView = ViewUtils.getContentView(this);
2465+
if (contentView == null) {
2466+
return;
24622467
}
2468+
2469+
contentView.getOverlay().remove(label);
2470+
label.detachView(contentView);
24632471
}
24642472

24652473
@Override
@@ -3511,7 +3519,11 @@ private void ensureLabelsRemoved() {
35113519
@Override
35123520
public void onAnimationEnd(Animator animation) {
35133521
super.onAnimationEnd(animation);
3514-
ViewOverlay contentViewOverlay = ViewUtils.getContentViewOverlay(BaseSlider.this);
3522+
final ViewOverlay contentViewOverlay = getContentViewOverlay();
3523+
if (contentViewOverlay == null) {
3524+
return;
3525+
}
3526+
35153527
for (TooltipDrawable label : labels) {
35163528
contentViewOverlay.remove(label);
35173529
}
@@ -3564,7 +3576,12 @@ private String formatValue(float value) {
35643576
private void setValueForLabel(TooltipDrawable label, float value) {
35653577
label.setText(formatValue(value));
35663578
positionLabel(label, value);
3567-
ViewUtils.getContentViewOverlay(this).add(label);
3579+
final ViewOverlay contentViewOverlay = getContentViewOverlay();
3580+
if (contentViewOverlay == null) {
3581+
return;
3582+
}
3583+
3584+
contentViewOverlay.add(label);
35683585
}
35693586

35703587
private void positionLabel(TooltipDrawable label, float value) {

0 commit comments

Comments
 (0)