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; |
| 23 | import android.view.View; |
| 24 | import android.webkit.WebView; |
| 25 | |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 26 | import com.android.browser.NavTabView.WebProxyView; |
| 27 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 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 | */ |
Michael Kolb | b14ff2f | 2011-07-01 15:33:56 -0700 | [diff] [blame^] | 33 | public class BrowserWebView extends WebView { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 34 | |
Romain Guy | 860662a | 2011-01-10 12:57:22 -0800 | [diff] [blame] | 35 | private boolean mBackgroundRemoved = false; |
Michael Kolb | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 36 | private TitleBarBase mTitleBar; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 37 | private int mCaptureSize; |
Michael Kolb | 08a687a | 2011-04-22 16:13:02 -0700 | [diff] [blame] | 38 | private Bitmap mCapture; |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 39 | private WebProxyView mProxyView; |
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); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 60 | init(); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param context |
| 65 | * @param attrs |
| 66 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 67 | public BrowserWebView(Context context, AttributeSet attrs) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 68 | super(context, attrs); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 69 | init(); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param context |
| 74 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 75 | public BrowserWebView(Context context) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 76 | super(context); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 77 | init(); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 80 | private void init() { |
| 81 | mCaptureSize = mContext.getResources().getDimensionPixelSize(R.dimen.tab_capture_size); |
| 82 | mCapture = Bitmap.createBitmap(mCaptureSize, mCaptureSize, |
| 83 | Bitmap.Config.RGB_565); |
Michael Kolb | 08a687a | 2011-04-22 16:13:02 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 86 | protected void setProxyView(WebProxyView p) { |
| 87 | mProxyView = p; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void invalidate() { |
| 92 | super.invalidate(); |
| 93 | if (mProxyView != null) { |
| 94 | mProxyView.invalidate(); |
| 95 | } |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 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 | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 103 | void hideEmbeddedTitleBar() { |
| 104 | scrollBy(0, getVisibleTitleHeight()); |
| 105 | } |
| 106 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 107 | @Override |
| 108 | public void setEmbeddedTitleBar(final View title) { |
| 109 | super.setEmbeddedTitleBar(title); |
Michael Kolb | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 110 | mTitleBar = (TitleBarBase) title; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 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 | 08a687a | 2011-04-22 16:13:02 -0700 | [diff] [blame] | 117 | protected Bitmap capture() { |
| 118 | if (mCapture == null) return null; |
| 119 | Canvas c = new Canvas(mCapture); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 120 | final int left = getScrollX(); |
| 121 | final int top = getScrollY() + getVisibleTitleHeight(); |
| 122 | c.translate(-left, -top); |
| 123 | float scale = mCaptureSize / (float) Math.max(getWidth(), getHeight()); |
| 124 | c.scale(scale, scale, left, top); |
Michael Kolb | 08a687a | 2011-04-22 16:13:02 -0700 | [diff] [blame] | 125 | onDraw(c); |
| 126 | return mCapture; |
| 127 | } |
| 128 | |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 129 | @Override |
| 130 | protected void onDraw(android.graphics.Canvas c) { |
Michael Kolb | f262892 | 2011-03-09 17:15:28 -0800 | [diff] [blame] | 131 | super.onDraw(c); |
| 132 | if (!mBackgroundRemoved && getRootView().getBackground() != null) { |
| 133 | mBackgroundRemoved = true; |
| 134 | post(new Runnable() { |
| 135 | public void run() { |
| 136 | getRootView().setBackgroundDrawable(null); |
| 137 | } |
| 138 | }); |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
John Reck | 378a410 | 2011-06-09 16:23:01 -0700 | [diff] [blame] | 142 | @Override |
| 143 | protected void updateCachedTextfield(String updatedText) { |
| 144 | super.updateCachedTextfield(updatedText); |
| 145 | CrashRecoveryHandler handler = CrashRecoveryHandler.getInstance(); |
| 146 | if (handler != null) { |
| 147 | handler.backupState(); |
| 148 | } |
| 149 | } |
| 150 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 151 | } |