Java Code Examples for org.telegram.messenger.AndroidUtilities#getPixelsInCM()

The following examples show how to use org.telegram.messenger.AndroidUtilities#getPixelsInCM() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BottomSheet.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void checkDismiss(float velX, float velY)
{
    float translationY = containerView.getTranslationY();
    boolean backAnimation = translationY < AndroidUtilities.getPixelsInCM(0.8f, false) && (velY < 3500 || Math.abs(velY) < Math.abs(velX)) || velY < 0 && Math.abs(velY) >= 3500;
    if (!backAnimation)
    {
        boolean allowOld = allowCustomAnimation;
        allowCustomAnimation = false;
        useFastDismiss = true;
        dismiss();
        allowCustomAnimation = allowOld;
    }
    else
    {
        currentAnimation = new AnimatorSet();
        currentAnimation.playTogether(ObjectAnimator.ofFloat(containerView, "translationY", 0));
        currentAnimation.setDuration((int) (150 * (translationY / AndroidUtilities.getPixelsInCM(0.8f, false))));
        currentAnimation.setInterpolator(new DecelerateInterpolator());
        currentAnimation.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationEnd(Animator animation)
            {
                if (currentAnimation != null && currentAnimation.equals(animation))
                {
                    currentAnimation = null;
                }
            }
        });
        currentAnimation.start();
    }
}
 
Example 2
Source File: SeekBarWaveform.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public boolean onTouch(int action, float x, float y) {
    if (action == MotionEvent.ACTION_DOWN) {
        if (0 <= x && x <= width && y >= 0 && y <= height) {
            startX = x;
            pressed = true;
            thumbDX = (int) (x - thumbX);
            startDraging = false;
            return true;
        }
    } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        if (pressed) {
            if (action == MotionEvent.ACTION_UP && delegate != null) {
                delegate.onSeekBarDrag((float) thumbX / (float) width);
            }
            pressed = false;
            return true;
        }
    } else if (action == MotionEvent.ACTION_MOVE) {
        if (pressed) {
            if (startDraging) {
                thumbX = (int) (x - thumbDX);
                if (thumbX < 0) {
                    thumbX = 0;
                } else if (thumbX > width) {
                    thumbX = width;
                }
            }
            if (startX != -1 && Math.abs(x - startX) > AndroidUtilities.getPixelsInCM(0.2f, true)) {
                if (parentView != null && parentView.getParent() != null) {
                    parentView.getParent().requestDisallowInterceptTouchEvent(true);
                }
                startDraging = true;
                startX = -1;
            }
            return true;
        }
    }
    return false;
}
 
Example 3
Source File: BottomSheet.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void checkDismiss(float velX, float velY)
{
    float translationY = containerView.getTranslationY();
    boolean backAnimation = translationY < AndroidUtilities.getPixelsInCM(0.8f, false) && (velY < 3500 || Math.abs(velY) < Math.abs(velX)) || velY < 0 && Math.abs(velY) >= 3500;
    if (!backAnimation)
    {
        boolean allowOld = allowCustomAnimation;
        allowCustomAnimation = false;
        useFastDismiss = true;
        dismiss();
        allowCustomAnimation = allowOld;
    }
    else
    {
        currentAnimation = new AnimatorSet();
        currentAnimation.playTogether(ObjectAnimator.ofFloat(containerView, "translationY", 0));
        currentAnimation.setDuration((int) (150 * (translationY / AndroidUtilities.getPixelsInCM(0.8f, false))));
        currentAnimation.setInterpolator(new DecelerateInterpolator());
        currentAnimation.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationEnd(Animator animation)
            {
                if (currentAnimation != null && currentAnimation.equals(animation))
                {
                    currentAnimation = null;
                }
            }
        });
        currentAnimation.start();
    }
}
 
