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; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * @param context |
| 36 | * @param attrs |
| 37 | * @param defStyle |
| 38 | * @param javascriptInterfaces |
| 39 | */ |
| 40 | public ScrollWebView(Context context, AttributeSet attrs, int defStyle, |
| 41 | Map<String, Object> javascriptInterfaces, boolean privateBrowsing) { |
| 42 | super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param context |
| 47 | * @param attrs |
| 48 | * @param defStyle |
| 49 | */ |
| 50 | public ScrollWebView(Context context, AttributeSet attrs, int defStyle, |
| 51 | boolean privateBrowsing) { |
| 52 | super(context, attrs, defStyle, privateBrowsing); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param context |
| 57 | * @param attrs |
| 58 | */ |
| 59 | public ScrollWebView(Context context, AttributeSet attrs) { |
| 60 | super(context, attrs); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param context |
| 65 | */ |
| 66 | public ScrollWebView(Context context) { |
| 67 | super(context); |
| 68 | } |
| 69 | |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame^] | 70 | // scroll runnable implementation |
| 71 | public void run() { |
| 72 | if (!mIsCancelled && (mScrollListener != null)) { |
| 73 | mScrollListener.onScroll(getVisibleTitleHeight()); |
| 74 | } |
| 75 | } |
| 76 | |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 77 | void hideEmbeddedTitleBar() { |
| 78 | scrollBy(0, getVisibleTitleHeight()); |
| 79 | } |
| 80 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 81 | @Override |
| 82 | public void setEmbeddedTitleBar(final View title) { |
| 83 | super.setEmbeddedTitleBar(title); |
| 84 | if (title != null && mScrollListener != null) { |
| 85 | // allow the scroll listener to initialize its state |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame^] | 86 | post(this); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void stopScroll() { |
| 92 | mIsCancelled = true; |
| 93 | super.stopScroll(); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | protected void onScrollChanged(int l, final int t, int ol, int ot) { |
| 98 | super.onScrollChanged(l, t, ol, ot); |
| 99 | if (!mIsCancelled) { |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame^] | 100 | post(this); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 101 | } else { |
| 102 | mIsCancelled = false; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void setScrollListener(ScrollListener l) { |
| 107 | mScrollListener = l; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // callback for scroll events |
| 111 | |
| 112 | interface ScrollListener { |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame^] | 113 | public void onScroll(int visibleTitleHeight); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | } |