Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.content.Context; |
Michael Kolb | db343c5 | 2011-05-29 12:18:52 -0700 | [diff] [blame] | 20 | import android.content.res.Resources; |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 21 | import android.graphics.Canvas; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.util.AttributeSet; |
Michael Kolb | db343c5 | 2011-05-29 12:18:52 -0700 | [diff] [blame] | 24 | import android.util.TypedValue; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 25 | import android.view.LayoutInflater; |
| 26 | import android.view.View; |
| 27 | import android.view.ViewGroup; |
| 28 | import android.widget.FrameLayout; |
| 29 | import android.widget.ImageButton; |
| 30 | import android.widget.ImageView; |
| 31 | import android.widget.LinearLayout; |
| 32 | import android.widget.TextView; |
| 33 | |
| 34 | public class NavTabView extends LinearLayout { |
| 35 | |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 36 | private Tab mTab; |
| 37 | private BrowserWebView mWebView; |
| 38 | private WebProxyView mProxy; |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame^] | 39 | private ImageView mClose; |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 40 | private FrameLayout mContainer; |
| 41 | private TextView mTitle; |
| 42 | private View mTitleBar; |
| 43 | private OnClickListener mClickListener; |
| 44 | private boolean mHighlighted; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 45 | |
| 46 | public NavTabView(Context context, AttributeSet attrs, int defStyle) { |
| 47 | super(context, attrs, defStyle); |
| 48 | init(); |
| 49 | } |
| 50 | |
| 51 | public NavTabView(Context context, AttributeSet attrs) { |
| 52 | super(context, attrs); |
| 53 | init(); |
| 54 | } |
| 55 | |
| 56 | public NavTabView(Context context) { |
| 57 | super(context); |
| 58 | init(); |
| 59 | } |
| 60 | |
| 61 | private void init() { |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame^] | 62 | LayoutInflater.from(mContext).inflate(R.layout.nav_tab_view, this); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 63 | mContainer = (FrameLayout) findViewById(R.id.tab_view); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame^] | 64 | mClose = (ImageView) findViewById(R.id.closetab); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 65 | mTitle = (TextView) findViewById(R.id.title); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 66 | mTitleBar = findViewById(R.id.titlebar); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | protected boolean isClose(View v) { |
| 70 | return v == mClose; |
| 71 | } |
| 72 | |
| 73 | protected boolean isTitle(View v) { |
| 74 | return v == mTitleBar; |
| 75 | } |
| 76 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 77 | protected boolean isWebView(View v) { |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 78 | return v == mProxy; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | protected void setHighlighted(boolean highlighted) { |
| 82 | if (highlighted == mHighlighted) return; |
| 83 | mHighlighted = highlighted; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private void setTitle() { |
| 87 | if (mTab == null) return; |
| 88 | if (mHighlighted) { |
| 89 | mTitle.setText(mTab.getUrl()); |
| 90 | } else { |
| 91 | String txt = mTab.getTitle(); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame^] | 92 | if (txt == null) { |
| 93 | txt = mTab.getUrl(); |
| 94 | } |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 95 | mTitle.setText(txt); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | protected boolean isHighlighted() { |
| 100 | return mHighlighted; |
| 101 | } |
| 102 | |
| 103 | protected void setWebView(PhoneUi ui, Tab tab) { |
| 104 | mTab = tab; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 105 | setTitle(); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 106 | BrowserWebView web = (BrowserWebView) tab.getWebView(); |
| 107 | if (web != null) { |
| 108 | mWebView = web; |
| 109 | removeFromParent(mWebView); |
| 110 | mProxy = new WebProxyView(mContext, mWebView); |
| 111 | mContainer.addView(mProxy, 0); |
| 112 | } |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | protected void hideTitle() { |
| 116 | mTitleBar.setVisibility(View.INVISIBLE); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void setOnClickListener(OnClickListener listener) { |
| 121 | mClickListener = listener; |
| 122 | mTitleBar.setOnClickListener(mClickListener); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 123 | mClose.setOnClickListener(mClickListener); |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 124 | if (mProxy != null) { |
| 125 | mProxy.setOnClickListener(mClickListener); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 129 | @Override |
| 130 | public void onDetachedFromWindow() { |
Michael Kolb | dbf3981 | 2011-06-10 14:06:40 -0700 | [diff] [blame] | 131 | if (mWebView != null) { |
| 132 | mWebView.setProxyView(null); |
| 133 | } |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame^] | 136 | @Override |
| 137 | public void onAttachedToWindow() { |
| 138 | if (mWebView != null) { |
| 139 | mWebView.invalidate(); |
| 140 | } |
| 141 | } |
| 142 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 143 | private static void removeFromParent(View v) { |
| 144 | if (v.getParent() != null) { |
| 145 | ((ViewGroup) v.getParent()).removeView(v); |
| 146 | } |
| 147 | } |
| 148 | |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 149 | static class WebProxyView extends View { |
| 150 | |
| 151 | private BrowserWebView mWeb; |
| 152 | |
| 153 | public WebProxyView(Context context, BrowserWebView web) { |
| 154 | super(context); |
| 155 | setWillNotDraw(false); |
| 156 | mWeb = web; |
| 157 | mWeb.setProxyView(this); |
| 158 | |
| 159 | } |
| 160 | |
| 161 | public void onDraw(Canvas c) { |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame^] | 162 | float scale = 0.7f; |
| 163 | int sx = mWeb.getScrollX(); |
| 164 | int sy = mWeb.getScrollY(); |
| 165 | c.scale(scale, scale); |
| 166 | c.translate(-sx, -sy); |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 167 | mWeb.onDraw(c); |
| 168 | } |
| 169 | |
| 170 | } |
| 171 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 172 | } |