blob: 55dd24a7a09a204b1df0eabe933d48d2544be3f7 [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;
Michael Kolb5ee018e2011-02-18 15:47:21 -080023import android.view.MotionEvent;
Michael Kolba2b2ba82010-08-04 17:54:03 -070024import android.view.View;
25import android.webkit.WebView;
26
Michael Kolb0f91e032011-06-01 09:54:20 -070027import com.android.browser.NavTabView.WebProxyView;
28
Michael Kolba2b2ba82010-08-04 17:54:03 -070029import java.util.Map;
30
31/**
Michael Kolb377ea312011-02-17 14:36:56 -080032 * Manage WebView scroll events
Michael Kolba2b2ba82010-08-04 17:54:03 -070033 */
John Reckb9a051b2011-03-18 11:55:48 -070034public class BrowserWebView extends WebView implements Runnable {
Michael Kolba2b2ba82010-08-04 17:54:03 -070035
36 private ScrollListener mScrollListener;
37 private boolean mIsCancelled;
Romain Guy860662a2011-01-10 12:57:22 -080038 private boolean mBackgroundRemoved = false;
Michael Kolb5ee018e2011-02-18 15:47:21 -080039 private boolean mUserInitiated = false;
Michael Kolb29ccf8a2011-02-23 16:13:24 -080040 private TitleBarBase mTitleBar;
Michael Kolba4261fd2011-05-05 11:27:37 -070041 private int mCaptureSize;
Michael Kolb08a687a2011-04-22 16:13:02 -070042 private Bitmap mCapture;
Michael Kolb0f91e032011-06-01 09:54:20 -070043 private WebProxyView mProxyView;
Michael Kolba2b2ba82010-08-04 17:54:03 -070044
45 /**
46 * @param context
47 * @param attrs
48 * @param defStyle
49 * @param javascriptInterfaces
50 */
John Reckb9a051b2011-03-18 11:55:48 -070051 public BrowserWebView(Context context, AttributeSet attrs, int defStyle,
Michael Kolba2b2ba82010-08-04 17:54:03 -070052 Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
53 super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing);
54 }
55
56 /**
57 * @param context
58 * @param attrs
59 * @param defStyle
60 */
John Reckb9a051b2011-03-18 11:55:48 -070061 public BrowserWebView(
Michael Kolb377ea312011-02-17 14:36:56 -080062 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070063 super(context, attrs, defStyle, privateBrowsing);
Michael Kolba4261fd2011-05-05 11:27:37 -070064 init();
Michael Kolba2b2ba82010-08-04 17:54:03 -070065 }
66
67 /**
68 * @param context
69 * @param attrs
70 */
John Reckb9a051b2011-03-18 11:55:48 -070071 public BrowserWebView(Context context, AttributeSet attrs) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070072 super(context, attrs);
Michael Kolba4261fd2011-05-05 11:27:37 -070073 init();
Michael Kolba2b2ba82010-08-04 17:54:03 -070074 }
75
76 /**
77 * @param context
78 */
John Reckb9a051b2011-03-18 11:55:48 -070079 public BrowserWebView(Context context) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070080 super(context);
Michael Kolba4261fd2011-05-05 11:27:37 -070081 init();
Michael Kolba2b2ba82010-08-04 17:54:03 -070082 }
83
Michael Kolba4261fd2011-05-05 11:27:37 -070084 private void init() {
85 mCaptureSize = mContext.getResources().getDimensionPixelSize(R.dimen.tab_capture_size);
86 mCapture = Bitmap.createBitmap(mCaptureSize, mCaptureSize,
87 Bitmap.Config.RGB_565);
Michael Kolb08a687a2011-04-22 16:13:02 -070088 }
89
Michael Kolb0f91e032011-06-01 09:54:20 -070090 protected void setProxyView(WebProxyView p) {
91 mProxyView = p;
92 }
93
94 @Override
95 public void invalidate() {
96 super.invalidate();
97 if (mProxyView != null) {
98 mProxyView.invalidate();
99 }
Michael Kolb2814a362011-05-19 15:49:41 -0700100 }
101
Michael Kolb08a687a2011-04-22 16:13:02 -0700102 @Override
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800103 protected int getTitleHeight() {
104 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
105 }
106
Michael Kolba0e2a752010-12-12 15:27:56 -0800107 // scroll runnable implementation
108 public void run() {
109 if (!mIsCancelled && (mScrollListener != null)) {
Michael Kolb5ee018e2011-02-18 15:47:21 -0800110 mScrollListener.onScroll(getVisibleTitleHeight(), mUserInitiated);
Michael Kolba0e2a752010-12-12 15:27:56 -0800111 }
112 }
113
Michael Kolbed217742010-08-10 17:52:34 -0700114 void hideEmbeddedTitleBar() {
115 scrollBy(0, getVisibleTitleHeight());
116 }
117
Michael Kolba2b2ba82010-08-04 17:54:03 -0700118 @Override
119 public void setEmbeddedTitleBar(final View title) {
120 super.setEmbeddedTitleBar(title);
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800121 mTitleBar = (TitleBarBase) title;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700122 if (title != null && mScrollListener != null) {
123 // allow the scroll listener to initialize its state
Michael Kolba0e2a752010-12-12 15:27:56 -0800124 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700125 }
126 }
127
Michael Kolb377ea312011-02-17 14:36:56 -0800128 public boolean hasTitleBar() {
129 return (mTitleBar != null);
130 }
131
Michael Kolba2b2ba82010-08-04 17:54:03 -0700132 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800133 public boolean onTouchEvent(MotionEvent evt) {
Michael Kolb0f91e032011-06-01 09:54:20 -0700134 if (MotionEvent.ACTION_DOWN == evt.getActionMasked()) {
135 mUserInitiated = true;
136 } else if (MotionEvent.ACTION_UP == evt.getActionMasked()
137 || (MotionEvent.ACTION_CANCEL == evt.getActionMasked())) {
138 mUserInitiated = false;
Michael Kolb5ee018e2011-02-18 15:47:21 -0800139 }
Michael Kolb0f91e032011-06-01 09:54:20 -0700140 return super.onTouchEvent(evt);
Michael Kolb5ee018e2011-02-18 15:47:21 -0800141 }
142
143 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700144 public void stopScroll() {
145 mIsCancelled = true;
146 super.stopScroll();
147 }
148
149 @Override
150 protected void onScrollChanged(int l, final int t, int ol, int ot) {
151 super.onScrollChanged(l, t, ol, ot);
152 if (!mIsCancelled) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800153 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700154 } else {
155 mIsCancelled = false;
156 }
157 }
158
159 void setScrollListener(ScrollListener l) {
160 mScrollListener = l;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700161 }
162
163 // callback for scroll events
164
165 interface ScrollListener {
Michael Kolb5ee018e2011-02-18 15:47:21 -0800166 public void onScroll(int visibleTitleHeight, boolean userInitiated);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700167 }
168
Michael Kolb08a687a2011-04-22 16:13:02 -0700169 protected Bitmap capture() {
170 if (mCapture == null) return null;
171 Canvas c = new Canvas(mCapture);
Michael Kolba4261fd2011-05-05 11:27:37 -0700172 final int left = getScrollX();
173 final int top = getScrollY() + getVisibleTitleHeight();
174 c.translate(-left, -top);
175 float scale = mCaptureSize / (float) Math.max(getWidth(), getHeight());
176 c.scale(scale, scale, left, top);
Michael Kolb08a687a2011-04-22 16:13:02 -0700177 onDraw(c);
178 return mCapture;
179 }
180
Michael Kolb377ea312011-02-17 14:36:56 -0800181 @Override
182 protected void onDraw(android.graphics.Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -0800183 super.onDraw(c);
184 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
185 mBackgroundRemoved = true;
186 post(new Runnable() {
187 public void run() {
188 getRootView().setBackgroundDrawable(null);
189 }
190 });
Michael Kolb377ea312011-02-17 14:36:56 -0800191 }
192 }
193
John Reck378a4102011-06-09 16:23:01 -0700194 @Override
195 protected void updateCachedTextfield(String updatedText) {
196 super.updateCachedTextfield(updatedText);
197 CrashRecoveryHandler handler = CrashRecoveryHandler.getInstance();
198 if (handler != null) {
199 handler.backupState();
200 }
201 }
202
Michael Kolba2b2ba82010-08-04 17:54:03 -0700203}