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