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; |
| 20 | import android.graphics.drawable.Drawable; |
| 21 | import android.util.AttributeSet; |
| 22 | import android.view.LayoutInflater; |
| 23 | import android.view.View; |
| 24 | import android.view.ViewGroup; |
| 25 | import android.widget.FrameLayout; |
| 26 | import android.widget.ImageButton; |
| 27 | import android.widget.ImageView; |
| 28 | import android.widget.LinearLayout; |
| 29 | import android.widget.TextView; |
| 30 | |
| 31 | public class NavTabView extends LinearLayout { |
| 32 | |
| 33 | Tab mTab; |
| 34 | BrowserWebView mWebView; |
| 35 | ImageButton mForward; |
| 36 | ImageButton mRefresh; |
| 37 | ImageView mFavicon; |
| 38 | ImageButton mClose; |
| 39 | FrameLayout mContainer; |
| 40 | TextView mTitle; |
| 41 | View mTitleBar; |
| 42 | OnClickListener mClickListener; |
| 43 | boolean mHighlighted; |
| 44 | Drawable mTitleBg; |
| 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() { |
| 62 | LayoutInflater.from(mContext).inflate(R.layout.nav_tab_view, |
| 63 | this); |
| 64 | mContainer = (FrameLayout) findViewById(R.id.tab_view); |
| 65 | mForward = (ImageButton) findViewById(R.id.forward); |
| 66 | mClose = (ImageButton) findViewById(R.id.closetab); |
| 67 | mRefresh = (ImageButton) findViewById(R.id.refresh); |
| 68 | mTitle = (TextView) findViewById(R.id.title); |
| 69 | mFavicon = (ImageView) findViewById(R.id.favicon); |
| 70 | mTitleBar = findViewById(R.id.titlebar); |
| 71 | mTitleBg = mContext.getResources().getDrawable(R.drawable.bg_urlbar); |
| 72 | setState(false); |
| 73 | // refresh titlebar |
| 74 | } |
| 75 | |
| 76 | protected boolean isRefresh(View v) { |
| 77 | return v == mRefresh; |
| 78 | } |
| 79 | |
| 80 | protected boolean isClose(View v) { |
| 81 | return v == mClose; |
| 82 | } |
| 83 | |
| 84 | protected boolean isTitle(View v) { |
| 85 | return v == mTitleBar; |
| 86 | } |
| 87 | |
| 88 | protected boolean isForward(View v) { |
| 89 | return v == mForward; |
| 90 | } |
| 91 | |
| 92 | protected boolean isWebView(View v) { |
| 93 | return v == mWebView; |
| 94 | } |
| 95 | |
| 96 | protected void setHighlighted(boolean highlighted) { |
| 97 | if (highlighted == mHighlighted) return; |
| 98 | mHighlighted = highlighted; |
| 99 | setState(highlighted); |
| 100 | } |
| 101 | |
| 102 | private void setState(boolean highlighted) { |
| 103 | if (highlighted) { |
| 104 | setAlpha(1.0f); |
| 105 | mRefresh.setVisibility(View.VISIBLE); |
| 106 | mFavicon.setVisibility(View.VISIBLE); |
| 107 | mForward.setVisibility(mWebView.canGoForward() |
| 108 | ? View.VISIBLE : View.GONE); |
| 109 | mTitleBar.setBackgroundDrawable(mTitleBg); |
| 110 | mClose.setVisibility(View.VISIBLE); |
| 111 | } else { |
| 112 | setAlpha(0.8f); |
| 113 | mForward.setVisibility(View.GONE); |
| 114 | mRefresh.setVisibility(View.INVISIBLE); |
| 115 | mFavicon.setVisibility(View.INVISIBLE); |
| 116 | mClose.setVisibility(View.GONE); |
| 117 | mTitleBar.setBackgroundDrawable(null); |
| 118 | } |
| 119 | setTitle(); |
| 120 | } |
| 121 | |
| 122 | private void setTitle() { |
| 123 | if (mTab == null) return; |
| 124 | if (mHighlighted) { |
| 125 | mTitle.setText(mTab.getUrl()); |
| 126 | } else { |
| 127 | String txt = mTab.getTitle(); |
| 128 | if (txt == null) txt = mTab.getUrl(); |
| 129 | mTitle.setText(txt); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | protected boolean isHighlighted() { |
| 134 | return mHighlighted; |
| 135 | } |
| 136 | |
| 137 | protected void setWebView(PhoneUi ui, Tab tab) { |
| 138 | mTab = tab; |
| 139 | BrowserWebView web = (BrowserWebView) tab.getWebView(); |
| 140 | if (web == null) return; |
| 141 | mWebView = web; |
| 142 | removeFromParent(mWebView); |
| 143 | mWebView.setNavMode(true); |
| 144 | mContainer.addView(mWebView, 0); |
| 145 | if (mWebView != null) { |
| 146 | mForward.setVisibility(mWebView.canGoForward() |
| 147 | ? View.VISIBLE : View.GONE); |
| 148 | } |
| 149 | mFavicon.setImageDrawable(ui.getFaviconDrawable(tab.getFavicon())); |
| 150 | setTitle(); |
| 151 | } |
| 152 | |
| 153 | protected void hideTitle() { |
| 154 | mTitleBar.setVisibility(View.INVISIBLE); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void setOnClickListener(OnClickListener listener) { |
| 159 | mClickListener = listener; |
| 160 | mTitleBar.setOnClickListener(mClickListener); |
| 161 | mRefresh.setOnClickListener(mClickListener); |
| 162 | mForward.setOnClickListener(mClickListener); |
| 163 | mClose.setOnClickListener(mClickListener); |
| 164 | if (mWebView != null) { |
| 165 | mWebView.setOnClickListener(mClickListener); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | private static void removeFromParent(View v) { |
| 170 | if (v.getParent() != null) { |
| 171 | ((ViewGroup) v.getParent()).removeView(v); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | } |