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; |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.Paint; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 22 | import android.util.AttributeSet; |
Michael Kolb | 5ee018e | 2011-02-18 15:47:21 -0800 | [diff] [blame] | 23 | import android.view.MotionEvent; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 24 | import android.view.View; |
| 25 | import android.webkit.WebView; |
| 26 | |
| 27 | import java.util.Map; |
| 28 | |
| 29 | /** |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 30 | * Manage WebView scroll events |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 31 | */ |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 32 | public class ScrollWebView extends WebView implements Runnable { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 33 | |
| 34 | private ScrollListener mScrollListener; |
| 35 | private boolean mIsCancelled; |
Romain Guy | 860662a | 2011-01-10 12:57:22 -0800 | [diff] [blame] | 36 | private boolean mBackgroundRemoved = false; |
Michael Kolb | 5ee018e | 2011-02-18 15:47:21 -0800 | [diff] [blame] | 37 | private boolean mUserInitiated = false; |
Michael Kolb | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 38 | private TitleBarBase mTitleBar; |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 39 | private boolean mDrawCached = false; |
| 40 | private Bitmap mBitmap; |
| 41 | private Paint mCachePaint = new Paint(); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * @param context |
| 45 | * @param attrs |
| 46 | * @param defStyle |
| 47 | * @param javascriptInterfaces |
| 48 | */ |
| 49 | public ScrollWebView(Context context, AttributeSet attrs, int defStyle, |
| 50 | Map<String, Object> javascriptInterfaces, boolean privateBrowsing) { |
| 51 | super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param context |
| 56 | * @param attrs |
| 57 | * @param defStyle |
| 58 | */ |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 59 | public ScrollWebView( |
| 60 | Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 61 | super(context, attrs, defStyle, privateBrowsing); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param context |
| 66 | * @param attrs |
| 67 | */ |
| 68 | public ScrollWebView(Context context, AttributeSet attrs) { |
| 69 | super(context, attrs); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param context |
| 74 | */ |
| 75 | public ScrollWebView(Context context) { |
| 76 | super(context); |
| 77 | } |
| 78 | |
Michael Kolb | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 79 | @Override |
| 80 | protected int getTitleHeight() { |
| 81 | return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0; |
| 82 | } |
| 83 | |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 84 | // scroll runnable implementation |
| 85 | public void run() { |
| 86 | if (!mIsCancelled && (mScrollListener != null)) { |
Michael Kolb | 5ee018e | 2011-02-18 15:47:21 -0800 | [diff] [blame] | 87 | mScrollListener.onScroll(getVisibleTitleHeight(), mUserInitiated); |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 91 | void hideEmbeddedTitleBar() { |
| 92 | scrollBy(0, getVisibleTitleHeight()); |
| 93 | } |
| 94 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 95 | @Override |
| 96 | public void setEmbeddedTitleBar(final View title) { |
| 97 | super.setEmbeddedTitleBar(title); |
Michael Kolb | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 98 | mTitleBar = (TitleBarBase) title; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 99 | if (title != null && mScrollListener != null) { |
| 100 | // allow the scroll listener to initialize its state |
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 | } |
| 103 | } |
| 104 | |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 105 | public boolean hasTitleBar() { |
| 106 | return (mTitleBar != null); |
| 107 | } |
| 108 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 109 | @Override |
Michael Kolb | 5ee018e | 2011-02-18 15:47:21 -0800 | [diff] [blame] | 110 | public boolean onTouchEvent(MotionEvent evt) { |
| 111 | if (MotionEvent.ACTION_DOWN == evt.getActionMasked()) { |
| 112 | mUserInitiated = true; |
| 113 | } else if (MotionEvent.ACTION_UP == evt.getActionMasked() |
| 114 | || (MotionEvent.ACTION_CANCEL == evt.getActionMasked())) { |
| 115 | mUserInitiated = false; |
| 116 | } |
| 117 | return super.onTouchEvent(evt); |
| 118 | } |
| 119 | |
| 120 | @Override |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 121 | public void stopScroll() { |
| 122 | mIsCancelled = true; |
| 123 | super.stopScroll(); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | protected void onScrollChanged(int l, final int t, int ol, int ot) { |
| 128 | super.onScrollChanged(l, t, ol, ot); |
| 129 | if (!mIsCancelled) { |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 130 | post(this); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 131 | } else { |
| 132 | mIsCancelled = false; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void setScrollListener(ScrollListener l) { |
| 137 | mScrollListener = l; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 140 | @Override |
| 141 | public void invalidate() { |
| 142 | if (!mDrawCached) { |
| 143 | super.invalidate(); |
| 144 | } |
| 145 | } |
| 146 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 147 | // callback for scroll events |
| 148 | |
| 149 | interface ScrollListener { |
Michael Kolb | 5ee018e | 2011-02-18 15:47:21 -0800 | [diff] [blame] | 150 | public void onScroll(int visibleTitleHeight, boolean userInitiated); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 153 | void setDrawCached(boolean cached) { |
Michael Kolb | d72f159 | 2011-03-03 16:30:25 -0800 | [diff] [blame] | 154 | if (cached == mDrawCached) return; |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 155 | if (cached) { |
| 156 | buildDrawingCache(); |
| 157 | mBitmap = getDrawingCache(false); |
| 158 | mDrawCached = (mBitmap != null); |
| 159 | } else { |
Michael Kolb | d72f159 | 2011-03-03 16:30:25 -0800 | [diff] [blame] | 160 | mDrawCached = false; |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 161 | mBitmap = null; |
| 162 | destroyDrawingCache(); |
Romain Guy | 860662a | 2011-01-10 12:57:22 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 165 | |
| 166 | @Override |
| 167 | protected void onDraw(android.graphics.Canvas c) { |
| 168 | if (mDrawCached) { |
| 169 | c.drawBitmap(mBitmap, getScrollX(), getScrollY(), mCachePaint); |
| 170 | } else { |
| 171 | super.onDraw(c); |
| 172 | if (!mBackgroundRemoved && getRootView().getBackground() != null) { |
| 173 | mBackgroundRemoved = true; |
| 174 | post(new Runnable() { |
| 175 | public void run() { |
| 176 | getRootView().setBackgroundDrawable(null); |
| 177 | } |
| 178 | }); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 183 | } |