blob: 5e27eab0243a738d2e26f09f4e9940fb76e7c2a0 [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;
Leon Scrogginsa3315562009-09-18 14:21:08 -040021import android.util.AttributeSet;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040022import android.view.LayoutInflater;
23import android.view.View;
John Reck18e93f72011-03-21 11:46:09 -070024import android.view.View.OnClickListener;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040025import android.view.ViewGroup;
John Reck18e93f72011-03-21 11:46:09 -070026import android.widget.AbsListView;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040027import android.widget.AdapterView;
John Reck18e93f72011-03-21 11:46:09 -070028import android.widget.AdapterView.OnItemClickListener;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040029import android.widget.BaseAdapter;
Leon Scrogginsa3315562009-09-18 14:21:08 -040030import android.widget.ImageView;
John Reck18e93f72011-03-21 11:46:09 -070031import android.widget.ImageView.ScaleType;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040032import android.widget.LinearLayout;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040033import android.widget.TextView;
34
John Reck18e93f72011-03-21 11:46:09 -070035interface OnCloseTab {
36 void onCloseTab(int position);
37}
Leon Scroggins0a64ba52009-09-08 15:35:33 -040038
John Reck18e93f72011-03-21 11:46:09 -070039public class ActiveTabsPage extends LinearLayout implements OnClickListener,
40 OnItemClickListener, OnCloseTab {
Michael Kolb8233fac2010-10-26 16:08:53 -070041
John Reck18e93f72011-03-21 11:46:09 -070042 private Context mContext;
43 private UiController mController;
44 private TabControl mTabControl;
45 private View mNewTab, mNewIncognitoTab;
46 private TabAdapter mAdapter;
47 private AbsListView mTabsList;
Michael Kolb8233fac2010-10-26 16:08:53 -070048
John Reck18e93f72011-03-21 11:46:09 -070049 public ActiveTabsPage(Context context, UiController controller) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -040050 super(context);
John Reck18e93f72011-03-21 11:46:09 -070051 mContext = context;
52 mController = controller;
53 mTabControl = mController.getTabControl();
54 setOrientation(VERTICAL);
55 setBackgroundResource(R.drawable.bg_browser);
56 LayoutInflater inflate = LayoutInflater.from(mContext);
57 inflate.inflate(R.layout.active_tabs, this, true);
58 mNewTab = findViewById(R.id.new_tab);
59 mNewIncognitoTab = findViewById(R.id.new_incognito_tab);
60 mNewTab.setOnClickListener(this);
61 mNewIncognitoTab.setOnClickListener(this);
62 int visibility = mTabControl.canCreateNewTab() ? View.VISIBLE : View.GONE;
63 mNewTab.setVisibility(visibility);
64 mNewIncognitoTab.setVisibility(visibility);
65 mTabsList = (AbsListView) findViewById(android.R.id.list);
66 mAdapter = new TabAdapter(mContext, mTabControl);
67 mAdapter.setOnCloseListener(this);
68 mTabsList.setAdapter(mAdapter);
69 mTabsList.setOnItemClickListener(this);
70 }
71
72 @Override
73 public void onClick(View v) {
74 if (v == mNewTab) {
75 mController.openTabToHomePage();
76 } else if (v == mNewIncognitoTab) {
77 mController.openIncognitoTab();
78 }
79 mController.removeActiveTabsPage(false);
80 }
81
82 @Override
83 public void onItemClick(
84 AdapterView<?> parent, View view, int position, long id) {
85 boolean needToAttach = !mController.switchToTab(position);
86 mController.removeActiveTabsPage(needToAttach);
87 }
88
89 @Override
90 public void onCloseTab(int position) {
91 Tab tab = mTabControl.getTab(position);
92 if (tab != null) {
93 mController.closeTab(tab);
94 if (mTabControl.getTabCount() == 0) {
95 mController.openTabToHomePage();
96 mController.removeActiveTabsPage(false);
97 } else {
98 mAdapter.notifyDataSetChanged();
99 }
100 }
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400101 }
102
Leon Scrogginsa3315562009-09-18 14:21:08 -0400103 /**
104 * Special class to hold the close drawable. Its sole purpose is to allow
105 * the parent to be pressed without being pressed itself. This way the line
106 * of a tab can be pressed, but the close button itself is not.
107 */
John Reck18e93f72011-03-21 11:46:09 -0700108 public static class CloseHolder extends ImageView {
Leon Scrogginsa3315562009-09-18 14:21:08 -0400109 public CloseHolder(Context context, AttributeSet attrs) {
110 super(context, attrs);
111 }
112
113 @Override
114 public void setPressed(boolean pressed) {
115 // If the parent is pressed, do not set to pressed.
116 if (pressed && ((View) getParent()).isPressed()) {
117 return;
118 }
119 super.setPressed(pressed);
120 }
121 }
122
John Reck18e93f72011-03-21 11:46:09 -0700123 static class TabAdapter extends BaseAdapter implements OnClickListener {
Patrick Scott49b11f92009-12-14 09:32:13 -0500124
John Reck18e93f72011-03-21 11:46:09 -0700125 LayoutInflater mInflater;
126 OnCloseTab mCloseListener;
127 TabControl mTabControl;
128
129 TabAdapter(Context context, TabControl tabs) {
130 mInflater = LayoutInflater.from(context);
131 mTabControl = tabs;
132 }
133
134 void setOnCloseListener(OnCloseTab listener) {
135 mCloseListener = listener;
136 }
137
138 @Override
139 public View getView(int position, View view, ViewGroup parent) {
140 if (view == null) {
141 view = mInflater.inflate(R.layout.tab_view, parent, false);
142 }
143 ImageView favicon = (ImageView) view.findViewById(R.id.favicon);
144 TextView title = (TextView) view.findViewById(R.id.title);
145 TextView url = (TextView) view.findViewById(R.id.url);
146 Tab tab = getItem(position);
147
148 title.setText(tab.getTitle());
149 url.setText(tab.getUrl());
150 Bitmap faviconBitmap = tab.getFavicon();
151 if (tab.isPrivateBrowsingEnabled()) {
152 favicon.setImageResource(R.drawable.ic_incognito_holo_dark);
153 favicon.setBackgroundDrawable(null);
154 } else {
155 if (faviconBitmap == null) {
156 favicon.setImageResource(R.drawable.app_web_browser_sm);
157 } else {
158 favicon.setImageBitmap(faviconBitmap);
159 }
160 favicon.setBackgroundResource(R.drawable.bookmark_list_favicon_bg);
161 }
162 View close = view.findViewById(R.id.close);
163 close.setTag(position);
164 close.setOnClickListener(this);
165 return view;
166 }
167
168 @Override
169 public void onClick(View v) {
170 int position = (Integer) v.getTag();
171 if (mCloseListener != null) {
172 mCloseListener.onCloseTab(position);
173 }
174 }
175
176 @Override
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400177 public int getCount() {
John Reck18e93f72011-03-21 11:46:09 -0700178 return mTabControl.getTabCount();
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400179 }
John Reck18e93f72011-03-21 11:46:09 -0700180
181 @Override
182 public Tab getItem(int position) {
183 return mTabControl.getTab(position);
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400184 }
John Reck18e93f72011-03-21 11:46:09 -0700185
186 @Override
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400187 public long getItemId(int position) {
188 return position;
189 }
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400190 }
191}