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