blob: 0feba9a4fd2cc87dc598a7fe75c0fd6648ec9da5 [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) {
Michael Kolbc831b632011-05-11 09:30:34 -070086 final Tab tab = mTabControl.getTab(position);
87 boolean needToAttach = !mController.switchToTab(tab);
John Reck18e93f72011-03-21 11:46:09 -070088 mController.removeActiveTabsPage(needToAttach);
89 }
90
91 @Override
92 public void onCloseTab(int position) {
93 Tab tab = mTabControl.getTab(position);
94 if (tab != null) {
95 mController.closeTab(tab);
96 if (mTabControl.getTabCount() == 0) {
97 mController.openTabToHomePage();
98 mController.removeActiveTabsPage(false);
99 } else {
100 mAdapter.notifyDataSetChanged();
101 }
102 }
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400103 }
104
Leon Scrogginsa3315562009-09-18 14:21:08 -0400105 /**
106 * Special class to hold the close drawable. Its sole purpose is to allow
107 * the parent to be pressed without being pressed itself. This way the line
108 * of a tab can be pressed, but the close button itself is not.
109 */
John Reck18e93f72011-03-21 11:46:09 -0700110 public static class CloseHolder extends ImageView {
Leon Scrogginsa3315562009-09-18 14:21:08 -0400111 public CloseHolder(Context context, AttributeSet attrs) {
112 super(context, attrs);
113 }
114
115 @Override
116 public void setPressed(boolean pressed) {
117 // If the parent is pressed, do not set to pressed.
118 if (pressed && ((View) getParent()).isPressed()) {
119 return;
120 }
121 super.setPressed(pressed);
122 }
123 }
124
John Reck18e93f72011-03-21 11:46:09 -0700125 static class TabAdapter extends BaseAdapter implements OnClickListener {
Patrick Scott49b11f92009-12-14 09:32:13 -0500126
John Reck18e93f72011-03-21 11:46:09 -0700127 LayoutInflater mInflater;
128 OnCloseTab mCloseListener;
129 TabControl mTabControl;
130
131 TabAdapter(Context context, TabControl tabs) {
132 mInflater = LayoutInflater.from(context);
133 mTabControl = tabs;
134 }
135
136 void setOnCloseListener(OnCloseTab listener) {
137 mCloseListener = listener;
138 }
139
140 @Override
141 public View getView(int position, View view, ViewGroup parent) {
142 if (view == null) {
143 view = mInflater.inflate(R.layout.tab_view, parent, false);
144 }
145 ImageView favicon = (ImageView) view.findViewById(R.id.favicon);
John Reck88a42b72011-03-21 16:30:12 -0700146 ImageView thumbnail = (ImageView) view.findViewById(R.id.thumb);
147 TextView title = (TextView) view.findViewById(R.id.label);
John Reck18e93f72011-03-21 11:46:09 -0700148 Tab tab = getItem(position);
149
John Reck88a42b72011-03-21 16:30:12 -0700150 String label = tab.getTitle();
151 if (TextUtils.isEmpty(label)) {
152 label = tab.getUrl();
153 }
154 title.setText(label);
155 Bitmap thumbnailBitmap = tab.getScreenshot();
156 if (thumbnailBitmap == null) {
157 thumbnail.setImageResource(R.drawable.browser_thumbnail);
158 } else {
159 thumbnail.setImageBitmap(thumbnailBitmap);
160 }
John Reck18e93f72011-03-21 11:46:09 -0700161 Bitmap faviconBitmap = tab.getFavicon();
162 if (tab.isPrivateBrowsingEnabled()) {
163 favicon.setImageResource(R.drawable.ic_incognito_holo_dark);
John Reck18e93f72011-03-21 11:46:09 -0700164 } else {
165 if (faviconBitmap == null) {
166 favicon.setImageResource(R.drawable.app_web_browser_sm);
167 } else {
168 favicon.setImageBitmap(faviconBitmap);
169 }
John Reck18e93f72011-03-21 11:46:09 -0700170 }
171 View close = view.findViewById(R.id.close);
172 close.setTag(position);
173 close.setOnClickListener(this);
174 return view;
175 }
176
177 @Override
178 public void onClick(View v) {
179 int position = (Integer) v.getTag();
180 if (mCloseListener != null) {
181 mCloseListener.onCloseTab(position);
182 }
183 }
184
185 @Override
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400186 public int getCount() {
John Reck18e93f72011-03-21 11:46:09 -0700187 return mTabControl.getTabCount();
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400188 }
John Reck18e93f72011-03-21 11:46:09 -0700189
190 @Override
191 public Tab getItem(int position) {
192 return mTabControl.getTab(position);
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400193 }
John Reck18e93f72011-03-21 11:46:09 -0700194
195 @Override
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400196 public long getItemId(int position) {
197 return position;
198 }
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400199 }
200}