Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.gfx; |
michael@0 | 7 | |
michael@0 | 8 | import android.content.Context; |
michael@0 | 9 | import android.graphics.Canvas; |
michael@0 | 10 | import android.os.Build; |
michael@0 | 11 | import android.widget.EdgeEffect; |
michael@0 | 12 | import android.view.View; |
michael@0 | 13 | |
michael@0 | 14 | |
michael@0 | 15 | public class OverscrollEdgeEffect implements Overscroll { |
michael@0 | 16 | // Used to index particular edges in the edges array |
michael@0 | 17 | private static final int TOP = 0; |
michael@0 | 18 | private static final int BOTTOM = 1; |
michael@0 | 19 | private static final int LEFT = 2; |
michael@0 | 20 | private static final int RIGHT = 3; |
michael@0 | 21 | |
michael@0 | 22 | // All four edges of the screen |
michael@0 | 23 | private final EdgeEffect[] mEdges = new EdgeEffect[4]; |
michael@0 | 24 | |
michael@0 | 25 | // The view we're showing this overscroll on. |
michael@0 | 26 | private final View mView; |
michael@0 | 27 | |
michael@0 | 28 | public OverscrollEdgeEffect(final View v) { |
michael@0 | 29 | mView = v; |
michael@0 | 30 | Context context = v.getContext(); |
michael@0 | 31 | for (int i = 0; i < 4; i++) { |
michael@0 | 32 | mEdges[i] = new EdgeEffect(context); |
michael@0 | 33 | } |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | public void setSize(final int width, final int height) { |
michael@0 | 37 | mEdges[LEFT].setSize(height, width); |
michael@0 | 38 | mEdges[RIGHT].setSize(height, width); |
michael@0 | 39 | mEdges[TOP].setSize(width, height); |
michael@0 | 40 | mEdges[BOTTOM].setSize(width, height); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | private EdgeEffect getEdgeForAxisAndSide(final Axis axis, final float side) { |
michael@0 | 44 | if (axis == Axis.Y) { |
michael@0 | 45 | if (side < 0) { |
michael@0 | 46 | return mEdges[TOP]; |
michael@0 | 47 | } else { |
michael@0 | 48 | return mEdges[BOTTOM]; |
michael@0 | 49 | } |
michael@0 | 50 | } else { |
michael@0 | 51 | if (side < 0) { |
michael@0 | 52 | return mEdges[LEFT]; |
michael@0 | 53 | } else { |
michael@0 | 54 | return mEdges[RIGHT]; |
michael@0 | 55 | } |
michael@0 | 56 | } |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | private void invalidate() { |
michael@0 | 60 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { |
michael@0 | 61 | mView.postInvalidateOnAnimation(); |
michael@0 | 62 | } else { |
michael@0 | 63 | mView.postInvalidateDelayed(10); |
michael@0 | 64 | } |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | public void setVelocity(final float velocity, final Axis axis) { |
michael@0 | 68 | final EdgeEffect edge = getEdgeForAxisAndSide(axis, velocity); |
michael@0 | 69 | |
michael@0 | 70 | // If we're showing overscroll already, start fading it out. |
michael@0 | 71 | if (!edge.isFinished()) { |
michael@0 | 72 | edge.onRelease(); |
michael@0 | 73 | } else { |
michael@0 | 74 | // Otherwise, show an absorb effect |
michael@0 | 75 | edge.onAbsorb((int)velocity); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | invalidate(); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | public void setDistance(final float distance, final Axis axis) { |
michael@0 | 82 | // The first overscroll event often has zero distance. Throw it out |
michael@0 | 83 | if (distance == 0.0f) { |
michael@0 | 84 | return; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | final EdgeEffect edge = getEdgeForAxisAndSide(axis, (int)distance); |
michael@0 | 88 | edge.onPull(distance / (axis == Axis.X ? mView.getWidth() : mView.getHeight())); |
michael@0 | 89 | invalidate(); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | public void draw(final Canvas canvas, final ImmutableViewportMetrics metrics) { |
michael@0 | 93 | if (metrics == null) { |
michael@0 | 94 | return; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | // If we're pulling an edge, or fading it out, draw! |
michael@0 | 98 | boolean invalidate = false; |
michael@0 | 99 | if (!mEdges[TOP].isFinished()) { |
michael@0 | 100 | invalidate |= draw(mEdges[TOP], canvas, metrics.marginLeft, metrics.marginTop, 0); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | if (!mEdges[BOTTOM].isFinished()) { |
michael@0 | 104 | invalidate |= draw(mEdges[BOTTOM], canvas, mView.getWidth(), mView.getHeight(), 180); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | if (!mEdges[LEFT].isFinished()) { |
michael@0 | 108 | invalidate |= draw(mEdges[LEFT], canvas, metrics.marginLeft, mView.getHeight(), 270); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | if (!mEdges[RIGHT].isFinished()) { |
michael@0 | 112 | invalidate |= draw(mEdges[RIGHT], canvas, mView.getWidth(), metrics.marginTop, 90); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | // If the edge effect is animating off screen, invalidate. |
michael@0 | 116 | if (invalidate) { |
michael@0 | 117 | invalidate(); |
michael@0 | 118 | } |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | public boolean draw(final EdgeEffect edge, final Canvas canvas, final float translateX, final float translateY, final float rotation) { |
michael@0 | 122 | final int state = canvas.save(); |
michael@0 | 123 | canvas.translate(translateX, translateY); |
michael@0 | 124 | canvas.rotate(rotation); |
michael@0 | 125 | boolean invalidate = edge.draw(canvas); |
michael@0 | 126 | canvas.restoreToCount(state); |
michael@0 | 127 | |
michael@0 | 128 | return invalidate; |
michael@0 | 129 | } |
michael@0 | 130 | } |