Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.AttributeSet; |
Michael Kolb | 5ee018e | 2011-02-18 15:47:21 -0800 | [diff] [blame] | 21 | import android.view.MotionEvent; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 22 | import android.view.View; |
| 23 | import android.webkit.WebView; |
| 24 | |
| 25 | import java.util.Map; |
| 26 | |
| 27 | /** |
| 28 | * Manage WebView scroll events |
| 29 | */ |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 30 | public class ScrollWebView extends WebView implements Runnable { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 31 | |
| 32 | private ScrollListener mScrollListener; |
| 33 | private boolean mIsCancelled; |
Romain Guy | 860662a | 2011-01-10 12:57:22 -0800 | [diff] [blame] | 34 | private boolean mBackgroundRemoved = false; |
Michael Kolb | 5ee018e | 2011-02-18 15:47:21 -0800 | [diff] [blame] | 35 | private boolean mUserInitiated = false; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * @param context |
| 39 | * @param attrs |
| 40 | * @param defStyle |
| 41 | * @param javascriptInterfaces |
| 42 | */ |
| 43 | public ScrollWebView(Context context, AttributeSet attrs, int defStyle, |
| 44 | Map<String, Object> javascriptInterfaces, boolean privateBrowsing) { |
| 45 | super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param context |
| 50 | * @param attrs |
| 51 | * @param defStyle |
| 52 | */ |
| 53 | public ScrollWebView(Context context, AttributeSet attrs, int defStyle, |
| 54 | boolean privateBrowsing) { |
| 55 | super(context, attrs, defStyle, privateBrowsing); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param context |
| 60 | * @param attrs |
| 61 | */ |
| 62 | public ScrollWebView(Context context, AttributeSet attrs) { |
| 63 | super(context, attrs); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @param context |
| 68 | */ |
| 69 | public ScrollWebView(Context context) { |
| 70 | super(context); |
| 71 | } |
| 72 | |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 73 | // scroll runnable implementation |
| 74 | public void run() { |
| 75 | if (!mIsCancelled && (mScrollListener != null)) { |
Michael Kolb | 5ee018e | 2011-02-18 15:47:21 -0800 | [diff] [blame] | 76 | mScrollListener.onScroll(getVisibleTitleHeight(), mUserInitiated); |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 80 | void hideEmbeddedTitleBar() { |
| 81 | scrollBy(0, getVisibleTitleHeight()); |
| 82 | } |
| 83 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 84 | @Override |
| 85 | public void setEmbeddedTitleBar(final View title) { |
| 86 | super.setEmbeddedTitleBar(title); |
| 87 | if (title != null && mScrollListener != null) { |
| 88 | // allow the scroll listener to initialize its state |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 89 | post(this); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | @Override |
Michael Kolb | 5ee018e | 2011-02-18 15:47:21 -0800 | [diff] [blame] | 94 | public boolean onTouchEvent(MotionEvent evt) { |
| 95 | if (MotionEvent.ACTION_DOWN == evt.getActionMasked()) { |
| 96 | mUserInitiated = true; |
| 97 | } else if (MotionEvent.ACTION_UP == evt.getActionMasked() |
| 98 | || (MotionEvent.ACTION_CANCEL == evt.getActionMasked())) { |
| 99 | mUserInitiated = false; |
| 100 | } |
| 101 | return super.onTouchEvent(evt); |
| 102 | } |
| 103 | |
| 104 | @Override |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 105 | public void stopScroll() { |
| 106 | mIsCancelled = true; |
| 107 | super.stopScroll(); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | protected void onScrollChanged(int l, final int t, int ol, int ot) { |
| 112 | super.onScrollChanged(l, t, ol, ot); |
| 113 | if (!mIsCancelled) { |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 114 | post(this); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 115 | } else { |
| 116 | mIsCancelled = false; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void setScrollListener(ScrollListener l) { |
| 121 | mScrollListener = l; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // callback for scroll events |
| 125 | |
| 126 | interface ScrollListener { |
Michael Kolb | 5ee018e | 2011-02-18 15:47:21 -0800 | [diff] [blame] | 127 | public void onScroll(int visibleTitleHeight, boolean userInitiated); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Romain Guy | 860662a | 2011-01-10 12:57:22 -0800 | [diff] [blame] | 130 | @Override |
| 131 | protected void onDraw(android.graphics.Canvas c) { |
| 132 | super.onDraw(c); |
| 133 | if (!mBackgroundRemoved && getRootView().getBackground() != null) { |
| 134 | mBackgroundRemoved = true; |
| 135 | post(new Runnable() { |
| 136 | public void run() { |
| 137 | getRootView().setBackgroundDrawable(null); |
| 138 | } |
| 139 | }); |
| 140 | } |
| 141 | } |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 142 | } |