Example 4
Source File: SeekBarWaveform.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public boolean onTouch(int action, float x, float y) {
    if (action == MotionEvent.ACTION_DOWN) {
        if (0 <= x && x <= width && y >= 0 && y <= height) {
            startX = x;
            pressed = true;
            thumbDX = (int) (x - thumbX);
            startDraging = false;
            return true;
        }
    } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        if (pressed) {
            if (action == MotionEvent.ACTION_UP && delegate != null) {
                delegate.onSeekBarDrag((float) thumbX / (float) width);
            }
            pressed = false;
            return true;
        }
    } else if (action == MotionEvent.ACTION_MOVE) {
        if (pressed) {
            if (startDraging) {
                thumbX = (int) (x - thumbDX);
                if (thumbX < 0) {
                    thumbX = 0;
                } else if (thumbX > width) {
                    thumbX = width;
                }
            }
            if (startX != -1 && Math.abs(x - startX) > AndroidUtilities.getPixelsInCM(0.2f, true)) {
                if (parentView != null && parentView.getParent() != null) {
                    parentView.getParent().requestDisallowInterceptTouchEvent(true);
                }
                startDraging = true;
                startX = -1;
            }
            return true;
        }
    }
    return false;
}
 
Example 5
Source File: BottomSheet.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private void checkDismiss(float velX, float velY) {
    float translationY = containerView.getTranslationY();
    boolean backAnimation = translationY < AndroidUtilities.getPixelsInCM(0.8f, false) && (velY < 3500 || Math.abs(velY) < Math.abs(velX)) || velY < 0 && Math.abs(velY) >= 3500;
    if (!backAnimation) {
        boolean allowOld = allowCustomAnimation;
        allowCustomAnimation = false;
        useFastDismiss = true;
        dismiss();
        allowCustomAnimation = allowOld;
    } else {
        currentAnimation = new AnimatorSet();
        currentAnimation.playTogether(ObjectAnimator.ofFloat(containerView, "translationY", 0));
        currentAnimation.setDuration((int) (150 * (translationY / AndroidUtilities.getPixelsInCM(0.8f, false))));
        currentAnimation.setInterpolator(CubicBezierInterpolator.EASE_OUT);
        currentAnimation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (currentAnimation != null && currentAnimation.equals(animation)) {
                    currentAnimation = null;
                }
                NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.startAllHeavyOperations, 512);
            }
        });
        NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.stopAllHeavyOperations, 512);
        currentAnimation.start();
    }
}
 
Example 6
Source File: SeekBarWaveform.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public boolean onTouch(int action, float x, float y) {
    if (action == MotionEvent.ACTION_DOWN) {
        if (0 <= x && x <= width && y >= 0 && y <= height) {
            startX = x;
            pressed = true;
            thumbDX = (int) (x - thumbX);
            startDraging = false;
            return true;
        }
    } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        if (pressed) {
            if (action == MotionEvent.ACTION_UP && delegate != null) {
                delegate.onSeekBarDrag((float) thumbX / (float) width);
            }
            pressed = false;
            return true;
        }
    } else if (action == MotionEvent.ACTION_MOVE) {
        if (pressed) {
            if (startDraging) {
                thumbX = (int) (x - thumbDX);
                if (thumbX < 0) {
                    thumbX = 0;
                } else if (thumbX > width) {
                    thumbX = width;
                }
            }
            if (startX != -1 && Math.abs(x - startX) > AndroidUtilities.getPixelsInCM(0.2f, true)) {
                if (parentView != null && parentView.getParent() != null) {
                    parentView.getParent().requestDisallowInterceptTouchEvent(true);
                }
                startDraging = true;
                startX = -1;
            }
            return true;
        }
    }
    return false;
}
 
Example 7
Source File: BottomSheet.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private void checkDismiss(float velX, float velY) {
    float translationY = containerView.getTranslationY();
    boolean backAnimation = translationY < AndroidUtilities.getPixelsInCM(0.8f, false) && (velY < 3500 || Math.abs(velY) < Math.abs(velX)) || velY < 0 && Math.abs(velY) >= 3500;
    if (!backAnimation) {
        boolean allowOld = allowCustomAnimation;
        allowCustomAnimation = false;
        useFastDismiss = true;
        dismiss();
        allowCustomAnimation = allowOld;
    } else {
        currentAnimation = new AnimatorSet();
        currentAnimation.playTogether(ObjectAnimator.ofFloat(containerView, "translationY", 0));
        currentAnimation.setDuration((int) (150 * (translationY / AndroidUtilities.getPixelsInCM(0.8f, false))));
        currentAnimation.setInterpolator(CubicBezierInterpolator.EASE_OUT);
        currentAnimation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (currentAnimation != null && currentAnimation.equals(animation)) {
                    currentAnimation = null;
                }
                NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.startAllHeavyOperations, 512);
            }
        });
        NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.stopAllHeavyOperations, 512);
        currentAnimation.start();
    }
}
 
