import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
+import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
-import android.view.ViewTreeObserver;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
private static final String TAG = "SwipeDismissLayout";
private static final float DISMISS_MIN_DRAG_WIDTH_RATIO = .33f;
- private boolean mUseDynamicTranslucency = true;
public interface OnDismissedListener {
void onDismissed(SwipeDismissLayout layout);
void onSwipeCancelled(SwipeDismissLayout layout);
}
+ private boolean mIsWindowNativelyTranslucent;
+
// Cached ViewConfiguration and system-wide constant values
private int mSlop;
private int mMinFlingVelocity;
private OnDismissedListener mDismissedListener;
private OnSwipeProgressChangedListener mProgressListener;
- private ViewTreeObserver.OnEnterAnimationCompleteListener mOnEnterAnimationCompleteListener =
- new ViewTreeObserver.OnEnterAnimationCompleteListener() {
- @Override
- public void onEnterAnimationComplete() {
- // SwipeDismissLayout assumes that the host Activity is translucent
- // and temporarily disables translucency when it is fully visible.
- // As soon as the user starts swiping, we will re-enable
- // translucency.
- if (mUseDynamicTranslucency && getContext() instanceof Activity) {
- ((Activity) getContext()).convertFromTranslucent();
- }
- }
- };
private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
private Runnable mRunnable = new Runnable() {
@Override
mMinFlingVelocity = vc.getScaledMinimumFlingVelocity();
TypedArray a = context.getTheme().obtainStyledAttributes(
com.android.internal.R.styleable.Theme);
- mUseDynamicTranslucency = !a.hasValue(
- com.android.internal.R.styleable.Window_windowIsTranslucent);
+ mIsWindowNativelyTranslucent = a.getBoolean(
+ com.android.internal.R.styleable.Window_windowIsTranslucent, false);
a.recycle();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
- if (getContext() instanceof Activity) {
- getViewTreeObserver().addOnEnterAnimationCompleteListener(
- mOnEnterAnimationCompleteListener);
- }
getContext().registerReceiver(mScreenOffReceiver, mScreenOffFilter);
}
@Override
protected void onDetachedFromWindow() {
getContext().unregisterReceiver(mScreenOffReceiver);
- if (getContext() instanceof Activity) {
- getViewTreeObserver().removeOnEnterAnimationCompleteListener(
- mOnEnterAnimationCompleteListener);
- }
super.onDetachedFromWindow();
}
mLastX = ev.getRawX();
updateSwiping(ev);
if (mSwiping) {
- if (mUseDynamicTranslucency && getContext() instanceof Activity) {
- ((Activity) getContext()).convertToTranslucent(null, null);
- }
setProgress(ev.getRawX() - mDownX);
break;
}
}
protected void cancel() {
- if (mUseDynamicTranslucency && getContext() instanceof Activity) {
- ((Activity) getContext()).convertFromTranslucent();
+ if (!mIsWindowNativelyTranslucent) {
+ Activity activity = findActivity();
+ if (activity != null) {
+ activity.convertFromTranslucent();
+ }
}
if (mProgressListener != null) {
mProgressListener.onSwipeCancelled(this);
}
private void updateSwiping(MotionEvent ev) {
+ boolean oldSwiping = mSwiping;
if (!mSwiping) {
float deltaX = ev.getRawX() - mDownX;
float deltaY = ev.getRawY() - mDownY;
mSwiping = false;
}
}
+
+ if (mSwiping && !oldSwiping) {
+ // Swiping has started
+ if (!mIsWindowNativelyTranslucent) {
+ Activity activity = findActivity();
+ if (activity != null) {
+ activity.convertToTranslucent(null, null);
+ }
+ }
+ }
}
private void updateDismiss(MotionEvent ev) {
return 1 - progress * progress * progress;
}
+ private Activity findActivity() {
+ Context context = getContext();
+ while (context instanceof ContextWrapper) {
+ if (context instanceof Activity) {
+ return (Activity) context;
+ }
+ context = ((ContextWrapper) context).getBaseContext();
+ }
+ return null;
+ }
+
private class DismissAnimator implements AnimatorUpdateListener, Animator.AnimatorListener {
private final TimeInterpolator DISMISS_INTERPOLATOR = new DecelerateInterpolator(1.5f);
private final long DISMISS_DURATION = 250;