blob: 7b547b82f59be27703d706bdddb503e7839d6b1c [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;
Michael Kolb9829b432011-06-04 13:29:00 -070039 private ImageView mClose;
Michael Kolb0f91e032011-06-01 09:54:20 -070040 private FrameLayout mContainer;
41 private TextView mTitle;
42 private View mTitleBar;
43 private OnClickListener mClickListener;
44 private boolean mHighlighted;
Michael Kolb4bd767d2011-05-27 11:33:55 -070045
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() {
Michael Kolb9829b432011-06-04 13:29:00 -070062 LayoutInflater.from(mContext).inflate(R.layout.nav_tab_view, this);
Michael Kolb4bd767d2011-05-27 11:33:55 -070063 mContainer = (FrameLayout) findViewById(R.id.tab_view);
Michael Kolb9829b432011-06-04 13:29:00 -070064 mClose = (ImageView) findViewById(R.id.closetab);
Michael Kolb4bd767d2011-05-27 11:33:55 -070065 mTitle = (TextView) findViewById(R.id.title);
Michael Kolb4bd767d2011-05-27 11:33:55 -070066 mTitleBar = findViewById(R.id.titlebar);
Michael Kolb4bd767d2011-05-27 11:33:55 -070067 }
68
69 protected boolean isClose(View v) {
70 return v == mClose;
71 }
72
73 protected boolean isTitle(View v) {
74 return v == mTitleBar;
75 }
76
Michael Kolb4bd767d2011-05-27 11:33:55 -070077 protected boolean isWebView(View v) {
Michael Kolb0f91e032011-06-01 09:54:20 -070078 return v == mProxy;
Michael Kolb4bd767d2011-05-27 11:33:55 -070079 }
80
81 protected void setHighlighted(boolean highlighted) {
82 if (highlighted == mHighlighted) return;
83 mHighlighted = highlighted;
Michael Kolb4bd767d2011-05-27 11:33:55 -070084 }
85
86 private void setTitle() {
87 if (mTab == null) return;
88 if (mHighlighted) {
89 mTitle.setText(mTab.getUrl());
90 } else {
91 String txt = mTab.getTitle();
Michael Kolb9829b432011-06-04 13:29:00 -070092 if (txt == null) {
93 txt = mTab.getUrl();
94 }
Michael Kolb4bd767d2011-05-27 11:33:55 -070095 mTitle.setText(txt);
96 }
97 }
98
99 protected boolean isHighlighted() {
100 return mHighlighted;
101 }
102
103 protected void setWebView(PhoneUi ui, Tab tab) {
104 mTab = tab;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700105 setTitle();
John Reckd8c74522011-06-14 08:45:00 -0700106 BrowserWebView web = (BrowserWebView) tab.getWebView();
107 if (web != null) {
108 mWebView = web;
109 removeFromParent(mWebView);
110 mProxy = new WebProxyView(mContext, mWebView);
111 mContainer.addView(mProxy, 0);
112 }
Michael Kolb4bd767d2011-05-27 11:33:55 -0700113 }
114
115 protected void hideTitle() {
116 mTitleBar.setVisibility(View.INVISIBLE);
117 }
118
119 @Override
120 public void setOnClickListener(OnClickListener listener) {
121 mClickListener = listener;
122 mTitleBar.setOnClickListener(mClickListener);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700123 mClose.setOnClickListener(mClickListener);
Michael Kolb0f91e032011-06-01 09:54:20 -0700124 if (mProxy != null) {
125 mProxy.setOnClickListener(mClickListener);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700126 }
127 }
128
Michael Kolb0f91e032011-06-01 09:54:20 -0700129 @Override
130 public void onDetachedFromWindow() {
Michael Kolbdbf39812011-06-10 14:06:40 -0700131 if (mWebView != null) {
132 mWebView.setProxyView(null);
133 }
Michael Kolb0f91e032011-06-01 09:54:20 -0700134 }
135
Michael Kolb9829b432011-06-04 13:29:00 -0700136 @Override
137 public void onAttachedToWindow() {
138 if (mWebView != null) {
139 mWebView.invalidate();
140 }
141 }
142
Michael Kolb4bd767d2011-05-27 11:33:55 -0700143 private static void removeFromParent(View v) {
144 if (v.getParent() != null) {
145 ((ViewGroup) v.getParent()).removeView(v);
146 }
147 }
148
Michael Kolb0f91e032011-06-01 09:54:20 -0700149 static class WebProxyView extends View {
150
151 private BrowserWebView mWeb;
152
153 public WebProxyView(Context context, BrowserWebView web) {
154 super(context);
155 setWillNotDraw(false);
156 mWeb = web;
157 mWeb.setProxyView(this);
158
159 }
160
161 public void onDraw(Canvas c) {
Michael Kolb9829b432011-06-04 13:29:00 -0700162 float scale = 0.7f;
163 int sx = mWeb.getScrollX();
164 int sy = mWeb.getScrollY();
165 c.scale(scale, scale);
166 c.translate(-sx, -sy);
Michael Kolb0f91e032011-06-01 09:54:20 -0700167 mWeb.onDraw(c);
168 }
169
170 }
171
Michael Kolb4bd767d2011-05-27 11:33:55 -0700172}