blob: b763cf10ede0459b7078cb47a842c7e8b51ba7bd [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;
John Reck8ee633f2011-08-09 16:00:35 -070020import android.graphics.Canvas;
Michael Kolba2b2ba82010-08-04 17:54:03 -070021import android.util.AttributeSet;
22import 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 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070030public class BrowserWebView extends WebView {
Michael Kolba2b2ba82010-08-04 17:54:03 -070031
John Reck718a24d2011-08-12 11:08:30 -070032 public interface OnScrollChangedListener {
33 void onScrollChanged(int l, int t, int oldl, int oldt);
34 }
35
Romain Guy860662a2011-01-10 12:57:22 -080036 private boolean mBackgroundRemoved = false;
John Reck0f602f32011-07-07 15:38:43 -070037 private TitleBar mTitleBar;
John Reck718a24d2011-08-12 11:08:30 -070038 private OnScrollChangedListener mOnScrollChangedListener;
Michael Kolba2b2ba82010-08-04 17:54:03 -070039
40 /**
41 * @param context
42 * @param attrs
43 * @param defStyle
44 * @param javascriptInterfaces
45 */
John Reckb9a051b2011-03-18 11:55:48 -070046 public BrowserWebView(Context context, AttributeSet attrs, int defStyle,
Michael Kolba2b2ba82010-08-04 17:54:03 -070047 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 */
John Reckb9a051b2011-03-18 11:55:48 -070056 public BrowserWebView(
Michael Kolb377ea312011-02-17 14:36:56 -080057 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 */
John Reckb9a051b2011-03-18 11:55:48 -070065 public BrowserWebView(Context context, AttributeSet attrs) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070066 super(context, attrs);
67 }
68
69 /**
70 * @param context
71 */
John Reckb9a051b2011-03-18 11:55:48 -070072 public BrowserWebView(Context context) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070073 super(context);
Michael Kolb2814a362011-05-19 15:49:41 -070074 }
75
Michael Kolb08a687a2011-04-22 16:13:02 -070076 @Override
Michael Kolb29ccf8a2011-02-23 16:13:24 -080077 protected int getTitleHeight() {
78 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
79 }
80
Michael Kolbed217742010-08-10 17:52:34 -070081 void hideEmbeddedTitleBar() {
82 scrollBy(0, getVisibleTitleHeight());
83 }
84
Michael Kolba2b2ba82010-08-04 17:54:03 -070085 @Override
86 public void setEmbeddedTitleBar(final View title) {
87 super.setEmbeddedTitleBar(title);
John Reck0f602f32011-07-07 15:38:43 -070088 mTitleBar = (TitleBar) title;
Michael Kolba2b2ba82010-08-04 17:54:03 -070089 }
90
Michael Kolb377ea312011-02-17 14:36:56 -080091 public boolean hasTitleBar() {
92 return (mTitleBar != null);
93 }
94
Michael Kolb377ea312011-02-17 14:36:56 -080095 @Override
John Reck718a24d2011-08-12 11:08:30 -070096 protected void onDraw(Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -080097 super.onDraw(c);
98 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
99 mBackgroundRemoved = true;
100 post(new Runnable() {
101 public void run() {
102 getRootView().setBackgroundDrawable(null);
103 }
104 });
Michael Kolb377ea312011-02-17 14:36:56 -0800105 }
106 }
107
John Reck8ee633f2011-08-09 16:00:35 -0700108 public void drawContent(Canvas c) {
109 onDraw(c);
110 }
111
John Reck718a24d2011-08-12 11:08:30 -0700112 @Override
113 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
114 super.onScrollChanged(l, t, oldl, oldt);
115 if (mOnScrollChangedListener != null) {
116 mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
117 }
118 }
119
120 public void setOnScrollChangedListener(OnScrollChangedListener listener) {
121 mOnScrollChangedListener = listener;
122 }
123
John Reckbd546312011-09-19 11:47:52 -0700124 @Override
125 public boolean showContextMenuForChild(View originalView) {
126 return false;
127 }
128
Michael Kolba2b2ba82010-08-04 17:54:03 -0700129}