blob: da5ef5f282bcb04755ef06c67cce5a3ca93586aa [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
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 {
Patrick Scott2ed6edb2009-04-22 10:07:45 -040036 private TabControl.PickerData mPickerData;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037 private boolean mUsesResource;
38
The Android Open Source Project0c908882009-03-03 19:32:16 -080039 public FakeWebView(Context context) {
40 this(context, null);
41 }
42
43 public FakeWebView(Context context, AttributeSet attrs) {
44 this(context, attrs, 0);
45 }
46
47 public FakeWebView(Context context, AttributeSet attrs, int defStyle) {
48 super(context, attrs, defStyle);
49 }
50
51 @Override
52 protected void onDraw(Canvas canvas) {
53 if (mUsesResource) {
54 super.onDraw(canvas);
55 } else {
56 // Always draw white behind the picture just in case the picture
57 // draws nothing.
58 // FIXME: We used to draw white only when the WebView was null but
59 // sometimes the picture was empty. So now we always draw white. It
60 // would be nice to know if the picture is empty so we can avoid
61 // drawing white.
62 canvas.drawColor(Color.WHITE);
Patrick Scott2ed6edb2009-04-22 10:07:45 -040063 if (mPickerData != null) {
64 final Picture p = mPickerData.mPicture;
65 if (p != null) {
66 canvas.save();
67 float scale = getWidth() * mPickerData.mScale
68 / mPickerData.mWidth;
69 // Check for NaN and infinity.
70 if (Float.isNaN(scale) || Float.isInfinite(scale)) {
71 scale = 1.0f;
The Android Open Source Project0c908882009-03-03 19:32:16 -080072 }
Patrick Scott2ed6edb2009-04-22 10:07:45 -040073 canvas.scale(scale, scale);
74 canvas.translate(-mPickerData.mScrollX,
75 -mPickerData.mScrollY);
76 canvas.drawPicture(p);
77 canvas.restore();
The Android Open Source Project0c908882009-03-03 19:32:16 -080078 }
79 }
80 }
81 }
82
83 @Override
84 public void setImageResource(int resId) {
85 mUsesResource = true;
Patrick Scott2ed6edb2009-04-22 10:07:45 -040086 mPickerData = null;
The Android Open Source Project0c908882009-03-03 19:32:16 -080087 super.setImageResource(resId);
88 }
89
90 /**
91 * Set a WebView for this FakeWebView to represent.
Patrick Scott2ed6edb2009-04-22 10:07:45 -040092 * @param t The tab whose picture and other data will be used in onDraw.
The Android Open Source Project0c908882009-03-03 19:32:16 -080093 */
94 public void setTab(TabControl.Tab t) {
95 mUsesResource = false;
Patrick Scott2ed6edb2009-04-22 10:07:45 -040096 if (mPickerData != null) {
97 // Clear the old tab's view first
98 mPickerData.mFakeWebView = null;
99 }
100 mPickerData = null;
101 if (t != null && t.getPickerData() != null) {
102 mPickerData = t.getPickerData();
103 mPickerData.mFakeWebView = this;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800104 }
105 }
106}