Example 8
Source File: SeekBarWaveform.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public boolean onTouch(int action, float x, float y) {
    if (action == MotionEvent.ACTION_DOWN) {
        if (0 <= x && x <= width && y >= 0 && y <= height) {
            startX = x;
            pressed = true;
            thumbDX = (int) (x - thumbX);
            startDraging = false;
            return true;
        }
    } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        if (pressed) {
            if (action == MotionEvent.ACTION_UP && delegate != null) {
                delegate.onSeekBarDrag((float) thumbX / (float) width);
            }
            pressed = false;
            return true;
        }
    } else if (action == MotionEvent.ACTION_MOVE) {
        if (pressed) {
            if (startDraging) {
                thumbX = (int) (x - thumbDX);
                if (thumbX < 0) {
                    thumbX = 0;
                } else if (thumbX > width) {
                    thumbX = width;
                }
            }
            if (startX != -1 && Math.abs(x - startX) > AndroidUtilities.getPixelsInCM(0.2f, true)) {
                if (parentView != null && parentView.getParent() != null) {
                    parentView.getParent().requestDisallowInterceptTouchEvent(true);
                }
                startDraging = true;
                startX = -1;
            }
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: DrawerLayoutContainer.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public boolean onTouchEvent(MotionEvent ev)
{
    if (!parentActionBarLayout.checkTransitionAnimation())
    {
        if (drawerOpened && ev != null && ev.getX() > drawerPosition && !startedTracking)
        {
            if (ev.getAction() == MotionEvent.ACTION_UP)
            {
                closeDrawer(false);
            }
            return true;
        }

        if (allowOpenDrawer && parentActionBarLayout.fragmentsStack.size() == 1)
        {
            if (ev != null && (ev.getAction() == MotionEvent.ACTION_DOWN || ev.getAction() == MotionEvent.ACTION_MOVE) && !startedTracking && !maybeStartTracking)
            {
                parentActionBarLayout.getHitRect(rect);
                startedTrackingX = (int) ev.getX();
                startedTrackingY = (int) ev.getY();
                if (rect.contains(startedTrackingX, startedTrackingY))
                {
                    startedTrackingPointerId = ev.getPointerId(0);
                    maybeStartTracking = true;
                    cancelCurrentAnimation();
                    if (velocityTracker != null)
                    {
                        velocityTracker.clear();
                    }
                }
            }
            else if (ev != null && ev.getAction() == MotionEvent.ACTION_MOVE && ev.getPointerId(0) == startedTrackingPointerId)
            {
                if (velocityTracker == null)
                {
                    velocityTracker = VelocityTracker.obtain();
                }
                float dx = (int) (ev.getX() - startedTrackingX);
                float dy = Math.abs((int) ev.getY() - startedTrackingY);
                velocityTracker.addMovement(ev);
                if (maybeStartTracking && !startedTracking && (dx > 0 && dx / 3.0f > Math.abs(dy) && Math.abs(dx) >= AndroidUtilities.getPixelsInCM(0.2f, true) || dx < 0 && Math.abs(dx) >= Math.abs(dy) && Math.abs(dx) >= AndroidUtilities.getPixelsInCM(0.4f, true)))
                {
                    prepareForDrawerOpen(ev);
                    startedTrackingX = (int) ev.getX();
                    requestDisallowInterceptTouchEvent(true);
                }
                else if (startedTracking)
                {
                    if (!beginTrackingSent)
                    {
                        if (((Activity) getContext()).getCurrentFocus() != null)
                        {
                            AndroidUtilities.hideKeyboard(((Activity) getContext()).getCurrentFocus());
                        }
                        beginTrackingSent = true;
                    }
                    moveDrawerByX(dx);
                    startedTrackingX = (int) ev.getX();
                }
            }
            else if (ev == null || ev != null && ev.getPointerId(0) == startedTrackingPointerId && (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_POINTER_UP))
            {
                if (velocityTracker == null)
                {
                    velocityTracker = VelocityTracker.obtain();
                }
                velocityTracker.computeCurrentVelocity(1000);
                if (startedTracking || drawerPosition != 0 && drawerPosition != drawerLayout.getMeasuredWidth())
                {
                    float velX = velocityTracker.getXVelocity();
                    float velY = velocityTracker.getYVelocity();
                    boolean backAnimation = drawerPosition < drawerLayout.getMeasuredWidth() / 2.0f && (velX < 3500 || Math.abs(velX) < Math.abs(velY)) || velX < 0 && Math.abs(velX) >= 3500;
                    if (!backAnimation)
                    {
                        openDrawer(!drawerOpened && Math.abs(velX) >= 3500);
                    }
                    else
                    {
                        closeDrawer(drawerOpened && Math.abs(velX) >= 3500);
                    }
                }
                startedTracking = false;
                maybeStartTracking = false;
                if (velocityTracker != null)
                {
                    velocityTracker.recycle();
                    velocityTracker = null;
                }
            }
        }
        return startedTracking;
    }
    return false;
}
 
Example 10
Source File: EmojiView.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (touched) {
        if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
            if (pickerViewPopup != null && pickerViewPopup.isShowing()) {
                pickerViewPopup.dismiss();

                String color = null;
                switch (pickerView.getSelection()) {
                    case 1:
                        color = "\uD83C\uDFFB";
                        break;
                    case 2:
                        color = "\uD83C\uDFFC";
                        break;
                    case 3:
                        color = "\uD83C\uDFFD";
                        break;
                    case 4:
                        color = "\uD83C\uDFFE";
                        break;
                    case 5:
                        color = "\uD83C\uDFFF";
                        break;
                }
                String code = (String) getTag();
                if (pager.getCurrentItem() != 0) {
                    if (color != null) {
                        Emoji.emojiColor.put(code, color);
                        code = addColorToCode(code, color);
                    } else {
                        Emoji.emojiColor.remove(code);
                    }
                    setImageDrawable(Emoji.getEmojiBigDrawable(code));
                    sendEmoji(null);
                    Emoji.saveEmojiColors();
                } else {
                    if (color != null) {
                        sendEmoji(addColorToCode(code, color));
                    } else {
                        sendEmoji(code);
                    }
                }
            }
            touched = false;
            touchedX = -10000;
            touchedY = -10000;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            boolean ignore = false;
            if (touchedX != -10000) {
                if (Math.abs(touchedX - event.getX()) > AndroidUtilities.getPixelsInCM(0.2f, true) || Math.abs(touchedY - event.getY()) > AndroidUtilities.getPixelsInCM(0.2f, false)) {
                    touchedX = -10000;
                    touchedY = -10000;
                } else {
                    ignore = true;
                }
            }
            if (!ignore) {
                getLocationOnScreen(location);
                float x = location[0] + event.getX();
                pickerView.getLocationOnScreen(location);
                x -= location[0] + AndroidUtilities.dp(3);
                int position = (int) (x / (emojiSize + AndroidUtilities.dp(4)));
                if (position < 0) {
                    position = 0;
                } else if (position > 5) {
                    position = 5;
                }
                pickerView.setSelection(position);
            }
        }
    }
    lastX = event.getX();
    lastY = event.getY();
    return super.onTouchEvent(event);
}
 
