UrlBarAutoShowManager.java revision bc6adb4501853dbaba1c3bb592a159ee901b70e7
1/*
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;
34    private static long SCROLL_TIMEOUT_DURATION = 150;
35    private static long IGNORE_INTERVAL = 250;
36
37    private BrowserWebView mTarget;
38    private BaseUi mUi;
39
40    private int mSlop;
41
42    private float mStartTouchX;
43    private float mStartTouchY;
44    private boolean mIsTracking;
45    private boolean mHasTriggered;
46    private long mLastScrollTime;
47    private long mTriggeredTime;
48
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) {
71        mLastScrollTime = System.currentTimeMillis();
72        if (t != oldt) {
73            if (t != 0) {
74                // If it is showing, extend it
75                if (mUi.isTitleBarShowing()) {
76                    long remaining = mLastScrollTime - mTriggeredTime;
77                    remaining = Math.max(BaseUi.HIDE_TITLEBAR_DELAY - remaining,
78                            SCROLL_TIMEOUT_DURATION);
79                    mUi.showTitleBarForDuration(remaining);
80                }
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) {
104                long sinceLastScroll =
105                        System.currentTimeMillis() - mLastScrollTime;
106                if (sinceLastScroll < IGNORE_INTERVAL) {
107                    break;
108                }
109                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) {
127                        mTriggeredTime = System.currentTimeMillis();
128                        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}
142