blob: 72f291c56e34f97ae72a9787da1be82c08a4697d [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.KeyEvent;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.AdapterView;
27import android.widget.BaseAdapter;
Leon Scrogginsa3315562009-09-18 14:21:08 -040028import android.widget.ImageView;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040029import android.widget.LinearLayout;
30import android.widget.ListView;
31import android.widget.TextView;
32
33public class ActiveTabsPage extends LinearLayout {
34 private final BrowserActivity mBrowserActivity;
35 private final LayoutInflater mFactory;
36 private final TabControl mControl;
37 private final TabsListAdapter mAdapter;
38 private final ListView mListView;
39
40 public ActiveTabsPage(BrowserActivity context, TabControl control) {
41 super(context);
42 mBrowserActivity = context;
43 mControl = control;
44 mFactory = LayoutInflater.from(context);
45 mFactory.inflate(R.layout.active_tabs, this);
46 mListView = (ListView) findViewById(R.id.list);
47 mAdapter = new TabsListAdapter();
48 mListView.setAdapter(mAdapter);
49 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
50 public void onItemClick(AdapterView<?> parent, View view,
51 int position, long id) {
Grace Kloba22ac16e2009-10-07 18:00:23 -070052 if (mControl.canCreateNewTab()) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -040053 position--;
54 }
55 boolean needToAttach = false;
56 if (position == -1) {
57 // Create a new tab
58 mBrowserActivity.openTabToHomePage();
59 } else {
60 // Open the corresponding tab
61 // If the tab is the current one, switchToTab will
62 // do nothing and return, so we need to make sure
63 // it gets attached back to its mContentView in
64 // removeActiveTabPage
65 needToAttach = !mBrowserActivity.switchToTab(position);
66 }
67 mBrowserActivity.removeActiveTabPage(needToAttach);
68 }
69 });
70 }
71
Leon Scrogginsa3315562009-09-18 14:21:08 -040072 /**
73 * Special class to hold the close drawable. Its sole purpose is to allow
74 * the parent to be pressed without being pressed itself. This way the line
75 * of a tab can be pressed, but the close button itself is not.
76 */
77 private static class CloseHolder extends ImageView {
78 public CloseHolder(Context context, AttributeSet attrs) {
79 super(context, attrs);
80 }
81
82 @Override
83 public void setPressed(boolean pressed) {
84 // If the parent is pressed, do not set to pressed.
85 if (pressed && ((View) getParent()).isPressed()) {
86 return;
87 }
88 super.setPressed(pressed);
89 }
90 }
91
Leon Scroggins0a64ba52009-09-08 15:35:33 -040092 private class TabsListAdapter extends BaseAdapter {
93 public int getCount() {
94 int count = mControl.getTabCount();
Grace Kloba22ac16e2009-10-07 18:00:23 -070095 if (mControl.canCreateNewTab()) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -040096 count++;
97 }
98 return count;
99 }
100 public Object getItem(int position) {
101 return null;
102 }
103 public long getItemId(int position) {
104 return position;
105 }
Patrick Scott555c5802009-09-23 08:08:17 -0400106 public int getViewTypeCount() {
107 return 2;
108 }
109 public int getItemViewType(int position) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700110 if (mControl.canCreateNewTab()) {
Patrick Scott555c5802009-09-23 08:08:17 -0400111 position--;
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400112 }
Patrick Scott555c5802009-09-23 08:08:17 -0400113 // Do not recycle the "add new tab" item.
114 return position == -1 ? IGNORE_ITEM_VIEW_TYPE : 1;
115 }
116 public View getView(int position, View convertView, ViewGroup parent) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400117 final int tabCount = mControl.getTabCount();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700118 if (mControl.canCreateNewTab()) {
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400119 position--;
120 }
Patrick Scott555c5802009-09-23 08:08:17 -0400121
122 if (convertView == null) {
123 convertView = mFactory.inflate(position == -1 ?
124 R.layout.tab_view_add_tab : R.layout.tab_view, null);
125 }
126
127 if (position != -1) {
128 TextView title =
129 (TextView) convertView.findViewById(R.id.title);
130 TextView url = (TextView) convertView.findViewById(R.id.url);
131 ImageView favicon =
132 (ImageView) convertView.findViewById(R.id.favicon);
133 View close = convertView.findViewById(R.id.close);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700134 Tab tab = mControl.getTab(position);
135 tab.populatePickerData();
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400136 title.setText(tab.getTitle());
137 url.setText(tab.getUrl());
Patrick Scott555c5802009-09-23 08:08:17 -0400138 Bitmap icon = tab.getFavicon();
139 if (icon != null) {
140 favicon.setImageBitmap(icon);
141 } else {
142 favicon.setImageResource(R.drawable.app_web_browser_sm);
143 }
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400144 final int closePosition = position;
145 close.setOnClickListener(new View.OnClickListener() {
146 public void onClick(View v) {
147 mBrowserActivity.closeTab(
148 mControl.getTab(closePosition));
149 if (tabCount == 1) {
150 mBrowserActivity.openTabToHomePage();
151 mBrowserActivity.removeActiveTabPage(false);
152 } else {
153 mListView.setAdapter(mAdapter);
154 }
155 }
156 });
157 }
158 return convertView;
159 }
160 }
161}