blob: b15e828f027d343bb68280eaef25c8aea65e5e39 [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 Kolb9ef259a2011-07-12 15:33:08 -070020import android.graphics.Bitmap;
Michael Kolb4bd767d2011-05-27 11:33:55 -070021import android.util.AttributeSet;
22import android.view.LayoutInflater;
23import android.view.View;
Michael Kolbc3af0672011-08-09 10:24:41 -070024import android.view.ViewGroup;
25import android.webkit.WebView;
Michael Kolb4bd767d2011-05-27 11:33:55 -070026import android.widget.ImageView;
27import android.widget.LinearLayout;
28import android.widget.TextView;
29
30public class NavTabView extends LinearLayout {
31
Michael Kolbc3af0672011-08-09 10:24:41 -070032 private ViewGroup mContent;
Michael Kolb0f91e032011-06-01 09:54:20 -070033 private Tab mTab;
Michael Kolb9829b432011-06-04 13:29:00 -070034 private ImageView mClose;
Michael Kolb0f91e032011-06-01 09:54:20 -070035 private TextView mTitle;
36 private View mTitleBar;
John Reck8ee633f2011-08-09 16:00:35 -070037 ImageView mImage;
Michael Kolb0f91e032011-06-01 09:54:20 -070038 private OnClickListener mClickListener;
39 private boolean mHighlighted;
Michael Kolb4bd767d2011-05-27 11:33:55 -070040
41 public NavTabView(Context context, AttributeSet attrs, int defStyle) {
42 super(context, attrs, defStyle);
43 init();
44 }
45
46 public NavTabView(Context context, AttributeSet attrs) {
47 super(context, attrs);
48 init();
49 }
50
51 public NavTabView(Context context) {
52 super(context);
53 init();
54 }
55
56 private void init() {
Michael Kolb9829b432011-06-04 13:29:00 -070057 LayoutInflater.from(mContext).inflate(R.layout.nav_tab_view, this);
Michael Kolbc3af0672011-08-09 10:24:41 -070058 mContent = (ViewGroup) findViewById(R.id.main);
Michael Kolb9829b432011-06-04 13:29:00 -070059 mClose = (ImageView) findViewById(R.id.closetab);
Michael Kolb4bd767d2011-05-27 11:33:55 -070060 mTitle = (TextView) findViewById(R.id.title);
Michael Kolb4bd767d2011-05-27 11:33:55 -070061 mTitleBar = findViewById(R.id.titlebar);
Michael Kolb9ef259a2011-07-12 15:33:08 -070062 mImage = (ImageView) findViewById(R.id.tab_view);
Michael Kolb4bd767d2011-05-27 11:33:55 -070063 }
64
65 protected boolean isClose(View v) {
66 return v == mClose;
67 }
68
69 protected boolean isTitle(View v) {
70 return v == mTitleBar;
71 }
72
Michael Kolb4bd767d2011-05-27 11:33:55 -070073 protected boolean isWebView(View v) {
Michael Kolb9ef259a2011-07-12 15:33:08 -070074 return v == mImage;
Michael Kolb4bd767d2011-05-27 11:33:55 -070075 }
76
Michael Kolb4bd767d2011-05-27 11:33:55 -070077 private void setTitle() {
78 if (mTab == null) return;
79 if (mHighlighted) {
80 mTitle.setText(mTab.getUrl());
81 } else {
82 String txt = mTab.getTitle();
Michael Kolb9829b432011-06-04 13:29:00 -070083 if (txt == null) {
84 txt = mTab.getUrl();
85 }
Michael Kolb4bd767d2011-05-27 11:33:55 -070086 mTitle.setText(txt);
87 }
John Reck502a3532011-08-16 14:21:46 -070088 if (mTab.isSnapshot()) {
89 setTitleIcon(R.drawable.ic_history_holo_dark);
90 } else if (mTab.isPrivateBrowsingEnabled()) {
91 setTitleIcon(R.drawable.ic_incognito_holo_dark);
92 } else {
93 setTitleIcon(0);
94 }
95 }
96
97 private void setTitleIcon(int id) {
98 if (id == 0) {
99 mTitle.setPadding(mTitle.getCompoundDrawablePadding(), 0, 0, 0);
100 } else {
101 mTitle.setPadding(0, 0, 0, 0);
102 }
103 mTitle.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700104 }
105
106 protected boolean isHighlighted() {
107 return mHighlighted;
108 }
109
Michael Kolbc3af0672011-08-09 10:24:41 -0700110 protected void setWebView(Tab tab) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700111 mTab = tab;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700112 setTitle();
Michael Kolb9ef259a2011-07-12 15:33:08 -0700113 Bitmap image = tab.getScreenshot();
114 if (image != null) {
115 mImage.setImageBitmap(image);
Michael Kolba9a59672011-11-17 12:50:10 -0800116 if (tab != null) {
117 mImage.setContentDescription(tab.getTitle());
118 }
John Reckd8c74522011-06-14 08:45:00 -0700119 }
Michael Kolb4bd767d2011-05-27 11:33:55 -0700120 }
121
Michael Kolb4bd767d2011-05-27 11:33:55 -0700122 @Override
123 public void setOnClickListener(OnClickListener listener) {
124 mClickListener = listener;
125 mTitleBar.setOnClickListener(mClickListener);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700126 mClose.setOnClickListener(mClickListener);
Michael Kolb9ef259a2011-07-12 15:33:08 -0700127 if (mImage != null) {
128 mImage.setOnClickListener(mClickListener);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700129 }
130 }
131
Michael Kolb4bd767d2011-05-27 11:33:55 -0700132}