Skip to content

Commit a17e208

Browse files
pubiqqhunterstich
authored andcommitted
[Internal] Restore binary compatibility (ViewOverlay)
Resolves #4821 Resolves #4822 - 88fa671 by pubiqq <[email protected]> PiperOrigin-RevId: 776589862
1 parent 51bc807 commit a17e208

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
@@ -2408,7 +2408,7 @@ protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
24082408
// When the visibility is set to VISIBLE, onDraw() is called again which adds or removes labels
24092409
// according to the setting.
24102410
if (visibility != VISIBLE) {
2411-
ViewOverlay contentViewOverlay = ViewUtils.getContentViewOverlay(this);
2411+
final ViewOverlay contentViewOverlay = getContentViewOverlay();
24122412
if (contentViewOverlay == null) {
24132413
return;
24142414
}
@@ -2418,6 +2418,12 @@ protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
24182418
}
24192419
}
24202420

2421+
@Nullable
2422+
private ViewOverlay getContentViewOverlay() {
2423+
final View contentView = ViewUtils.getContentView(this);
2424+
return contentView == null ? null : contentView.getOverlay();
2425+
}
2426+
24212427
@Override
24222428
public void setEnabled(boolean enabled) {
24232429
super.setEnabled(enabled);
@@ -2493,11 +2499,13 @@ protected void onDetachedFromWindow() {
24932499
}
24942500

24952501
private void detachLabelFromContentView(TooltipDrawable label) {
2496-
ViewOverlay contentViewOverlay = ViewUtils.getContentViewOverlay(this);
2497-
if (contentViewOverlay != null) {
2498-
contentViewOverlay.remove(label);
2499-
label.detachView(ViewUtils.getContentView(this));
2502+
final View contentView = ViewUtils.getContentView(this);
2503+
if (contentView == null) {
2504+
return;
25002505
}
2506+
2507+
contentView.getOverlay().remove(label);
2508+
label.detachView(contentView);
25012509
}
25022510

25032511
@Override
@@ -3550,7 +3558,11 @@ private void ensureLabelsRemoved() {
35503558
@Override
35513559
public void onAnimationEnd(Animator animation) {
35523560
super.onAnimationEnd(animation);
3553-
ViewOverlay contentViewOverlay = ViewUtils.getContentViewOverlay(BaseSlider.this);
3561+
final ViewOverlay contentViewOverlay = getContentViewOverlay();
3562+
if (contentViewOverlay == null) {
3563+
return;
3564+
}
3565+
35543566
for (TooltipDrawable label : labels) {
35553567
contentViewOverlay.remove(label);
35563568
}
@@ -3603,7 +3615,12 @@ private String formatValue(float value) {
36033615
private void setValueForLabel(TooltipDrawable label, float value) {
36043616
label.setText(formatValue(value));
36053617
positionLabel(label, value);
3606-
ViewUtils.getContentViewOverlay(this).add(label);
3618+
final ViewOverlay contentViewOverlay = getContentViewOverlay();
3619+
if (contentViewOverlay == null) {
3620+
return;
3621+
}
3622+
3623+
contentViewOverlay.add(label);
36073624
}
36083625

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

0 commit comments

Comments
 (0)