blob: 16404aa83828e95dec1df0e6c8a1d11e3c6f0ebc [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;
21import android.view.View;
22import android.webkit.WebView;
23
24import java.util.Map;
25
26/**
27 * Manage WebView scroll events
28 */
29public class ScrollWebView extends WebView {
30
31 private ScrollListener mScrollListener;
32 private boolean mIsCancelled;
33 private Runnable mScrollRunnable;
34
35 /**
36 * @param context
37 * @param attrs
38 * @param defStyle
39 * @param javascriptInterfaces
40 */
41 public ScrollWebView(Context context, AttributeSet attrs, int defStyle,
42 Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
43 super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing);
44 }
45
46 /**
47 * @param context
48 * @param attrs
49 * @param defStyle
50 */
51 public ScrollWebView(Context context, AttributeSet attrs, int defStyle,
52 boolean privateBrowsing) {
53 super(context, attrs, defStyle, privateBrowsing);
54 }
55
56 /**
57 * @param context
58 * @param attrs
59 */
60 public ScrollWebView(Context context, AttributeSet attrs) {
61 super(context, attrs);
62 }
63
64 /**
65 * @param context
66 */
67 public ScrollWebView(Context context) {
68 super(context);
69 }
70
71 @Override
72 public void setEmbeddedTitleBar(final View title) {
73 super.setEmbeddedTitleBar(title);
74 if (title != null && mScrollListener != null) {
75 // allow the scroll listener to initialize its state
76 post(new Runnable() {
77 @Override
78 public void run() {
79 mScrollListener.onScroll((title.getHeight() == 0 ||
80 getVisibleTitleHeight() > 0));
81 }
82 });
83 }
84 }
85
86 @Override
87 public void stopScroll() {
88 mIsCancelled = true;
89 super.stopScroll();
90 }
91
92 @Override
93 protected void onScrollChanged(int l, final int t, int ol, int ot) {
94 super.onScrollChanged(l, t, ol, ot);
95 if (!mIsCancelled) {
96 post(mScrollRunnable);
97 } else {
98 mIsCancelled = false;
99 }
100 }
101
102 void setScrollListener(ScrollListener l) {
103 mScrollListener = l;
104 if (mScrollListener != null) {
105 mScrollRunnable = new Runnable() {
106 public void run() {
107 if (!mIsCancelled) {
108 mScrollListener.onScroll(getVisibleTitleHeight() > 0);
109 }
110 }
111 };
112 }
113 }
114
115 // callback for scroll events
116
117 interface ScrollListener {
118 public void onScroll(boolean titleVisible);
119 }
120
121}