DrawerLayout.java revision 09c38625b5400dcda3d872dbe865c867cc02e5eb
11d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell/*
21d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * Copyright (C) 2013 The Android Open Source Project
31d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell *
41d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * Licensed under the Apache License, Version 2.0 (the "License");
51d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * you may not use this file except in compliance with the License.
61d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * You may obtain a copy of the License at
71d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell *
81d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell *      http://www.apache.org/licenses/LICENSE-2.0
91d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell *
101d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * Unless required by applicable law or agreed to in writing, software
111d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * distributed under the License is distributed on an "AS IS" BASIS,
121d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * See the License for the specific language governing permissions and
141d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * limitations under the License.
151d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell */
161d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
171d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
181d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellpackage android.support.v4.widget;
191d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
201d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.content.Context;
211d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.content.res.TypedArray;
221d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.graphics.Canvas;
231d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.graphics.Paint;
24b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powellimport android.graphics.PixelFormat;
25b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powellimport android.graphics.drawable.Drawable;
2657902cff6c54c245feea589b06d12dba8f1a2a24Adam Powellimport android.os.Parcel;
2757902cff6c54c245feea589b06d12dba8f1a2a24Adam Powellimport android.os.Parcelable;
28cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powellimport android.os.SystemClock;
291d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.support.v4.view.GravityCompat;
30791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powellimport android.support.v4.view.KeyEventCompat;
311d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.support.v4.view.MotionEventCompat;
321d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.support.v4.view.ViewCompat;
331d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.util.AttributeSet;
341d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.view.Gravity;
35791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powellimport android.view.KeyEvent;
361d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.view.MotionEvent;
371d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.view.View;
381d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellimport android.view.ViewGroup;
391d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
401d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell/**
411d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * DrawerLayout acts as a top-level container for window content that allows for
421d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * interactive "drawer" views to be pulled out from the edge of the window.
431d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell *
441d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * <p>Drawer positioning and layout is controlled using the <code>android:layout_gravity</code>
4509c38625b5400dcda3d872dbe865c867cc02e5ebAdam Powell * attribute on child views corresponding to which side of the view you want the drawer
4609c38625b5400dcda3d872dbe865c867cc02e5ebAdam Powell * to emerge from: left or right. (Or start/end on platform versions that support layout direction.)
4709c38625b5400dcda3d872dbe865c867cc02e5ebAdam Powell * </p>
4809c38625b5400dcda3d872dbe865c867cc02e5ebAdam Powell *
4909c38625b5400dcda3d872dbe865c867cc02e5ebAdam Powell * <p>To use a DrawerLayout, position your primary content view as the first child with
5009c38625b5400dcda3d872dbe865c867cc02e5ebAdam Powell * a width and height of <code>match_parent</code>. Add drawers as child views after the main
5109c38625b5400dcda3d872dbe865c867cc02e5ebAdam Powell * content view and set the <code>layout_gravity</code> appropriately. Drawers commonly use
5209c38625b5400dcda3d872dbe865c867cc02e5ebAdam Powell * <code>match_parent</code> for height with a fixed width.</p>
5309c38625b5400dcda3d872dbe865c867cc02e5ebAdam Powell *
5409c38625b5400dcda3d872dbe865c867cc02e5ebAdam Powell * <p>{@link DrawerListener} can be used to monitor the state and motion of drawer views.
5509c38625b5400dcda3d872dbe865c867cc02e5ebAdam Powell * Avoid performing expensive operations such as layout during animation as it can cause
5609c38625b5400dcda3d872dbe865c867cc02e5ebAdam Powell * stuttering; try to perform expensive operations during the {@link #STATE_IDLE} state.
5709c38625b5400dcda3d872dbe865c867cc02e5ebAdam Powell * {@link SimpleDrawerListener} offers default/no-op implementations of each callback method.</p>
581d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell *
591d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * <p>As per the Android Design guide, any drawers positioned to the left/start should
601d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * always contain content for navigating around the application, whereas any drawers
611d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * positioned to the right/end should always contain actions to take on the current content.
621d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * This preserves the same navigation left, actions right structure present in the Action Bar
631d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell * and elsewhere.</p>
641d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell */
651d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powellpublic class DrawerLayout extends ViewGroup {
661d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private static final String TAG = "DrawerLayout";
671d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
681d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
691d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Indicates that any drawers are in an idle, settled state. No animation is in progress.
701d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
711d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public static final int STATE_IDLE = ViewDragHelper.STATE_IDLE;
721d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
731d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
741d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Indicates that a drawer is currently being dragged by the user.
751d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
761d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public static final int STATE_DRAGGING = ViewDragHelper.STATE_DRAGGING;
771d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
781d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
791d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Indicates that a drawer is in the process of settling to a final position.
801d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
811d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public static final int STATE_SETTLING = ViewDragHelper.STATE_SETTLING;
821d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
830492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    /**
840492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * The drawer is unlocked.
850492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     */
860492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    public static final int LOCK_MODE_UNLOCKED = 0;
870492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell
880492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    /**
890492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * The drawer is locked closed. The user may not open it, though
900492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * the app may open it programmatically.
910492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     */
920492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    public static final int LOCK_MODE_LOCKED_CLOSED = 1;
930492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell
940492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    /**
950492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * The drawer is locked open. The user may not close it, though the app
960492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * may close it programmatically.
970492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     */
980492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    public static final int LOCK_MODE_LOCKED_OPEN = 2;
990492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell
1001d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private static final int MIN_DRAWER_MARGIN = 64; // dp
1011d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
1021d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private static final int DEFAULT_SCRIM_COLOR = 0x99000000;
1031d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
104ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell    /**
1051732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     * Length of time to delay before peeking the drawer.
1061732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell     */
1071732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    private static final int PEEK_DELAY = 160; // ms
1081732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
1091732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    /**
110ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell     * Experimental feature.
111ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell     */
112ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell    private static final boolean ALLOW_EDGE_LOCK = false;
113ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell
1141d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private static final int[] LAYOUT_ATTRS = new int[] {
1151d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            android.R.attr.layout_gravity
1161d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    };
1171d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
1181d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private int mMinDrawerMargin;
1191d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
1201d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private int mScrimColor = DEFAULT_SCRIM_COLOR;
1211d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private float mScrimOpacity;
1221d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private Paint mScrimPaint = new Paint();
1231d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
1241d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private final ViewDragHelper mLeftDragger;
1251d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private final ViewDragHelper mRightDragger;
1261732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    private final ViewDragCallback mLeftCallback;
1271732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    private final ViewDragCallback mRightCallback;
1281d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private int mDrawerState;
129b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    private boolean mInLayout;
13057902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    private boolean mFirstLayout = true;
1310492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    private int mLockModeLeft;
1320492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    private int mLockModeRight;
1331732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell    private boolean mDisallowInterceptRequested;
134cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell    private boolean mChildrenCanceledTouch;
1351d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
1361d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private DrawerListener mListener;
1371d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
1381d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private float mInitialMotionX;
1391d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private float mInitialMotionY;
1401d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
141b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    private Drawable mShadowLeft;
142b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    private Drawable mShadowRight;
143b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
1441d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
1451d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Listener for monitoring events about drawers.
1461d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
1471d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public interface DrawerListener {
1481d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        /**
1491d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * Called when a drawer's position changes.
1501d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * @param drawerView The child view that was moved
1511d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * @param slideOffset The new offset of this drawer within its range, from 0-1
1521d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         */
1531d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public void onDrawerSlide(View drawerView, float slideOffset);
1541d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
1551d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        /**
1561d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * Called when a drawer has settled in a completely open state.
1571d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * The drawer is interactive at this point.
1581d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         *
1591d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * @param drawerView Drawer view that is now open
1601d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         */
1611d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public void onDrawerOpened(View drawerView);
1621d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
1631d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        /**
1641d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * Called when a drawer has settled in a completely closed state.
1651d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         *
1661d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * @param drawerView Drawer view that is now closed
1671d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         */
1681d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public void onDrawerClosed(View drawerView);
1691d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
1701d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        /**
1711d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * Called when the drawer motion state changes. The new state will
1721d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * be one of {@link #STATE_IDLE}, {@link #STATE_DRAGGING} or {@link #STATE_SETTLING}.
1731d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         *
1741d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         * @param newState The new drawer motion state
1751d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell         */
1761d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public void onDrawerStateChanged(int newState);
1771d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
1781d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
179b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    /**
180b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * Stub/no-op implementations of all methods of {@link DrawerListener}.
181b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * Override this if you only care about a few of the available callback methods.
182b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     */
183b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    public static abstract class SimpleDrawerListener implements DrawerListener {
184b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        @Override
185b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        public void onDrawerSlide(View drawerView, float slideOffset) {
186b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        }
187b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
188b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        @Override
189b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        public void onDrawerOpened(View drawerView) {
190b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        }
191b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
192b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        @Override
193b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        public void onDrawerClosed(View drawerView) {
194b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        }
195b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
196b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        @Override
197b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        public void onDrawerStateChanged(int newState) {
198b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        }
199b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    }
200b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
2011d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public DrawerLayout(Context context) {
2021d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        this(context, null);
2031d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
2041d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
2051d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public DrawerLayout(Context context, AttributeSet attrs) {
2061d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        this(context, attrs, 0);
2071d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
2081d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
2091d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public DrawerLayout(Context context, AttributeSet attrs, int defStyle) {
2101d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        super(context, attrs, defStyle);
2111d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
2121d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final float density = getResources().getDisplayMetrics().density;
2131d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        mMinDrawerMargin = (int) (MIN_DRAWER_MARGIN * density + 0.5f);
2141d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
2151732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mLeftCallback = new ViewDragCallback(Gravity.LEFT);
2161732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mRightCallback = new ViewDragCallback(Gravity.RIGHT);
2171d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
2181732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mLeftDragger = ViewDragHelper.create(this, 0.5f, mLeftCallback);
2191d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        mLeftDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
2201732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mLeftCallback.setDragger(mLeftDragger);
2211d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
2221732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mRightDragger = ViewDragHelper.create(this, 0.5f, mRightCallback);
2231d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        mRightDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_RIGHT);
2241732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mRightCallback.setDragger(mRightDragger);
225791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell
226791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell        // So that we can catch the back button
227791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell        setFocusableInTouchMode(true);
2281d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
2291d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
2301d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
231b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * Set a simple drawable used for the left or right shadow.
232b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * The drawable provided must have a nonzero intrinsic width.
233b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     *
234b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * @param shadowDrawable Shadow drawable to use at the edge of a drawer
235b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * @param gravity Which drawer the shadow should apply to
236b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     */
237b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    public void setDrawerShadow(Drawable shadowDrawable, int gravity) {
238b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        /*
239b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell         * TODO Someone someday might want to set more complex drawables here.
240b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell         * They're probably nuts, but we might want to consider registering callbacks,
241b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell         * setting states, etc. properly.
242b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell         */
243b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
244b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        final int absGravity = GravityCompat.getAbsoluteGravity(gravity,
245b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                ViewCompat.getLayoutDirection(this));
246b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        if ((absGravity & Gravity.LEFT) == Gravity.LEFT) {
247b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            mShadowLeft = shadowDrawable;
248b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            invalidate();
249b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        }
250b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        if ((absGravity & Gravity.RIGHT) == Gravity.RIGHT) {
251b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            mShadowRight = shadowDrawable;
252b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            invalidate();
253b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        }
254b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    }
255b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
256b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    /**
257b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * Set a simple drawable used for the left or right shadow.
258b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * The drawable provided must have a nonzero intrinsic width.
259b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     *
260b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * @param resId Resource id of a shadow drawable to use at the edge of a drawer
261b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * @param gravity Which drawer the shadow should apply to
262b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     */
263b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    public void setDrawerShadow(int resId, int gravity) {
264b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        setDrawerShadow(getResources().getDrawable(resId), gravity);
265b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    }
266b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
267b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    /**
268fe130779be95af79e875bdbeac7182a424b8b9a6Adam Powell     * Set a color to use for the scrim that obscures primary content while a drawer is open.
269fe130779be95af79e875bdbeac7182a424b8b9a6Adam Powell     *
270fe130779be95af79e875bdbeac7182a424b8b9a6Adam Powell     * @param color Color to use in 0xAARRGGBB format.
271fe130779be95af79e875bdbeac7182a424b8b9a6Adam Powell     */
272fe130779be95af79e875bdbeac7182a424b8b9a6Adam Powell    public void setScrimColor(int color) {
273fe130779be95af79e875bdbeac7182a424b8b9a6Adam Powell        mScrimColor = color;
274fe130779be95af79e875bdbeac7182a424b8b9a6Adam Powell        invalidate();
275fe130779be95af79e875bdbeac7182a424b8b9a6Adam Powell    }
276fe130779be95af79e875bdbeac7182a424b8b9a6Adam Powell
277fe130779be95af79e875bdbeac7182a424b8b9a6Adam Powell    /**
2781d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Set a listener to be notified of drawer events.
2791d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     *
2801d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @param listener Listener to notify when drawer events occur
2811d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @see DrawerListener
2821d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
2831d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public void setDrawerListener(DrawerListener listener) {
2841d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        mListener = listener;
2851d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
2861d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
2871d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
2880492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * Enable or disable interaction with all drawers.
2890492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *
2900492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * <p>This allows the application to restrict the user's ability to open or close
2910492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * any drawer within this layout. DrawerLayout will still respond to calls to
2920492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * {@link #openDrawer(int)}, {@link #closeDrawer(int)} and friends if a drawer is locked.</p>
2930492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *
2940492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * <p>Locking drawers open or closed will implicitly open or close
2950492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * any drawers as appropriate.</p>
2960492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *
2970492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @param lockMode The new lock mode for the given drawer. One of {@link #LOCK_MODE_UNLOCKED},
2980492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *                 {@link #LOCK_MODE_LOCKED_CLOSED} or {@link #LOCK_MODE_LOCKED_OPEN}.
2990492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     */
3000492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    public void setDrawerLockMode(int lockMode) {
3010492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        setDrawerLockMode(lockMode, Gravity.LEFT);
3020492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        setDrawerLockMode(lockMode, Gravity.RIGHT);
3030492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    }
3040492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell
3050492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    /**
3060492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * Enable or disable interaction with the given drawer.
3070492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *
3080492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * <p>This allows the application to restrict the user's ability to open or close
3090492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * the given drawer. DrawerLayout will still respond to calls to {@link #openDrawer(int)},
3100492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * {@link #closeDrawer(int)} and friends if a drawer is locked.</p>
3110492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *
3120492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * <p>Locking a drawer open or closed will implicitly open or close
3130492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * that drawer as appropriate.</p>
3140492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *
3150492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @param lockMode The new lock mode for the given drawer. One of {@link #LOCK_MODE_UNLOCKED},
3160492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *                 {@link #LOCK_MODE_LOCKED_CLOSED} or {@link #LOCK_MODE_LOCKED_OPEN}.
3170492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @param edgeGravity Gravity.LEFT, RIGHT, START or END.
3180492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *                    Expresses which drawer to change the mode for.
3190492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *
3200492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @see #LOCK_MODE_UNLOCKED
3210492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @see #LOCK_MODE_LOCKED_CLOSED
3220492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @see #LOCK_MODE_LOCKED_OPEN
3230492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     */
3240492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    public void setDrawerLockMode(int lockMode, int edgeGravity) {
3250492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        final int absGrav = GravityCompat.getAbsoluteGravity(edgeGravity,
3260492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                ViewCompat.getLayoutDirection(this));
3270492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        if (absGrav == Gravity.LEFT) {
3280492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            mLockModeLeft = lockMode;
3290492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        } else if (absGrav == Gravity.RIGHT) {
3300492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            mLockModeRight = lockMode;
3310492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        }
3320492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        if (lockMode != LOCK_MODE_UNLOCKED) {
3330492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            // Cancel interaction in progress
3340492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            final ViewDragHelper helper = absGrav == Gravity.LEFT ? mLeftDragger : mRightDragger;
3350492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            helper.cancel();
3360492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        }
3370492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        switch (lockMode) {
3380492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            case LOCK_MODE_LOCKED_OPEN:
3390492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                final View toOpen = findDrawerWithGravity(absGrav);
3400492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                if (toOpen != null) {
3410492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                    openDrawer(toOpen);
3420492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                }
3430492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                break;
3440492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            case LOCK_MODE_LOCKED_CLOSED:
3450492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                final View toClose = findDrawerWithGravity(absGrav);
3460492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                if (toClose != null) {
3470492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                    closeDrawer(toClose);
3480492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                }
3490492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                break;
3500492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            // default: do nothing
3510492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        }
3520492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    }
3530492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell
3540492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    /**
3550492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * Enable or disable interaction with the given drawer.
3560492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *
3570492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * <p>This allows the application to restrict the user's ability to open or close
3580492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * the given drawer. DrawerLayout will still respond to calls to {@link #openDrawer(int)},
3590492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * {@link #closeDrawer(int)} and friends if a drawer is locked.</p>
3600492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *
3610492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * <p>Locking a drawer open or closed will implicitly open or close
3620492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * that drawer as appropriate.</p>
3630492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *
3640492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @param lockMode The new lock mode for the given drawer. One of {@link #LOCK_MODE_UNLOCKED},
3650492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *                 {@link #LOCK_MODE_LOCKED_CLOSED} or {@link #LOCK_MODE_LOCKED_OPEN}.
3660492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @param drawerView The drawer view to change the lock mode for
3670492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *
3680492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @see #LOCK_MODE_UNLOCKED
3690492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @see #LOCK_MODE_LOCKED_CLOSED
3700492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @see #LOCK_MODE_LOCKED_OPEN
3710492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     */
3720492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    public void setDrawerLockMode(int lockMode, View drawerView) {
3730492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        if (!isDrawerView(drawerView)) {
3740492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            throw new IllegalArgumentException("View " + drawerView + " is not a " +
3750492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                    "drawer with appropriate layout_gravity");
3760492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        }
3770492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        setDrawerLockMode(lockMode, getDrawerViewGravity(drawerView));
3780492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    }
3790492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell
3800492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    /**
3810492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * Check the lock mode of the drawer with the given gravity.
3820492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *
3830492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @param edgeGravity Gravity of the drawer to check
3840492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @return one of {@link #LOCK_MODE_UNLOCKED}, {@link #LOCK_MODE_LOCKED_CLOSED} or
3850492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *         {@link #LOCK_MODE_LOCKED_OPEN}.
3860492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     */
3870492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    public int getDrawerLockMode(int edgeGravity) {
3880492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        final int absGrav = GravityCompat.getAbsoluteGravity(edgeGravity,
3890492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                ViewCompat.getLayoutDirection(this));
3900492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        if (absGrav == Gravity.LEFT) {
3910492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            return mLockModeLeft;
3920492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        } else if (absGrav == Gravity.RIGHT) {
3930492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            return mLockModeRight;
3940492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        }
3950492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        return LOCK_MODE_UNLOCKED;
3960492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    }
3970492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell
3980492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    /**
3990492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * Check the lock mode of the given drawer view.
4000492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *
4010492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @param drawerView Drawer view to check lock mode
4020492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     * @return one of {@link #LOCK_MODE_UNLOCKED}, {@link #LOCK_MODE_LOCKED_CLOSED} or
4030492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     *         {@link #LOCK_MODE_LOCKED_OPEN}.
4040492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell     */
4050492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    public int getDrawerLockMode(View drawerView) {
4060492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        final int gravity = getDrawerViewGravity(drawerView);
4070492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        if (gravity == Gravity.LEFT) {
4080492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            return mLockModeLeft;
4090492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        } else if (gravity == Gravity.RIGHT) {
4100492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            return mLockModeRight;
4110492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        }
4120492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell
4130492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        return LOCK_MODE_UNLOCKED;
4140492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    }
4150492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell
4160492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    /**
4171d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Resolve the shared state of all drawers from the component ViewDragHelpers.
4181d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Should be called whenever a ViewDragHelper's state changes.
4191d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
420b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    void updateDrawerState(int forGravity, int activeState, View activeDrawer) {
4211d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int leftState = mLeftDragger.getViewDragState();
4221d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int rightState = mRightDragger.getViewDragState();
4231d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
4241d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int state;
4251d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (leftState == STATE_DRAGGING || rightState == STATE_DRAGGING) {
4261d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            state = STATE_DRAGGING;
4271d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        } else if (leftState == STATE_SETTLING || rightState == STATE_SETTLING) {
4281d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            state = STATE_SETTLING;
4291d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        } else {
4301d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            state = STATE_IDLE;
4311d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
4321d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
433b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        if (activeDrawer != null && activeState == STATE_IDLE) {
434b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            final LayoutParams lp = (LayoutParams) activeDrawer.getLayoutParams();
435b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            if (lp.onScreen == 0) {
436b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                dispatchOnDrawerClosed(activeDrawer);
437b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            } else if (lp.onScreen == 1) {
438b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                dispatchOnDrawerOpened(activeDrawer);
439b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            }
440b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        }
441b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
4421d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (state != mDrawerState) {
4431d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mDrawerState = state;
444b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
4451d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if (mListener != null) {
4461d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                mListener.onDrawerStateChanged(state);
4471d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
4481d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
4491d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
4501d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
4511d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    void dispatchOnDrawerClosed(View drawerView) {
4521d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final LayoutParams lp = (LayoutParams) drawerView.getLayoutParams();
4531d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (lp.knownOpen) {
4541d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            lp.knownOpen = false;
4551d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if (mListener != null) {
4561d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                mListener.onDrawerClosed(drawerView);
4571d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
4581d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
4591d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
4601d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
4611d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    void dispatchOnDrawerOpened(View drawerView) {
4621d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final LayoutParams lp = (LayoutParams) drawerView.getLayoutParams();
4631d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (!lp.knownOpen) {
4641d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            lp.knownOpen = true;
4651d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if (mListener != null) {
4661d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                mListener.onDrawerOpened(drawerView);
4671d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
4681d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
4691d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
4701d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
4711d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    void dispatchOnDrawerSlide(View drawerView, float slideOffset) {
4721d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (mListener != null) {
4731d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mListener.onDrawerSlide(drawerView, slideOffset);
4741d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
4751d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
4761d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
4771d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    void setDrawerViewOffset(View drawerView, float slideOffset) {
4781d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final LayoutParams lp = (LayoutParams) drawerView.getLayoutParams();
479b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        if (slideOffset == lp.onScreen) {
4801d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            return;
4811d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
4821d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
483b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        lp.onScreen = slideOffset;
4841d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        dispatchOnDrawerSlide(drawerView, slideOffset);
4851d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
4861d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
4871d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    float getDrawerViewOffset(View drawerView) {
488b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        return ((LayoutParams) drawerView.getLayoutParams()).onScreen;
4891d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
4901d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
4911d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    int getDrawerViewGravity(View drawerView) {
4921d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int gravity = ((LayoutParams) drawerView.getLayoutParams()).gravity;
4931d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(drawerView));
4941d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
4951d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
4961d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    boolean checkDrawerViewGravity(View drawerView, int checkFor) {
4971d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int absGrav = getDrawerViewGravity(drawerView);
4981d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return (absGrav & checkFor) == checkFor;
4991d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
5001d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
5010492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    View findOpenDrawer() {
5020492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        final int childCount = getChildCount();
5030492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        for (int i = 0; i < childCount; i++) {
5040492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            final View child = getChildAt(i);
5050492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            if (((LayoutParams) child.getLayoutParams()).knownOpen) {
5060492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                return child;
5070492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            }
5080492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        }
5090492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        return null;
5100492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    }
5110492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell
5121d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    void moveDrawerToOffset(View drawerView, float slideOffset) {
5131d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final float oldOffset = getDrawerViewOffset(drawerView);
5141d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int width = drawerView.getWidth();
5151d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int oldPos = (int) (width * oldOffset);
5161d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int newPos = (int) (width * slideOffset);
5171d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int dx = newPos - oldPos;
5181d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
5191d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        drawerView.offsetLeftAndRight(checkDrawerViewGravity(drawerView, Gravity.LEFT) ? dx : -dx);
5201d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        setDrawerViewOffset(drawerView, slideOffset);
5211d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
5221d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
5231d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    View findDrawerWithGravity(int gravity) {
5241d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int childCount = getChildCount();
5251d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        for (int i = 0; i < childCount; i++) {
5261d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final View child = getChildAt(i);
5271d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final int childGravity = getDrawerViewGravity(child);
5281d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if ((childGravity & Gravity.HORIZONTAL_GRAVITY_MASK) ==
5291d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                    (gravity & Gravity.HORIZONTAL_GRAVITY_MASK)) {
5301d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                return child;
5311d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
5321d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
5331d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return null;
5341d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
5351d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
5361d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
5371d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Simple gravity to string - only supports LEFT and RIGHT for debugging output.
5381d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     *
5391d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @param gravity Absolute gravity value
5401d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @return LEFT or RIGHT as appropriate, or a hex string
5411d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
5421d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    static String gravityToString(int gravity) {
5431d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if ((gravity & Gravity.LEFT) == Gravity.LEFT) {
5441d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            return "LEFT";
5451d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
5461d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if ((gravity & Gravity.RIGHT) == Gravity.RIGHT) {
5471d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            return "RIGHT";
5481d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
5491d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return Integer.toHexString(gravity);
5501d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
5511d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
5521d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    @Override
55357902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    protected void onDetachedFromWindow() {
55457902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        super.onDetachedFromWindow();
55557902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        mFirstLayout = true;
55657902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    }
55757902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
55857902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    @Override
55957902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    protected void onAttachedToWindow() {
56057902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        super.onAttachedToWindow();
56157902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        mFirstLayout = true;
56257902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    }
56357902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
56457902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    @Override
5651d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
5661d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
5671d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
5681d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
5691d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
5701d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
5711d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
5721d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            throw new IllegalArgumentException(
5731d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                    "DrawerLayout must be measured with MeasureSpec.EXACTLY.");
5741d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
5751d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
5761d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        setMeasuredDimension(widthSize, heightSize);
5771d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
5781d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        // Gravity value for each drawer we've seen. Only one of each permitted.
5791d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        int foundDrawers = 0;
5801d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int childCount = getChildCount();
5811d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        for (int i = 0; i < childCount; i++) {
5821d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final View child = getChildAt(i);
5831d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
5841d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if (child.getVisibility() == GONE) {
5851d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                continue;
5861d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
5871d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
58800ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
58900ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell
5901d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if (isContentView(child)) {
5911d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                // Content views get measured at exactly the layout's size.
59200ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                final int contentWidthSpec = MeasureSpec.makeMeasureSpec(
59300ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
59400ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                final int contentHeightSpec = MeasureSpec.makeMeasureSpec(
59500ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
59600ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                child.measure(contentWidthSpec, contentHeightSpec);
5971d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            } else if (isDrawerView(child)) {
5981d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                final int childGravity =
5991d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                        getDrawerViewGravity(child) & Gravity.HORIZONTAL_GRAVITY_MASK;
6001d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                if ((foundDrawers & childGravity) != 0) {
6011d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                    throw new IllegalStateException("Child drawer has absolute gravity " +
6021d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                            gravityToString(childGravity) + " but this " + TAG + " already has a " +
6031d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                            "drawer view along that edge");
6041d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                }
60500ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec,
60600ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        mMinDrawerMargin + lp.leftMargin + lp.rightMargin,
60700ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        lp.width);
60800ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec,
60900ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        lp.topMargin + lp.bottomMargin,
61000ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        lp.height);
61100ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                child.measure(drawerWidthSpec, drawerHeightSpec);
6121d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            } else {
6131d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                throw new IllegalStateException("Child " + child + " at index " + i +
6141d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                        " does not have a valid layout_gravity - must be Gravity.LEFT, " +
6151d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                        "Gravity.RIGHT or Gravity.NO_GRAVITY");
6161d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
6171d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
6181d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
6191d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
6201d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    @Override
6211d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    protected void onLayout(boolean changed, int l, int t, int r, int b) {
622b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        mInLayout = true;
6231d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int childCount = getChildCount();
6241d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        for (int i = 0; i < childCount; i++) {
6251d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final View child = getChildAt(i);
6261d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
6271d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if (child.getVisibility() == GONE) {
6281d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                continue;
6291d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
6301d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
63100ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
63200ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell
6331d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if (isContentView(child)) {
63400ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                child.layout(lp.leftMargin, lp.topMargin,
63500ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        lp.leftMargin + child.getMeasuredWidth(),
63600ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        lp.topMargin + child.getMeasuredHeight());
6371d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            } else { // Drawer, if it wasn't onMeasure would have thrown an exception.
6381d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                final int childWidth = child.getMeasuredWidth();
63900ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                final int childHeight = child.getMeasuredHeight();
6401d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                int childLeft;
6411d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
6421d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                if (checkDrawerViewGravity(child, Gravity.LEFT)) {
643b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                    childLeft = -childWidth + (int) (childWidth * lp.onScreen);
6441d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                } else { // Right; onMeasure checked for us.
645b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                    childLeft = r - l - (int) (childWidth * lp.onScreen);
6461d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                }
6471d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
64800ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
64900ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell
65000ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                switch (vgrav) {
65100ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                    default:
65200ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                    case Gravity.TOP: {
65300ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        child.layout(childLeft, lp.topMargin, childLeft + childWidth, childHeight);
65400ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        break;
65500ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                    }
65600ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell
65700ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                    case Gravity.BOTTOM: {
65800ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        final int height = b - t;
65900ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        child.layout(childLeft,
66000ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                                height - lp.bottomMargin - child.getMeasuredHeight(),
66100ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                                childLeft + childWidth,
66200ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                                height - lp.bottomMargin);
66300ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        break;
66400ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                    }
66500ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell
66600ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                    case Gravity.CENTER_VERTICAL: {
66700ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        final int height = b - t;
66800ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        int childTop = (height - childHeight) / 2;
66900ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell
67000ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        // Offset for margins. If things don't fit right because of
67100ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        // bad measurement before, oh well.
67200ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        if (childTop < lp.topMargin) {
67300ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                            childTop = lp.topMargin;
67400ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        } else if (childTop + childHeight > height - lp.bottomMargin) {
67500ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                            childTop = height - lp.bottomMargin - childHeight;
67600ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        }
67700ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        child.layout(childLeft, childTop, childLeft + childWidth,
67800ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                                childTop + childHeight);
67900ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        break;
68000ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                    }
68100ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                }
682b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
683b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                if (lp.onScreen == 0) {
684b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                    child.setVisibility(INVISIBLE);
685b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                }
6861d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
6871d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
688b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        mInLayout = false;
68957902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        mFirstLayout = false;
690b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    }
691b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
692b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    @Override
693b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    public void requestLayout() {
694b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        if (!mInLayout) {
695b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            super.requestLayout();
696b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        }
6971d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
6981d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
6991d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    @Override
7001d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public void computeScroll() {
7011d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int childCount = getChildCount();
7021d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        float scrimOpacity = 0;
7031d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        for (int i = 0; i < childCount; i++) {
704b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            final float onscreen = ((LayoutParams) getChildAt(i).getLayoutParams()).onScreen;
7051d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            scrimOpacity = Math.max(scrimOpacity, onscreen);
7061d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
7071d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        mScrimOpacity = scrimOpacity;
7081d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
7091d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        // "|" used on purpose; both need to run.
7101d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (mLeftDragger.continueSettling(true) | mRightDragger.continueSettling(true)) {
7111d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            ViewCompat.postInvalidateOnAnimation(this);
7121d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
7131d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
7141d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
715b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    private static boolean hasOpaqueBackground(View v) {
716b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        final Drawable bg = v.getBackground();
717b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        if (bg != null) {
718b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            return bg.getOpacity() == PixelFormat.OPAQUE;
719b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        }
720b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        return false;
721b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    }
722b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
7231d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    @Override
7241d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
72500ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell        final int height = getHeight();
726b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        final boolean drawingContent = isContentView(child);
727b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        int clipLeft = 0, clipRight = getWidth();
728b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
7291d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int restoreCount = canvas.save();
730b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        if (drawingContent) {
731b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            final int childCount = getChildCount();
732b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            for (int i = 0; i < childCount; i++) {
733b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                final View v = getChildAt(i);
734b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                if (v == child || v.getVisibility() != VISIBLE ||
73500ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        !hasOpaqueBackground(v) || !isDrawerView(v) ||
73600ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                        v.getHeight() < height) {
737b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                    continue;
738b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                }
739b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
740b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                if (checkDrawerViewGravity(v, Gravity.LEFT)) {
741b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                    final int vright = v.getRight();
742b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                    if (vright > clipLeft) clipLeft = vright;
743b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                } else {
744b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                    final int vleft = v.getLeft();
745b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                    if (vleft < clipRight) clipRight = vleft;
746b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                }
747b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            }
748b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            canvas.clipRect(clipLeft, 0, clipRight, getHeight());
749b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        }
7501d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final boolean result = super.drawChild(canvas, child, drawingTime);
7511d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        canvas.restoreToCount(restoreCount);
752b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
753b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        if (mScrimOpacity > 0 && drawingContent) {
7541d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final int baseAlpha = (mScrimColor & 0xff000000) >>> 24;
7551d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final int imag = (int) (baseAlpha * mScrimOpacity);
7561d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final int color = imag << 24 | (mScrimColor & 0xffffff);
7571d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mScrimPaint.setColor(color);
7581d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
759b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            canvas.drawRect(clipLeft, 0, clipRight, getHeight(), mScrimPaint);
760b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        } else if (mShadowLeft != null && checkDrawerViewGravity(child, Gravity.LEFT)) {
761b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            final int shadowWidth = mShadowLeft.getIntrinsicWidth();
762b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            final int childRight = child.getRight();
763ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell            final int drawerPeekDistance = mLeftDragger.getEdgeSize();
764b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            final float alpha =
765ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell                    Math.max(0, Math.min((float) childRight / drawerPeekDistance, 1.f));
766b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            mShadowLeft.setBounds(childRight, child.getTop(),
767b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                    childRight + shadowWidth, child.getBottom());
768b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            mShadowLeft.setAlpha((int) (0xff * alpha));
769b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            mShadowLeft.draw(canvas);
770b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        } else if (mShadowRight != null && checkDrawerViewGravity(child, Gravity.RIGHT)) {
771b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            final int shadowWidth = mShadowRight.getIntrinsicWidth();
772b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            final int childLeft = child.getLeft();
773b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            final int showing = getWidth() - childLeft;
774ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell            final int drawerPeekDistance = mRightDragger.getEdgeSize();
775b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            final float alpha =
776ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell                    Math.max(0, Math.min((float) showing / drawerPeekDistance, 1.f));
777b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            mShadowRight.setBounds(childLeft - shadowWidth, child.getTop(),
778b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                    childLeft, child.getBottom());
779b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            mShadowRight.setAlpha((int) (0xff * alpha));
780b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            mShadowRight.draw(canvas);
7811d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
7821d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return result;
7831d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
7841d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
7851d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    boolean isContentView(View child) {
7861d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return ((LayoutParams) child.getLayoutParams()).gravity == Gravity.NO_GRAVITY;
7871d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
7881d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
7891d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    boolean isDrawerView(View child) {
7901d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
7911d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int absGravity = GravityCompat.getAbsoluteGravity(gravity,
7921d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                ViewCompat.getLayoutDirection(child));
7931d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return (absGravity & (Gravity.LEFT | Gravity.RIGHT)) != 0;
7941d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
7951d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
7961d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    @Override
7971d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public boolean onInterceptTouchEvent(MotionEvent ev) {
7981d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int action = MotionEventCompat.getActionMasked(ev);
7991d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
8001d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        // "|" used deliberately here; both methods should be invoked.
8011d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final boolean interceptForDrag = mLeftDragger.shouldInterceptTouchEvent(ev) |
8021d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                mRightDragger.shouldInterceptTouchEvent(ev);
8031d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
8041d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        boolean interceptForTap = false;
8051d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
8061d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        switch (action) {
8071d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            case MotionEvent.ACTION_DOWN: {
8081d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                final float x = ev.getX();
8091d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                final float y = ev.getY();
8101d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                mInitialMotionX = x;
8111d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                mInitialMotionY = y;
8121d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                if (mScrimOpacity > 0 &&
8131d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                        isContentView(mLeftDragger.findTopChildUnder((int) x, (int) y))) {
8141d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                    interceptForTap = true;
8151d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                }
8161732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                mDisallowInterceptRequested = false;
817cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell                mChildrenCanceledTouch = false;
8181732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                break;
8191732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            }
8201732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
8211732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            case MotionEvent.ACTION_MOVE: {
8221732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                // If we cross the touch slop, don't perform the delayed peek for an edge touch.
8231732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                if (mLeftDragger.checkTouchSlop(ViewDragHelper.DIRECTION_ALL)) {
8241732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                    mLeftCallback.removeCallbacks();
8251732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                    mRightCallback.removeCallbacks();
8261732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                }
8271d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                break;
8281d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
8291d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
8301d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            case MotionEvent.ACTION_CANCEL:
8311d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            case MotionEvent.ACTION_UP: {
8321d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                closeDrawers(true);
8331732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                mDisallowInterceptRequested = false;
834cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell                mChildrenCanceledTouch = false;
8351d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
8361d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
8371732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
838cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell        return interceptForDrag || interceptForTap || hasPeekingDrawer() || mChildrenCanceledTouch;
8391d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
8401d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
8411d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    @Override
8421d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public boolean onTouchEvent(MotionEvent ev) {
8431d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        mLeftDragger.processTouchEvent(ev);
8441d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        mRightDragger.processTouchEvent(ev);
8451d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
8461d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int action = ev.getAction();
8471d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        boolean wantTouchEvents = true;
8481d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
8491d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        switch (action & MotionEventCompat.ACTION_MASK) {
8501d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            case MotionEvent.ACTION_DOWN: {
8511d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                final float x = ev.getX();
8521d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                final float y = ev.getY();
8531d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                mInitialMotionX = x;
8541d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                mInitialMotionY = y;
8551732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                mDisallowInterceptRequested = false;
856cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell                mChildrenCanceledTouch = false;
8571d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                break;
8581d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
8591d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
8601d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            case MotionEvent.ACTION_UP: {
8611d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                final float x = ev.getX();
8621d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                final float y = ev.getY();
8631d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                boolean peekingOnly = true;
8648bc268e9c40e4ae375a0d65dc1293dccc541186fAdam Powell                final View touchedView = mLeftDragger.findTopChildUnder((int) x, (int) y);
8658bc268e9c40e4ae375a0d65dc1293dccc541186fAdam Powell                if (touchedView != null && isContentView(touchedView)) {
8661d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                    final float dx = x - mInitialMotionX;
8671d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                    final float dy = y - mInitialMotionY;
8681d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                    final int slop = mLeftDragger.getTouchSlop();
8691d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                    if (dx * dx + dy * dy < slop * slop) {
8700492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                        // Taps close a dimmed open drawer but only if it isn't locked open.
8710492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                        final View openDrawer = findOpenDrawer();
8720492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                        if (openDrawer != null) {
8730492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                            peekingOnly = getDrawerLockMode(openDrawer) == LOCK_MODE_LOCKED_OPEN;
8740492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                        }
8751d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                    }
8761d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                }
8771d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                closeDrawers(peekingOnly);
8781732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                mDisallowInterceptRequested = false;
8791d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                break;
8801d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
8811d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
8821d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            case MotionEvent.ACTION_CANCEL: {
8831d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                closeDrawers(true);
8841732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                mDisallowInterceptRequested = false;
885cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell                mChildrenCanceledTouch = false;
8861d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                break;
8871d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
8881d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
8891d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
8901d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return wantTouchEvents;
8911d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
8921d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
893ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell    public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
8941732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        if (!mLeftDragger.isEdgeTouched(ViewDragHelper.EDGE_LEFT) &&
8951732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                !mRightDragger.isEdgeTouched(ViewDragHelper.EDGE_RIGHT)) {
8961732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            // If we have an edge touch we want to skip this and track it for later instead.
8971732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            super.requestDisallowInterceptTouchEvent(disallowIntercept);
8981732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        }
8991732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mDisallowInterceptRequested = disallowIntercept;
900ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell        if (disallowIntercept) {
901ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell            closeDrawers(true);
902ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell        }
903ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell    }
904ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell
9051d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
9061d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Close all currently open drawer views by animating them out of view.
9071d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
9081d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public void closeDrawers() {
9091d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        closeDrawers(false);
9101d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
9111d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
9121d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    void closeDrawers(boolean peekingOnly) {
9131d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        boolean needsInvalidate = false;
9141d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int childCount = getChildCount();
9151d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        for (int i = 0; i < childCount; i++) {
9161d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final View child = getChildAt(i);
9171d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
9181d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
9191d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if (!isDrawerView(child) || (peekingOnly && !lp.isPeeking)) {
9201d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                continue;
9211d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
9221d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
9231d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final int childWidth = child.getWidth();
9241d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
9251d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if (checkDrawerViewGravity(child, Gravity.LEFT)) {
9261d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                needsInvalidate |= mLeftDragger.smoothSlideViewTo(child,
9271d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                        -childWidth, child.getTop());
9281d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            } else {
9291d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                needsInvalidate |= mRightDragger.smoothSlideViewTo(child,
9301d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                        getWidth(), child.getTop());
9311d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
9321d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
9331d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            lp.isPeeking = false;
9341d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
9351d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
9361732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mLeftCallback.removeCallbacks();
9371732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        mRightCallback.removeCallbacks();
9381732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
9391d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (needsInvalidate) {
9401d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            invalidate();
9411d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
9421d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
9431d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
9441d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
9451d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Open the specified drawer view by animating it into view.
9461d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     *
9471d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @param drawerView Drawer view to open
9481d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
9491d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public void openDrawer(View drawerView) {
9501d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (!isDrawerView(drawerView)) {
9511d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            throw new IllegalArgumentException("View " + drawerView + " is not a sliding drawer");
9521d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
9531d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
95457902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        if (mFirstLayout) {
95557902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            final LayoutParams lp = (LayoutParams) drawerView.getLayoutParams();
95657902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            lp.onScreen = 1.f;
95757902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            lp.knownOpen = true;
9581d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        } else {
95957902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            if (checkDrawerViewGravity(drawerView, Gravity.LEFT)) {
96057902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell                mLeftDragger.smoothSlideViewTo(drawerView, 0, drawerView.getTop());
96157902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            } else {
96257902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell                mRightDragger.smoothSlideViewTo(drawerView, getWidth() - drawerView.getWidth(),
96357902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell                        drawerView.getTop());
96457902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            }
9651d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
9661d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        invalidate();
9671d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
9681d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
9691d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
9701d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Open the specified drawer by animating it out of view.
9711d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     *
9721d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @param gravity Gravity.LEFT to move the left drawer or Gravity.RIGHT for the right.
9731d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     *                GravityCompat.START or GravityCompat.END may also be used.
9741d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
9751d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public void openDrawer(int gravity) {
9761d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int absGravity = GravityCompat.getAbsoluteGravity(gravity,
9771d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                ViewCompat.getLayoutDirection(this));
9781d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final View drawerView = findDrawerWithGravity(absGravity);
9791d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
9801d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (drawerView == null) {
9811d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            throw new IllegalArgumentException("No drawer view found with absolute gravity " +
9821d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                    gravityToString(absGravity));
9831d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
9841d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        openDrawer(drawerView);
9851d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
9861d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
9871d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
9881d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Close the specified drawer view by animating it into view.
9891d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     *
9901d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @param drawerView Drawer view to close
9911d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
9921d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public void closeDrawer(View drawerView) {
9931d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (!isDrawerView(drawerView)) {
9941d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            throw new IllegalArgumentException("View " + drawerView + " is not a sliding drawer");
9951d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
9961d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
99757902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        if (mFirstLayout) {
99857902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            final LayoutParams lp = (LayoutParams) drawerView.getLayoutParams();
99957902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            lp.onScreen = 0.f;
100057902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            lp.knownOpen = false;
10011d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        } else {
100257902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            if (checkDrawerViewGravity(drawerView, Gravity.LEFT)) {
100357902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell                mLeftDragger.smoothSlideViewTo(drawerView, -drawerView.getWidth(),
100457902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell                        drawerView.getTop());
100557902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            } else {
100657902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell                mRightDragger.smoothSlideViewTo(drawerView, getWidth(), drawerView.getTop());
100757902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            }
10081d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
10091d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        invalidate();
10101d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
10111d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
10121d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    /**
10131d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * Close the specified drawer by animating it out of view.
10141d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     *
10151d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     * @param gravity Gravity.LEFT to move the left drawer or Gravity.RIGHT for the right.
10161d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     *                GravityCompat.START or GravityCompat.END may also be used.
10171d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell     */
10181d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public void closeDrawer(int gravity) {
10191d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final int absGravity = GravityCompat.getAbsoluteGravity(gravity,
10201d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                ViewCompat.getLayoutDirection(this));
10211d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        final View drawerView = findDrawerWithGravity(absGravity);
10221d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
10231d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        if (drawerView == null) {
10241d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            throw new IllegalArgumentException("No drawer view found with absolute gravity " +
10251d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                    gravityToString(absGravity));
10261d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
10271d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        closeDrawer(drawerView);
10281d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
10291d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
1030b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    /**
1031b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * Check if the given drawer view is currently in an open state.
1032b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * To be considered "open" the drawer must have settled into its fully
1033b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * visible state. To check for partial visibility use
1034b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * {@link #isDrawerVisible(android.view.View)}.
1035b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     *
1036b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * @param drawer Drawer view to check
1037b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * @return true if the given drawer view is in an open state
1038b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * @see #isDrawerVisible(android.view.View)
1039b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     */
1040b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    public boolean isDrawerOpen(View drawer) {
1041b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        if (!isDrawerView(drawer)) {
1042b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            throw new IllegalArgumentException("View " + drawer + " is not a drawer");
1043b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        }
1044b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        return ((LayoutParams) drawer.getLayoutParams()).knownOpen;
1045b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    }
1046b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
1047b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    /**
1048b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * Check if a given drawer view is currently visible on-screen. The drawer
1049b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * may be only peeking onto the screen, fully extended, or anywhere inbetween.
1050b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     *
1051b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * @param drawer Drawer view to check
1052b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * @return true if the given drawer is visible on-screen
1053b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     * @see #isDrawerOpen(android.view.View)
1054b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell     */
1055b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    public boolean isDrawerVisible(View drawer) {
1056b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        if (!isDrawerView(drawer)) {
1057b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            throw new IllegalArgumentException("View " + drawer + " is not a drawer");
1058b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        }
1059b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        return ((LayoutParams) drawer.getLayoutParams()).onScreen > 0;
1060b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell    }
1061b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell
1062ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell    private boolean hasPeekingDrawer() {
1063ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell        final int childCount = getChildCount();
1064ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell        for (int i = 0; i < childCount; i++) {
1065ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell            final LayoutParams lp = (LayoutParams) getChildAt(i).getLayoutParams();
1066ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell            if (lp.isPeeking) {
1067ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell                return true;
1068ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell            }
1069ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell        }
1070ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell        return false;
1071ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell    }
1072ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell
10731d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    @Override
10741d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
10751d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
10761d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
10771d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
10781d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    @Override
10791d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
10801d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return p instanceof LayoutParams
10811d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                ? new LayoutParams((LayoutParams) p)
108200ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                : p instanceof ViewGroup.MarginLayoutParams
108300ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell                ? new LayoutParams((MarginLayoutParams) p)
10841d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                : new LayoutParams(p);
10851d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
10861d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
10871d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    @Override
10881d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
10891d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return p instanceof LayoutParams && super.checkLayoutParams(p);
10901d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
10911d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
10921d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    @Override
10931d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
10941d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        return new LayoutParams(getContext(), attrs);
10951d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
10961d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
1097791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell    private boolean hasVisibleDrawer() {
10980492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        return findVisibleDrawer() != null;
10990492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    }
11000492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell
11010492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell    private View findVisibleDrawer() {
1102791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell        final int childCount = getChildCount();
1103791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell        for (int i = 0; i < childCount; i++) {
1104791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell            final View child = getChildAt(i);
1105791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell            if (isDrawerView(child) && isDrawerVisible(child)) {
11060492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                return child;
1107791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell            }
1108791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell        }
11090492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        return null;
1110791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell    }
1111791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell
1112cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell    void cancelChildViewTouch() {
1113cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell        // Cancel child touches
1114cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell        if (!mChildrenCanceledTouch) {
1115cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell            final long now = SystemClock.uptimeMillis();
1116cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell            final MotionEvent cancelEvent = MotionEvent.obtain(now, now,
1117cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell                    MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
1118cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell            final int childCount = getChildCount();
1119cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell            for (int i = 0; i < childCount; i++) {
1120cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell                getChildAt(i).dispatchTouchEvent(cancelEvent);
1121cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell            }
1122cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell            cancelEvent.recycle();
1123cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell            mChildrenCanceledTouch = true;
1124cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell        }
1125cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell    }
1126cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell
1127791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell    @Override
1128791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell    public boolean onKeyDown(int keyCode, KeyEvent event) {
1129791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell        if (keyCode == KeyEvent.KEYCODE_BACK && hasVisibleDrawer()) {
1130791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell            KeyEventCompat.startTracking(event);
1131791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell            return true;
1132791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell        }
1133791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell        return super.onKeyDown(keyCode, event);
1134791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell    }
1135791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell
1136791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell    @Override
1137791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell    public boolean onKeyUp(int keyCode, KeyEvent event) {
11380492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell        if (keyCode == KeyEvent.KEYCODE_BACK) {
11390492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            final View visibleDrawer = findVisibleDrawer();
11400492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            if (visibleDrawer != null && getDrawerLockMode(visibleDrawer) == LOCK_MODE_UNLOCKED) {
11410492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                closeDrawers();
11420492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            }
11430492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            return visibleDrawer != null;
1144791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell        }
1145791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell        return super.onKeyUp(keyCode, event);
1146791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell    }
1147791f31bbba40b8b51694a1b2cdc804f360786ed1Adam Powell
114857902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    @Override
114957902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    protected void onRestoreInstanceState(Parcelable state) {
115057902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        final SavedState ss = (SavedState) state;
115157902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        super.onRestoreInstanceState(ss.getSuperState());
115257902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
115357902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        if (ss.openDrawerGravity != Gravity.NO_GRAVITY) {
115457902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            final View toOpen = findDrawerWithGravity(ss.openDrawerGravity);
115557902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            if (toOpen != null) {
115657902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell                openDrawer(toOpen);
115757902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            }
115857902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        }
11598e01d24e718d603974182cd48f648369c07edec1Adam Powell
11608e01d24e718d603974182cd48f648369c07edec1Adam Powell        setDrawerLockMode(ss.lockModeLeft, Gravity.LEFT);
11618e01d24e718d603974182cd48f648369c07edec1Adam Powell        setDrawerLockMode(ss.lockModeRight, Gravity.RIGHT);
116257902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    }
116357902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
116457902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    @Override
116557902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    protected Parcelable onSaveInstanceState() {
116657902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        final Parcelable superState = super.onSaveInstanceState();
116757902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
116857902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        final SavedState ss = new SavedState(superState);
116957902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
117057902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        final int childCount = getChildCount();
117157902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        for (int i = 0; i < childCount; i++) {
117257902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            final View child = getChildAt(i);
117357902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            if (!isDrawerView(child)) {
117457902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell                continue;
117557902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            }
117657902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
117757902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
117857902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            if (lp.knownOpen) {
117957902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell                ss.openDrawerGravity = lp.gravity;
118057902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell                // Only one drawer can be open at a time.
118157902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell                break;
118257902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            }
118357902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        }
118457902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
11858e01d24e718d603974182cd48f648369c07edec1Adam Powell        ss.lockModeLeft = mLockModeLeft;
11868e01d24e718d603974182cd48f648369c07edec1Adam Powell        ss.lockModeRight = mLockModeRight;
11878e01d24e718d603974182cd48f648369c07edec1Adam Powell
118857902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        return ss;
118957902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    }
119057902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
119157902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    /**
119257902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell     * State persisted across instances
119357902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell     */
119457902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    protected static class SavedState extends BaseSavedState {
119557902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        int openDrawerGravity = Gravity.NO_GRAVITY;
11968e01d24e718d603974182cd48f648369c07edec1Adam Powell        int lockModeLeft = LOCK_MODE_UNLOCKED;
11978e01d24e718d603974182cd48f648369c07edec1Adam Powell        int lockModeRight = LOCK_MODE_UNLOCKED;
119857902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
119957902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        public SavedState(Parcel in) {
120057902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            super(in);
120157902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            openDrawerGravity = in.readInt();
120257902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        }
120357902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
120457902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        public SavedState(Parcelable superState) {
120557902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            super(superState);
120657902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        }
120757902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
120857902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        @Override
120957902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        public void writeToParcel(Parcel dest, int flags) {
121057902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            super.writeToParcel(dest, flags);
121157902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            dest.writeInt(openDrawerGravity);
121257902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        }
121357902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
121457902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        public static final Parcelable.Creator<SavedState> CREATOR =
121557902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell                new Parcelable.Creator<SavedState>() {
121657902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            @Override
121757902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            public SavedState createFromParcel(Parcel source) {
121857902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell                return new SavedState(source);
121957902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            }
122057902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
122157902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            @Override
122257902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            public SavedState[] newArray(int size) {
122357902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell                return new SavedState[size];
122457902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell            }
122557902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell        };
122657902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell    }
122757902cff6c54c245feea589b06d12dba8f1a2a24Adam Powell
12281d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    private class ViewDragCallback extends ViewDragHelper.Callback {
12291d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        private final int mGravity;
12301d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        private ViewDragHelper mDragger;
12311d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
12321732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        private final Runnable mPeekRunnable = new Runnable() {
12331732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            @Override public void run() {
12341732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell                peekDrawer();
12351732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            }
12361732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        };
12371732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
12381d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public ViewDragCallback(int gravity) {
12391d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mGravity = gravity;
12401d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
12411d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
12421d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public void setDragger(ViewDragHelper dragger) {
12431d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mDragger = dragger;
12441d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
12451d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
12461732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        public void removeCallbacks() {
12471732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            DrawerLayout.this.removeCallbacks(mPeekRunnable);
12481732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        }
12491732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
12501d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        @Override
12511d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public boolean tryCaptureView(View child, int pointerId) {
12521d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            // Only capture views where the gravity matches what we're looking for.
12531d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            // This lets us use two ViewDragHelpers, one for each side drawer.
12540492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            return isDrawerView(child) && checkDrawerViewGravity(child, mGravity) &&
12550492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                    getDrawerLockMode(child) == LOCK_MODE_UNLOCKED;
12561d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
12571d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
12581d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        @Override
12591d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public void onViewDragStateChanged(int state) {
1260b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            updateDrawerState(mGravity, state, mDragger.getCapturedView());
12611d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
12621d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
12631d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        @Override
12641d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
12651d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            float offset;
12661d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final int childWidth = changedView.getWidth();
12671d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
12681d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            // This reverses the positioning shown in onLayout.
12691d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if (checkDrawerViewGravity(changedView, Gravity.LEFT)) {
12701d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                offset = (float) (childWidth + left) / childWidth;
12711d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            } else {
12721d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                final int width = getWidth();
12731d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                offset = (float) (width - left) / childWidth;
12741d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
12751d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            setDrawerViewOffset(changedView, offset);
1276b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            changedView.setVisibility(offset == 0 ? INVISIBLE : VISIBLE);
12771d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            invalidate();
12781d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
12791d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
12801d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        @Override
12811d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public void onViewCaptured(View capturedChild, int activePointerId) {
12821d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final LayoutParams lp = (LayoutParams) capturedChild.getLayoutParams();
12831d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            lp.isPeeking = false;
12841d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
12851d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            closeOtherDrawer();
12861d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
12871d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
12881d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        private void closeOtherDrawer() {
12891d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final int otherGrav = mGravity == Gravity.LEFT ? Gravity.RIGHT : Gravity.LEFT;
12901d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final View toClose = findDrawerWithGravity(otherGrav);
12911d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if (toClose != null) {
12921d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                closeDrawer(toClose);
12931d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
12941d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
12951d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
12961d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        @Override
12971d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public void onViewReleased(View releasedChild, float xvel, float yvel) {
12981d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            // Offset is how open the drawer is, therefore left/right values
12991d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            // are reversed from one another.
13001d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final float offset = getDrawerViewOffset(releasedChild);
13011d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final int childWidth = releasedChild.getWidth();
13021d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
13031d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            int left;
13041d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if (checkDrawerViewGravity(releasedChild, Gravity.LEFT)) {
13051d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                left = xvel > 0 || xvel == 0 && offset > 0.5f ? 0 : -childWidth;
13061d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            } else {
13071d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                final int width = getWidth();
13081d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                left = xvel < 0 || xvel == 0 && offset < 0.5f ? width - childWidth : width;
13091d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
13101d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
13111d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            mDragger.settleCapturedViewAt(left, releasedChild.getTop());
13121d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            invalidate();
13131d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
13141d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
13151d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        @Override
13161d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public void onEdgeTouched(int edgeFlags, int pointerId) {
13171732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            postDelayed(mPeekRunnable, PEEK_DELAY);
13181732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        }
13191732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell
13201732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell        private void peekDrawer() {
13211d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final View toCapture;
13221d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final int childLeft;
1323ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell            final int peekDistance = mDragger.getEdgeSize();
13241732720ad57fe6d01392cd06551f1a25cff0333cAdam Powell            final boolean leftEdge = mGravity == Gravity.LEFT;
1325b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            if (leftEdge) {
13261d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                toCapture = findDrawerWithGravity(Gravity.LEFT);
1327ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell                childLeft = (toCapture != null ? -toCapture.getWidth() : 0) + peekDistance;
13281d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            } else {
13291d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                toCapture = findDrawerWithGravity(Gravity.RIGHT);
1330ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell                childLeft = getWidth() - peekDistance;
13311d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
13320492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            // Only peek if it would mean making the drawer more visible and the drawer isn't locked
1333b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell            if (toCapture != null && ((leftEdge && toCapture.getLeft() < childLeft) ||
13340492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                    (!leftEdge && toCapture.getLeft() > childLeft)) &&
13350492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell                    getDrawerLockMode(toCapture) == LOCK_MODE_UNLOCKED) {
1336b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                final LayoutParams lp = (LayoutParams) toCapture.getLayoutParams();
13371d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                mDragger.smoothSlideViewTo(toCapture, childLeft, toCapture.getTop());
1338b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell                lp.isPeeking = true;
13391d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                invalidate();
13401d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
13411d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                closeOtherDrawer();
1342cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell
1343cde2707260e8241ffb816a03cbf5d52c28004b8aAdam Powell                cancelChildViewTouch();
13441d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
13451d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
13461d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
13471d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        @Override
1348ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell        public boolean onEdgeLock(int edgeFlags) {
1349ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell            if (ALLOW_EDGE_LOCK) {
1350ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell                final View drawer = findDrawerWithGravity(mGravity);
1351ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell                if (drawer != null && !isDrawerOpen(drawer)) {
1352ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell                    closeDrawer(drawer);
1353ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell                }
1354ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell                return true;
1355acc82321ad119706485db342eaa12b225fa9b667Adam Powell            }
1356ea7b10f4d5531713506e98c8093e7aab811f21f3Adam Powell            return false;
1357acc82321ad119706485db342eaa12b225fa9b667Adam Powell        }
1358acc82321ad119706485db342eaa12b225fa9b667Adam Powell
1359acc82321ad119706485db342eaa12b225fa9b667Adam Powell        @Override
13601d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public void onEdgeDragStarted(int edgeFlags, int pointerId) {
13611d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final View toCapture;
13621d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if ((edgeFlags & ViewDragHelper.EDGE_LEFT) == ViewDragHelper.EDGE_LEFT) {
13631d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                toCapture = findDrawerWithGravity(Gravity.LEFT);
13641d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            } else {
13651d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                toCapture = findDrawerWithGravity(Gravity.RIGHT);
13661d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
13671d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
13680492ea23e32dbf509a7613d29e5791fdd44b2135Adam Powell            if (toCapture != null && getDrawerLockMode(toCapture) == LOCK_MODE_UNLOCKED) {
13691d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                mDragger.captureChildView(toCapture, pointerId);
13701d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
13711d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
13721d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
13731d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        @Override
13741d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public int getViewHorizontalDragRange(View child) {
13751d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            return child.getWidth();
13761d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
13771d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
13781d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        @Override
13791d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public int clampViewPositionHorizontal(View child, int left, int dx) {
13801d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            if (checkDrawerViewGravity(child, Gravity.LEFT)) {
13811d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                return Math.max(-child.getWidth(), Math.min(left, 0));
13821d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            } else {
13831d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                final int width = getWidth();
13841d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell                return Math.max(width - child.getWidth(), Math.min(left, width));
13851d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            }
13861d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
138700ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell
138800ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell        @Override
138900ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell        public int clampViewPositionVertical(View child, int top, int dy) {
139000ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell            return child.getTop();
139100ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell        }
13921d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
13931d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
139400ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell    public static class LayoutParams extends ViewGroup.MarginLayoutParams {
13951d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
13961d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public int gravity = Gravity.NO_GRAVITY;
1397b562d856f6ff58b7fc421a3b0cd1e0e089b994abAdam Powell        float onScreen;
13981d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        boolean isPeeking;
13991d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        boolean knownOpen;
14001d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
14011d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public LayoutParams(Context c, AttributeSet attrs) {
14021d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            super(c, attrs);
14031d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
14041d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            final TypedArray a = c.obtainStyledAttributes(attrs, LAYOUT_ATTRS);
14051d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            this.gravity = a.getInt(0, Gravity.NO_GRAVITY);
14061d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            a.recycle();
14071d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
14081d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
14091d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public LayoutParams(int width, int height) {
14101d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            super(width, height);
14111d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
14121d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
14131d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public LayoutParams(int width, int height, int gravity) {
14141d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            this(width, height);
14151d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            this.gravity = gravity;
14161d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
14171d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
14181d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public LayoutParams(LayoutParams source) {
14191d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            super(source);
14201d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            this.gravity = source.gravity;
14211d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
14221d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell
14231d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        public LayoutParams(ViewGroup.LayoutParams source) {
14241d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell            super(source);
14251d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell        }
142600ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell
142700ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell        public LayoutParams(ViewGroup.MarginLayoutParams source) {
142800ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell            super(source);
142900ca2532981fbd705f947a637ffd967a8d6f733bAdam Powell        }
14301d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell    }
14311d26501f0c8e9f3577f651938a03f6b3a1a672c7Adam Powell}
1432