Skip to content

Commit

Permalink
feature: perfect QMUINotchHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
cgspine committed Aug 2, 2018
1 parent d2519c5 commit 9f977ec
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static boolean hasNotch(Activity activity) {
return sHasNotch;
}

private static boolean has3rdNotch(Context context){
public static boolean has3rdNotch(Context context){
if (QMUIDeviceHelper.isHuawei()) {
return hasNotchInHuawei(context);
} else if (QMUIDeviceHelper.isVivo()) {
Expand Down
15 changes: 14 additions & 1 deletion qmui/src/main/java/com/qmuiteam/qmui/util/QMUIResHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,21 @@ public static ColorStateList getAttrColorStateList(Context context, int attrRes)
public static Drawable getAttrDrawable(Context context, int attrRes){
int[] attrs = new int[] { attrRes };
TypedArray ta = context.obtainStyledAttributes(attrs);
Drawable drawable = ta.getDrawable(0);
Drawable drawable = getAttrDrawable(context, ta, 0);
ta.recycle();
return drawable;
}

public static Drawable getAttrDrawable(Context context, TypedArray typedArray, int index){
TypedValue value = typedArray.peekValue(index);
if(value != null){
if(value.type != TypedValue.TYPE_ATTRIBUTE && value.resourceId != 0){
return QMUIDrawableHelper.getVectorDrawable(context, value.resourceId);
}
}
return null;
}

public static int getAttrDimen(Context context, int attrRes){
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(attrRes, typedValue, true);
Expand Down Expand Up @@ -101,6 +111,9 @@ public static void assignTextViewWithAttr(TextView textView, int attrRes){
textView.setCompoundDrawablePadding(a.getDimensionPixelSize(attr, 0));
}else if(attr == R.styleable.QMUITextCommonStyleDef_android_textColorHint){
textView.setHintTextColor(a.getColor(attr, 0));
}else if(attr == R.styleable.QMUITextCommonStyleDef_android_textStyle){
int styleIndex = a.getInt(attr, -1);
textView.setTypeface(null, styleIndex);
}
}
textView.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public static void translucent(Activity activity) {

private static boolean supportTranslucent() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
// Essential Phone 不支持沉浸式,否则系统又不从状态栏下方开始布局又给你下发 WindowInsets
&& !QMUIDeviceHelper.isEssentialPhone();
// Essential Phone 在 Android 8 之前沉浸式做得不全,系统不从状态栏顶部开始布局却会下发 WindowInsets
&& !(QMUIDeviceHelper.isEssentialPhone() && Build.VERSION.SDK_INT < Build.VERSION_CODES.O);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,7 @@ public QMUIWindowInsetHelper(ViewGroup viewGroup, IWindowInsetLayout windowInset
KEYBOARD_HEIGHT_BOUNDARY = QMUIDisplayHelper.dp2px(viewGroup.getContext(), 100);

if (QMUINotchHelper.isNotchOfficialSupport()) {
// WindowInsetsCompat does not exist DisplayCutout stuff...
viewGroup.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
if (mWindowInsetLayoutWR.get() != null &&
mWindowInsetLayoutWR.get().applySystemWindowInsets(windowInsets)) {
windowInsets = windowInsets.consumeDisplayCutout();
return windowInsets.consumeSystemWindowInsets();
}
return windowInsets;
}
});
setOnApplyWindowInsetsListener28(viewGroup);
} else {
// some rom crash with WindowInsets...
ViewCompat.setOnApplyWindowInsetsListener(viewGroup,
Expand All @@ -64,6 +53,26 @@ public WindowInsetsCompat onApplyWindowInsets(View v,
}
}

@TargetApi(28)
private void setOnApplyWindowInsetsListener28(ViewGroup viewGroup) {
// WindowInsetsCompat does not exist DisplayCutout stuff...
viewGroup.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
if (mWindowInsetLayoutWR.get() != null &&
mWindowInsetLayoutWR.get().applySystemWindowInsets21(windowInsets)) {
windowInsets = windowInsets.consumeSystemWindowInsets();
DisplayCutout displayCutout = windowInsets.getDisplayCutout();
if (displayCutout != null) {
windowInsets = windowInsets.consumeDisplayCutout();
}
return windowInsets;
}
return windowInsets;
}
});
}

