blob: 4eec702f445f80dac0f3d7748898c2408eeaefae [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
77 protected void setHighlighted(boolean highlighted) {
78 if (highlighted == mHighlighted) return;
79 mHighlighted = highlighted;
Michael Kolb4bd767d2011-05-27 11:33:55 -070080 }
81
82 private void setTitle() {
83 if (mTab == null) return;
84 if (mHighlighted) {
85 mTitle.setText(mTab.getUrl());
86 } else {
87 String txt = mTab.getTitle();
Michael Kolb9829b432011-06-04 13:29:00 -070088 if (txt == null) {
89 txt = mTab.getUrl();
90 }
Michael Kolb4bd767d2011-05-27 11:33:55 -070091 mTitle.setText(txt);
92 }
John Reck502a3532011-08-16 14:21:46 -070093 if (mTab.isSnapshot()) {
94 setTitleIcon(R.drawable.ic_history_holo_dark);
95 } else if (mTab.isPrivateBrowsingEnabled()) {
96 setTitleIcon(R.drawable.ic_incognito_holo_dark);
97 } else {
98 setTitleIcon(0);
99 }
100 }
101
102 private void setTitleIcon(int id) {
103 if (id == 0) {
104 mTitle.setPadding(mTitle.getCompoundDrawablePadding(), 0, 0, 0);
105 } else {
106 mTitle.setPadding(0, 0, 0, 0);
107 }
108 mTitle.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700109 }
110
111 protected boolean isHighlighted() {
112 return mHighlighted;
113 }
114
Michael Kolbc3af0672011-08-09 10:24:41 -0700115 protected void setWebView(WebView w) {
116 mContent.addView(w, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
117 }
118
119 protected void setWebView(Tab tab) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700120 mTab = tab;
Michael Kolb4bd767d2011-05-27 11:33:55 -0700121 setTitle();
Michael Kolb9ef259a2011-07-12 15:33:08 -0700122 Bitmap image = tab.getScreenshot();
123 if (image != null) {
124 mImage.setImageBitmap(image);
John Reckd8c74522011-06-14 08:45:00 -0700125 }
Michael Kolb4bd767d2011-05-27 11:33:55 -0700126 }
127
128 protected void hideTitle() {
129 mTitleBar.setVisibility(View.INVISIBLE);
130 }
131
132 @Override
133 public void setOnClickListener(OnClickListener listener) {
134 mClickListener = listener;
135 mTitleBar.setOnClickListener(mClickListener);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700136 mClose.setOnClickListener(mClickListener);
Michael Kolb9ef259a2011-07-12 15:33:08 -0700137 if (mImage != null) {
138 mImage.setOnClickListener(mClickListener);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700139 }
140 }
141
Michael Kolb4bd767d2011-05-27 11:33:55 -0700142}