blob: e589d42f1197524054734211534c0fbffbcf1948 [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;
20import android.util.AttributeSet;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040021import android.view.KeyEvent;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.AdapterView;
26import android.widget.BaseAdapter;
Leon Scrogginsa3315562009-09-18 14:21:08 -040027import android.widget.ImageView;
Leon Scroggins0a64ba52009-09-08 15:35:33 -040028import android.widget.LinearLayout;
29import android.widget.ListView;
30import android.widget.TextView;
31
32public class ActiveTabsPage extends LinearLayout {
33 private final BrowserActivity mBrowserActivity;
34 private final LayoutInflater mFactory;
35 private final TabControl mControl;
36 private final TabsListAdapter mAdapter;
37 private final ListView mListView;
38
39 public ActiveTabsPage(BrowserActivity context, TabControl control) {
40 super(context);
41 mBrowserActivity = context;
42 mControl = control;
43 mFactory = LayoutInflater.from(context);
44 mFactory.inflate(R.layout.active_tabs, this);
45 mListView = (ListView) findViewById(R.id.list);
46 mAdapter = new TabsListAdapter();
47 mListView.setAdapter(mAdapter);
48 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
49 public void onItemClick(AdapterView<?> parent, View view,
50 int position, long id) {
51 if (mControl.getTabCount() < TabControl.MAX_TABS) {
52 position--;
53 }
54 boolean needToAttach = false;
55 if (position == -1) {
56 // Create a new tab
57 mBrowserActivity.openTabToHomePage();
58 } else {
59 // Open the corresponding tab
60 // If the tab is the current one, switchToTab will
61 // do nothing and return, so we need to make sure
62 // it gets attached back to its mContentView in
63 // removeActiveTabPage
64 needToAttach = !mBrowserActivity.switchToTab(position);
65 }
66 mBrowserActivity.removeActiveTabPage(needToAttach);
67 }
68 });
69 }
70
Leon Scrogginsa3315562009-09-18 14:21:08 -040071 /**
72 * Special class to hold the close drawable. Its sole purpose is to allow
73 * the parent to be pressed without being pressed itself. This way the line
74 * of a tab can be pressed, but the close button itself is not.
75 */
76 private static class CloseHolder extends ImageView {
77 public CloseHolder(Context context, AttributeSet attrs) {
78 super(context, attrs);
79 }
80
81 @Override
82 public void setPressed(boolean pressed) {
83 // If the parent is pressed, do not set to pressed.
84 if (pressed && ((View) getParent()).isPressed()) {
85 return;
86 }
87 super.setPressed(pressed);
88 }
89 }
90
Leon Scroggins0a64ba52009-09-08 15:35:33 -040091 private class TabsListAdapter extends BaseAdapter {
92 public int getCount() {
93 int count = mControl.getTabCount();
94 if (count < TabControl.MAX_TABS) {
95 count++;
96 }
97 return count;
98 }
99 public Object getItem(int position) {
100 return null;
101 }
102 public long getItemId(int position) {
103 return position;
104 }
105 public View getView(int position, View convertView, ViewGroup parent) {
106 if (convertView == null) {
107 convertView = mFactory.inflate(R.layout.tab_view, null);
108 }
109 TextView title = (TextView) convertView.findViewById(R.id.title);
110 TextView url = (TextView) convertView.findViewById(R.id.url);
111 FakeWebView webView
112 = (FakeWebView) convertView.findViewById(R.id.screen_shot);
113 View close = convertView.findViewById(R.id.close);
Leon Scrogginsb17d2eb2009-09-11 11:12:58 -0400114 View divider = convertView.findViewById(R.id.divider);
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400115
116 final int tabCount = mControl.getTabCount();
117 if (tabCount < TabControl.MAX_TABS) {
118 position--;
119 }
120 if (position == -1) {
121 title.setText(R.string.new_tab);
Leon Scrogginsb17d2eb2009-09-11 11:12:58 -0400122 url.setVisibility(View.GONE);
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400123 webView.setImageResource(R.drawable.ic_add_tab);
124 close.setVisibility(View.GONE);
Leon Scrogginsb17d2eb2009-09-11 11:12:58 -0400125 divider.setVisibility(View.GONE);
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400126 } else {
127 TabControl.Tab tab = mControl.getTab(position);
128 mControl.populatePickerData(tab);
129 title.setText(tab.getTitle());
130 url.setText(tab.getUrl());
Leon Scrogginsb17d2eb2009-09-11 11:12:58 -0400131 url.setVisibility(View.VISIBLE);
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400132 webView.setTab(tab);
Leon Scrogginsb17d2eb2009-09-11 11:12:58 -0400133 divider.setVisibility(View.VISIBLE);
Leon Scroggins0a64ba52009-09-08 15:35:33 -0400134 close.setVisibility(View.VISIBLE);
135 final int closePosition = position;
136 close.setOnClickListener(new View.OnClickListener() {
137 public void onClick(View v) {
138 mBrowserActivity.closeTab(
139 mControl.getTab(closePosition));
140 if (tabCount == 1) {
141 mBrowserActivity.openTabToHomePage();
142 mBrowserActivity.removeActiveTabPage(false);
143 } else {
144 mListView.setAdapter(mAdapter);
145 }
146 }
147 });
148 }
149 return convertView;
150 }
151 }
152}