• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.browser;
18 
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.util.AttributeSet;
22 import android.view.KeyEvent;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.AdapterView;
27 import android.widget.BaseAdapter;
28 import android.widget.ImageView;
29 import android.widget.LinearLayout;
30 import android.widget.ListView;
31 import android.widget.TextView;
32 
33 public 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 
ActiveTabsPage(BrowserActivity context, TabControl control)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) {
52                     if (mControl.getTabCount() < TabControl.MAX_TABS) {
53                         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 
72     /**
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 {
CloseHolder(Context context, AttributeSet attrs)78         public CloseHolder(Context context, AttributeSet attrs) {
79             super(context, attrs);
80         }
81 
82         @Override
setPressed(boolean pressed)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 
92     private class TabsListAdapter extends BaseAdapter {
getCount()93         public int getCount() {
94             int count = mControl.getTabCount();
95             if (count < TabControl.MAX_TABS) {
96                 count++;
97             }
98             return count;
99         }
getItem(int position)100         public Object getItem(int position) {
101             return null;
102         }
getItemId(int position)103         public long getItemId(int position) {
104             return position;
105         }
getViewTypeCount()106         public int getViewTypeCount() {
107             return 2;
108         }
getItemViewType(int position)109         public int getItemViewType(int position) {
110             if (mControl.getTabCount() < TabControl.MAX_TABS) {
111                 position--;
112             }
113             // Do not recycle the "add new tab" item.
114             return position == -1 ? IGNORE_ITEM_VIEW_TYPE : 1;
115         }
getView(int position, View convertView, ViewGroup parent)116         public View getView(int position, View convertView, ViewGroup parent) {
117             final int tabCount = mControl.getTabCount();
118             if (tabCount < TabControl.MAX_TABS) {
119                 position--;
120             }
121 
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);
134                 TabControl.Tab tab = mControl.getTab(position);
135                 mControl.populatePickerData(tab);
136                 title.setText(tab.getTitle());
137                 url.setText(tab.getUrl());
138                 Bitmap icon = tab.getFavicon();
139                 if (icon != null) {
140                     favicon.setImageBitmap(icon);
141                 } else {
142                     favicon.setImageResource(R.drawable.app_web_browser_sm);
143                 }
144                 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 }
162