Example 11
Source File: DrawerLayoutContainer.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public boolean onTouchEvent(MotionEvent ev)
{
    if (!parentActionBarLayout.checkTransitionAnimation())
    {
        if (drawerOpened && ev != null && ev.getX() > drawerPosition && !startedTracking)
        {
            if (ev.getAction() == MotionEvent.ACTION_UP)
            {
                closeDrawer(false);
            }
            return true;
        }

        if (allowOpenDrawer && parentActionBarLayout.fragmentsStack.size() == 1)
        {
            if (ev != null && (ev.getAction() == MotionEvent.ACTION_DOWN || ev.getAction() == MotionEvent.ACTION_MOVE) && !startedTracking && !maybeStartTracking)
            {
                parentActionBarLayout.getHitRect(rect);
                startedTrackingX = (int) ev.getX();
                startedTrackingY = (int) ev.getY();
                if (rect.contains(startedTrackingX, startedTrackingY))
                {
                    startedTrackingPointerId = ev.getPointerId(0);
                    maybeStartTracking = true;
                    cancelCurrentAnimation();
                    if (velocityTracker != null)
                    {
                        velocityTracker.clear();
                    }
                }
            }
            else if (ev != null && ev.getAction() == MotionEvent.ACTION_MOVE && ev.getPointerId(0) == startedTrackingPointerId)
            {
                if (velocityTracker == null)
                {
                    velocityTracker = VelocityTracker.obtain();
                }
                float dx = (int) (ev.getX() - startedTrackingX);
                float dy = Math.abs((int) ev.getY() - startedTrackingY);
                velocityTracker.addMovement(ev);
                if (maybeStartTracking && !startedTracking && (dx > 0 && dx / 3.0f > Math.abs(dy) && Math.abs(dx) >= AndroidUtilities.getPixelsInCM(0.2f, true) || dx < 0 && Math.abs(dx) >= Math.abs(dy) && Math.abs(dx) >= AndroidUtilities.getPixelsInCM(0.4f, true)))
                {
                    prepareForDrawerOpen(ev);
                    startedTrackingX = (int) ev.getX();
                    requestDisallowInterceptTouchEvent(true);
                }
                else if (startedTracking)
                {
                    if (!beginTrackingSent)
                    {
                        if (((Activity) getContext()).getCurrentFocus() != null)
                        {
                            AndroidUtilities.hideKeyboard(((Activity) getContext()).getCurrentFocus());
                        }
                        beginTrackingSent = true;
                    }
                    moveDrawerByX(dx);
                    startedTrackingX = (int) ev.getX();
                }
            }
            else if (ev == null || ev != null && ev.getPointerId(0) == startedTrackingPointerId && (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_POINTER_UP))
            {
                if (velocityTracker == null)
                {
                    velocityTracker = VelocityTracker.obtain();
                }
                velocityTracker.computeCurrentVelocity(1000);
                if (startedTracking || drawerPosition != 0 && drawerPosition != drawerLayout.getMeasuredWidth())
                {
                    float velX = velocityTracker.getXVelocity();
                    float velY = velocityTracker.getYVelocity();
                    boolean backAnimation = drawerPosition < drawerLayout.getMeasuredWidth() / 2.0f && (velX < 3500 || Math.abs(velX) < Math.abs(velY)) || velX < 0 && Math.abs(velX) >= 3500;
                    if (!backAnimation)
                    {
                        openDrawer(!drawerOpened && Math.abs(velX) >= 3500);
                    }
                    else
                    {
                        closeDrawer(drawerOpened && Math.abs(velX) >= 3500);
                    }
                }
                startedTracking = false;
                maybeStartTracking = false;
                if (velocityTracker != null)
                {
                    velocityTracker.recycle();
                    velocityTracker = null;
                }
            }
        }
        return startedTracking;
    }
    return false;
}
 
