Home | History | Annotate | Download | only in browser
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      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
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     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.
     15  */
     16 
     17 package com.android.browser;
     18 
     19 import android.content.Context;
     20 import android.graphics.Canvas;
     21 import android.util.AttributeSet;
     22 import android.view.View;
     23 import android.webkit.WebChromeClient;
     24 import android.webkit.WebStorage;
     25 import android.webkit.WebStorageClassic;
     26 import android.webkit.WebView;
     27 import android.webkit.WebViewClassic;
     28 import android.webkit.WebViewClient;
     29 
     30 import java.util.Map;
     31 
     32 /**
     33  * Manage WebView scroll events
     34  */
     35 public class BrowserWebView extends WebView implements WebViewClassic.TitleBarDelegate {
     36 
     37     public interface OnScrollChangedListener {
     38         void onScrollChanged(int l, int t, int oldl, int oldt);
     39     }
     40 
     41     private boolean mBackgroundRemoved = false;
     42     private TitleBar mTitleBar;
     43     private OnScrollChangedListener mOnScrollChangedListener;
     44     private WebChromeClient mWebChromeClient;
     45     private WebViewClient mWebViewClient;
     46 
     47     /**
     48      * @param context
     49      * @param attrs
     50      * @param defStyle
     51      * @param javascriptInterfaces
     52      */
     53     public BrowserWebView(Context context, AttributeSet attrs, int defStyle,
     54             Map<String, Object> javascriptInterfaces, boolean privateBrowsing) {
     55         super(context, attrs, defStyle, javascriptInterfaces, privateBrowsing);
     56     }
     57 
     58     /**
     59      * @param context
     60      * @param attrs
     61      * @param defStyle
     62      */
     63     public BrowserWebView(
     64             Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
     65         super(context, attrs, defStyle, privateBrowsing);
     66     }
     67 
     68     /**
     69      * @param context
     70      * @param attrs
     71      */
     72     public BrowserWebView(Context context, AttributeSet attrs) {
     73         super(context, attrs);
     74     }
     75 
     76     /**
     77      * @param context
     78      */
     79     public BrowserWebView(Context context) {
     80         super(context);
     81     }
     82 
     83     public static boolean isClassic() {
     84         // Using WebStorage for convenience of access in a static method.
     85         return WebStorage.getInstance() instanceof WebStorageClassic;
     86     }
     87 
     88     @Override
     89     public void setWebChromeClient(WebChromeClient client) {
     90         mWebChromeClient = client;
     91         super.setWebChromeClient(client);
     92     }
     93 
     94     public WebChromeClient getWebChromeClient() {
     95       return mWebChromeClient;
     96     }
     97 
     98     @Override
     99     public void setWebViewClient(WebViewClient client) {
    100         mWebViewClient = client;
    101         super.setWebViewClient(client);
    102     }
    103 
    104     public WebViewClient getWebViewClient() {
    105       return mWebViewClient;
    106     }
    107 
    108     public void setTitleBar(TitleBar title) {
    109         mTitleBar = title;
    110     }
    111 
    112     // From TitleBarDelegate
    113     @Override
    114     public int getTitleHeight() {
    115         return (mTitleBar != null) ? mTitleBar.getEmbeddedHeight() : 0;
    116     }
    117 
    118     // From TitleBarDelegate
    119     @Override
    120     public void onSetEmbeddedTitleBar(final View title) {
    121         // TODO: Remove this method; it is never invoked.
    122     }
    123 
    124     public boolean hasTitleBar() {
    125         return (mTitleBar != null);
    126     }
    127 
    128     @Override
    129     protected void onDraw(Canvas c) {
    130         super.onDraw(c);
    131         if (!mBackgroundRemoved && getRootView().getBackground() != null) {
    132             mBackgroundRemoved = true;
    133             post(new Runnable() {
    134                 public void run() {
    135                     getRootView().setBackgroundDrawable(null);
    136                 }
    137             });
    138         }
    139     }
    140 
    141     public void drawContent(Canvas c) {
    142         onDraw(c);
    143     }
    144 
    145     @Override
    146     protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    147         super.onScrollChanged(l, t, oldl, oldt);
    148         if (mTitleBar != null) {
    149             mTitleBar.onScrollChanged();
    150         }
    151         if (mOnScrollChangedListener != null) {
    152             mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
    153         }
    154     }
    155 
    156     public void setOnScrollChangedListener(OnScrollChangedListener listener) {
    157         mOnScrollChangedListener = listener;
    158     }
    159 
    160     @Override
    161     public boolean showContextMenuForChild(View originalView) {
    162         return false;
    163     }
    164 
    165     @Override
    166     public void destroy() {
    167         BrowserSettings.getInstance().stopManagingSettings(getSettings());
    168         super.destroy();
    169     }
    170 
    171 }
    172