The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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(); |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame^] | 73 | if (p != null) { |
| 74 | canvas.save(); |
| 75 | float scale = getWidth() * w.getScale() / w.getWidth(); |
| 76 | canvas.scale(scale, scale); |
| 77 | canvas.translate(-w.getScrollX(), -w.getScrollY()); |
| 78 | canvas.drawPicture(p); |
| 79 | canvas.restore(); |
| 80 | } |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void setImageResource(int resId) { |
| 88 | mUsesResource = true; |
| 89 | mTab = null; |
| 90 | super.setImageResource(resId); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Set a WebView for this FakeWebView to represent. |
| 95 | * @param v WebView whose picture and other data will be used in onDraw. |
| 96 | */ |
| 97 | public void setTab(TabControl.Tab t) { |
| 98 | mUsesResource = false; |
| 99 | mTab = t; |
| 100 | if (t != null && t.getWebView() != null) { |
| 101 | Listener l = new Listener(); |
| 102 | t.getWebView().setPictureListener(l); |
| 103 | if (t.getSubWebView() != null) { |
| 104 | t.getSubWebView().setPictureListener(l); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |