blob: 2bf07e1299b52703b8473e8da88b22726b636a33 [file] [log] [blame]
Michael Kolba2b2ba82010-08-04 17:54:03 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
19import android.content.Context;
20import android.util.AttributeSet;
Michael Kolb5ee018e2011-02-18 15:47:21 -080021import android.view.MotionEvent;
Michael Kolba2b2ba82010-08-04 17:54:03 -070022import android.view.View;
23import android.webkit.WebView;
24
25import java.util.Map;
26
27/**
28 * Manage WebView scroll events
29 */
Michael Kolba0e2a752010-12-12 15:27:56 -080030public class ScrollWebView extends WebView implements Runnable {
Michael Kolba2b2ba82010-08-04 17:54:03 -070031
32 private ScrollListener mScrollListener;
33 private boolean mIsCancelled;
Romain Guy860662a2011-01-10 12:57:22 -080034 private boolean mBackgroundRemoved = false;
Michael Kolb5ee018e2011-02-18 15:47:21 -080035 private boolean mUserInitiated = false;
Michael Kolb29ccf8a2011-02-23 16:13:24 -080036 private TitleBarBase mTitleBar;
Michael Kolba2b2ba82010-08-04 17:54:03 -070037
38 /**
39 * @param context
40 * @param attrs
41 * @param defStyle
42 * @param javascriptInterfaces
43 */
44 public ScrollWebView(Context context, AttributeSet attrs, int defStyle,
45 Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
46 super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing);
47 }
48
49 /**
50 * @param context
51 * @param attrs
52 * @param defStyle
53 */
54 public ScrollWebView(Context context, AttributeSet attrs, int defStyle,
55 boolean privateBrowsing) {
56 super(context, attrs, defStyle, privateBrowsing);
57 }
58
59 /**
60 * @param context
61 * @param attrs
62 */
63 public ScrollWebView(Context context, AttributeSet attrs) {
64 super(context, attrs);
65 }
66
67 /**
68 * @param context
69 */
70 public ScrollWebView(Context context) {
71 super(context);
72 }
73
Michael Kolb29ccf8a2011-02-23 16:13:24 -080074 @Override
75 protected int getTitleHeight() {
76 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
77 }
78
Michael Kolba0e2a752010-12-12 15:27:56 -080079 // scroll runnable implementation
80 public void run() {
81 if (!mIsCancelled && (mScrollListener != null)) {
Michael Kolb5ee018e2011-02-18 15:47:21 -080082 mScrollListener.onScroll(getVisibleTitleHeight(), mUserInitiated);
Michael Kolba0e2a752010-12-12 15:27:56 -080083 }
84 }
85
Michael Kolbed217742010-08-10 17:52:34 -070086 void hideEmbeddedTitleBar() {
87 scrollBy(0, getVisibleTitleHeight());
88 }
89
Michael Kolba2b2ba82010-08-04 17:54:03 -070090 @Override
91 public void setEmbeddedTitleBar(final View title) {
92 super.setEmbeddedTitleBar(title);
Michael Kolb29ccf8a2011-02-23 16:13:24 -080093 mTitleBar = (TitleBarBase) title;
Michael Kolba2b2ba82010-08-04 17:54:03 -070094 if (title != null && mScrollListener != null) {
95 // allow the scroll listener to initialize its state
Michael Kolba0e2a752010-12-12 15:27:56 -080096 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -070097 }
98 }
99
100 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800101 public boolean onTouchEvent(MotionEvent evt) {
102 if (MotionEvent.ACTION_DOWN == evt.getActionMasked()) {
103 mUserInitiated = true;
104 } else if (MotionEvent.ACTION_UP == evt.getActionMasked()
105 || (MotionEvent.ACTION_CANCEL == evt.getActionMasked())) {
106 mUserInitiated = false;
107 }
108 return super.onTouchEvent(evt);
109 }
110
111 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700112 public void stopScroll() {
113 mIsCancelled = true;
114 super.stopScroll();
115 }
116
117 @Override
118 protected void onScrollChanged(int l, final int t, int ol, int ot) {
119 super.onScrollChanged(l, t, ol, ot);
120 if (!mIsCancelled) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800121 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700122 } else {
123 mIsCancelled = false;
124 }
125 }
126
127 void setScrollListener(ScrollListener l) {
128 mScrollListener = l;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700129 }
130
131 // callback for scroll events
132
133 interface ScrollListener {
Michael Kolb5ee018e2011-02-18 15:47:21 -0800134 public void onScroll(int visibleTitleHeight, boolean userInitiated);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700135 }
136
Romain Guy860662a2011-01-10 12:57:22 -0800137 @Override
138 protected void onDraw(android.graphics.Canvas c) {
139 super.onDraw(c);
140 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
141 mBackgroundRemoved = true;
142 post(new Runnable() {
143 public void run() {
144 getRootView().setBackgroundDrawable(null);
145 }
146 });
147 }
148 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700149}