blob: 51df9582ed300736f11f5736d423f7a5b3534e84 [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 */
Michael Kolba0e2a752010-12-12 15:27:56 -080029public class ScrollWebView extends WebView implements Runnable {
Michael Kolba2b2ba82010-08-04 17:54:03 -070030
31 private ScrollListener mScrollListener;
32 private boolean mIsCancelled;
Michael Kolba2b2ba82010-08-04 17:54:03 -070033
34 /**
35 * @param context
36 * @param attrs
37 * @param defStyle
38 * @param javascriptInterfaces
39 */
40 public ScrollWebView(Context context, AttributeSet attrs, int defStyle,
41 Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
42 super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing);
43 }
44
45 /**
46 * @param context
47 * @param attrs
48 * @param defStyle
49 */
50 public ScrollWebView(Context context, AttributeSet attrs, int defStyle,
51 boolean privateBrowsing) {
52 super(context, attrs, defStyle, privateBrowsing);
53 }
54
55 /**
56 * @param context
57 * @param attrs
58 */
59 public ScrollWebView(Context context, AttributeSet attrs) {
60 super(context, attrs);
61 }
62
63 /**
64 * @param context
65 */
66 public ScrollWebView(Context context) {
67 super(context);
68 }
69
Michael Kolba0e2a752010-12-12 15:27:56 -080070 // scroll runnable implementation
71 public void run() {
72 if (!mIsCancelled && (mScrollListener != null)) {
73 mScrollListener.onScroll(getVisibleTitleHeight());
74 }
75 }
76
Michael Kolbed217742010-08-10 17:52:34 -070077 void hideEmbeddedTitleBar() {
78 scrollBy(0, getVisibleTitleHeight());
79 }
80
Michael Kolba2b2ba82010-08-04 17:54:03 -070081 @Override
82 public void setEmbeddedTitleBar(final View title) {
83 super.setEmbeddedTitleBar(title);
84 if (title != null && mScrollListener != null) {
85 // allow the scroll listener to initialize its state
Michael Kolba0e2a752010-12-12 15:27:56 -080086 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -070087 }
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) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800100 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700101 } else {
102 mIsCancelled = false;
103 }
104 }
105
106 void setScrollListener(ScrollListener l) {
107 mScrollListener = l;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700108 }
109
110 // callback for scroll events
111
112 interface ScrollListener {
Michael Kolba0e2a752010-12-12 15:27:56 -0800113 public void onScroll(int visibleTitleHeight);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700114 }
115
116}