OSDN Git Service

Initial Contribution
[android-x86/packages-apps-Browser.git] / src / com / android / browser / FakeWebView.java
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17  
18 package com.android.browser;
19
20 import android.content.Context;
21 import android.graphics.Canvas;
22 import android.graphics.Color;
23 import android.graphics.Picture;
24 import android.util.AttributeSet;
25 import android.view.View;
26 import android.webkit.WebView;
27 import android.widget.ImageView;
28
29 import android.util.Log;
30
31 /**
32  *  This class is used by ImageAdapter to draw a representation of each tab. It 
33  *  overrides ImageView so it can be used for the new tab image as well.
34  */
35 public class FakeWebView extends ImageView {
36     private TabControl.Tab mTab;
37     private boolean        mUsesResource;
38
39     private class Listener implements WebView.PictureListener {
40         public void onNewPicture(WebView view, Picture p) {
41             FakeWebView.this.invalidate();
42         }
43     };
44
45     public FakeWebView(Context context) {
46         this(context, null);
47     }
48     
49     public FakeWebView(Context context, AttributeSet attrs) {
50         this(context, attrs, 0);
51     }
52     
53     public FakeWebView(Context context, AttributeSet attrs, int defStyle) {
54         super(context, attrs, defStyle);
55     }
56
57     @Override
58     protected void onDraw(Canvas canvas) {
59         if (mUsesResource) {
60             super.onDraw(canvas);
61         } else {
62             // Always draw white behind the picture just in case the picture
63             // draws nothing.
64             // FIXME: We used to draw white only when the WebView was null but
65             // sometimes the picture was empty. So now we always draw white. It
66             // would be nice to know if the picture is empty so we can avoid
67             // drawing white.
68             canvas.drawColor(Color.WHITE);
69             if (mTab != null) {
70                 final WebView w = mTab.getTopWindow();
71                 if (w != null) {
72                     Picture p = w.capturePicture();
73                     canvas.save();
74                     float scale = getWidth() * w.getScale() / w.getWidth();
75                     canvas.scale(scale, scale);
76                     canvas.translate(-w.getScrollX(), -w.getScrollY());
77                     canvas.drawPicture(p);
78                     canvas.restore();
79                 }
80             }
81         }
82     }
83     
84     @Override
85     public void setImageResource(int resId) {
86         mUsesResource = true;
87         mTab = null;
88         super.setImageResource(resId);
89     }
90
91     /**
92      *  Set a WebView for this FakeWebView to represent.
93      *  @param  v WebView whose picture and other data will be used in onDraw.
94      */
95     public void setTab(TabControl.Tab t) {
96         mUsesResource = false;
97         mTab = t;
98         if (t != null && t.getWebView() != null) {
99             Listener l = new Listener();
100             t.getWebView().setPictureListener(l);
101             if (t.getSubWebView() != null) {
102                 t.getSubWebView().setPictureListener(l);
103             }
104         }
105     }
106 }