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