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 | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 21 | import android.graphics.drawable.Drawable; |
| 22 | import android.util.AttributeSet; |
Michael Kolb | db343c5 | 2011-05-29 12:18:52 -0700 | [diff] [blame^] | 23 | import android.util.TypedValue; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 24 | import android.view.LayoutInflater; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
| 27 | import android.widget.FrameLayout; |
| 28 | import android.widget.ImageButton; |
| 29 | import android.widget.ImageView; |
| 30 | import android.widget.LinearLayout; |
| 31 | import android.widget.TextView; |
| 32 | |
| 33 | public class NavTabView extends LinearLayout { |
| 34 | |
| 35 | Tab mTab; |
| 36 | BrowserWebView mWebView; |
| 37 | ImageButton mForward; |
| 38 | ImageButton mRefresh; |
| 39 | ImageView mFavicon; |
| 40 | ImageButton mClose; |
| 41 | FrameLayout mContainer; |
| 42 | TextView mTitle; |
| 43 | View mTitleBar; |
| 44 | OnClickListener mClickListener; |
| 45 | boolean mHighlighted; |
| 46 | Drawable mTitleBg; |
Michael Kolb | db343c5 | 2011-05-29 12:18:52 -0700 | [diff] [blame^] | 47 | Drawable mUrlBg; |
| 48 | float mMediumTextSize; |
| 49 | float mSmallTextSize; |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 50 | |
| 51 | public NavTabView(Context context, AttributeSet attrs, int defStyle) { |
| 52 | super(context, attrs, defStyle); |
| 53 | init(); |
| 54 | } |
| 55 | |
| 56 | public NavTabView(Context context, AttributeSet attrs) { |
| 57 | super(context, attrs); |
| 58 | init(); |
| 59 | } |
| 60 | |
| 61 | public NavTabView(Context context) { |
| 62 | super(context); |
| 63 | init(); |
| 64 | } |
| 65 | |
| 66 | private void init() { |
Michael Kolb | db343c5 | 2011-05-29 12:18:52 -0700 | [diff] [blame^] | 67 | final Resources res = mContext.getResources(); |
| 68 | mMediumTextSize = res.getDimension(R.dimen.nav_tab_text_normal); |
| 69 | mSmallTextSize = res.getDimension(R.dimen.nav_tab_text_small); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 70 | LayoutInflater.from(mContext).inflate(R.layout.nav_tab_view, |
| 71 | this); |
| 72 | mContainer = (FrameLayout) findViewById(R.id.tab_view); |
| 73 | mForward = (ImageButton) findViewById(R.id.forward); |
| 74 | mClose = (ImageButton) findViewById(R.id.closetab); |
| 75 | mRefresh = (ImageButton) findViewById(R.id.refresh); |
| 76 | mTitle = (TextView) findViewById(R.id.title); |
| 77 | mFavicon = (ImageView) findViewById(R.id.favicon); |
| 78 | mTitleBar = findViewById(R.id.titlebar); |
Michael Kolb | db343c5 | 2011-05-29 12:18:52 -0700 | [diff] [blame^] | 79 | mTitleBg = res.getDrawable(R.drawable.bg_urlbar); |
| 80 | mUrlBg = res.getDrawable( |
| 81 | com.android.internal.R.drawable.edit_text_holo_dark); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 82 | setState(false); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | protected boolean isRefresh(View v) { |
| 86 | return v == mRefresh; |
| 87 | } |
| 88 | |
| 89 | protected boolean isClose(View v) { |
| 90 | return v == mClose; |
| 91 | } |
| 92 | |
| 93 | protected boolean isTitle(View v) { |
| 94 | return v == mTitleBar; |
| 95 | } |
| 96 | |
| 97 | protected boolean isForward(View v) { |
| 98 | return v == mForward; |
| 99 | } |
| 100 | |
| 101 | protected boolean isWebView(View v) { |
| 102 | return v == mWebView; |
| 103 | } |
| 104 | |
| 105 | protected void setHighlighted(boolean highlighted) { |
| 106 | if (highlighted == mHighlighted) return; |
| 107 | mHighlighted = highlighted; |
| 108 | setState(highlighted); |
| 109 | } |
| 110 | |
| 111 | private void setState(boolean highlighted) { |
| 112 | if (highlighted) { |
| 113 | setAlpha(1.0f); |
| 114 | mRefresh.setVisibility(View.VISIBLE); |
| 115 | mFavicon.setVisibility(View.VISIBLE); |
| 116 | mForward.setVisibility(mWebView.canGoForward() |
| 117 | ? View.VISIBLE : View.GONE); |
| 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; |
| 152 | BrowserWebView web = (BrowserWebView) tab.getWebView(); |
| 153 | if (web == null) return; |
| 154 | mWebView = web; |
| 155 | removeFromParent(mWebView); |
| 156 | mWebView.setNavMode(true); |
| 157 | mContainer.addView(mWebView, 0); |
| 158 | if (mWebView != null) { |
| 159 | mForward.setVisibility(mWebView.canGoForward() |
| 160 | ? View.VISIBLE : View.GONE); |
| 161 | } |
| 162 | mFavicon.setImageDrawable(ui.getFaviconDrawable(tab.getFavicon())); |
| 163 | setTitle(); |
| 164 | } |
| 165 | |
| 166 | protected void hideTitle() { |
| 167 | mTitleBar.setVisibility(View.INVISIBLE); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void setOnClickListener(OnClickListener listener) { |
| 172 | mClickListener = listener; |
| 173 | mTitleBar.setOnClickListener(mClickListener); |
| 174 | mRefresh.setOnClickListener(mClickListener); |
| 175 | mForward.setOnClickListener(mClickListener); |
| 176 | mClose.setOnClickListener(mClickListener); |
| 177 | if (mWebView != null) { |
| 178 | mWebView.setOnClickListener(mClickListener); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | private static void removeFromParent(View v) { |
| 183 | if (v.getParent() != null) { |
| 184 | ((ViewGroup) v.getParent()).removeView(v); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | } |