Skip to content

Commit 88fa671

Browse files
committed
[Internal] Restore binary compatibility (ViewOverlay)
1 parent 827e440 commit 88fa671

File tree

3 files changed

+115
-11
lines changed

3 files changed

+115
-11
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
* Backward-compatible implementation of {@link android.view.ViewOverlay} for API < 18.
27+
* TODO(b/144937975): Remove and use the official version from androidx when it's available.
28+
*
29+
* @deprecated Use {@link android.view.ViewOverlay} instead.
30+
* @hide
31+
*/
32+
@Deprecated
33+
@RestrictTo(LIBRARY_GROUP)
34+
public interface ViewOverlayImpl {
35+
36+
/**
37+
* Adds a Drawable to the overlay. The bounds of the drawable should be relative to the host view.
38+
* Any drawable added to the overlay should be removed when it is no longer needed or no longer
39+
* visible.
40+
*
41+
* @param drawable The Drawable to be added to the overlay. This drawable will be drawn when the
42+
* view redraws its overlay.
43+
* @see #remove(Drawable)
44+
*/
45+
void add(@NonNull Drawable drawable);
46+
47+
/**
48+
* Removes the specified Drawable from the overlay.
49+
*
50+
* @param drawable The Drawable to be removed from the overlay.
51+
* @see #add(Drawable)
52+
*/
53+
void remove(@NonNull Drawable drawable);
54+
}

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

Lines changed: 33 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,32 @@ public static float getParentAbsoluteElevation(@NonNull View view) {
361361
return absoluteElevation;
362362
}
363363

364+
/**
365+
* Backward-compatible {@link View#getOverlay()} for API < 18.
366+
* TODO(b/144937975): Remove and use the official version from androidx when it's available.
367+
*
368+
* @deprecated Use {@link View#getOverlay()} instead.
369+
*/
370+
@Deprecated
371+
@Nullable
372+
public static ViewOverlayImpl getOverlay(@Nullable View view) {
373+
if (view == null) {
374+
return null;
375+
}
376+
377+
return new ViewOverlayImpl() {
378+
@Override
379+
public void add(@NonNull Drawable drawable) {
380+
view.getOverlay().add(drawable);
381+
}
382+
383+
@Override
384+
public void remove(@NonNull Drawable drawable) {
385+
view.getOverlay().remove(drawable);
386+
}
387+
};
388+
}
389+
364390
/** Returns the content view that is the parent of the provided view. */
365391
@Nullable
366392
public static ViewGroup getContentView(@Nullable View view) {
@@ -387,11 +413,14 @@ public static ViewGroup getContentView(@Nullable View view) {
387413

388414
/**
389415
* Returns the content view overlay that can be used to add drawables on top of all other views.
416+
*
417+
* @deprecated Use {@link View#getOverlay()} on the result of {@link
418+
* ViewUtils#getContentView(View)} instead.
390419
*/
420+
@Deprecated
391421
@Nullable
392-
public static ViewOverlay getContentViewOverlay(@NonNull View view) {
393-
ViewGroup contentView = getContentView(view);
394-
return contentView != null ? contentView.getOverlay() : null;
422+
public static ViewOverlayImpl getContentViewOverlay(@NonNull View view) {
423+
return getOverlay(getContentView(view));
395424
}
396425

397426
public static void addOnGlobalLayoutListener(

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

Lines changed: 28 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,16 @@ 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+
if (contentView == null) {
2425+
return null;
2426+
}
2427+
2428+
return contentView.getOverlay();
2429+
}
2430+
24212431
@Override
24222432
public void setEnabled(boolean enabled) {
24232433
super.setEnabled(enabled);
@@ -2493,11 +2503,13 @@ protected void onDetachedFromWindow() {
24932503
}
24942504

24952505
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));
2506+
final View contentView = ViewUtils.getContentView(this);
2507+
if (contentView == null) {
2508+
return;
25002509
}
2510+
2511+
contentView.getOverlay().remove(label);
2512+
label.detachView(contentView);
25012513
}
25022514

25032515
@Override
@@ -3550,7 +3562,11 @@ private void ensureLabelsRemoved() {
35503562
@Override
35513563
public void onAnimationEnd(Animator animation) {
35523564
super.onAnimationEnd(animation);
3553-
ViewOverlay contentViewOverlay = ViewUtils.getContentViewOverlay(BaseSlider.this);
3565+
final ViewOverlay contentViewOverlay = getContentViewOverlay();
3566+
if (contentViewOverlay == null) {
3567+
return;
3568+
}
3569+
35543570
for (TooltipDrawable label : labels) {
35553571
contentViewOverlay.remove(label);
35563572
}
@@ -3603,7 +3619,12 @@ private String formatValue(float value) {
36033619
private void setValueForLabel(TooltipDrawable label, float value) {
36043620
label.setText(formatValue(value));
36053621
positionLabel(label, value);
3606-
ViewUtils.getContentViewOverlay(this).add(label);
3622+
final ViewOverlay contentViewOverlay = getContentViewOverlay();
3623+
if (contentViewOverlay == null) {
3624+
return;
3625+
}
3626+
3627+
contentViewOverlay.add(label);
36073628
}
36083629

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

0 commit comments

Comments
 (0)