Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 4 | * 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 Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 7 | * |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 11 | * 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 Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.content.Context; |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 20 | import android.graphics.Canvas; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 21 | import android.util.AttributeSet; |
| 22 | import android.view.View; |
| 23 | import android.webkit.WebView; |
| 24 | |
| 25 | import java.util.Map; |
| 26 | |
| 27 | /** |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 28 | * Manage WebView scroll events |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 29 | */ |
Michael Kolb | b14ff2f | 2011-07-01 15:33:56 -0700 | [diff] [blame] | 30 | public class BrowserWebView extends WebView { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 31 | |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 32 | public interface OnScrollChangedListener { |
| 33 | void onScrollChanged(int l, int t, int oldl, int oldt); |
| 34 | } |
| 35 | |
Romain Guy | 860662a | 2011-01-10 12:57:22 -0800 | [diff] [blame] | 36 | private boolean mBackgroundRemoved = false; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 37 | private TitleBar mTitleBar; |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 38 | private OnScrollChangedListener mOnScrollChangedListener; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * @param context |
| 42 | * @param attrs |
| 43 | * @param defStyle |
| 44 | * @param javascriptInterfaces |
| 45 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 46 | public BrowserWebView(Context context, AttributeSet attrs, int defStyle, |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 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 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 56 | public BrowserWebView( |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 57 | Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 58 | super(context, attrs, defStyle, privateBrowsing); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @param context |
| 63 | * @param attrs |
| 64 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 65 | public BrowserWebView(Context context, AttributeSet attrs) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 66 | super(context, attrs); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param context |
| 71 | */ |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 72 | public BrowserWebView(Context context) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 73 | super(context); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Michael Kolb | 08a687a | 2011-04-22 16:13:02 -0700 | [diff] [blame] | 76 | @Override |
Michael Kolb | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 77 | protected int getTitleHeight() { |
| 78 | return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0; |
| 79 | } |
| 80 | |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 81 | void hideEmbeddedTitleBar() { |
| 82 | scrollBy(0, getVisibleTitleHeight()); |
| 83 | } |
| 84 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 85 | @Override |
| 86 | public void setEmbeddedTitleBar(final View title) { |
| 87 | super.setEmbeddedTitleBar(title); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 88 | mTitleBar = (TitleBar) title; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 91 | public boolean hasTitleBar() { |
| 92 | return (mTitleBar != null); |
| 93 | } |
| 94 | |
Michael Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 95 | @Override |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 96 | protected void onDraw(Canvas c) { |
Michael Kolb | f262892 | 2011-03-09 17:15:28 -0800 | [diff] [blame] | 97 | 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 Kolb | 377ea31 | 2011-02-17 14:36:56 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 108 | public void drawContent(Canvas c) { |
| 109 | onDraw(c); |
| 110 | } |
| 111 | |
John Reck | 718a24d | 2011-08-12 11:08:30 -0700 | [diff] [blame] | 112 | @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 Reck | bd54631 | 2011-09-19 11:47:52 -0700 | [diff] [blame^] | 124 | @Override |
| 125 | public boolean showContextMenuForChild(View originalView) { |
| 126 | return false; |
| 127 | } |
| 128 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 129 | } |