blob: 6111aa6985a7c0df9a560ad971071f97514c0d79 [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;
20import android.util.AttributeSet;
Michael Kolb5ee018e2011-02-18 15:47:21 -080021import android.view.MotionEvent;
Michael Kolba2b2ba82010-08-04 17:54:03 -070022import android.view.View;
23import android.webkit.WebView;
24
25import java.util.Map;
26
27/**
Michael Kolb377ea312011-02-17 14:36:56 -080028 * Manage WebView scroll events
Michael Kolba2b2ba82010-08-04 17:54:03 -070029 */
John Reckb9a051b2011-03-18 11:55:48 -070030public class BrowserWebView extends WebView implements Runnable {
Michael Kolba2b2ba82010-08-04 17:54:03 -070031
32 private ScrollListener mScrollListener;
33 private boolean mIsCancelled;
Romain Guy860662a2011-01-10 12:57:22 -080034 private boolean mBackgroundRemoved = false;
Michael Kolb5ee018e2011-02-18 15:47:21 -080035 private boolean mUserInitiated = false;
Michael Kolb29ccf8a2011-02-23 16:13:24 -080036 private TitleBarBase mTitleBar;
Michael Kolba2b2ba82010-08-04 17:54:03 -070037
38 /**
39 * @param context
40 * @param attrs
41 * @param defStyle
42 * @param javascriptInterfaces
43 */
John Reckb9a051b2011-03-18 11:55:48 -070044 public BrowserWebView(Context context, AttributeSet attrs, int defStyle,
Michael Kolba2b2ba82010-08-04 17:54:03 -070045 Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
46 super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing);
47 }
48
49 /**
50 * @param context
51 * @param attrs
52 * @param defStyle
53 */
John Reckb9a051b2011-03-18 11:55:48 -070054 public BrowserWebView(
Michael Kolb377ea312011-02-17 14:36:56 -080055 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070056 super(context, attrs, defStyle, privateBrowsing);
57 }
58
59 /**
60 * @param context
61 * @param attrs
62 */
John Reckb9a051b2011-03-18 11:55:48 -070063 public BrowserWebView(Context context, AttributeSet attrs) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070064 super(context, attrs);
65 }
66
67 /**
68 * @param context
69 */
John Reckb9a051b2011-03-18 11:55:48 -070070 public BrowserWebView(Context context) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070071 super(context);
72 }
73
Michael Kolb29ccf8a2011-02-23 16:13:24 -080074 @Override
75 protected int getTitleHeight() {
76 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
77 }
78
Michael Kolba0e2a752010-12-12 15:27:56 -080079 // scroll runnable implementation
80 public void run() {
81 if (!mIsCancelled && (mScrollListener != null)) {
Michael Kolb5ee018e2011-02-18 15:47:21 -080082 mScrollListener.onScroll(getVisibleTitleHeight(), mUserInitiated);
Michael Kolba0e2a752010-12-12 15:27:56 -080083 }
84 }
85
Michael Kolbed217742010-08-10 17:52:34 -070086 void hideEmbeddedTitleBar() {
87 scrollBy(0, getVisibleTitleHeight());
88 }
89
Michael Kolba2b2ba82010-08-04 17:54:03 -070090 @Override
91 public void setEmbeddedTitleBar(final View title) {
92 super.setEmbeddedTitleBar(title);
Michael Kolb29ccf8a2011-02-23 16:13:24 -080093 mTitleBar = (TitleBarBase) title;
Michael Kolba2b2ba82010-08-04 17:54:03 -070094 if (title != null && mScrollListener != null) {
95 // allow the scroll listener to initialize its state
Michael Kolba0e2a752010-12-12 15:27:56 -080096 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -070097 }
98 }
99
Michael Kolb377ea312011-02-17 14:36:56 -0800100 public boolean hasTitleBar() {
101 return (mTitleBar != null);
102 }
103
Michael Kolba2b2ba82010-08-04 17:54:03 -0700104 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800105 public boolean onTouchEvent(MotionEvent evt) {
106 if (MotionEvent.ACTION_DOWN == evt.getActionMasked()) {
107 mUserInitiated = true;
108 } else if (MotionEvent.ACTION_UP == evt.getActionMasked()
109 || (MotionEvent.ACTION_CANCEL == evt.getActionMasked())) {
110 mUserInitiated = false;
111 }
112 return super.onTouchEvent(evt);
113 }
114
115 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700116 public void stopScroll() {
117 mIsCancelled = true;
118 super.stopScroll();
119 }
120
121 @Override
122 protected void onScrollChanged(int l, final int t, int ol, int ot) {
123 super.onScrollChanged(l, t, ol, ot);
124 if (!mIsCancelled) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800125 post(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700126 } else {
127 mIsCancelled = false;
128 }
129 }
130
131 void setScrollListener(ScrollListener l) {
132 mScrollListener = l;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700133 }
134
135 // callback for scroll events
136
137 interface ScrollListener {
Michael Kolb5ee018e2011-02-18 15:47:21 -0800138 public void onScroll(int visibleTitleHeight, boolean userInitiated);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700139 }
140
Michael Kolb377ea312011-02-17 14:36:56 -0800141 @Override
142 protected void onDraw(android.graphics.Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -0800143 super.onDraw(c);
144 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
145 mBackgroundRemoved = true;
146 post(new Runnable() {
147 public void run() {
148 getRootView().setBackgroundDrawable(null);
149 }
150 });
Michael Kolb377ea312011-02-17 14:36:56 -0800151 }
152 }
153
Michael Kolba2b2ba82010-08-04 17:54:03 -0700154}