blob: 9f7827b2d66b475f9df46fc8a1702183cf1622e6 [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 Reck576c37f2011-11-14 16:18:50 -080074 mIsScrolling = true;
75 if (t != 0) {
76 // If it is showing, extend it
77 if (mUi.isTitleBarShowing()) {
78 long remaining = mLastScrollTime - mTriggeredTime;
79 remaining = Math.max(BaseUi.HIDE_TITLEBAR_DELAY - remaining,
80 SCROLL_TIMEOUT_DURATION);
81 mUi.showTitleBarForDuration(remaining);
John Reck718a24d2011-08-12 11:08:30 -070082 }
John Reck576c37f2011-11-14 16:18:50 -080083 } else {
84 mUi.suggestHideTitleBar();
John Reck718a24d2011-08-12 11:08:30 -070085 }
86 }
87
88 void stopTracking() {
89 if (mIsTracking) {
90 mIsTracking = false;
John Reck868efdf2011-10-31 11:28:30 -070091 mIsScrolling = false;
John Reck718a24d2011-08-12 11:08:30 -070092 if (mUi.isTitleBarShowing()) {
93 mUi.showTitleBarForDuration();
94 }
95 }
96 }
97
98 @Override
99 public boolean onTouch(View v, MotionEvent event) {
100 if (event.getPointerCount() > 1) {
101 stopTracking();
102 }
103 switch (event.getAction()) {
104 case MotionEvent.ACTION_DOWN:
105 if (!mIsTracking && event.getPointerCount() == 1) {
John Reckbc6adb42011-09-01 18:03:20 -0700106 long sinceLastScroll =
John Reckda5d3482011-09-15 10:11:08 -0700107 SystemClock.uptimeMillis() - mLastScrollTime;
John Reckbc6adb42011-09-01 18:03:20 -0700108 if (sinceLastScroll < IGNORE_INTERVAL) {
109 break;
110 }
John Reck718a24d2011-08-12 11:08:30 -0700111 mStartTouchY = event.getY();
112 mStartTouchX = event.getX();
113 mIsTracking = true;
114 mHasTriggered = false;
115 }
116 break;
117 case MotionEvent.ACTION_MOVE:
118 if (mIsTracking && !mHasTriggered) {
119 WebView web = (WebView) v;
120 float dy = event.getY() - mStartTouchY;
121 float ady = Math.abs(dy);
122 float adx = Math.abs(event.getX() - mStartTouchX);
123 if (ady > mSlop) {
124 mHasTriggered = true;
125 float angle = (float) Math.atan2(ady, adx);
126 if (dy > mSlop && angle > V_TRIGGER_ANGLE
127 && !mUi.isTitleBarShowing()
John Reck868efdf2011-10-31 11:28:30 -0700128 && (web.getVisibleTitleHeight() == 0
129 || (!mIsScrolling && web.getScrollY() > 0))) {
John Reckbb3a4f22011-09-12 11:56:18 -0700130 mTriggeredTime = SystemClock.uptimeMillis();
John Reck718a24d2011-08-12 11:08:30 -0700131 mUi.showTitleBar();
132 }
133 }
134 }
135 break;
136 case MotionEvent.ACTION_CANCEL:
137 case MotionEvent.ACTION_UP:
138 stopTracking();
139 break;
140 }
141 return false;
142 }
143
144}