Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 7 | * |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 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 | /** |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 28 | * Manage WebView scroll events |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 29 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame^] | 30 | public class BrowserWebView 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 | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 36 | private TitleBarBase mTitleBar; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * @param context |
| 40 | * @param attrs |
| 41 | * @param defStyle |
| 42 | * @param javascriptInterfaces |
| 43 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame^] | 44 | public BrowserWebView(Context context, AttributeSet attrs, int defStyle, |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 45 | Map<String, Object> javascriptInterfaces, boolean privateBrowsing) { |
| 46 | super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param context |
| 51 | * @param attrs |
| 52 | * @param defStyle |
| 53 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame^] | 54 | public BrowserWebView( |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 55 | Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 56 | super(context, attrs, defStyle, privateBrowsing); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @param context |
| 61 | * @param attrs |
| 62 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame^] | 63 | public BrowserWebView(Context context, AttributeSet attrs) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 64 | super(context, attrs); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param context |
| 69 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame^] | 70 | public BrowserWebView(Context context) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 71 | super(context); |
| 72 | } |
| 73 | |
Michael Kolb | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 74 | @Override |
| 75 | protected int getTitleHeight() { |
| 76 | return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0; |
| 77 | } |
| 78 | |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 79 | // scroll runnable implementation |
| 80 | public void run() { |
| 81 | if (!mIsCancelled && (mScrollListener != null)) { |
Michael Kolb | 5ee018e | 2011-02-18 15:47:21 -0800 | [diff] [blame] | 82 | mScrollListener.onScroll(getVisibleTitleHeight(), mUserInitiated); |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 86 | void hideEmbeddedTitleBar() { |
| 87 | scrollBy(0, getVisibleTitleHeight()); |
| 88 | } |
| 89 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 90 | @Override |
| 91 | public void setEmbeddedTitleBar(final View title) { |
| 92 | super.setEmbeddedTitleBar(title); |
Michael Kolb | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 93 | mTitleBar = (TitleBarBase) title; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 94 | if (title != null && mScrollListener != null) { |
| 95 | // allow the scroll listener to initialize its state |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 96 | post(this); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 100 | public boolean hasTitleBar() { |
| 101 | return (mTitleBar != null); |
| 102 | } |
| 103 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 104 | @Override |
Michael Kolb | 5ee018e | 2011-02-18 15:47:21 -0800 | [diff] [blame] | 105 | public boolean onTouchEvent(MotionEvent evt) { |
| 106 | if (MotionEvent.ACTION_DOWN == evt.getActionMasked()) { |
| 107 | mUserInitiated = true; |
| 108 | } else if (MotionEvent.ACTION_UP == evt.getActionMasked() |
| 109 | || (MotionEvent.ACTION_CANCEL == evt.getActionMasked())) { |
| 110 | mUserInitiated = false; |
| 111 | } |
| 112 | return super.onTouchEvent(evt); |
| 113 | } |
| 114 | |
| 115 | @Override |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 116 | public void stopScroll() { |
| 117 | mIsCancelled = true; |
| 118 | super.stopScroll(); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | protected void onScrollChanged(int l, final int t, int ol, int ot) { |
| 123 | super.onScrollChanged(l, t, ol, ot); |
| 124 | if (!mIsCancelled) { |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 125 | post(this); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 126 | } else { |
| 127 | mIsCancelled = false; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void setScrollListener(ScrollListener l) { |
| 132 | mScrollListener = l; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // callback for scroll events |
| 136 | |
| 137 | interface ScrollListener { |
Michael Kolb | 5ee018e | 2011-02-18 15:47:21 -0800 | [diff] [blame] | 138 | public void onScroll(int visibleTitleHeight, boolean userInitiated); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 141 | @Override |
| 142 | protected void onDraw(android.graphics.Canvas c) { |
Michael Kolb | f262892 | 2011-03-09 17:15:28 -0800 | [diff] [blame] | 143 | super.onDraw(c); |
| 144 | if (!mBackgroundRemoved && getRootView().getBackground() != null) { |
| 145 | mBackgroundRemoved = true; |
| 146 | post(new Runnable() { |
| 147 | public void run() { |
| 148 | getRootView().setBackgroundDrawable(null); |
| 149 | } |
| 150 | }); |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 154 | } |