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; |
| 43 | |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 44 | public NavTabGallery(Context context, AttributeSet attrs, int defStyle) { |
| 45 | super(context, attrs, defStyle); |
| 46 | } |
| 47 | |
| 48 | public NavTabGallery(Context context, AttributeSet attrs) { |
| 49 | super(context, attrs); |
| 50 | } |
| 51 | |
| 52 | public NavTabGallery(Context context) { |
| 53 | super(context); |
| 54 | } |
| 55 | |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame^] | 56 | public void setOnRemoveListener(OnRemoveListener l) { |
| 57 | mRemoveListener = l; |
| 58 | } |
| 59 | |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 60 | protected void setSelection(int ix) { |
| 61 | super.setSelectedPositionInt(ix); |
| 62 | } |
| 63 | |
| 64 | protected int getSelectionIndex() { |
| 65 | return getSelectedItemPosition(); |
| 66 | } |
| 67 | |
| 68 | protected Tab getSelectedItem() { |
| 69 | return (Tab) mAdapter.getItem(getSelectedItemPosition()); |
| 70 | } |
| 71 | |
| 72 | View getSelectedTab() { |
| 73 | return getSelectedView(); |
| 74 | } |
| 75 | |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame^] | 76 | @Override |
| 77 | protected void onOrthoDrag(View v, MotionEvent down, MotionEvent move, |
| 78 | float distance) { |
| 79 | offsetView(v, - distance); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected void onOrthoFling(View v, MotionEvent down, MotionEvent move, |
| 84 | float velocity) { |
| 85 | if (Math.abs(velocity) > MIN_VELOCITY) { |
| 86 | mBlockUpCallback = true; |
| 87 | animateOut(v, velocity); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | protected void onUp(View downView) { |
| 93 | if (mBlockUpCallback) { |
| 94 | mBlockUpCallback = false; |
| 95 | return; |
| 96 | } |
| 97 | if (mIsOrthoDragged && downView != null) { |
| 98 | // offset |
| 99 | int diff = calculateTop(downView, false) - (mHorizontal ? downView.getTop() |
| 100 | : downView.getLeft()); |
| 101 | if (Math.abs(diff) > (mHorizontal ? downView.getHeight() : downView.getWidth()) / 2) { |
| 102 | // remove it |
| 103 | animateOut(downView, - Math.signum(diff) * MIN_VELOCITY); |
| 104 | } else { |
| 105 | // snap back |
| 106 | offsetView(downView, diff); |
| 107 | } |
| 108 | } else { |
| 109 | super.onUp(downView); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private void offsetView(View v, float distance) { |
| 114 | if (mHorizontal) { |
| 115 | v.offsetTopAndBottom((int) distance); |
| 116 | } else { |
| 117 | v.offsetLeftAndRight((int) distance); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private void animateOut(View v, float velocity) { |
| 122 | final int position = mDownTouchPosition; |
| 123 | int target = 0; |
| 124 | if (velocity < 0) { |
| 125 | target = mHorizontal ? -v.getHeight() : - v.getWidth(); |
| 126 | } else { |
| 127 | target = mHorizontal ? getHeight() : getWidth(); |
| 128 | } |
| 129 | int distance = target - (mHorizontal ? v.getTop() : v.getLeft()); |
| 130 | long duration = (long) (Math.abs(distance) * 1000 / Math.abs(velocity)); |
| 131 | ObjectAnimator animator = null; |
| 132 | if (mHorizontal) { |
| 133 | animator = ObjectAnimator.ofFloat(v, TRANSLATION_Y, 0, target); |
| 134 | } else { |
| 135 | animator = ObjectAnimator.ofFloat(v, TRANSLATION_X, 0, target); |
| 136 | } |
| 137 | animator.setDuration(duration); |
| 138 | animator.addListener(new AnimatorListenerAdapter() { |
| 139 | public void onAnimationEnd(Animator a) { |
| 140 | if (mRemoveListener != null) { |
| 141 | mRemoveListener.onRemovePosition(position); |
| 142 | } |
| 143 | } |
| 144 | }); |
| 145 | animator.start(); |
| 146 | } |
| 147 | |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 148 | } |