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; |
| 39 | private ImageButton mForward; |
| 40 | private ImageButton mRefresh; |
| 41 | private ImageView mFavicon; |
| 42 | private ImageButton mClose; |
| 43 | private FrameLayout mContainer; |
| 44 | private TextView mTitle; |
| 45 | private View mTitleBar; |
| 46 | private OnClickListener mClickListener; |
| 47 | private boolean mHighlighted; |
| 48 | private Drawable mTitleBg; |
| 49 | private Drawable mUrlBg; |
| 50 | private float mMediumTextSize; |
| 51 | private float mSmallTextSize; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 52 | |
| 53 | public NavTabView(Context context, AttributeSet attrs, int defStyle) { |
| 54 | super(context, attrs, defStyle); |
| 55 | init(); |
| 56 | } |
| 57 | |
| 58 | public NavTabView(Context context, AttributeSet attrs) { |
| 59 | super(context, attrs); |
| 60 | init(); |
| 61 | } |
| 62 | |
| 63 | public NavTabView(Context context) { |
| 64 | super(context); |
| 65 | init(); |
| 66 | } |
| 67 | |
| 68 | private void init() { |
Michael Kolb | db343c5 | 2011-05-29 12:18:52 -0700 | [diff] [blame] | 69 | final Resources res = mContext.getResources(); |
| 70 | mMediumTextSize = res.getDimension(R.dimen.nav_tab_text_normal); |
| 71 | mSmallTextSize = res.getDimension(R.dimen.nav_tab_text_small); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 72 | LayoutInflater.from(mContext).inflate(R.layout.nav_tab_view, |
| 73 | this); |
| 74 | mContainer = (FrameLayout) findViewById(R.id.tab_view); |
| 75 | mForward = (ImageButton) findViewById(R.id.forward); |
| 76 | mClose = (ImageButton) findViewById(R.id.closetab); |
| 77 | mRefresh = (ImageButton) findViewById(R.id.refresh); |
| 78 | mTitle = (TextView) findViewById(R.id.title); |
| 79 | mFavicon = (ImageView) findViewById(R.id.favicon); |
| 80 | mTitleBar = findViewById(R.id.titlebar); |
Michael Kolb | db343c5 | 2011-05-29 12:18:52 -0700 | [diff] [blame] | 81 | mTitleBg = res.getDrawable(R.drawable.bg_urlbar); |
| 82 | mUrlBg = res.getDrawable( |
| 83 | com.android.internal.R.drawable.edit_text_holo_dark); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 84 | setState(false); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | protected boolean isRefresh(View v) { |
| 88 | return v == mRefresh; |
| 89 | } |
| 90 | |
| 91 | protected boolean isClose(View v) { |
| 92 | return v == mClose; |
| 93 | } |
| 94 | |
| 95 | protected boolean isTitle(View v) { |
| 96 | return v == mTitleBar; |
| 97 | } |
| 98 | |
| 99 | protected boolean isForward(View v) { |
| 100 | return v == mForward; |
| 101 | } |
| 102 | |
| 103 | protected boolean isWebView(View v) { |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 104 | return v == mProxy; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | protected void setHighlighted(boolean highlighted) { |
| 108 | if (highlighted == mHighlighted) return; |
| 109 | mHighlighted = highlighted; |
| 110 | setState(highlighted); |
| 111 | } |
| 112 | |
| 113 | private void setState(boolean highlighted) { |
| 114 | if (highlighted) { |
| 115 | setAlpha(1.0f); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 116 | mFavicon.setVisibility(View.VISIBLE); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 117 | setupButtons(); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 118 | mTitleBar.setBackgroundDrawable(mTitleBg); |
| 119 | mClose.setVisibility(View.VISIBLE); |
Michael Kolb | db343c5 | 2011-05-29 12:18:52 -0700 | [diff] [blame] | 120 | mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMediumTextSize); |
| 121 | mTitle.setBackgroundDrawable(mUrlBg); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 122 | } else { |
| 123 | setAlpha(0.8f); |
| 124 | mForward.setVisibility(View.GONE); |
| 125 | mRefresh.setVisibility(View.INVISIBLE); |
| 126 | mFavicon.setVisibility(View.INVISIBLE); |
| 127 | mClose.setVisibility(View.GONE); |
| 128 | mTitleBar.setBackgroundDrawable(null); |
Michael Kolb | db343c5 | 2011-05-29 12:18:52 -0700 | [diff] [blame] | 129 | mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, mSmallTextSize); |
| 130 | mTitle.setBackgroundDrawable(null); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 131 | } |
| 132 | setTitle(); |
| 133 | } |
| 134 | |
| 135 | private void setTitle() { |
| 136 | if (mTab == null) return; |
| 137 | if (mHighlighted) { |
| 138 | mTitle.setText(mTab.getUrl()); |
| 139 | } else { |
| 140 | String txt = mTab.getTitle(); |
| 141 | if (txt == null) txt = mTab.getUrl(); |
| 142 | mTitle.setText(txt); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | protected boolean isHighlighted() { |
| 147 | return mHighlighted; |
| 148 | } |
| 149 | |
| 150 | protected void setWebView(PhoneUi ui, Tab tab) { |
| 151 | mTab = tab; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 152 | mFavicon.setImageDrawable(ui.getFaviconDrawable(tab.getFavicon())); |
| 153 | setTitle(); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 154 | BrowserWebView web = (BrowserWebView) tab.getWebView(); |
| 155 | if (web != null) { |
| 156 | mWebView = web; |
| 157 | removeFromParent(mWebView); |
| 158 | mProxy = new WebProxyView(mContext, mWebView); |
| 159 | mContainer.addView(mProxy, 0); |
| 160 | } |
| 161 | setupButtons(); |
| 162 | } |
| 163 | |
| 164 | void setupButtons() { |
| 165 | if (mTab.isSnapshot()) { |
| 166 | mForward.setVisibility(View.GONE); |
| 167 | mRefresh.setVisibility(View.GONE); |
| 168 | } else if (mWebView != null) { |
| 169 | mForward.setVisibility(mWebView.canGoForward() |
| 170 | ? View.VISIBLE : View.GONE); |
| 171 | mRefresh.setVisibility(View.VISIBLE); |
| 172 | } |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | protected void hideTitle() { |
| 176 | mTitleBar.setVisibility(View.INVISIBLE); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void setOnClickListener(OnClickListener listener) { |
| 181 | mClickListener = listener; |
| 182 | mTitleBar.setOnClickListener(mClickListener); |
| 183 | mRefresh.setOnClickListener(mClickListener); |
| 184 | mForward.setOnClickListener(mClickListener); |
| 185 | mClose.setOnClickListener(mClickListener); |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 186 | if (mProxy != null) { |
| 187 | mProxy.setOnClickListener(mClickListener); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 191 | @Override |
| 192 | public void onDetachedFromWindow() { |
Michael Kolb | dbf3981 | 2011-06-10 14:06:40 -0700 | [diff] [blame] | 193 | if (mWebView != null) { |
| 194 | mWebView.setProxyView(null); |
| 195 | } |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 198 | private static void removeFromParent(View v) { |
| 199 | if (v.getParent() != null) { |
| 200 | ((ViewGroup) v.getParent()).removeView(v); |
| 201 | } |
| 202 | } |
| 203 | |
Michael Kolb | 0f91e03 | 2011-06-01 09:54:20 -0700 | [diff] [blame] | 204 | static class WebProxyView extends View { |
| 205 | |
| 206 | private BrowserWebView mWeb; |
| 207 | |
| 208 | public WebProxyView(Context context, BrowserWebView web) { |
| 209 | super(context); |
| 210 | setWillNotDraw(false); |
| 211 | mWeb = web; |
| 212 | mWeb.setProxyView(this); |
| 213 | |
| 214 | } |
| 215 | |
| 216 | public void onDraw(Canvas c) { |
| 217 | c.translate(-mWeb.getScrollX(), -mWeb.getScrollY()); |
| 218 | mWeb.onDraw(c); |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 223 | } |