blob: daa50137d0bfe06e74fbc4df3001901f19d05a28 [file] [log] [blame]
Michael Kolb4bd767d2011-05-27 11:33:55 -07001/*
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
17package com.android.browser;
18
19import android.content.Context;
Michael Kolbdb343c52011-05-29 12:18:52 -070020import android.content.res.Resources;
Michael Kolb0f91e032011-06-01 09:54:20 -070021import android.graphics.Canvas;
Michael Kolb4bd767d2011-05-27 11:33:55 -070022import android.graphics.drawable.Drawable;
23import android.util.AttributeSet;
Michael Kolbdb343c52011-05-29 12:18:52 -070024import android.util.TypedValue;
Michael Kolb4bd767d2011-05-27 11:33:55 -070025import android.view.LayoutInflater;
26import android.view.View;
27import android.view.ViewGroup;
28import android.widget.FrameLayout;
29import android.widget.ImageButton;
30import android.widget.ImageView;
31import android.widget.LinearLayout;
32import android.widget.TextView;
33
34public class NavTabView extends LinearLayout {
35
Michael Kolb0f91e032011-06-01 09:54:20 -070036 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 Kolb4bd767d2011-05-27 11:33:55 -070052
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 Kolbdb343c52011-05-29 12:18:52 -070069 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 Kolb4bd767d2011-05-27 11:33:55 -070072 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 Kolbdb343c52011-05-29 12:18:52 -070081 mTitleBg = res.getDrawable(R.drawable.bg_urlbar);
82 mUrlBg = res.getDrawable(
83 com.android.internal.R.drawable.edit_text_holo_dark);
Michael Kolb4bd767d2011-05-27 11:33:55 -070084 setState(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -070085 }
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 Kolb0f91e032011-06-01 09:54:20 -0700104 return v == mProxy;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700105 }
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 Kolb4bd767d2011-05-27 11:33:55 -0700116 mFavicon.setVisibility(View.VISIBLE);
John Reckd8c74522011-06-14 08:45:00 -0700117 setupButtons();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700118 mTitleBar.setBackgroundDrawable(mTitleBg);
119 mClose.setVisibility(View.VISIBLE);
Michael Kolbdb343c52011-05-29 12:18:52 -0700120 mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMediumTextSize);
121 mTitle.setBackgroundDrawable(mUrlBg);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700122 } 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 Kolbdb343c52011-05-29 12:18:52 -0700129 mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, mSmallTextSize);
130 mTitle.setBackgroundDrawable(null);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700131 }
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 Kolb4bd767d2011-05-27 11:33:55 -0700152 mFavicon.setImageDrawable(ui.getFaviconDrawable(tab.getFavicon()));
153 setTitle();
John Reckd8c74522011-06-14 08:45:00 -0700154 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 Kolb4bd767d2011-05-27 11:33:55 -0700173 }
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 Kolb0f91e032011-06-01 09:54:20 -0700186 if (mProxy != null) {
187 mProxy.setOnClickListener(mClickListener);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700188 }
189 }
190
Michael Kolb0f91e032011-06-01 09:54:20 -0700191 @Override
192 public void onDetachedFromWindow() {
Michael Kolbdbf39812011-06-10 14:06:40 -0700193 if (mWebView != null) {
194 mWebView.setProxyView(null);
195 }
Michael Kolb0f91e032011-06-01 09:54:20 -0700196 }
197
Michael Kolb4bd767d2011-05-27 11:33:55 -0700198 private static void removeFromParent(View v) {
199 if (v.getParent() != null) {
200 ((ViewGroup) v.getParent()).removeView(v);
201 }
202 }
203
Michael Kolb0f91e032011-06-01 09:54:20 -0700204 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 Kolb4bd767d2011-05-27 11:33:55 -0700223}