blob: 8c89e51fd14755120bc95499f8fdeec996bec5f9 [file] [log] [blame]
Michael Kolba2b2ba82010-08-04 17:54:03 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
Michael Kolb377ea312011-02-17 14:36:56 -08004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
Michael Kolba2b2ba82010-08-04 17:54:03 -07007 *
Michael Kolb377ea312011-02-17 14:36:56 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Michael Kolba2b2ba82010-08-04 17:54:03 -07009 *
10 * Unless required by applicable law or agreed to in writing, software
Michael Kolb377ea312011-02-17 14:36:56 -080011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
Michael Kolba2b2ba82010-08-04 17:54:03 -070015 */
16
17package com.android.browser;
18
19import android.content.Context;
Michael Kolb377ea312011-02-17 14:36:56 -080020import android.graphics.Bitmap;
Michael Kolba2b2ba82010-08-04 17:54:03 -070021import android.util.AttributeSet;
Michael Kolb5ee018e2011-02-18 15:47:21 -080022import android.view.MotionEvent;
Michael Kolba2b2ba82010-08-04 17:54:03 -070023import android.view.View;
24import android.webkit.WebView;
25
26import java.util.Map;
27
28/**
Michael Kolb377ea312011-02-17 14:36:56 -080029 * Manage WebView scroll events
Michael Kolba2b2ba82010-08-04 17:54:03 -070030 */
Michael Kolba0e2a752010-12-12 15:27:56 -080031public class ScrollWebView extends WebView implements Runnable {
Michael Kolba2b2ba82010-08-04 17:54:03 -070032
33 private ScrollListener mScrollListener;
34 private boolean mIsCancelled;
Romain Guy860662a2011-01-10 12:57:22 -080035 private boolean mBackgroundRemoved = false;
Michael Kolb5ee018e2011-02-18 15:47:21 -080036 private boolean mUserInitiated = false;
Michael Kolb29ccf8a2011-02-23 16:13:24 -080037 private TitleBarBase mTitleBar;
Michael Kolb377ea312011-02-17 14:36:56 -080038 private Bitmap mBitmap;
Michael Kolba2b2ba82010-08-04 17:54:03 -070039
40 /**
41 * @param context
42 * @param attrs
43 * @param defStyle
44 * @param javascriptInterfaces
45 */
46 public ScrollWebView(Context context, AttributeSet attrs, int defStyle,
47 Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
48 super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing);
49 }
50
51 /**
52 * @param context
53 * @param attrs
54 * @param defStyle
55 */
Michael Kolb377ea312011-02-17 14:36:56 -080056 public ScrollWebView(
57 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070058 super(context, attrs, defStyle, privateBrowsing);
59 }
60
61 /**
62 * @param context
63 * @param attrs
64 */
65 public ScrollWebView(Context context, AttributeSet attrs) {
66 super(context, attrs);
67 }
68
69 /**
70 * @param context
71 */
72 public ScrollWebView(Context context) {
73 super(context);
74 }
75
Michael Kolb29ccf8a2011-02-23 16:13:24 -080076 @Override
77 protected int getTitleHeight() {
78 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
79 }
80
Michael Kolba0e2a752010-12-12 15:27:56 -080081 // scroll runnable implementation
82 public void run() {
83 if (!mIsCancelled && (mScrollListener != null)) {
Michael Kolb5ee018e2011-02-18 15:47:21 -080084 mScrollListener.onScroll(getVisibleTitleHeight(), mUserInitiated);
Michael Kolba0e2a752010-12-12 15:27:56 -080085 }
86 }
87
Michael Kolbed217742010-08-10 17:52:34 -070088 void hideEmbeddedTitleBar() {
89 scrollBy(0, getVisibleTitleHeight());
90 }
91
Michael Kolba2b2ba82010-08-04 17:54:03 -070092 @Override
93 public void setEmbeddedTitleBar(final View title) {
94 super.setEmbeddedTitleBar(title);
Michael Kolb29ccf8a2011-02-23 16:13:24 -080095 mTitleBar = (TitleBarBase) title;
Michael Kolba2b2ba82010-08-04 17:54:03 -070096 if (title != null && mScrollListener != null) {
97 // allow the scroll listener to initialize its state
Michael Kolba0e2a752010-12-12 15:27:56 -080098 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -070099 }
100 }
101
Michael Kolb377ea312011-02-17 14:36:56 -0800102 public boolean hasTitleBar() {
103 return (mTitleBar != null);
104 }
105
Michael Kolba2b2ba82010-08-04 17:54:03 -0700106 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800107 public boolean onTouchEvent(MotionEvent evt) {
108 if (MotionEvent.ACTION_DOWN == evt.getActionMasked()) {
109 mUserInitiated = true;
110 } else if (MotionEvent.ACTION_UP == evt.getActionMasked()
111 || (MotionEvent.ACTION_CANCEL == evt.getActionMasked())) {
112 mUserInitiated = false;
113 }
114 return super.onTouchEvent(evt);
115 }
116
117 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700118 public void stopScroll() {
119 mIsCancelled = true;
120 super.stopScroll();
121 }
122
123 @Override
124 protected void onScrollChanged(int l, final int t, int ol, int ot) {
125 super.onScrollChanged(l, t, ol, ot);
126 if (!mIsCancelled) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800127 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700128 } else {
129 mIsCancelled = false;
130 }
131 }
132
133 void setScrollListener(ScrollListener l) {
134 mScrollListener = l;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700135 }
136
137 // callback for scroll events
138
139 interface ScrollListener {
Michael Kolb5ee018e2011-02-18 15:47:21 -0800140 public void onScroll(int visibleTitleHeight, boolean userInitiated);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700141 }
142
Michael Kolb377ea312011-02-17 14:36:56 -0800143 @Override
144 protected void onDraw(android.graphics.Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -0800145 super.onDraw(c);
146 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
147 mBackgroundRemoved = true;
148 post(new Runnable() {
149 public void run() {
150 getRootView().setBackgroundDrawable(null);
151 }
152 });
Michael Kolb377ea312011-02-17 14:36:56 -0800153 }
154 }
155
Michael Kolba2b2ba82010-08-04 17:54:03 -0700156}