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