The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [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 { |
Patrick Scott | 2ed6edb | 2009-04-22 10:07:45 -0400 | [diff] [blame] | 36 | private TabControl.PickerData mPickerData; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 37 | private boolean mUsesResource; |
| 38 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | 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 Scott | 2ed6edb | 2009-04-22 10:07:45 -0400 | [diff] [blame] | 63 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 72 | } |
Patrick Scott | 2ed6edb | 2009-04-22 10:07:45 -0400 | [diff] [blame] | 73 | canvas.scale(scale, scale); |
| 74 | canvas.translate(-mPickerData.mScrollX, |
| 75 | -mPickerData.mScrollY); |
| 76 | canvas.drawPicture(p); |
| 77 | canvas.restore(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void setImageResource(int resId) { |
| 85 | mUsesResource = true; |
Patrick Scott | 2ed6edb | 2009-04-22 10:07:45 -0400 | [diff] [blame] | 86 | mPickerData = null; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 87 | super.setImageResource(resId); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set a WebView for this FakeWebView to represent. |
Patrick Scott | 2ed6edb | 2009-04-22 10:07:45 -0400 | [diff] [blame] | 92 | * @param t The tab whose picture and other data will be used in onDraw. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 93 | */ |
| 94 | public void setTab(TabControl.Tab t) { |
| 95 | mUsesResource = false; |
Patrick Scott | 2ed6edb | 2009-04-22 10:07:45 -0400 | [diff] [blame] | 96 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |