blob: 0db31ff4a7c1219e5ac5f67b67eedafb857968f2 [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
18import android.view.MotionEvent;
19import android.view.View;
20import android.view.View.OnTouchListener;
21import android.view.ViewConfiguration;
22import android.webkit.WebView;
23
24import com.android.browser.BrowserWebView.OnScrollChangedListener;
25
26/**
27 * Helper class to manage when to show the URL bar based off of touch
28 * input, and when to begin the hide timer.
29 */
30public class UrlBarAutoShowManager implements OnTouchListener,
31 OnScrollChangedListener {
32
33 private static float V_TRIGGER_ANGLE = .9f;
John Reckbc6adb42011-09-01 18:03:20 -070034 private static long SCROLL_TIMEOUT_DURATION = 150;
35 private static long IGNORE_INTERVAL = 250;
John Reck718a24d2011-08-12 11:08:30 -070036
37 private BrowserWebView mTarget;
38 private BaseUi mUi;
39
40 private int mSlop;
41
42 private float mStartTouchX;
43 private float mStartTouchY;
John Reck718a24d2011-08-12 11:08:30 -070044 private boolean mIsTracking;
45 private boolean mHasTriggered;
John Reckbc6adb42011-09-01 18:03:20 -070046 private long mLastScrollTime;
47 private long mTriggeredTime;
John Reck718a24d2011-08-12 11:08:30 -070048
49 public UrlBarAutoShowManager(BaseUi ui) {
50 mUi = ui;
51 ViewConfiguration config = ViewConfiguration.get(mUi.getActivity());
52 mSlop = config.getScaledTouchSlop() * 2;
53 }
54
55 public void setTarget(BrowserWebView v) {
56 if (mTarget == v) return;
57
58 if (mTarget != null) {
59 mTarget.setOnTouchListener(null);
60 mTarget.setOnScrollChangedListener(null);
61 }
62 mTarget = v;
63 if (mTarget != null) {
64 mTarget.setOnTouchListener(this);
65 mTarget.setOnScrollChangedListener(this);
66 }
67 }
68
69 @Override
70 public void onScrollChanged(int l, int t, int oldl, int oldt) {
John Reckbc6adb42011-09-01 18:03:20 -070071 mLastScrollTime = System.currentTimeMillis();
John Reck718a24d2011-08-12 11:08:30 -070072 if (t != oldt) {
73 if (t != 0) {
74 // If it is showing, extend it
75 if (mUi.isTitleBarShowing()) {
John Reckbc6adb42011-09-01 18:03:20 -070076 long remaining = mLastScrollTime - mTriggeredTime;
77 remaining = Math.max(BaseUi.HIDE_TITLEBAR_DELAY - remaining,
78 SCROLL_TIMEOUT_DURATION);
79 mUi.showTitleBarForDuration(remaining);
John Reck718a24d2011-08-12 11:08:30 -070080 }
81 } else {
82 mUi.suggestHideTitleBar();
83 }
84 }
85 }
86
87 void stopTracking() {
88 if (mIsTracking) {
89 mIsTracking = false;
90 if (mUi.isTitleBarShowing()) {
91 mUi.showTitleBarForDuration();
92 }
93 }
94 }
95
96 @Override
97 public boolean onTouch(View v, MotionEvent event) {
98 if (event.getPointerCount() > 1) {
99 stopTracking();
100 }
101 switch (event.getAction()) {
102 case MotionEvent.ACTION_DOWN:
103 if (!mIsTracking && event.getPointerCount() == 1) {
John Reckbc6adb42011-09-01 18:03:20 -0700104 long sinceLastScroll =
105 System.currentTimeMillis() - mLastScrollTime;
106 if (sinceLastScroll < IGNORE_INTERVAL) {
107 break;
108 }
John Reck718a24d2011-08-12 11:08:30 -0700109 mStartTouchY = event.getY();
110 mStartTouchX = event.getX();
111 mIsTracking = true;
112 mHasTriggered = false;
113 }
114 break;
115 case MotionEvent.ACTION_MOVE:
116 if (mIsTracking && !mHasTriggered) {
117 WebView web = (WebView) v;
118 float dy = event.getY() - mStartTouchY;
119 float ady = Math.abs(dy);
120 float adx = Math.abs(event.getX() - mStartTouchX);
121 if (ady > mSlop) {
122 mHasTriggered = true;
123 float angle = (float) Math.atan2(ady, adx);
124 if (dy > mSlop && angle > V_TRIGGER_ANGLE
125 && !mUi.isTitleBarShowing()
126 && web.getVisibleTitleHeight() == 0) {
John Reckbc6adb42011-09-01 18:03:20 -0700127 mTriggeredTime = System.currentTimeMillis();
John Reck718a24d2011-08-12 11:08:30 -0700128 mUi.showTitleBar();
129 }
130 }
131 }
132 break;
133 case MotionEvent.ACTION_CANCEL:
134 case MotionEvent.ACTION_UP:
135 stopTracking();
136 break;
137 }
138 return false;
139 }
140
141}