Example 12
Source File: EmojiView.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (touched) {
        if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
            if (pickerViewPopup != null && pickerViewPopup.isShowing()) {
                pickerViewPopup.dismiss();

                String color = null;
                switch (pickerView.getSelection()) {
                    case 1:
                        color = "\uD83C\uDFFB";
                        break;
                    case 2:
                        color = "\uD83C\uDFFC";
                        break;
                    case 3:
                        color = "\uD83C\uDFFD";
                        break;
                    case 4:
                        color = "\uD83C\uDFFE";
                        break;
                    case 5:
                        color = "\uD83C\uDFFF";
                        break;
                }
                String code = (String) getTag();
                if (pager.getCurrentItem() != 0) {
                    if (color != null) {
                        Emoji.emojiColor.put(code, color);
                        code = addColorToCode(code, color);
                    } else {
                        Emoji.emojiColor.remove(code);
                    }
                    setImageDrawable(Emoji.getEmojiBigDrawable(code));
                    sendEmoji(null);
                    Emoji.saveEmojiColors();
                } else {
                    if (color != null) {
                        sendEmoji(addColorToCode(code, color));
                    } else {
                        sendEmoji(code);
                    }
                }
            }
            touched = false;
            touchedX = -10000;
            touchedY = -10000;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            boolean ignore = false;
            if (touchedX != -10000) {
                if (Math.abs(touchedX - event.getX()) > AndroidUtilities.getPixelsInCM(0.2f, true) || Math.abs(touchedY - event.getY()) > AndroidUtilities.getPixelsInCM(0.2f, false)) {
                    touchedX = -10000;
                    touchedY = -10000;
                } else {
                    ignore = true;
                }
            }
            if (!ignore) {
                getLocationOnScreen(location);
                float x = location[0] + event.getX();
                pickerView.getLocationOnScreen(location);
                x -= location[0] + AndroidUtilities.dp(3);
                int position = (int) (x / (emojiSize + AndroidUtilities.dp(4)));
                if (position < 0) {
                    position = 0;
                } else if (position > 5) {
                    position = 5;
                }
                pickerView.setSelection(position);
            }
        }
    }
    lastX = event.getX();
    lastY = event.getY();
    return super.onTouchEvent(event);
}
 
