blob: 12d511f722635d2a3f6447b59e2f149982668124 [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;
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000024import android.webkit.WebViewClassic;
Michael Kolba2b2ba82010-08-04 17:54:03 -070025
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 */
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000031public class BrowserWebView extends WebView implements WebViewClassic.TitleBarDelegate {
Michael Kolba2b2ba82010-08-04 17:54:03 -070032
John Reck718a24d2011-08-12 11:08:30 -070033 public interface OnScrollChangedListener {
34 void onScrollChanged(int l, int t, int oldl, int oldt);
35 }
36
Romain Guy860662a2011-01-10 12:57:22 -080037 private boolean mBackgroundRemoved = false;
John Reck0f602f32011-07-07 15:38:43 -070038 private TitleBar mTitleBar;
John Reck718a24d2011-08-12 11:08:30 -070039 private OnScrollChangedListener mOnScrollChangedListener;
Michael Kolba2b2ba82010-08-04 17:54:03 -070040
41 /**
42 * @param context
43 * @param attrs
44 * @param defStyle
45 * @param javascriptInterfaces
46 */
John Reckb9a051b2011-03-18 11:55:48 -070047 public BrowserWebView(Context context, AttributeSet attrs, int defStyle,
Michael Kolba2b2ba82010-08-04 17:54:03 -070048 Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
49 super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing);
50 }
51
52 /**
53 * @param context
54 * @param attrs
55 * @param defStyle
56 */
John Reckb9a051b2011-03-18 11:55:48 -070057 public BrowserWebView(
Michael Kolb377ea312011-02-17 14:36:56 -080058 Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070059 super(context, attrs, defStyle, privateBrowsing);
60 }
61
62 /**
63 * @param context
64 * @param attrs
65 */
John Reckb9a051b2011-03-18 11:55:48 -070066 public BrowserWebView(Context context, AttributeSet attrs) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070067 super(context, attrs);
68 }
69
70 /**
71 * @param context
72 */
John Reckb9a051b2011-03-18 11:55:48 -070073 public BrowserWebView(Context context) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070074 super(context);
Michael Kolb2814a362011-05-19 15:49:41 -070075 }
76
Michael Kolb4923c222012-04-02 16:18:36 -070077 public void setTitleBar(TitleBar title) {
78 mTitleBar = title;
79 }
80
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000081 // From TitleBarDelegate
Michael Kolb08a687a2011-04-22 16:13:02 -070082 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000083 public int getTitleHeight() {
Michael Kolb29ccf8a2011-02-23 16:13:24 -080084 return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
85 }
86
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000087 // From TitleBarDelegate
Michael Kolba2b2ba82010-08-04 17:54:03 -070088 @Override
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +000089 public void onSetEmbeddedTitleBar(final View title) {
Michael Kolba2b2ba82010-08-04 17:54:03 -070090 }
91
Michael Kolb377ea312011-02-17 14:36:56 -080092 public boolean hasTitleBar() {
93 return (mTitleBar != null);
94 }
95
Michael Kolb377ea312011-02-17 14:36:56 -080096 @Override
John Reck718a24d2011-08-12 11:08:30 -070097 protected void onDraw(Canvas c) {
Michael Kolbf2628922011-03-09 17:15:28 -080098 super.onDraw(c);
99 if (!mBackgroundRemoved && getRootView().getBackground() != null) {
100 mBackgroundRemoved = true;
101 post(new Runnable() {
102 public void run() {
103 getRootView().setBackgroundDrawable(null);
104 }
105 });
Michael Kolb377ea312011-02-17 14:36:56 -0800106 }
107 }
108
John Reck8ee633f2011-08-09 16:00:35 -0700109 public void drawContent(Canvas c) {
110 onDraw(c);
111 }
112
John Reck718a24d2011-08-12 11:08:30 -0700113 @Override
114 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
115 super.onScrollChanged(l, t, oldl, oldt);
Michael Kolb4923c222012-04-02 16:18:36 -0700116 if (mTitleBar != null) {
117 mTitleBar.onScrollChanged();
118 }
John Reck718a24d2011-08-12 11:08:30 -0700119 if (mOnScrollChangedListener != null) {
120 mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
121 }
122 }
123
124 public void setOnScrollChangedListener(OnScrollChangedListener listener) {
125 mOnScrollChangedListener = listener;
126 }
127
John Reckbd546312011-09-19 11:47:52 -0700128 @Override
129 public boolean showContextMenuForChild(View originalView) {
130 return false;
131 }
132
John Reckd1d87312012-03-08 13:25:00 -0800133 @Override
134 public void destroy() {
135 BrowserSettings.getInstance().stopManagingSettings(getSettings());
136 super.destroy();
137 }
138
Michael Kolba2b2ba82010-08-04 17:54:03 -0700139}