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