blob: d1dc25bcaff75a9a36008204e230204b8a10285f [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 Kolb377ea312011-02-17 14:36:56 -080020import android.graphics.Bitmap;
21import android.graphics.Paint;
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 */
Michael Kolba0e2a752010-12-12 15:27:56 -080032public class ScrollWebView 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 Kolb377ea312011-02-17 14:36:56 -080039 private boolean mDrawCached = false;
40 private Bitmap mBitmap;
41 private Paint mCachePaint = new Paint();
Michael Kolba2b2ba82010-08-04 17:54:03 -070042
43 /**
44 * @param context
45 * @param attrs
46 * @param defStyle
47 * @param javascriptInterfaces
48 */
49 public ScrollWebView(Context context, AttributeSet attrs, int defStyle,
50 Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
51 super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing);
52 }
53
54 /**
55 * @param context
56 * @param attrs
57 * @param defStyle
58 */
Michael Kolb377ea312011-02-17 14:36:56 -080059 public ScrollWebView(
60 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070061 super(context, attrs, defStyle, privateBrowsing);
62 }
63
64 /**
65 * @param context
66 * @param attrs
67 */
68 public ScrollWebView(Context context, AttributeSet attrs) {
69 super(context, attrs);
70 }
71
72 /**
73 * @param context
74 */
75 public ScrollWebView(Context context) {
76 super(context);
77 }
78
Michael Kolb29ccf8a2011-02-23 16:13:24 -080079 @Override
80 protected int getTitleHeight() {
81 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
82 }
83
Michael Kolba0e2a752010-12-12 15:27:56 -080084 // scroll runnable implementation
85 public void run() {
86 if (!mIsCancelled && (mScrollListener != null)) {
Michael Kolb5ee018e2011-02-18 15:47:21 -080087 mScrollListener.onScroll(getVisibleTitleHeight(), mUserInitiated);
Michael Kolba0e2a752010-12-12 15:27:56 -080088 }
89 }
90
Michael Kolbed217742010-08-10 17:52:34 -070091 void hideEmbeddedTitleBar() {
92 scrollBy(0, getVisibleTitleHeight());
93 }
94
Michael Kolba2b2ba82010-08-04 17:54:03 -070095 @Override
96 public void setEmbeddedTitleBar(final View title) {
97 super.setEmbeddedTitleBar(title);
Michael Kolb29ccf8a2011-02-23 16:13:24 -080098 mTitleBar = (TitleBarBase) title;
Michael Kolba2b2ba82010-08-04 17:54:03 -070099 if (title != null && mScrollListener != null) {
100 // allow the scroll listener to initialize its state
Michael Kolba0e2a752010-12-12 15:27:56 -0800101 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700102 }
103 }
104
Michael Kolb377ea312011-02-17 14:36:56 -0800105 public boolean hasTitleBar() {
106 return (mTitleBar != null);
107 }
108
Michael Kolba2b2ba82010-08-04 17:54:03 -0700109 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800110 public boolean onTouchEvent(MotionEvent evt) {
111 if (MotionEvent.ACTION_DOWN == evt.getActionMasked()) {
112 mUserInitiated = true;
113 } else if (MotionEvent.ACTION_UP == evt.getActionMasked()
114 || (MotionEvent.ACTION_CANCEL == evt.getActionMasked())) {
115 mUserInitiated = false;
116 }
117 return super.onTouchEvent(evt);
118 }
119
120 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700121 public void stopScroll() {
122 mIsCancelled = true;
123 super.stopScroll();
124 }
125
126 @Override
127 protected void onScrollChanged(int l, final int t, int ol, int ot) {
128 super.onScrollChanged(l, t, ol, ot);
129 if (!mIsCancelled) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800130 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700131 } else {
132 mIsCancelled = false;
133 }
134 }
135
136 void setScrollListener(ScrollListener l) {
137 mScrollListener = l;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700138 }
139
Michael Kolb377ea312011-02-17 14:36:56 -0800140 @Override
141 public void invalidate() {
142 if (!mDrawCached) {
143 super.invalidate();
144 }
145 }
146
Michael Kolba2b2ba82010-08-04 17:54:03 -0700147 // callback for scroll events
148
149 interface ScrollListener {
Michael Kolb5ee018e2011-02-18 15:47:21 -0800150 public void onScroll(int visibleTitleHeight, boolean userInitiated);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700151 }
152
Michael Kolb377ea312011-02-17 14:36:56 -0800153 void setDrawCached(boolean cached) {
154 if (cached) {
155 buildDrawingCache();
156 mBitmap = getDrawingCache(false);
157 mDrawCached = (mBitmap != null);
158 } else {
159 mBitmap = null;
160 destroyDrawingCache();
161 mDrawCached = false;
Romain Guy860662a2011-01-10 12:57:22 -0800162 }
163 }
Michael Kolb377ea312011-02-17 14:36:56 -0800164
165 @Override
166 protected void onDraw(android.graphics.Canvas c) {
167 if (mDrawCached) {
168 c.drawBitmap(mBitmap, getScrollX(), getScrollY(), mCachePaint);
169 } else {
170 super.onDraw(c);
171 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
172 mBackgroundRemoved = true;
173 post(new Runnable() {
174 public void run() {
175 getRootView().setBackgroundDrawable(null);
176 }
177 });
178 }
179 }
180 }
181
Michael Kolba2b2ba82010-08-04 17:54:03 -0700182}