Example 13
Source File: DrawerLayoutContainer.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
public boolean onTouchEvent(MotionEvent ev) {
    if (!parentActionBarLayout.checkTransitionAnimation()) {
        if (drawerOpened && ev != null && ev.getX() > drawerPosition && !startedTracking) {
            if (ev.getAction() == MotionEvent.ACTION_UP) {
                closeDrawer(false);
            }
            return true;
        }

        if ((allowOpenDrawerBySwipe || drawerOpened) && allowOpenDrawer && parentActionBarLayout.fragmentsStack.size() == 1) {
            if (ev != null && (ev.getAction() == MotionEvent.ACTION_DOWN || ev.getAction() == MotionEvent.ACTION_MOVE) && !startedTracking && !maybeStartTracking) {
                parentActionBarLayout.getHitRect(rect);
                startedTrackingX = (int) ev.getX();
                startedTrackingY = (int) ev.getY();
                if (rect.contains(startedTrackingX, startedTrackingY)) {
                    startedTrackingPointerId = ev.getPointerId(0);
                    maybeStartTracking = true;
                    cancelCurrentAnimation();
                    if (velocityTracker != null) {
                        velocityTracker.clear();
                    }
                }
            } else if (ev != null && ev.getAction() == MotionEvent.ACTION_MOVE && ev.getPointerId(0) == startedTrackingPointerId) {
                if (velocityTracker == null) {
                    velocityTracker = VelocityTracker.obtain();
                }
                float dx = (int) (ev.getX() - startedTrackingX);
                float dy = Math.abs((int) ev.getY() - startedTrackingY);
                velocityTracker.addMovement(ev);
                if (maybeStartTracking && !startedTracking && (dx > 0 && dx / 3.0f > Math.abs(dy) && Math.abs(dx) >= AndroidUtilities.getPixelsInCM(0.2f, true) || drawerOpened && dx < 0 && Math.abs(dx) >= Math.abs(dy) && Math.abs(dx) >= AndroidUtilities.getPixelsInCM(0.4f, true))) {
                    prepareForDrawerOpen(ev);
                    startedTrackingX = (int) ev.getX();
                    requestDisallowInterceptTouchEvent(true);
                } else if (startedTracking) {
                    if (!beginTrackingSent) {
                        if (((Activity) getContext()).getCurrentFocus() != null) {
                            AndroidUtilities.hideKeyboard(((Activity) getContext()).getCurrentFocus());
                        }
                        beginTrackingSent = true;
                    }
                    moveDrawerByX(dx);
                    startedTrackingX = (int) ev.getX();
                }
            } else if (ev == null || ev != null && ev.getPointerId(0) == startedTrackingPointerId && (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_POINTER_UP)) {
                if (velocityTracker == null) {
                    velocityTracker = VelocityTracker.obtain();
                }
                velocityTracker.computeCurrentVelocity(1000);
                if (startedTracking || drawerPosition != 0 && drawerPosition != drawerLayout.getMeasuredWidth()) {
                    float velX = velocityTracker.getXVelocity();
                    float velY = velocityTracker.getYVelocity();
                    boolean backAnimation = drawerPosition < drawerLayout.getMeasuredWidth() / 2.0f && (velX < 3500 || Math.abs(velX) < Math.abs(velY)) || velX < 0 && Math.abs(velX) >= 3500;
                    if (!backAnimation) {
                        openDrawer(!drawerOpened && Math.abs(velX) >= 3500);
                    } else {
                        closeDrawer(drawerOpened && Math.abs(velX) >= 3500);
                    }
                }
                startedTracking = false;
                maybeStartTracking = false;
                if (velocityTracker != null) {
                    velocityTracker.recycle();
                    velocityTracker = null;
                }
            }
        } else {
            if (ev == null || ev != null && ev.getPointerId(0) == startedTrackingPointerId && (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_POINTER_UP)) {
                startedTracking = false;
                maybeStartTracking = false;
                if (velocityTracker != null) {
                    velocityTracker.recycle();
                    velocityTracker = null;
                }
            }
        }
        return startedTracking;
    }
    return false;
}
 
