blob: 294115e7f24f05ae46b26f4dcd210ab552e88874 [file] [log] [blame]
John Reck718a24d2011-08-12 11:08:30 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.browser;
17
John Reckbb3a4f22011-09-12 11:56:18 -070018import android.os.SystemClock;
John Reck718a24d2011-08-12 11:08:30 -070019import android.view.MotionEvent;
20import android.view.View;
21import android.view.View.OnTouchListener;
22import android.view.ViewConfiguration;
23import android.webkit.WebView;
24
25import com.android.browser.BrowserWebView.OnScrollChangedListener;
26
27/**
28 * Helper class to manage when to show the URL bar based off of touch
29 * input, and when to begin the hide timer.
30 */
31public class UrlBarAutoShowManager implements OnTouchListener,
32 OnScrollChangedListener {
33
34 private static float V_TRIGGER_ANGLE = .9f;
John Reckbc6adb42011-09-01 18:03:20 -070035 private static long SCROLL_TIMEOUT_DURATION = 150;
36 private static long IGNORE_INTERVAL = 250;
John Reck718a24d2011-08-12 11:08:30 -070037
38 private BrowserWebView mTarget;
39 private BaseUi mUi;
40
41 private int mSlop;
42
43 private float mStartTouchX;
44 private float mStartTouchY;
John Reck718a24d2011-08-12 11:08:30 -070045 private boolean mIsTracking;
46 private boolean mHasTriggered;
John Reckbc6adb42011-09-01 18:03:20 -070047 private long mLastScrollTime;
48 private long mTriggeredTime;
John Reck868efdf2011-10-31 11:28:30 -070049 private boolean mIsScrolling;
John Reck718a24d2011-08-12 11:08:30 -070050
51 public UrlBarAutoShowManager(BaseUi ui) {
52 mUi = ui;
53 ViewConfiguration config = ViewConfiguration.get(mUi.getActivity());
54 mSlop = config.getScaledTouchSlop() * 2;
55 }
56
57 public void setTarget(BrowserWebView v) {
58 if (mTarget == v) return;
59
60 if (mTarget != null) {
61 mTarget.setOnTouchListener(null);
62 mTarget.setOnScrollChangedListener(null);
63 }
64 mTarget = v;
65 if (mTarget != null) {
66 mTarget.setOnTouchListener(this);
67 mTarget.setOnScrollChangedListener(this);
68 }
69 }
70
71 @Override
72 public void onScrollChanged(int l, int t, int oldl, int oldt) {
John Reckbb3a4f22011-09-12 11:56:18 -070073 mLastScrollTime = SystemClock.uptimeMillis();
John Reck718a24d2011-08-12 11:08:30 -070074 if (t != oldt) {
John Reck868efdf2011-10-31 11:28:30 -070075 mIsScrolling = true;
John Reck718a24d2011-08-12 11:08:30 -070076 if (t != 0) {
77 // If it is showing, extend it
78 if (mUi.isTitleBarShowing()) {
John Reckbc6adb42011-09-01 18:03:20 -070079 long remaining = mLastScrollTime - mTriggeredTime;
80 remaining = Math.max(BaseUi.HIDE_TITLEBAR_DELAY - remaining,
81 SCROLL_TIMEOUT_DURATION);
82 mUi.showTitleBarForDuration(remaining);
John Reck718a24d2011-08-12 11:08:30 -070083 }
84 } else {
85 mUi.suggestHideTitleBar();
86 }
87 }
88 }
89
90 void stopTracking() {
91 if (mIsTracking) {
92 mIsTracking = false;
John Reck868efdf2011-10-31 11:28:30 -070093 mIsScrolling = false;
John Reck718a24d2011-08-12 11:08:30 -070094 if (mUi.isTitleBarShowing()) {
95 mUi.showTitleBarForDuration();
96 }
97 }
98 }
99
100 @Override
101 public boolean onTouch(View v, MotionEvent event) {
102 if (event.getPointerCount() > 1) {
103 stopTracking();
104 }
105 switch (event.getAction()) {
106 case MotionEvent.ACTION_DOWN:
107 if (!mIsTracking && event.getPointerCount() == 1) {
John Reckbc6adb42011-09-01 18:03:20 -0700108 long sinceLastScroll =
John Reckda5d3482011-09-15 10:11:08 -0700109 SystemClock.uptimeMillis() - mLastScrollTime;
John Reckbc6adb42011-09-01 18:03:20 -0700110 if (sinceLastScroll < IGNORE_INTERVAL) {
111 break;
112 }
John Reck718a24d2011-08-12 11:08:30 -0700113 mStartTouchY = event.getY();
114 mStartTouchX = event.getX();
115 mIsTracking = true;
116 mHasTriggered = false;
117 }
118 break;
119 case MotionEvent.ACTION_MOVE:
120 if (mIsTracking && !mHasTriggered) {
121 WebView web = (WebView) v;
122 float dy = event.getY() - mStartTouchY;
123 float ady = Math.abs(dy);
124 float adx = Math.abs(event.getX() - mStartTouchX);
125 if (ady > mSlop) {
126 mHasTriggered = true;
127 float angle = (float) Math.atan2(ady, adx);
128 if (dy > mSlop && angle > V_TRIGGER_ANGLE
129 && !mUi.isTitleBarShowing()
John Reck868efdf2011-10-31 11:28:30 -0700130 && (web.getVisibleTitleHeight() == 0
131 || (!mIsScrolling && web.getScrollY() > 0))) {
John Reckbb3a4f22011-09-12 11:56:18 -0700132 mTriggeredTime = SystemClock.uptimeMillis();
John Reck718a24d2011-08-12 11:08:30 -0700133 mUi.showTitleBar();
134 }
135 }
136 }
137 break;
138 case MotionEvent.ACTION_CANCEL:
139 case MotionEvent.ACTION_UP:
140 stopTracking();
141 break;
142 }
143 return false;
144 }
145
146}