blob: 80f4a5384f0e28660ada442f3c9826ef1af4eec2 [file] [log] [blame]
Michael Kolba2b2ba82010-08-04 17:54:03 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
Michael Kolb377ea312011-02-17 14:36:56 -08004 * 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 Kolba2b2ba82010-08-04 17:54:03 -07007 *
Michael Kolb377ea312011-02-17 14:36:56 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Michael Kolba2b2ba82010-08-04 17:54:03 -07009 *
10 * Unless required by applicable law or agreed to in writing, software
Michael Kolb377ea312011-02-17 14:36:56 -080011 * 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 Kolba2b2ba82010-08-04 17:54:03 -070015 */
16
17package com.android.browser;
18
19import android.content.Context;
Michael Kolb08a687a2011-04-22 16:13:02 -070020import android.graphics.Bitmap;
21import android.graphics.Canvas;
Michael Kolba2b2ba82010-08-04 17:54:03 -070022import android.util.AttributeSet;
23import android.view.View;
24import android.webkit.WebView;
25
Michael Kolb0f91e032011-06-01 09:54:20 -070026import com.android.browser.NavTabView.WebProxyView;
27
Michael Kolba2b2ba82010-08-04 17:54:03 -070028import java.util.Map;
29
30/**
Michael Kolb377ea312011-02-17 14:36:56 -080031 * Manage WebView scroll events
Michael Kolba2b2ba82010-08-04 17:54:03 -070032 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070033public class BrowserWebView extends WebView {
Michael Kolba2b2ba82010-08-04 17:54:03 -070034
Romain Guy860662a2011-01-10 12:57:22 -080035 private boolean mBackgroundRemoved = false;
Michael Kolb29ccf8a2011-02-23 16:13:24 -080036 private TitleBarBase mTitleBar;
Michael Kolba4261fd2011-05-05 11:27:37 -070037 private int mCaptureSize;
Michael Kolb08a687a2011-04-22 16:13:02 -070038 private Bitmap mCapture;
Michael Kolb0f91e032011-06-01 09:54:20 -070039 private WebProxyView mProxyView;
Michael Kolba2b2ba82010-08-04 17:54:03 -070040
41 /**
42 * @param context
43 * @param attrs
44 * @param defStyle
45 * @param javascriptInterfaces
46 */
John Reckb9a051b2011-03-18 11:55:48 -070047 public BrowserWebView(Context context, AttributeSet attrs, int defStyle,
Michael Kolba2b2ba82010-08-04 17:54:03 -070048 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 Reckb9a051b2011-03-18 11:55:48 -070057 public BrowserWebView(
Michael Kolb377ea312011-02-17 14:36:56 -080058 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070059 super(context, attrs, defStyle, privateBrowsing);
Michael Kolba4261fd2011-05-05 11:27:37 -070060 init();
Michael Kolba2b2ba82010-08-04 17:54:03 -070061 }
62
63 /**
64 * @param context
65 * @param attrs
66 */
John Reckb9a051b2011-03-18 11:55:48 -070067 public BrowserWebView(Context context, AttributeSet attrs) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070068 super(context, attrs);
Michael Kolba4261fd2011-05-05 11:27:37 -070069 init();
Michael Kolba2b2ba82010-08-04 17:54:03 -070070 }
71
72 /**
73 * @param context
74 */
John Reckb9a051b2011-03-18 11:55:48 -070075 public BrowserWebView(Context context) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070076 super(context);
Michael Kolba4261fd2011-05-05 11:27:37 -070077 init();
Michael Kolba2b2ba82010-08-04 17:54:03 -070078 }
79
Michael Kolba4261fd2011-05-05 11:27:37 -070080 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 Kolb08a687a2011-04-22 16:13:02 -070084 }
85
Michael Kolb0f91e032011-06-01 09:54:20 -070086 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 Kolb2814a362011-05-19 15:49:41 -070096 }
97
Michael Kolb08a687a2011-04-22 16:13:02 -070098 @Override
Michael Kolb29ccf8a2011-02-23 16:13:24 -080099 protected int getTitleHeight() {
100 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
101 }
102
Michael Kolbed217742010-08-10 17:52:34 -0700103 void hideEmbeddedTitleBar() {
104 scrollBy(0, getVisibleTitleHeight());
105 }
106
Michael Kolba2b2ba82010-08-04 17:54:03 -0700107 @Override
108 public void setEmbeddedTitleBar(final View title) {
109 super.setEmbeddedTitleBar(title);
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800110 mTitleBar = (TitleBarBase) title;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700111 }
112
Michael Kolb377ea312011-02-17 14:36:56 -0800113 public boolean hasTitleBar() {
114 return (mTitleBar != null);
115 }
116
Michael Kolb08a687a2011-04-22 16:13:02 -0700117 protected Bitmap capture() {
118 if (mCapture == null) return null;
119 Canvas c = new Canvas(mCapture);
Michael Kolba4261fd2011-05-05 11:27:37 -0700120 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 Kolb08a687a2011-04-22 16:13:02 -0700125 onDraw(c);
126 return mCapture;
127 }
128
Michael Kolb377ea312011-02-17 14:36:56 -0800129 @Override
130 protected void onDraw(android.graphics.Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -0800131 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 Kolb377ea312011-02-17 14:36:56 -0800139 }
140 }
141
John Reck378a4102011-06-09 16:23:01 -0700142 @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 Kolba2b2ba82010-08-04 17:54:03 -0700151}