Skip to content

Commit

Permalink
[SearchView] Refactored status bar spacer edge-to-edge detection to w…
Browse files Browse the repository at this point in the history
…ork based on insets

This fixes the detection when using WindowCompat.setDecorFitsSystemWindows, and we shouldn't need to be checking specific edge-to-edge flags anymore

PiperOrigin-RevId: 492309763
  • Loading branch information
dsn5ft authored and raajkumars committed Dec 2, 2022
1 parent 4bff273 commit a241ee9
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions lib/java/com/google/android/material/search/SearchView.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Parcel;
import android.os.Parcelable;
Expand Down Expand Up @@ -147,6 +146,7 @@ public class SearchView extends FrameLayout implements CoordinatorLayout.Attache
private boolean animatedMenuItems;
private boolean autoShowKeyboard;
private boolean useWindowInsetsController;
private boolean statusBarSpacerEnabledOverride;
@NonNull private TransitionState currentTransitionState = TransitionState.HIDDEN;
private Map<View, Integer> childImportantForAccessibilityMap;

Expand Down Expand Up @@ -225,11 +225,7 @@ public void addView(View child, int index, ViewGroup.LayoutParams params) {
protected void onFinishInflate() {
super.onFinishInflate();

Window window = getActivityWindow();
if (window != null) {
this.softInputMode = window.getAttributes().softInputMode;
setStatusBarSpacerEnabled(shouldShowStatusBarSpacer(window));
}
updateSoftInputMode();
}

@RequiresApi(VERSION_CODES.LOLLIPOP)
Expand Down Expand Up @@ -441,7 +437,11 @@ private void setUpStatusBarSpacerInsetListener() {
ViewCompat.setOnApplyWindowInsetsListener(
statusBarSpacer,
(v, insets) -> {
setUpStatusBarSpacer(insets.getSystemWindowInsetTop());
int systemWindowInsetTop = insets.getSystemWindowInsetTop();
setUpStatusBarSpacer(systemWindowInsetTop);
if (!statusBarSpacerEnabledOverride) {
setStatusBarSpacerEnabledInternal(systemWindowInsetTop > 0);
}
return insets;
});
}
Expand Down Expand Up @@ -682,14 +682,18 @@ public void updateSoftInputMode() {
/**
* Enables/disables the status bar spacer, which can be used in cases where the status bar is
* translucent and the {@link SearchView} should not overlap the status bar area. This will be set
* automatically by the {@link SearchView} during initial render based on {@link
* #shouldShowStatusBarSpacer(Window)}, but make sure to invoke this if you would like to override
* the default behavior.
* automatically by the {@link SearchView} during initial render, but make sure to invoke this if
* you would like to override the default behavior.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
public void setStatusBarSpacerEnabled(boolean enabled) {
statusBarSpacerEnabledOverride = true;
setStatusBarSpacerEnabledInternal(enabled);
}

private void setStatusBarSpacerEnabledInternal(boolean enabled) {
statusBarSpacer.setVisibility(enabled ? VISIBLE : GONE);
}

Expand Down Expand Up @@ -800,25 +804,6 @@ public void clearFocusAndHideKeyboard() {
ViewUtils.hideKeyboard(editText, useWindowInsetsController);
}

private static boolean shouldShowStatusBarSpacer(@Nullable Window window) {
if (window == null) {
return false;
}
WindowManager.LayoutParams lp = window.getAttributes();
boolean translucentStatus =
VERSION.SDK_INT >= VERSION_CODES.KITKAT
&& (lp.flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
== WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
boolean layoutNoLimits =
(lp.flags & WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
== WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
boolean edgeToEdge =
VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN
&& (window.getDecorView().getSystemUiVisibility() & ViewUtils.EDGE_TO_EDGE_FLAGS)
== ViewUtils.EDGE_TO_EDGE_FLAGS;
return translucentStatus || layoutNoLimits || edgeToEdge;
}

boolean isAdjustNothingSoftInputMode() {
return softInputMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING;
}
Expand Down

0 comments on commit a241ee9

Please sign in to comment.