blob: 52d943fc4303435725577468dea7db045f9aba95 [file] [log] [blame]
Leon Scroggins0a64ba52009-09-08 15:35:33 -04001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Leon Scrogginsa3315562009-09-18 14:21:08 -040019import android.content.Context;
Patrick Scott555c5802009-09-23 08:08:17 -040020import android.graphics.Bitmap;
John Reck88a42b72011-03-21 16:30:12 -070021import android.text.TextUtils;
Leon Scrogginsa3315562009-09-18 14:21:08 -040022import android.util.AttributeSet;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040023import android.view.LayoutInflater;
24import android.view.View;
John Reck18e93f72011-03-21 11:46:09 -070025import android.view.View.OnClickListener;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040026import android.view.ViewGroup;
John Reck18e93f72011-03-21 11:46:09 -070027import android.widget.AbsListView;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040028import android.widget.AdapterView;
John Reck18e93f72011-03-21 11:46:09 -070029import android.widget.AdapterView.OnItemClickListener;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040030import android.widget.BaseAdapter;
Leon Scrogginsa3315562009-09-18 14:21:08 -040031import android.widget.ImageView;
John Reck18e93f72011-03-21 11:46:09 -070032import android.widget.ImageView.ScaleType;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040033import android.widget.LinearLayout;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040034import android.widget.TextView;
35
John Reck18e93f72011-03-21 11:46:09 -070036interface OnCloseTab {
37 void onCloseTab(int position);
38}
Leon Scroggins0a64ba52009-09-08 15:35:33 -040039
John Reck18e93f72011-03-21 11:46:09 -070040public class ActiveTabsPage extends LinearLayout implements OnClickListener,
41 OnItemClickListener, OnCloseTab {
Michael Kolb8233fac2010-10-26 16:08:53 -070042
John Reck18e93f72011-03-21 11:46:09 -070043 private Context mContext;
44 private UiController mController;
45 private TabControl mTabControl;
46 private View mNewTab, mNewIncognitoTab;
47 private TabAdapter mAdapter;
48 private AbsListView mTabsList;
Michael Kolb8233fac2010-10-26 16:08:53 -070049
John Reck18e93f72011-03-21 11:46:09 -070050 public ActiveTabsPage(Context context, UiController controller) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -040051 super(context);
John Reck18e93f72011-03-21 11:46:09 -070052 mContext = context;
53 mController = controller;
54 mTabControl = mController.getTabControl();
55 setOrientation(VERTICAL);
56 setBackgroundResource(R.drawable.bg_browser);
57 LayoutInflater inflate = LayoutInflater.from(mContext);
58 inflate.inflate(R.layout.active_tabs, this, true);
59 mNewTab = findViewById(R.id.new_tab);
60 mNewIncognitoTab = findViewById(R.id.new_incognito_tab);
61 mNewTab.setOnClickListener(this);
62 mNewIncognitoTab.setOnClickListener(this);
63 int visibility = mTabControl.canCreateNewTab() ? View.VISIBLE : View.GONE;
64 mNewTab.setVisibility(visibility);
65 mNewIncognitoTab.setVisibility(visibility);
66 mTabsList = (AbsListView) findViewById(android.R.id.list);
67 mAdapter = new TabAdapter(mContext, mTabControl);
68 mAdapter.setOnCloseListener(this);
69 mTabsList.setAdapter(mAdapter);
70 mTabsList.setOnItemClickListener(this);
71 }
72
73 @Override
74 public void onClick(View v) {
75 if (v == mNewTab) {
76 mController.openTabToHomePage();
77 } else if (v == mNewIncognitoTab) {
Michael Kolb519d2282011-05-09 17:03:19 -070078 mController.openIncognitoTab();
John Reck18e93f72011-03-21 11:46:09 -070079 }
80 mController.removeActiveTabsPage(false);
81 }
82
83 @Override
84 public void onItemClick(
85 AdapterView<?> parent, View view, int position, long id) {
86 boolean needToAttach = !mController.switchToTab(position);
87 mController.removeActiveTabsPage(needToAttach);
88 }
89
90 @Override
91 public void onCloseTab(int position) {
92 Tab tab = mTabControl.getTab(position);
93 if (tab != null) {
94 mController.closeTab(tab);
95 if (mTabControl.getTabCount() == 0) {
96 mController.openTabToHomePage();
97 mController.removeActiveTabsPage(false);
98 } else {
99 mAdapter.notifyDataSetChanged();
100 }
101 }
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400102 }
103
Leon Scrogginsa3315562009-09-18 14:21:08 -0400104 /**
105 * Special class to hold the close drawable. Its sole purpose is to allow
106 * the parent to be pressed without being pressed itself. This way the line
107 * of a tab can be pressed, but the close button itself is not.
108 */
John Reck18e93f72011-03-21 11:46:09 -0700109 public static class CloseHolder extends ImageView {
Leon Scrogginsa3315562009-09-18 14:21:08 -0400110 public CloseHolder(Context context, AttributeSet attrs) {
111 super(context, attrs);
112 }
113
114 @Override
115 public void setPressed(boolean pressed) {
116 // If the parent is pressed, do not set to pressed.
117 if (pressed && ((View) getParent()).isPressed()) {
118 return;
119 }
120 super.setPressed(pressed);
121 }
122 }
123
John Reck18e93f72011-03-21 11:46:09 -0700124 static class TabAdapter extends BaseAdapter implements OnClickListener {
Patrick Scott49b11f92009-12-14 09:32:13 -0500125
John Reck18e93f72011-03-21 11:46:09 -0700126 LayoutInflater mInflater;
127 OnCloseTab mCloseListener;
128 TabControl mTabControl;
129
130 TabAdapter(Context context, TabControl tabs) {
131 mInflater = LayoutInflater.from(context);
132 mTabControl = tabs;
133 }
134
135 void setOnCloseListener(OnCloseTab listener) {
136 mCloseListener = listener;
137 }
138
139 @Override
140 public View getView(int position, View view, ViewGroup parent) {
141 if (view == null) {
142 view = mInflater.inflate(R.layout.tab_view, parent, false);
143 }
144 ImageView favicon = (ImageView) view.findViewById(R.id.favicon);
John Reck88a42b72011-03-21 16:30:12 -0700145 ImageView thumbnail = (ImageView) view.findViewById(R.id.thumb);
146 TextView title = (TextView) view.findViewById(R.id.label);
John Reck18e93f72011-03-21 11:46:09 -0700147 Tab tab = getItem(position);
148
John Reck88a42b72011-03-21 16:30:12 -0700149 String label = tab.getTitle();
150 if (TextUtils.isEmpty(label)) {
151 label = tab.getUrl();
152 }
153 title.setText(label);
154 Bitmap thumbnailBitmap = tab.getScreenshot();
155 if (thumbnailBitmap == null) {
156 thumbnail.setImageResource(R.drawable.browser_thumbnail);
157 } else {
158 thumbnail.setImageBitmap(thumbnailBitmap);
159 }
John Reck18e93f72011-03-21 11:46:09 -0700160 Bitmap faviconBitmap = tab.getFavicon();
161 if (tab.isPrivateBrowsingEnabled()) {
162 favicon.setImageResource(R.drawable.ic_incognito_holo_dark);
John Reck18e93f72011-03-21 11:46:09 -0700163 } else {
164 if (faviconBitmap == null) {
165 favicon.setImageResource(R.drawable.app_web_browser_sm);
166 } else {
167 favicon.setImageBitmap(faviconBitmap);
168 }
John Reck18e93f72011-03-21 11:46:09 -0700169 }
170 View close = view.findViewById(R.id.close);
171 close.setTag(position);
172 close.setOnClickListener(this);
173 return view;
174 }
175
176 @Override
177 public void onClick(View v) {
178 int position = (Integer) v.getTag();
179 if (mCloseListener != null) {
180 mCloseListener.onCloseTab(position);
181 }
182 }
183
184 @Override
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400185 public int getCount() {
John Reck18e93f72011-03-21 11:46:09 -0700186 return mTabControl.getTabCount();
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400187 }
John Reck18e93f72011-03-21 11:46:09 -0700188
189 @Override
190 public Tab getItem(int position) {
191 return mTabControl.getTab(position);
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400192 }
John Reck18e93f72011-03-21 11:46:09 -0700193
194 @Override
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400195 public long getItemId(int position) {
196 return position;
197 }
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400198 }
199}