blob: 633b799dee11d6379efdb5b24e87b7351673ea51 [file] [log] [blame]
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -07001/*
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
18package com.android.browser;
19
20import android.content.Context;
21import android.graphics.Canvas;
22import android.graphics.Color;
23import android.graphics.Picture;
24import android.util.AttributeSet;
25import android.view.View;
26import android.webkit.WebView;
27import android.widget.ImageView;
28
29import 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 */
35public class FakeWebView extends ImageView {
36 private TabControl.Tab mTab;
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -080037 private Picture mPicture;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070038 private boolean mUsesResource;
39
40 private class Listener implements WebView.PictureListener {
41 public void onNewPicture(WebView view, Picture p) {
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -080042 FakeWebView.this.mPicture = p;
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070043 FakeWebView.this.invalidate();
44 }
45 };
46
47 public FakeWebView(Context context) {
48 this(context, null);
49 }
50
51 public FakeWebView(Context context, AttributeSet attrs) {
52 this(context, attrs, 0);
53 }
54
55 public FakeWebView(Context context, AttributeSet attrs, int defStyle) {
56 super(context, attrs, defStyle);
57 }
58
59 @Override
60 protected void onDraw(Canvas canvas) {
61 if (mUsesResource) {
62 super.onDraw(canvas);
63 } else {
64 // Always draw white behind the picture just in case the picture
65 // draws nothing.
66 // FIXME: We used to draw white only when the WebView was null but
67 // sometimes the picture was empty. So now we always draw white. It
68 // would be nice to know if the picture is empty so we can avoid
69 // drawing white.
70 canvas.drawColor(Color.WHITE);
71 if (mTab != null) {
72 final WebView w = mTab.getTopWindow();
73 if (w != null) {
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -080074 if (mPicture != null) {
The Android Open Source Projected217d92008-12-17 18:05:52 -080075 canvas.save();
76 float scale = getWidth() * w.getScale() / w.getWidth();
77 canvas.scale(scale, scale);
78 canvas.translate(-w.getScrollX(), -w.getScrollY());
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -080079 canvas.drawPicture(mPicture);
The Android Open Source Projected217d92008-12-17 18:05:52 -080080 canvas.restore();
81 }
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -070082 }
83 }
84 }
85 }
86
87 @Override
88 public void setImageResource(int resId) {
89 mUsesResource = true;
90 mTab = null;
91 super.setImageResource(resId);
92 }
93
94 /**
95 * Set a WebView for this FakeWebView to represent.
96 * @param v WebView whose picture and other data will be used in onDraw.
97 */
98 public void setTab(TabControl.Tab t) {
99 mUsesResource = false;
100 mTab = t;
101 if (t != null && t.getWebView() != null) {
102 Listener l = new Listener();
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700103 if (t.getSubWebView() != null) {
104 t.getSubWebView().setPictureListener(l);
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800105 } else {
106 t.getWebView().setPictureListener(l);
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700107 }
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800108 mPicture = mTab.getTopWindow().capturePicture();
The Android Open Source Projectba6d7b82008-10-21 07:00:00 -0700109 }
110 }
111}