John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 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 | */ |
| 16 | package com.android.browser; |
| 17 | |
| 18 | import android.view.MotionEvent; |
| 19 | import android.view.View; |
| 20 | import android.view.View.OnTouchListener; |
| 21 | import android.view.ViewConfiguration; |
| 22 | import android.webkit.WebView; |
| 23 | |
| 24 | import 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 | */ |
| 30 | public class UrlBarAutoShowManager implements OnTouchListener, |
| 31 | OnScrollChangedListener { |
| 32 | |
| 33 | private static float V_TRIGGER_ANGLE = .9f; |
| 34 | |
| 35 | private BrowserWebView mTarget; |
| 36 | private BaseUi mUi; |
| 37 | |
| 38 | private int mSlop; |
| 39 | |
| 40 | private float mStartTouchX; |
| 41 | private float mStartTouchY; |
| 42 | private float mLastTouchX; |
| 43 | private float mLastTouchY; |
| 44 | private boolean mIsTracking; |
| 45 | private boolean mHasTriggered; |
| 46 | |
| 47 | public UrlBarAutoShowManager(BaseUi ui) { |
| 48 | mUi = ui; |
| 49 | ViewConfiguration config = ViewConfiguration.get(mUi.getActivity()); |
| 50 | mSlop = config.getScaledTouchSlop() * 2; |
| 51 | } |
| 52 | |
| 53 | public void setTarget(BrowserWebView v) { |
| 54 | if (mTarget == v) return; |
| 55 | |
| 56 | if (mTarget != null) { |
| 57 | mTarget.setOnTouchListener(null); |
| 58 | mTarget.setOnScrollChangedListener(null); |
| 59 | } |
| 60 | mTarget = v; |
| 61 | if (mTarget != null) { |
| 62 | mTarget.setOnTouchListener(this); |
| 63 | mTarget.setOnScrollChangedListener(this); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void onScrollChanged(int l, int t, int oldl, int oldt) { |
| 69 | if (t != oldt) { |
| 70 | if (t != 0) { |
| 71 | // If it is showing, extend it |
| 72 | if (mUi.isTitleBarShowing()) { |
| 73 | mUi.showTitleBarForDuration(); |
| 74 | } |
| 75 | } else { |
| 76 | mUi.suggestHideTitleBar(); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void stopTracking() { |
| 82 | if (mIsTracking) { |
| 83 | mIsTracking = false; |
| 84 | if (mUi.isTitleBarShowing()) { |
| 85 | mUi.showTitleBarForDuration(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean onTouch(View v, MotionEvent event) { |
| 92 | if (event.getPointerCount() > 1) { |
| 93 | stopTracking(); |
| 94 | } |
| 95 | switch (event.getAction()) { |
| 96 | case MotionEvent.ACTION_DOWN: |
| 97 | if (!mIsTracking && event.getPointerCount() == 1) { |
| 98 | mStartTouchY = event.getY(); |
| 99 | mStartTouchX = event.getX(); |
| 100 | mIsTracking = true; |
| 101 | mHasTriggered = false; |
| 102 | } |
| 103 | break; |
| 104 | case MotionEvent.ACTION_MOVE: |
| 105 | if (mIsTracking && !mHasTriggered) { |
| 106 | WebView web = (WebView) v; |
| 107 | float dy = event.getY() - mStartTouchY; |
| 108 | float ady = Math.abs(dy); |
| 109 | float adx = Math.abs(event.getX() - mStartTouchX); |
| 110 | if (ady > mSlop) { |
| 111 | mHasTriggered = true; |
| 112 | float angle = (float) Math.atan2(ady, adx); |
| 113 | if (dy > mSlop && angle > V_TRIGGER_ANGLE |
| 114 | && !mUi.isTitleBarShowing() |
| 115 | && web.getVisibleTitleHeight() == 0) { |
| 116 | mUi.showTitleBar(); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | break; |
| 121 | case MotionEvent.ACTION_CANCEL: |
| 122 | case MotionEvent.ACTION_UP: |
| 123 | stopTracking(); |
| 124 | break; |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | } |