blob: 9be9ad9b0aa8b1f80bab9584ddc85c3747dd4708 [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 Kolb08a687a2011-04-22 16:13:02 -070039 private Bitmap mCapture;
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);
60 }
61
62 /**
63 * @param context
64 * @param attrs
65 */
John Reckb9a051b2011-03-18 11:55:48 -070066 public BrowserWebView(Context context, AttributeSet attrs) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070067 super(context, attrs);
68 }
69
70 /**
71 * @param context
72 */
John Reckb9a051b2011-03-18 11:55:48 -070073 public BrowserWebView(Context context) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070074 super(context);
75 }
76
Michael Kolb29ccf8a2011-02-23 16:13:24 -080077 @Override
Michael Kolb08a687a2011-04-22 16:13:02 -070078 protected void onSizeChanged(int w, int h, int ow, int oh) {
79 super.onSizeChanged(w, h, ow, oh);
80 mCapture = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
81 }
82
83 @Override
Michael Kolb29ccf8a2011-02-23 16:13:24 -080084 protected int getTitleHeight() {
85 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
86 }
87
Michael Kolba0e2a752010-12-12 15:27:56 -080088 // scroll runnable implementation
89 public void run() {
90 if (!mIsCancelled && (mScrollListener != null)) {
Michael Kolb5ee018e2011-02-18 15:47:21 -080091 mScrollListener.onScroll(getVisibleTitleHeight(), mUserInitiated);
Michael Kolba0e2a752010-12-12 15:27:56 -080092 }
93 }
94
Michael Kolbed217742010-08-10 17:52:34 -070095 void hideEmbeddedTitleBar() {
96 scrollBy(0, getVisibleTitleHeight());
97 }
98
Michael Kolba2b2ba82010-08-04 17:54:03 -070099 @Override
100 public void setEmbeddedTitleBar(final View title) {
101 super.setEmbeddedTitleBar(title);
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800102 mTitleBar = (TitleBarBase) title;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700103 if (title != null && mScrollListener != null) {
104 // allow the scroll listener to initialize its state
Michael Kolba0e2a752010-12-12 15:27:56 -0800105 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700106 }
107 }
108
Michael Kolb377ea312011-02-17 14:36:56 -0800109 public boolean hasTitleBar() {
110 return (mTitleBar != null);
111 }
112
Michael Kolba2b2ba82010-08-04 17:54:03 -0700113 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800114 public boolean onTouchEvent(MotionEvent evt) {
115 if (MotionEvent.ACTION_DOWN == evt.getActionMasked()) {
116 mUserInitiated = true;
117 } else if (MotionEvent.ACTION_UP == evt.getActionMasked()
118 || (MotionEvent.ACTION_CANCEL == evt.getActionMasked())) {
119 mUserInitiated = false;
120 }
121 return super.onTouchEvent(evt);
122 }
123
124 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700125 public void stopScroll() {
126 mIsCancelled = true;
127 super.stopScroll();
128 }
129
130 @Override
131 protected void onScrollChanged(int l, final int t, int ol, int ot) {
132 super.onScrollChanged(l, t, ol, ot);
133 if (!mIsCancelled) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800134 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700135 } else {
136 mIsCancelled = false;
137 }
138 }
139
140 void setScrollListener(ScrollListener l) {
141 mScrollListener = l;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700142 }
143
144 // callback for scroll events
145
146 interface ScrollListener {
Michael Kolb5ee018e2011-02-18 15:47:21 -0800147 public void onScroll(int visibleTitleHeight, boolean userInitiated);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700148 }
149
Michael Kolb08a687a2011-04-22 16:13:02 -0700150 protected Bitmap capture() {
151 if (mCapture == null) return null;
152 Canvas c = new Canvas(mCapture);
153 c.translate(-getScrollX(), -(getScrollY() + getVisibleTitleHeight()));
154 onDraw(c);
155 return mCapture;
156 }
157
Michael Kolb377ea312011-02-17 14:36:56 -0800158 @Override
159 protected void onDraw(android.graphics.Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -0800160 super.onDraw(c);
161 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
162 mBackgroundRemoved = true;
163 post(new Runnable() {
164 public void run() {
165 getRootView().setBackgroundDrawable(null);
166 }
167 });
Michael Kolb377ea312011-02-17 14:36:56 -0800168 }
169 }
170
Michael Kolba2b2ba82010-08-04 17:54:03 -0700171}