• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.PickerData mPickerData;
37     private boolean        mUsesResource;
38 
FakeWebView(Context context)39     public FakeWebView(Context context) {
40         this(context, null);
41     }
42 
FakeWebView(Context context, AttributeSet attrs)43     public FakeWebView(Context context, AttributeSet attrs) {
44         this(context, attrs, 0);
45     }
46 
FakeWebView(Context context, AttributeSet attrs, int defStyle)47     public FakeWebView(Context context, AttributeSet attrs, int defStyle) {
48         super(context, attrs, defStyle);
49     }
50 
51     @Override
onDraw(Canvas canvas)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);
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;
72                     }
73                     canvas.scale(scale, scale);
74                     canvas.translate(-mPickerData.mScrollX,
75                             -mPickerData.mScrollY);
76                     canvas.drawPicture(p);
77                     canvas.restore();
78                 }
79             }
80         }
81     }
82 
83     @Override
setImageResource(int resId)84     public void setImageResource(int resId) {
85         mUsesResource = true;
86         mPickerData = null;
87         super.setImageResource(resId);
88     }
89 
90     /**
91      *  Set a WebView for this FakeWebView to represent.
92      *  @param  t The tab whose picture and other data will be used in onDraw.
93      */
setTab(TabControl.Tab t)94     public void setTab(TabControl.Tab t) {
95         mUsesResource = false;
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;
104         }
105     }
106 }
107