@SuppressWarnings("deprecation")
@TargetApi(19)
public boolean defaultApplySystemWindowInsets19(ViewGroup viewGroup, Rect insets) {
Expand Down Expand Up @@ -100,8 +109,16 @@ public boolean defaultApplySystemWindowInsets19(ViewGroup viewGroup, Rect insets
return consumed;
}

public boolean defaultApplySystemWindowInsets21(ViewGroup viewGroup, Object insets) {
if (QMUINotchHelper.isNotchOfficialSupport()) {
return defaultApplySystemWindowInsets(viewGroup, (WindowInsets) insets);
} else {
return defaultApplySystemWindowInsetsComapt(viewGroup, (WindowInsetsCompat) insets);
}
}

@TargetApi(21)
public boolean defaultApplySystemWindowInsets21(ViewGroup viewGroup, WindowInsetsCompat insets) {
public boolean defaultApplySystemWindowInsetsComapt(ViewGroup viewGroup, WindowInsetsCompat insets) {
if (!insets.hasSystemWindowInsets()) {
return false;
}
Expand Down Expand Up @@ -135,7 +152,7 @@ public boolean defaultApplySystemWindowInsets21(ViewGroup viewGroup, WindowInset
return consumed;
}

@TargetApi(23)
@TargetApi(28)
public boolean defaultApplySystemWindowInsets(ViewGroup viewGroup, WindowInsets insets) {
sApplySystemWindowInsetsCount++;
if (QMUINotchHelper.isNotchOfficialSupport()) {
Expand Down Expand Up @@ -182,8 +199,12 @@ public boolean defaultApplySystemWindowInsets(ViewGroup viewGroup, WindowInsets

private void dispatchNotchInsetChange(View view) {
if (view instanceof INotchInsetConsumer) {
((INotchInsetConsumer) view).notifyInsetMaybeChanged();
} else if (view instanceof ViewGroup) {
boolean stop = ((INotchInsetConsumer) view).notifyInsetMaybeChanged();
if (stop) {
return;
}
}
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
int childCount = viewGroup.getChildCount();
for (int i = 0; i < childCount; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.qmuiteam.qmui.widget;

public interface INotchInsetConsumer {
void notifyInsetMaybeChanged();
}
/**
*
* @return if true stop dispatch to child view
*/
boolean notifyInsetMaybeChanged();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.qmuiteam.qmui.widget;

import android.graphics.Rect;
import android.support.v4.view.WindowInsetsCompat;
import android.view.WindowInsets;

/**
* @author cginechen
Expand All @@ -12,7 +10,5 @@
public interface IWindowInsetLayout {
boolean applySystemWindowInsets19(Rect insets);

boolean applySystemWindowInsets21(WindowInsetsCompat insets);

boolean applySystemWindowInsets(WindowInsets insets);
boolean applySystemWindowInsets21(Object insets);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.qmuiteam.qmui.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Rect;
import android.support.design.widget.AppBarLayout;
Expand Down Expand Up @@ -72,7 +73,7 @@ public int getSystemWindowInsetTop() {
}

@Override
public boolean applySystemWindowInsets21(WindowInsetsCompat insets) {
public boolean applySystemWindowInsets21(Object insets) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.qmuiteam.qmui.R;
import com.qmuiteam.qmui.util.QMUICollapsingTextHelper;
import com.qmuiteam.qmui.util.QMUILangHelper;
import com.qmuiteam.qmui.util.QMUINotchHelper;
import com.qmuiteam.qmui.util.QMUIViewHelper;
import com.qmuiteam.qmui.util.QMUIViewOffsetHelper;

Expand Down Expand Up @@ -101,8 +102,7 @@ public class QMUICollapsingTopBarLayout extends FrameLayout implements IWindowIn

int mCurrentOffset;

WindowInsetsCompat mLastInsets;
Rect mLastInsetRect;
Object mLastInsets;

public QMUICollapsingTopBarLayout(Context context) {
this(context, null);
Expand Down Expand Up @@ -265,10 +265,13 @@ public void draw(Canvas canvas) {

private int getWindowInsetTop() {
if (mLastInsets != null) {
return mLastInsets.getSystemWindowInsetTop();
}
if (mLastInsetRect != null) {
return mLastInsetRect.top;
if(QMUINotchHelper.isNotchOfficialSupport()){
return ((WindowInsets) mLastInsets).getSystemWindowInsetTop();
}else if(mLastInsets instanceof WindowInsetsCompat){
return ((WindowInsetsCompat) mLastInsets).getSystemWindowInsetTop();
}else if(mLastInsets instanceof Rect){
return ((Rect) mLastInsets).top;
}
}
return 0;
}
Expand Down Expand Up @@ -357,7 +360,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (mLastInsets != null || mLastInsetRect != null) {
if (mLastInsets != null) {
// Shift down any views which are not set to fit system windows
final int insetTop = getWindowInsetTop();
for (int i = 0, z = getChildCount(); i < z; i++) {
Expand Down Expand Up @@ -1040,7 +1043,7 @@ public boolean applySystemWindowInsets19(Rect insets) {

// If our insets have changed, keep them and invalidate the scroll ranges...
if (!QMUILangHelper.objectEquals(mLastInsets, newInsets)) {
mLastInsetRect = newInsets;
mLastInsets = newInsets;
requestLayout();
}

Expand All @@ -1050,9 +1053,8 @@ public boolean applySystemWindowInsets19(Rect insets) {
}

@Override
public boolean applySystemWindowInsets21(WindowInsetsCompat insets) {
WindowInsetsCompat newInsets = null;

public boolean applySystemWindowInsets21(Object insets) {
Object newInsets = null;
if (ViewCompat.getFitsSystemWindows(this)) {
// If we're set to fit system windows, keep the insets
newInsets = insets;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.qmuiteam.qmui.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Rect;
Expand Down Expand Up @@ -103,7 +104,7 @@ public boolean applySystemWindowInsets19(Rect insets) {
}

@Override
public boolean applySystemWindowInsets21(WindowInsetsCompat insets) {
public boolean applySystemWindowInsets21(Object insets) {
return mQMUIWindowInsetHelper.defaultApplySystemWindowInsets21(this, insets);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.qmuiteam.qmui.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
Expand Down Expand Up @@ -54,7 +55,7 @@ public boolean applySystemWindowInsets19(Rect insets) {
}

@Override
public boolean applySystemWindowInsets21(WindowInsetsCompat insets) {
public boolean applySystemWindowInsets21(Object insets) {
return mQMUIWindowInsetHelper.defaultApplySystemWindowInsets21(this, insets);
}

Expand Down

0 comments on commit 9f977ec

Please sign in to comment.