blob: 1d7f23a4c2ecfad1b95f05268d8dcc32a7aa864b [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 Kolba2b2ba82010-08-04 17:54:03 -070036
37 /**
38 * @param context
39 * @param attrs
40 * @param defStyle
41 * @param javascriptInterfaces
42 */
43 public ScrollWebView(Context context, AttributeSet attrs, int defStyle,
44 Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
45 super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing);
46 }
47
48 /**
49 * @param context
50 * @param attrs
51 * @param defStyle
52 */
53 public ScrollWebView(Context context, AttributeSet attrs, int defStyle,
54 boolean privateBrowsing) {
55 super(context, attrs, defStyle, privateBrowsing);
56 }
57
58 /**
59 * @param context
60 * @param attrs
61 */
62 public ScrollWebView(Context context, AttributeSet attrs) {
63 super(context, attrs);
64 }
65
66 /**
67 * @param context
68 */
69 public ScrollWebView(Context context) {
70 super(context);
71 }
72
Michael Kolba0e2a752010-12-12 15:27:56 -080073 // scroll runnable implementation
74 public void run() {
75 if (!mIsCancelled && (mScrollListener != null)) {
Michael Kolb5ee018e2011-02-18 15:47:21 -080076 mScrollListener.onScroll(getVisibleTitleHeight(), mUserInitiated);
Michael Kolba0e2a752010-12-12 15:27:56 -080077 }
78 }
79
Michael Kolbed217742010-08-10 17:52:34 -070080 void hideEmbeddedTitleBar() {
81 scrollBy(0, getVisibleTitleHeight());
82 }
83
Michael Kolba2b2ba82010-08-04 17:54:03 -070084 @Override
85 public void setEmbeddedTitleBar(final View title) {
86 super.setEmbeddedTitleBar(title);
87 if (title != null && mScrollListener != null) {
88 // allow the scroll listener to initialize its state
Michael Kolba0e2a752010-12-12 15:27:56 -080089 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -070090 }
91 }
92
93 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -080094 public boolean onTouchEvent(MotionEvent evt) {
95 if (MotionEvent.ACTION_DOWN == evt.getActionMasked()) {
96 mUserInitiated = true;
97 } else if (MotionEvent.ACTION_UP == evt.getActionMasked()
98 || (MotionEvent.ACTION_CANCEL == evt.getActionMasked())) {
99 mUserInitiated = false;
100 }
101 return super.onTouchEvent(evt);
102 }
103
104 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700105 public void stopScroll() {
106 mIsCancelled = true;
107 super.stopScroll();
108 }
109
110 @Override
111 protected void onScrollChanged(int l, final int t, int ol, int ot) {
112 super.onScrollChanged(l, t, ol, ot);
113 if (!mIsCancelled) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800114 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700115 } else {
116 mIsCancelled = false;
117 }
118 }
119
120 void setScrollListener(ScrollListener l) {
121 mScrollListener = l;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700122 }
123
124 // callback for scroll events
125
126 interface ScrollListener {
Michael Kolb5ee018e2011-02-18 15:47:21 -0800127 public void onScroll(int visibleTitleHeight, boolean userInitiated);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700128 }
129
Romain Guy860662a2011-01-10 12:57:22 -0800130 @Override
131 protected void onDraw(android.graphics.Canvas c) {
132 super.onDraw(c);
133 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
134 mBackgroundRemoved = true;
135 post(new Runnable() {
136 public void run() {
137 getRootView().setBackgroundDrawable(null);
138 }
139 });
140 }
141 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700142}