blob: 061e0240c4a135453cb90d2cd45a3219b0ca2a36 [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;
52 private boolean mPaused;
Michael Kolb4bd767d2011-05-27 11:33:55 -070053
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 Kolbdb343c52011-05-29 12:18:52 -070070 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 Kolb4bd767d2011-05-27 11:33:55 -070073 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 Kolbdb343c52011-05-29 12:18:52 -070082 mTitleBg = res.getDrawable(R.drawable.bg_urlbar);
83 mUrlBg = res.getDrawable(
84 com.android.internal.R.drawable.edit_text_holo_dark);
Michael Kolb4bd767d2011-05-27 11:33:55 -070085 setState(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -070086 }
87
Michael Kolb0f91e032011-06-01 09:54:20 -070088 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 Kolb4bd767d2011-05-27 11:33:55 -0700102 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 Kolb0f91e032011-06-01 09:54:20 -0700119 return v == mProxy;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700120 }
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 Kolbdb343c52011-05-29 12:18:52 -0700137 mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMediumTextSize);
138 mTitle.setBackgroundDrawable(mUrlBg);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700139 } 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 Kolbdb343c52011-05-29 12:18:52 -0700146 mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, mSmallTextSize);
147 mTitle.setBackgroundDrawable(null);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700148 }
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 Kolb0f91e032011-06-01 09:54:20 -0700173 mProxy = new WebProxyView(mContext, mWebView);
174 mContainer.addView(mProxy, 0);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700175 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 Kolb0f91e032011-06-01 09:54:20 -0700194 if (mProxy != null) {
195 mProxy.setOnClickListener(mClickListener);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700196 }
197 }
198
Michael Kolb0f91e032011-06-01 09:54:20 -0700199 @Override
200 public void onDetachedFromWindow() {
201 mWebView.setProxyView(null);
202 }
203
Michael Kolb4bd767d2011-05-27 11:33:55 -0700204 private static void removeFromParent(View v) {
205 if (v.getParent() != null) {
206 ((ViewGroup) v.getParent()).removeView(v);
207 }
208 }
209
Michael Kolb0f91e032011-06-01 09:54:20 -0700210 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 Kolb4bd767d2011-05-27 11:33:55 -0700229}