blob: a1d8c2d580f63e6803fd1af7bb2a56fc412a8675 [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
27import java.util.Map;
28
29/**
Michael Kolb377ea312011-02-17 14:36:56 -080030 * Manage WebView scroll events
Michael Kolba2b2ba82010-08-04 17:54:03 -070031 */
John Reckb9a051b2011-03-18 11:55:48 -070032public class BrowserWebView extends WebView implements Runnable {
Michael Kolba2b2ba82010-08-04 17:54:03 -070033
34 private ScrollListener mScrollListener;
35 private boolean mIsCancelled;
Romain Guy860662a2011-01-10 12:57:22 -080036 private boolean mBackgroundRemoved = false;
Michael Kolb5ee018e2011-02-18 15:47:21 -080037 private boolean mUserInitiated = false;
Michael Kolb29ccf8a2011-02-23 16:13:24 -080038 private TitleBarBase mTitleBar;
Michael Kolba4261fd2011-05-05 11:27:37 -070039 private int mCaptureSize;
Michael Kolb08a687a2011-04-22 16:13:02 -070040 private Bitmap mCapture;
Michael Kolba2b2ba82010-08-04 17:54:03 -070041
42 /**
43 * @param context
44 * @param attrs
45 * @param defStyle
46 * @param javascriptInterfaces
47 */
John Reckb9a051b2011-03-18 11:55:48 -070048 public BrowserWebView(Context context, AttributeSet attrs, int defStyle,
Michael Kolba2b2ba82010-08-04 17:54:03 -070049 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 Reckb9a051b2011-03-18 11:55:48 -070058 public BrowserWebView(
Michael Kolb377ea312011-02-17 14:36:56 -080059 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070060 super(context, attrs, defStyle, privateBrowsing);
Michael Kolba4261fd2011-05-05 11:27:37 -070061 init();
Michael Kolba2b2ba82010-08-04 17:54:03 -070062 }
63
64 /**
65 * @param context
66 * @param attrs
67 */
John Reckb9a051b2011-03-18 11:55:48 -070068 public BrowserWebView(Context context, AttributeSet attrs) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070069 super(context, attrs);
Michael Kolba4261fd2011-05-05 11:27:37 -070070 init();
Michael Kolba2b2ba82010-08-04 17:54:03 -070071 }
72
73 /**
74 * @param context
75 */
John Reckb9a051b2011-03-18 11:55:48 -070076 public BrowserWebView(Context context) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070077 super(context);
Michael Kolba4261fd2011-05-05 11:27:37 -070078 init();
Michael Kolba2b2ba82010-08-04 17:54:03 -070079 }
80
Michael Kolba4261fd2011-05-05 11:27:37 -070081 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 Kolb08a687a2011-04-22 16:13:02 -070085 }
86
87 @Override
Michael Kolb29ccf8a2011-02-23 16:13:24 -080088 protected int getTitleHeight() {
89 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
90 }
91
Michael Kolba0e2a752010-12-12 15:27:56 -080092 // scroll runnable implementation
93 public void run() {
94 if (!mIsCancelled && (mScrollListener != null)) {
Michael Kolb5ee018e2011-02-18 15:47:21 -080095 mScrollListener.onScroll(getVisibleTitleHeight(), mUserInitiated);
Michael Kolba0e2a752010-12-12 15:27:56 -080096 }
97 }
98
Michael Kolbed217742010-08-10 17:52:34 -070099 void hideEmbeddedTitleBar() {
100 scrollBy(0, getVisibleTitleHeight());
101 }
102
Michael Kolba2b2ba82010-08-04 17:54:03 -0700103 @Override
104 public void setEmbeddedTitleBar(final View title) {
105 super.setEmbeddedTitleBar(title);
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800106 mTitleBar = (TitleBarBase) title;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700107 if (title != null && mScrollListener != null) {
108 // allow the scroll listener to initialize its state
Michael Kolba0e2a752010-12-12 15:27:56 -0800109 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700110 }
111 }
112
Michael Kolb377ea312011-02-17 14:36:56 -0800113 public boolean hasTitleBar() {
114 return (mTitleBar != null);
115 }
116
Michael Kolba2b2ba82010-08-04 17:54:03 -0700117 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800118 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 Kolba2b2ba82010-08-04 17:54:03 -0700129 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 Kolba0e2a752010-12-12 15:27:56 -0800138 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700139 } else {
140 mIsCancelled = false;
141 }
142 }
143
144 void setScrollListener(ScrollListener l) {
145 mScrollListener = l;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700146 }
147
148 // callback for scroll events
149
150 interface ScrollListener {
Michael Kolb5ee018e2011-02-18 15:47:21 -0800151 public void onScroll(int visibleTitleHeight, boolean userInitiated);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700152 }
153
Michael Kolb08a687a2011-04-22 16:13:02 -0700154 protected Bitmap capture() {
155 if (mCapture == null) return null;
156 Canvas c = new Canvas(mCapture);
Michael Kolba4261fd2011-05-05 11:27:37 -0700157 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 Kolb08a687a2011-04-22 16:13:02 -0700162 onDraw(c);
163 return mCapture;
164 }
165
Michael Kolb377ea312011-02-17 14:36:56 -0800166 @Override
167 protected void onDraw(android.graphics.Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -0800168 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 Kolb377ea312011-02-17 14:36:56 -0800176 }
177 }
178
Michael Kolba2b2ba82010-08-04 17:54:03 -0700179}