Skip to content

Commit

Permalink
Merge branch 'master' into androidx
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/src/main/java/per/goweii/android/anylayer/CommonActivity.java
#	app/src/main/java/per/goweii/android/anylayer/StatusBarUtils.java
  • Loading branch information
mbacuiz committed Dec 5, 2019
2 parents 5a2a668 + 1e4e364 commit 6339a35
Show file tree
Hide file tree
Showing 16 changed files with 284 additions and 289 deletions.
3 changes: 0 additions & 3 deletions anylayer/src/main/java/per/goweii/anylayer/DecorLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
Expand Down
33 changes: 22 additions & 11 deletions anylayer/src/main/java/per/goweii/anylayer/DialogLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public class DialogLayer extends DecorLayer {

private SoftInputHelper mSoftInputHelper = null;

public DialogLayer(Context context) {
this(Utils.getActivity(Utils.requireNonNull(context, "context == null")));
}

public DialogLayer(Activity activity) {
super(activity);
getViewHolder().setActivityContent(getViewHolder().getDecor().findViewById(android.R.id.content));
}

public DialogLayer(Context context) {
this(Utils.getActivity(Utils.requireNonNull(context, "context == null")));
}

@Override
protected Level getLevel() {
return Level.DIALOG;
Expand Down Expand Up @@ -567,15 +567,26 @@ public ImageView getBackground() {
* 在{@link OnVisibleChangeListener#onShow(Layer)}中调用
* 应该和{@link #removeSoftInput()}成对出现
*
* @param editText 焦点EditTexts
* @param editTexts 焦点EditTexts
*/
public void compatSoftInput(EditText... editText) {
Activity activity = Utils.getActivity(getActivity());
if (activity != null) {
mSoftInputHelper = SoftInputHelper.attach(activity)
.init(getViewHolder().getContentWrapper(), getViewHolder().getContent(), editText)
.moveWithTranslation();
public DialogLayer compatSoftInput(boolean bottomToContentView, EditText... editTexts) {
if (mSoftInputHelper == null) {
mSoftInputHelper = SoftInputHelper.attach(getActivity())
.moveWithTranslation()
.moveBy(getViewHolder().getContentWrapper());
}
if (bottomToContentView) {
mSoftInputHelper.moveWith(getViewHolder().getContent(), editTexts);
} else {
for (EditText editText : editTexts) {
mSoftInputHelper.moveWith(editText, editText);
}
}
return this;
}

public DialogLayer compatSoftInput(EditText... editTexts) {
return compatSoftInput(true, editTexts);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion anylayer/src/main/java/per/goweii/anylayer/FloatLayer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package per.goweii.anylayer;

import android.animation.Animator;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -16,7 +17,11 @@
public class FloatLayer extends DecorLayer {

public FloatLayer(Context context) {
super(Utils.getActivity(Utils.requireNonNull(context, "context == null")));
this(Utils.getActivity(Utils.requireNonNull(context, "context == null")));
}

public FloatLayer(Activity activity) {
super(activity);
interceptKeyEvent(false);
cancelableOnKeyBack(false);
}
Expand Down
7 changes: 6 additions & 1 deletion anylayer/src/main/java/per/goweii/anylayer/GuideLayer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package per.goweii.anylayer;

import android.animation.Animator;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -16,7 +17,11 @@
public class GuideLayer extends DecorLayer {

public GuideLayer(Context context) {
super(Utils.getActivity(Utils.requireNonNull(context, "context == null")));
this(Utils.getActivity(Utils.requireNonNull(context, "context == null")));
}

public GuideLayer(Activity activity) {
super(activity);
cancelableOnKeyBack(false);
}

Expand Down
7 changes: 6 additions & 1 deletion anylayer/src/main/java/per/goweii/anylayer/Layer.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,22 @@ public void onPreRemove() {
mAnimatorOut = onCreateOutAnimator(mViewManager.getChild());
if (mAnimatorOut != null) {
mAnimatorOut.addListener(new Animator.AnimatorListener() {
private boolean beenCanceled = false;

@Override
public void onAnimationStart(Animator animation) {
}

@Override
public void onAnimationEnd(Animator animation) {
mViewManager.detach();
if (!beenCanceled) {
mViewManager.detach();
}
}

@Override
public void onAnimationCancel(Animator animation) {
beenCanceled = true;
}

@Override
Expand Down
7 changes: 6 additions & 1 deletion anylayer/src/main/java/per/goweii/anylayer/PopupLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.util.TypedValue;
Expand All @@ -23,7 +24,11 @@ public class PopupLayer extends DialogLayer {
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;

public PopupLayer(Context context) {
super(Utils.requireNonNull(context, "context == null"));
super(context);
}

public PopupLayer(Activity activity) {
super(activity);
}

public PopupLayer(View targetView) {
Expand Down
132 changes: 63 additions & 69 deletions anylayer/src/main/java/per/goweii/anylayer/SoftInputHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;
import android.widget.EditText;

import java.util.HashMap;
import java.util.Map;

/**
* 监听软键盘的打开和隐藏
Expand All @@ -24,10 +26,9 @@ final class SoftInputHelper implements ViewTreeObserver.OnGlobalLayoutListener,
private final Window window;
private final View rootView;

private long duration = 200;
private long duration = 300;
private View moveView = null;
private View bottomView = null;
private EditText[] focusViews = null;
private Map<View, View> focusBottomMap = new HashMap<>(1);
private OnSoftInputListener onSoftInputListener = null;

private boolean moveWithScroll = false;
Expand All @@ -39,18 +40,7 @@ final class SoftInputHelper implements ViewTreeObserver.OnGlobalLayoutListener,
private Runnable moveRunnable = new Runnable() {
@Override
public void run() {
if (isViewFocus()) {
Rect rect = getRootViewRect();
int bottomViewY = getBottomViewY();
if (bottomViewY > rect.bottom) {
int offHeight = bottomViewY - rect.bottom;
moveHeight += offHeight;
move();
}
} else {
moveHeight = 0;
move();
}
calcToMove();
}
};

Expand All @@ -66,6 +56,7 @@ private SoftInputHelper(Activity activity) {
ViewTreeObserver observer = rootView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(this);
observer.addOnGlobalFocusChangeListener(this);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
}

public void detach() {
Expand All @@ -79,13 +70,17 @@ public void detach() {
}
}

public SoftInputHelper init(View moveView, View bottomView, EditText... focusViews) {
public SoftInputHelper moveBy(View moveView) {
Utils.requireNonNull(moveView, "moveView == null");
Utils.requireNonNull(bottomView, "bottomView == null");
this.moveView = moveView;
this.bottomView = bottomView;
this.focusViews = focusViews;
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
return this;
}

public SoftInputHelper moveWith(View bottomView, View... focusViews) {
Utils.requireNonNull(bottomView, "bottomView == null");
for (View focusView : focusViews) {
focusBottomMap.put(focusView, bottomView);
}
return this;
}

Expand Down Expand Up @@ -121,41 +116,20 @@ public SoftInputHelper moveWithTranslation() {

@Override
public void onGlobalLayout() {
Rect rect = getRootViewRect();
boolean isOpen = isSoftOpen(rect.bottom - rect.top, rootView.getHeight());
boolean isOpen = isSoftOpen();
if (isOpen) {
if (!isOpened) {
isOpened = true;
if (onSoftInputListener != null) {
onSoftInputListener.onOpen();
}
}
if (moveView != null && bottomView != null && focusViews != null) {
if (moveView != null) {
if (isFocusChange) {
isFocusChange = false;
rootView.removeCallbacks(moveRunnable);
}
if (isViewFocus()) {
int bottomViewY = getBottomViewY();
if (bottomViewY > rect.bottom) {
int offHeight = bottomViewY - rect.bottom;
moveHeight += offHeight;
move();
} else if (bottomViewY < rect.bottom) {
int offHeight = -(bottomViewY - rect.bottom);
if (moveHeight > 0) {
if (moveHeight >= offHeight) {
moveHeight -= offHeight;
} else {
moveHeight = 0;
}
move();
}
}
} else {
moveHeight = 0;
move();
}
calcToMove();
}
} else {
if (isOpened) {
Expand All @@ -164,28 +138,53 @@ public void onGlobalLayout() {
onSoftInputListener.onClose();
}
}
if (moveView != null && bottomView != null && focusViews != null) {
if (moveView != null) {
moveHeight = 0;
move();
}
}
}

private void calcToMove(){
View focusView = isViewFocus();
if (focusView != null) {
View bottomView = focusBottomMap.get(focusView);
if (bottomView != null) {
Rect rect = getRootViewRect();
int bottomViewY = getBottomViewY(bottomView);
if (bottomViewY > rect.bottom) {
int offHeight = bottomViewY - rect.bottom;
moveHeight += offHeight;
move();
} else if (bottomViewY < rect.bottom) {
int offHeight = -(bottomViewY - rect.bottom);
if (moveHeight > 0) {
if (moveHeight >= offHeight) {
moveHeight -= offHeight;
} else {
moveHeight = 0;
}
move();
}
}
}
} else {
moveHeight = 0;
move();
}
}

@Override
public void onGlobalFocusChanged(View oldFocus, View newFocus) {
if (isOpened) {
if (newFocus instanceof EditText) {
if (moveView != null && bottomView != null && focusViews != null) {
isFocusChange = true;
rootView.postDelayed(moveRunnable, 100);
}
} else {
InputMethodUtils.hide(oldFocus);
if (moveView != null) {
isFocusChange = true;
rootView.postDelayed(moveRunnable, 100);
}
}
}

private int getBottomViewY() {
private int getBottomViewY(View bottomView) {
int[] bottomLocation = new int[2];
bottomView.getLocationOnScreen(bottomLocation);
return bottomLocation[1] + bottomView.getHeight();
Expand Down Expand Up @@ -231,11 +230,12 @@ private void scrollTo(int to) {
* 判断软键盘打开状态的阈值
* 此处以用户可用高度变化值大于1/4总高度时作为判断依据。
*
* @param usableHeightNow 当前可被用户使用的高度
* @param usableHeightSansKeyboard 总高度,及包含软键盘占位的高度
* @return boolean
*/
private boolean isSoftOpen(int usableHeightNow, int usableHeightSansKeyboard) {
private boolean isSoftOpen() {
Rect rect = getRootViewRect();
int usableHeightNow = rect.bottom - rect.top;
int usableHeightSansKeyboard = rootView.getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard / 4)) {
return true;
Expand All @@ -244,20 +244,14 @@ private boolean isSoftOpen(int usableHeightNow, int usableHeightSansKeyboard) {
}
}

private boolean isViewFocus() {
boolean focus = false;
if (focusViews == null || focusViews.length == 0) {
focus = true;
} else {
View focusView = window.getCurrentFocus();
for (EditText editText : focusViews) {
if (focusView == editText) {
focus = true;
break;
}
private View isViewFocus() {
View focusView = window.getCurrentFocus();
for (View view : focusBottomMap.keySet()) {
if (focusView == view) {
return view;
}
}
return focus;
return null;
}

public interface OnSoftInputListener {
Expand Down
7 changes: 6 additions & 1 deletion anylayer/src/main/java/per/goweii/anylayer/ToastLayer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package per.goweii.anylayer;

import android.animation.Animator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
Expand All @@ -25,7 +26,11 @@
public class ToastLayer extends DecorLayer implements Runnable {

public ToastLayer(Context context) {
super(Utils.getActivity(Utils.requireNonNull(context, "context == null")));
this(Utils.getActivity(Utils.requireNonNull(context, "context == null")));
}

public ToastLayer(Activity activity) {
super(activity);
interceptKeyEvent(false);
cancelableOnKeyBack(false);
}
Expand Down
Loading

0 comments on commit 6339a35

Please sign in to comment.