blob: 0cd1f82fa8c1dedcac75697a0b1755fdeacba62b [file] [log] [blame]
Michael Kolb9829b432011-06-04 13:29:00 -07001/*
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
17package com.android.browser;
18
Michael Kolbdcd81d32011-07-22 10:30:49 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
Michael Kolb9829b432011-06-04 13:29:00 -070022import android.content.Context;
23import android.util.AttributeSet;
Michael Kolbdcd81d32011-07-22 10:30:49 -070024import android.view.MotionEvent;
Michael Kolb9829b432011-06-04 13:29:00 -070025import android.view.View;
26
27import com.android.browser.view.Gallery;
28
29/**
30 * custom view for displaying tabs in the nav screen
31 */
32public class NavTabGallery extends Gallery {
33
Michael Kolbdcd81d32011-07-22 10:30:49 -070034 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 Kolb9829b432011-06-04 13:29:00 -070044 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 Kolbdcd81d32011-07-22 10:30:49 -070056 public void setOnRemoveListener(OnRemoveListener l) {
57 mRemoveListener = l;
58 }
59
Michael Kolb9829b432011-06-04 13:29:00 -070060 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 Kolbdcd81d32011-07-22 10:30:49 -070076 @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 Kolb9829b432011-06-04 13:29:00 -0700148}