blob: 97bd2c7a1d281f61443ae168de433464393575a9 [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
Michael Kolbed217742010-08-10 17:52:34 -070071 void hideEmbeddedTitleBar() {
72 scrollBy(0, getVisibleTitleHeight());
73 }
74
Michael Kolba2b2ba82010-08-04 17:54:03 -070075 @Override
76 public void setEmbeddedTitleBar(final View title) {
77 super.setEmbeddedTitleBar(title);
78 if (title != null && mScrollListener != null) {
79 // allow the scroll listener to initialize its state
80 post(new Runnable() {
81 @Override
82 public void run() {
83 mScrollListener.onScroll((title.getHeight() == 0 ||
84 getVisibleTitleHeight() > 0));
85 }
86 });
87 }
88 }
89
90 @Override
91 public void stopScroll() {
92 mIsCancelled = true;
93 super.stopScroll();
94 }
95
96 @Override
97 protected void onScrollChanged(int l, final int t, int ol, int ot) {
98 super.onScrollChanged(l, t, ol, ot);
99 if (!mIsCancelled) {
100 post(mScrollRunnable);
101 } else {
102 mIsCancelled = false;
103 }
104 }
105
106 void setScrollListener(ScrollListener l) {
107 mScrollListener = l;
108 if (mScrollListener != null) {
109 mScrollRunnable = new Runnable() {
110 public void run() {
111 if (!mIsCancelled) {
112 mScrollListener.onScroll(getVisibleTitleHeight() > 0);
113 }
114 }
115 };
116 }
117 }
118
119 // callback for scroll events
120
121 interface ScrollListener {
122 public void onScroll(boolean titleVisible);
123 }
124
125}