Example 14
Source File: DrawerLayoutContainer.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
public boolean onTouchEvent(MotionEvent ev) {
    if (!parentActionBarLayout.checkTransitionAnimation()) {
        if (drawerOpened && ev != null && ev.getX() > drawerPosition && !startedTracking) {
            if (ev.getAction() == MotionEvent.ACTION_UP) {
                closeDrawer(false);
            }
            return true;
        }

        if ((allowOpenDrawerBySwipe || drawerOpened) && allowOpenDrawer && parentActionBarLayout.fragmentsStack.size() == 1) {
            if (ev != null && (ev.getAction() == MotionEvent.ACTION_DOWN || ev.getAction() == MotionEvent.ACTION_MOVE) && !startedTracking && !maybeStartTracking) {
                parentActionBarLayout.getHitRect(rect);
                startedTrackingX = (int) ev.getX();
                startedTrackingY = (int) ev.getY();
                if (rect.contains(startedTrackingX, startedTrackingY)) {
                    startedTrackingPointerId = ev.getPointerId(0);
                    maybeStartTracking = true;
                    cancelCurrentAnimation();
                    if (velocityTracker != null) {
                        velocityTracker.clear();
                    }
                }
            } else if (ev != null && ev.getAction() == MotionEvent.ACTION_MOVE && ev.getPointerId(0) == startedTrackingPointerId) {
                if (velocityTracker == null) {
                    velocityTracker = VelocityTracker.obtain();
                }
                float dx = (int) (ev.getX() - startedTrackingX);
                float dy = Math.abs((int) ev.getY() - startedTrackingY);
                velocityTracker.addMovement(ev);
                if (maybeStartTracking && !startedTracking && (dx > 0 && dx / 3.0f > Math.abs(dy) && Math.abs(dx) >= AndroidUtilities.getPixelsInCM(0.2f, true) || drawerOpened && dx < 0 && Math.abs(dx) >= Math.abs(dy) && Math.abs(dx) >= AndroidUtilities.getPixelsInCM(0.4f, true))) {
                    prepareForDrawerOpen(ev);
                    startedTrackingX = (int) ev.getX();
                    requestDisallowInterceptTouchEvent(true);
                } else if (startedTracking) {
                    if (!beginTrackingSent) {
                        if (((Activity) getContext()).getCurrentFocus() != null) {
                            AndroidUtilities.hideKeyboard(((Activity) getContext()).getCurrentFocus());
                        }
                        beginTrackingSent = true;
                    }
                    moveDrawerByX(dx);
                    startedTrackingX = (int) ev.getX();
                }
            } else if (ev == null || ev != null && ev.getPointerId(0) == startedTrackingPointerId && (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_POINTER_UP)) {
                if (velocityTracker == null) {
                    velocityTracker = VelocityTracker.obtain();
                }
                velocityTracker.computeCurrentVelocity(1000);
                if (startedTracking || drawerPosition != 0 && drawerPosition != drawerLayout.getMeasuredWidth()) {
                    float velX = velocityTracker.getXVelocity();
                    float velY = velocityTracker.getYVelocity();
                    boolean backAnimation = drawerPosition < drawerLayout.getMeasuredWidth() / 2.0f && (velX < 3500 || Math.abs(velX) < Math.abs(velY)) || velX < 0 && Math.abs(velX) >= 3500;
                    if (!backAnimation) {
                        openDrawer(!drawerOpened && Math.abs(velX) >= 3500);
                    } else {
                        closeDrawer(drawerOpened && Math.abs(velX) >= 3500);
                    }
                }
                startedTracking = false;
                maybeStartTracking = false;
                if (velocityTracker != null) {
                    velocityTracker.recycle();
                    velocityTracker = null;
                }
            }
        } else {
            if (ev == null || ev != null && ev.getPointerId(0) == startedTrackingPointerId && (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_POINTER_UP)) {
                startedTracking = false;
                maybeStartTracking = false;
                if (velocityTracker != null) {
                    velocityTracker.recycle();
                    velocityTracker = null;
                }
            }
        }
        return startedTracking;
    }
    return false;
}