blob: e2ef902ff6f4e7dddb9270c74df3bb87ddac88a5 [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;
Romain Guy860662a2011-01-10 12:57:22 -080033 private boolean mBackgroundRemoved = false;
Michael Kolba2b2ba82010-08-04 17:54:03 -070034
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 Kolba0e2a752010-12-12 15:27:56 -080071 // scroll runnable implementation
72 public void run() {
73 if (!mIsCancelled && (mScrollListener != null)) {
74 mScrollListener.onScroll(getVisibleTitleHeight());
75 }
76 }
77
Michael Kolbed217742010-08-10 17:52:34 -070078 void hideEmbeddedTitleBar() {
79 scrollBy(0, getVisibleTitleHeight());
80 }
81
Michael Kolba2b2ba82010-08-04 17:54:03 -070082 @Override
83 public void setEmbeddedTitleBar(final View title) {
84 super.setEmbeddedTitleBar(title);
85 if (title != null && mScrollListener != null) {
86 // allow the scroll listener to initialize its state
Michael Kolba0e2a752010-12-12 15:27:56 -080087 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -070088 }
89 }
90
91 @Override
92 public void stopScroll() {
93 mIsCancelled = true;
94 super.stopScroll();
95 }
96
97 @Override
98 protected void onScrollChanged(int l, final int t, int ol, int ot) {
99 super.onScrollChanged(l, t, ol, ot);
100 if (!mIsCancelled) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800101 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700102 } else {
103 mIsCancelled = false;
104 }
105 }
106
107 void setScrollListener(ScrollListener l) {
108 mScrollListener = l;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700109 }
110
111 // callback for scroll events
112
113 interface ScrollListener {
Michael Kolba0e2a752010-12-12 15:27:56 -0800114 public void onScroll(int visibleTitleHeight);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700115 }
116
Romain Guy860662a2011-01-10 12:57:22 -0800117 @Override
118 protected void onDraw(android.graphics.Canvas c) {
119 super.onDraw(c);
120 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
121 mBackgroundRemoved = true;
122 post(new Runnable() {
123 public void run() {
124 getRootView().setBackgroundDrawable(null);
125 }
126 });
127 }
128 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700129}