<commit_before>package net.simonvt.menudrawer;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;

public class OverlayDrawer extends DraggableDrawer {

    private static final String TAG = "OverlayDrawer";

    OverlayDrawer(Activity activity, int dragMode) {
        super(activity, dragMode);
    }

    public OverlayDrawer(Context context) {
        super(context);
    }

    public OverlayDrawer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public OverlayDrawer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void initDrawer(Context context, AttributeSet attrs, int defStyle) {
        super.initDrawer(context, attrs, defStyle);
        super.addView(mContentContainer, -1, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        super.addView(mMenuContainer, -1, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

    @Override
    protected void drawOverlay(Canvas canvas) {
        final int width = getWidth();
        final int height = getHeight();
        final int offsetPixels = (int) mOffsetPixels;
        final float openRatio = Math.abs(mOffsetPixels) / mMenuSize;

        switch (mPosition) {
            case LEFT:
                mMenuOverlay.setBounds(offsetPixels, 0, width, height);
                break;

            case RIGHT:
                mMenuOverlay.setBounds(0, 0, width + offsetPixels, height);
                break;

            case TOP:
                mMenuOverlay.setBounds(0, offsetPixels, width, height);
                break;

            case BOTTOM:
                mMenuOverlay.setBounds(0, 0, width, height + offsetPixels);
                break;
        }

        mMenuOverlay.setAlpha((int) (MAX_MENU_OVERLAY_ALPHA * openRatio));
        mMenuOverlay.draw(canvas);
    }

    @Override
    public void openMenu(boolean animate) {
        int animateTo = 0;
        switch (mPosition) {
            case LEFT:
            case TOP:
                animateTo = mMenuSize;
                break;

            case RIGHT:
            case BOTTOM:
                animateTo = -mMenuSize;
                break;
        }

        animateOffsetTo(animateTo, 0, animate);
    }

    @Override
    public void closeMenu(boolean animate) {
        animateOffsetTo(0, 0, animate);
    }

    @Override
    protected void onOffsetPixelsChanged(int offsetPixels) {
        if (USE_TRANSLATIONS) {
            switch (mPosition) {
                case LEFT:
                    mMenuContainer.setTranslationX(offsetPixels - mMenuSize);
                    break;

                case TOP:
                    mMenuContainer.setTranslationY(offsetPixels - mMenuSize);
                    break;

                case RIGHT:
                    mMenuContainer.setTranslationX(offsetPixels + mMenuSize);
                    break;

                case BOTTOM:
                    mMenuContainer.setTranslationY(offsetPixels + mMenuSize);
                    break;
            }
        } else {
            switch (mPosition) {
                case TOP:
                case BOTTOM:
                    mMenuContainer.offsetTopAndBottom(offsetPixels - mMenuContainer.getTop());
                    break;

                default:
                    mMenuContainer.offsetLeftAndRight(offsetPixels - mMenuContainer.getLeft());
                    break;
            }
        }

        invalidate();
    }

    @Override
    protected void initPeekScroller() {
        switch (mPosition) {
            case RIGHT:
            case BOTTOM: {
                final int dx = -mMenuSize / 3;
                mPeekScroller.startScroll(0, 0, dx, 0, PEEK_DURATION);
                break;
            }

            default: {
                final int dx = mMenuSize / 3;
                mPeekScroller.startScroll(0, 0, dx, 0, PEEK_DURATION);
                break;
            }
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        onOffsetPixelsChanged((int) mOffsetPixels);
    }

    @Override
    protected GradientDrawable.Orientation getDropShadowOrientation() {
        switch (mPosition) {
            case TOP:
                return GradientDrawable.Orientation.TOP_BOTTOM;

            case RIGHT:
                return GradientDrawable.Orientation.RIGHT_LEFT;

            case BOTTOM:
                return GradientDrawable.Orientation.BOTTOM_TOP;

            default:
                return GradientDrawable.Orientation.LEFT_RIGHT;
        }
    }

    @Override
    protected void updateDropShadowRect() {
        final float openRatio = Math.abs(mOffsetPixels) / mMenuSize;
        final int dropShadowSize = (int) (mDropShadowSize * openRatio);

        switch (mPosition) {
            case LEFT:
                mDropShadowRect.top = 0;
                mDropShadowRect.bottom = getHeight();
                mDropShadowRect.left = ViewHelper.getRight(mMenuContainer);
                mDropShadowRect.right = mDropShadowRect.left + dropShadowSize;
                break;

            case TOP:
                mDropShadowRect.left = 0;
                mDropShadowRect.right = getWidth();
                mDropShadowRect.top = ViewHelper.getBottom(mMenuContainer);
                mDropShadowRect.bottom = mDropShadowRect.top + dropShadowSize;
                break;

            case RIGHT:
                mDropShadowRect.top = 0;
                mDropShadowRect.bottom = getHeight();
                mDropShadowRect.right = ViewHelper.getLeft(mMenuContainer);
                mDropShadowRect.left = mDropShadowRect.right - dropShadowSize;
                break;

            case BOTTOM:
                mDropShadowRect.left = 0;
                mDropShadowRect.right = getWidth();
                mDropShadowRect.bottom = ViewHelper.getTop(mMenuContainer);
                mDropShadowRect.top = mDropShadowRect.bottom - dropShadowSize;
                break;
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width = r - l;
        final int height = b - t;

        mContentContainer.layout(0, 0, width, height);

        switch (mPosition) {
            case LEFT:
                mMenuContainer.layout(0, 0, mMenuSize, height);
                break;

            case RIGHT:
                mMenuContainer.layout(width - mMenuSize, 0, width, height);
                break;

            case TOP:
                mMenuContainer.layout(0, 0, width, mMenuSize);
                break;

            case BOTTOM:
                mMenuContainer.layout(0, height - mMenuSize, width, height);
                break;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
            throw new IllegalStateException("Must measure with an exact size");
        }

        final int width = MeasureSpec.getSize(widthMeasureSpec);
        final int height = MeasureSpec.getSize(heightMeasureSpec);

        if (mOffsetPixels == -1) openMenu(false);

        int menuWidthMeasureSpec;
        int menuHeightMeasureSpec;
        switch (mPosition) {
            case TOP:
            case BOTTOM:
                menuWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, 0, width);
                menuHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, 0, mMenuSize);
                break;<commit_msg>Fix overlay drawer on obsolete platforms.<commit_after>package net.simonvt.menudrawer;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;

public class OverlayDrawer extends DraggableDrawer {

    private static final String TAG = "OverlayDrawer";

    OverlayDrawer(Activity activity, int dragMode) {
        super(activity, dragMode);
    }

    public OverlayDrawer(Context context) {
        super(context);
    }

    public OverlayDrawer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public OverlayDrawer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void initDrawer(Context context, AttributeSet attrs, int defStyle) {
        super.initDrawer(context, attrs, defStyle);
        super.addView(mContentContainer, -1, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        super.addView(mMenuContainer, -1, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

    @Override
    protected void drawOverlay(Canvas canvas) {
        final int width = getWidth();
        final int height = getHeight();
        final int offsetPixels = (int) mOffsetPixels;
        final float openRatio = Math.abs(mOffsetPixels) / mMenuSize;

        switch (mPosition) {
            case LEFT:
                mMenuOverlay.setBounds(offsetPixels, 0, width, height);
                break;

            case RIGHT:
                mMenuOverlay.setBounds(0, 0, width + offsetPixels, height);
                break;

            case TOP:
                mMenuOverlay.setBounds(0, offsetPixels, width, height);
                break;

            case BOTTOM:
                mMenuOverlay.setBounds(0, 0, width, height + offsetPixels);
                break;
        }

        mMenuOverlay.setAlpha((int) (MAX_MENU_OVERLAY_ALPHA * openRatio));
        mMenuOverlay.draw(canvas);
    }

    @Override
    public void openMenu(boolean animate) {
        int animateTo = 0;
        switch (mPosition) {
            case LEFT:
            case TOP:
                animateTo = mMenuSize;
                break;

            case RIGHT:
            case BOTTOM:
                animateTo = -mMenuSize;
                break;
        }

        animateOffsetTo(animateTo, 0, animate);
    }

    @Override
    public void closeMenu(boolean animate) {
        animateOffsetTo(0, 0, animate);
    }

    @Override
    protected void onOffsetPixelsChanged(int offsetPixels) {
        if (USE_TRANSLATIONS) {
            switch (mPosition) {
                case LEFT:
                    mMenuContainer.setTranslationX(offsetPixels - mMenuSize);
                    break;

                case TOP:
                    mMenuContainer.setTranslationY(offsetPixels - mMenuSize);
                    break;

                case RIGHT:
                    mMenuContainer.setTranslationX(offsetPixels + mMenuSize);
                    break;

                case BOTTOM:
                    mMenuContainer.setTranslationY(offsetPixels + mMenuSize);
                    break;
            }
        } else {
            switch (mPosition) {
                case TOP:
                    mMenuContainer.offsetTopAndBottom(offsetPixels - mMenuContainer.getBottom());
                    break;

                case BOTTOM:
                    mMenuContainer.offsetTopAndBottom(offsetPixels - (mMenuContainer.getTop() - getHeight()));
                    break;

                case LEFT:
                    mMenuContainer.offsetLeftAndRight(offsetPixels - mMenuContainer.getRight());
                    break;

                case RIGHT:
                    mMenuContainer.offsetLeftAndRight(offsetPixels - (mMenuContainer.getLeft() - getWidth()));
                    break;
            }
        }

        invalidate();
    }

    @Override
    protected void initPeekScroller() {
        switch (mPosition) {
            case RIGHT:
            case BOTTOM: {
                final int dx = -mMenuSize / 3;
                mPeekScroller.startScroll(0, 0, dx, 0, PEEK_DURATION);
                break;
            }

            default: {
                final int dx = mMenuSize / 3;
                mPeekScroller.startScroll(0, 0, dx, 0, PEEK_DURATION);
                break;
            }
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        onOffsetPixelsChanged((int) mOffsetPixels);
    }

    @Override
    protected GradientDrawable.Orientation getDropShadowOrientation() {
        switch (mPosition) {
            case TOP:
                return GradientDrawable.Orientation.TOP_BOTTOM;

            case RIGHT:
                return GradientDrawable.Orientation.RIGHT_LEFT;

            case BOTTOM:
                return GradientDrawable.Orientation.BOTTOM_TOP;

            default:
                return GradientDrawable.Orientation.LEFT_RIGHT;
        }
    }

    @Override
    protected void updateDropShadowRect() {
        final float openRatio = Math.abs(mOffsetPixels) / mMenuSize;
        final int dropShadowSize = (int) (mDropShadowSize * openRatio);

        switch (mPosition) {
            case LEFT:
                mDropShadowRect.top = 0;
                mDropShadowRect.bottom = getHeight();
                mDropShadowRect.left = ViewHelper.getRight(mMenuContainer);
                mDropShadowRect.right = mDropShadowRect.left + dropShadowSize;
                break;

            case TOP:
                mDropShadowRect.left = 0;
                mDropShadowRect.right = getWidth();
                mDropShadowRect.top = ViewHelper.getBottom(mMenuContainer);
                mDropShadowRect.bottom = mDropShadowRect.top + dropShadowSize;
                break;

            case RIGHT:
                mDropShadowRect.top = 0;
                mDropShadowRect.bottom = getHeight();
                mDropShadowRect.right = ViewHelper.getLeft(mMenuContainer);
                mDropShadowRect.left = mDropShadowRect.right - dropShadowSize;
                break;

            case BOTTOM:
                mDropShadowRect.left = 0;
                mDropShadowRect.right = getWidth();
                mDropShadowRect.bottom = ViewHelper.getTop(mMenuContainer);
                mDropShadowRect.top = mDropShadowRect.bottom - dropShadowSize;
                break;
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width = r - l;
        final int height = b - t;

        mContentContainer.layout(0, 0, width, height);

        if (USE_TRANSLATIONS) {
            switch (mPosition) {
                case LEFT:
                    mMenuContainer.layout(0, 0, mMenuSize, height);
                    break;

                case RIGHT:
                    mMenuContainer.layout(width - mMenuSize, 0, width, height);
                    break;

                case TOP:
                    mMenuContainer.layout(0, 0, width, mMenuSize);
                    break;

                case BOTTOM:
                    mMenuContainer.layout(0, height - mMenuSize, width, height);
                    break;
            }

        } else {
            final int offsetPixels = (int) mOffsetPixels;
            final int menuSize = mMenuSize;

            switch (mPosition) {
                case LEFT:
                    mMenuContainer.layout(-menuSize + offsetPixels, 0, offsetPixels, height);
                    break;

                case RIGHT:
                    mMenuContainer.layout(width + offsetPixels, 0, width + menuSize + offsetPixels, height);
                    break;

                case TOP:
                    mMenuContainer.layout(0, -menuSize + offsetPixels, width, offsetPixels);
                    break;

                case BOTTOM:
                    mMenuContainer.layout(0, height + offsetPixels, width, height + menuSize + offsetPixels);
                    break;
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
            throw new IllegalStateException("Must measure with an exact size");
        }

        final int width = MeasureSpec.getSize(widthMeasureSpec);
        final int height = MeasureSpec.getSize(heightMeasureSpec);

        if (mOffsetPixels == -1) openMenu(false);

        int menuWidthMeasureSpec;
        int menuHeightMeasureSpec;
        switch (mPosition) {
            case TOP:
            case BOTTOM:
                menuWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, 0, width);
                menuHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, 0, mMenuSize);
                break;
{'fraction_non_alphanumeric': 0.07724923041180229, 'fraction_numerical': 0.004298077481558925, 'mean_word_length': 1.7265241488519398, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2976246', 'n_tokens_mistral': 4768, 'n_tokens_neox': 4370, 'n_words': 1001}