Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ObjectAnimator; |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.util.AttributeSet; |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 24 | import android.view.MotionEvent; |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 25 | import android.view.View; |
| 26 | |
| 27 | import com.android.browser.view.Gallery; |
| 28 | |
| 29 | /** |
| 30 | * custom view for displaying tabs in the nav screen |
| 31 | */ |
| 32 | public class NavTabGallery extends Gallery { |
| 33 | |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 34 | interface OnRemoveListener { |
| 35 | public void onRemovePosition(int position); |
| 36 | } |
| 37 | |
| 38 | // after drag animation velocity in pixels/sec |
| 39 | private static final float MIN_VELOCITY = 1500; |
| 40 | |
| 41 | private OnRemoveListener mRemoveListener; |
| 42 | private boolean mBlockUpCallback; |
Michael Kolb | e76f704 | 2011-07-27 15:16:32 -0700 | [diff] [blame] | 43 | private Animator mAnimator; |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 44 | |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 45 | public NavTabGallery(Context context, AttributeSet attrs, int defStyle) { |
| 46 | super(context, attrs, defStyle); |
| 47 | } |
| 48 | |
| 49 | public NavTabGallery(Context context, AttributeSet attrs) { |
| 50 | super(context, attrs); |
| 51 | } |
| 52 | |
| 53 | public NavTabGallery(Context context) { |
| 54 | super(context); |
| 55 | } |
| 56 | |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 57 | public void setOnRemoveListener(OnRemoveListener l) { |
| 58 | mRemoveListener = l; |
| 59 | } |
| 60 | |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 61 | protected void setSelection(int ix) { |
| 62 | super.setSelectedPositionInt(ix); |
| 63 | } |
| 64 | |
| 65 | protected int getSelectionIndex() { |
| 66 | return getSelectedItemPosition(); |
| 67 | } |
| 68 | |
| 69 | protected Tab getSelectedItem() { |
| 70 | return (Tab) mAdapter.getItem(getSelectedItemPosition()); |
| 71 | } |
| 72 | |
| 73 | View getSelectedTab() { |
| 74 | return getSelectedView(); |
| 75 | } |
| 76 | |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 77 | @Override |
| 78 | protected void onOrthoDrag(View v, MotionEvent down, MotionEvent move, |
| 79 | float distance) { |
Michael Kolb | e76f704 | 2011-07-27 15:16:32 -0700 | [diff] [blame] | 80 | if (mAnimator == null) { |
| 81 | offsetView(v, - distance); |
| 82 | } |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | protected void onOrthoFling(View v, MotionEvent down, MotionEvent move, |
| 87 | float velocity) { |
Michael Kolb | e76f704 | 2011-07-27 15:16:32 -0700 | [diff] [blame] | 88 | if ((mAnimator == null) && (Math.abs(velocity) > MIN_VELOCITY)) { |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 89 | mBlockUpCallback = true; |
| 90 | animateOut(v, velocity); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | protected void onUp(View downView) { |
Michael Kolb | e76f704 | 2011-07-27 15:16:32 -0700 | [diff] [blame] | 96 | if (mAnimator != null) return; |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 97 | if (mBlockUpCallback) { |
| 98 | mBlockUpCallback = false; |
| 99 | return; |
| 100 | } |
| 101 | if (mIsOrthoDragged && downView != null) { |
| 102 | // offset |
| 103 | int diff = calculateTop(downView, false) - (mHorizontal ? downView.getTop() |
| 104 | : downView.getLeft()); |
| 105 | if (Math.abs(diff) > (mHorizontal ? downView.getHeight() : downView.getWidth()) / 2) { |
| 106 | // remove it |
| 107 | animateOut(downView, - Math.signum(diff) * MIN_VELOCITY); |
| 108 | } else { |
| 109 | // snap back |
| 110 | offsetView(downView, diff); |
| 111 | } |
| 112 | } else { |
| 113 | super.onUp(downView); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | private void offsetView(View v, float distance) { |
| 118 | if (mHorizontal) { |
| 119 | v.offsetTopAndBottom((int) distance); |
| 120 | } else { |
| 121 | v.offsetLeftAndRight((int) distance); |
| 122 | } |
| 123 | } |
| 124 | |
Michael Kolb | e76f704 | 2011-07-27 15:16:32 -0700 | [diff] [blame] | 125 | protected void animateOut(View v) { |
| 126 | animateOut(v, -MIN_VELOCITY); |
| 127 | } |
| 128 | |
| 129 | private void animateOut(final View v, float velocity) { |
Michael Kolb | 3bb854b | 2011-08-01 10:45:16 -0700 | [diff] [blame] | 130 | if ((v == null) || (mAnimator != null)) return; |
Michael Kolb | e76f704 | 2011-07-27 15:16:32 -0700 | [diff] [blame] | 131 | final int position = mFirstPosition + indexOfChild(v); |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 132 | int target = 0; |
| 133 | if (velocity < 0) { |
| 134 | target = mHorizontal ? -v.getHeight() : - v.getWidth(); |
| 135 | } else { |
| 136 | target = mHorizontal ? getHeight() : getWidth(); |
| 137 | } |
| 138 | int distance = target - (mHorizontal ? v.getTop() : v.getLeft()); |
| 139 | long duration = (long) (Math.abs(distance) * 1000 / Math.abs(velocity)); |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 140 | if (mHorizontal) { |
Michael Kolb | e76f704 | 2011-07-27 15:16:32 -0700 | [diff] [blame] | 141 | mAnimator = ObjectAnimator.ofFloat(v, TRANSLATION_Y, 0, target); |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 142 | } else { |
Michael Kolb | e76f704 | 2011-07-27 15:16:32 -0700 | [diff] [blame] | 143 | mAnimator = ObjectAnimator.ofFloat(v, TRANSLATION_X, 0, target); |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 144 | } |
Michael Kolb | e76f704 | 2011-07-27 15:16:32 -0700 | [diff] [blame] | 145 | mAnimator.setDuration(duration); |
| 146 | mAnimator.addListener(new AnimatorListenerAdapter() { |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 147 | public void onAnimationEnd(Animator a) { |
| 148 | if (mRemoveListener != null) { |
Michael Kolb | e76f704 | 2011-07-27 15:16:32 -0700 | [diff] [blame] | 149 | boolean needsGap = position < (mAdapter.getCount() - 1); |
| 150 | if (needsGap) { |
| 151 | setGapPosition(position, mHorizontal ? v.getWidth() : v.getHeight()); |
| 152 | } |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 153 | mRemoveListener.onRemovePosition(position); |
Michael Kolb | c1eeb12 | 2011-08-01 09:56:26 -0700 | [diff] [blame] | 154 | if (!needsGap && (position > 0) && (mAdapter.getCount() > 0)) { |
Michael Kolb | e76f704 | 2011-07-27 15:16:32 -0700 | [diff] [blame] | 155 | scrollToChild(position - 1); |
| 156 | } |
| 157 | mAnimator = null; |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | }); |
Michael Kolb | e76f704 | 2011-07-27 15:16:32 -0700 | [diff] [blame] | 161 | mAnimator.start(); |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 164 | } |