Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 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 | |
Leon Scroggins | a331556 | 2009-09-18 14:21:08 -0400 | [diff] [blame] | 19 | import android.content.Context; |
Patrick Scott | 555c580 | 2009-09-23 08:08:17 -0400 | [diff] [blame] | 20 | import android.graphics.Bitmap; |
Patrick Scott | 49b11f9 | 2009-12-14 09:32:13 -0500 | [diff] [blame] | 21 | import android.os.Handler; |
Leon Scroggins | a331556 | 2009-09-18 14:21:08 -0400 | [diff] [blame] | 22 | import android.util.AttributeSet; |
Leon Scroggins | 70a153b | 2010-05-10 17:27:26 -0400 | [diff] [blame] | 23 | import android.util.Log; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 24 | import android.view.KeyEvent; |
| 25 | import android.view.LayoutInflater; |
| 26 | import android.view.View; |
| 27 | import android.view.ViewGroup; |
| 28 | import android.widget.AdapterView; |
| 29 | import android.widget.BaseAdapter; |
Leon Scroggins | a331556 | 2009-09-18 14:21:08 -0400 | [diff] [blame] | 30 | import android.widget.ImageView; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 31 | import android.widget.LinearLayout; |
| 32 | import android.widget.ListView; |
| 33 | import android.widget.TextView; |
| 34 | |
| 35 | public class ActiveTabsPage extends LinearLayout { |
Leon Scroggins | 70a153b | 2010-05-10 17:27:26 -0400 | [diff] [blame] | 36 | private static final String LOGTAG = "TabPicker"; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 37 | private final BrowserActivity mBrowserActivity; |
| 38 | private final LayoutInflater mFactory; |
| 39 | private final TabControl mControl; |
| 40 | private final TabsListAdapter mAdapter; |
| 41 | private final ListView mListView; |
| 42 | |
| 43 | public ActiveTabsPage(BrowserActivity context, TabControl control) { |
| 44 | super(context); |
| 45 | mBrowserActivity = context; |
| 46 | mControl = control; |
| 47 | mFactory = LayoutInflater.from(context); |
| 48 | mFactory.inflate(R.layout.active_tabs, this); |
| 49 | mListView = (ListView) findViewById(R.id.list); |
| 50 | mAdapter = new TabsListAdapter(); |
| 51 | mListView.setAdapter(mAdapter); |
| 52 | mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 53 | public void onItemClick(AdapterView<?> parent, View view, |
| 54 | int position, long id) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 55 | if (mControl.canCreateNewTab()) { |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 56 | position -= 2; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 57 | } |
| 58 | boolean needToAttach = false; |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 59 | if (position == -2) { |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 60 | // Create a new tab |
| 61 | mBrowserActivity.openTabToHomePage(); |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 62 | } else if (position == -1) { |
| 63 | // Create a new incognito tab |
| 64 | mBrowserActivity.openIncognitoTab(); |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 65 | } else { |
| 66 | // Open the corresponding tab |
| 67 | // If the tab is the current one, switchToTab will |
| 68 | // do nothing and return, so we need to make sure |
| 69 | // it gets attached back to its mContentView in |
| 70 | // removeActiveTabPage |
| 71 | needToAttach = !mBrowserActivity.switchToTab(position); |
| 72 | } |
| 73 | mBrowserActivity.removeActiveTabPage(needToAttach); |
| 74 | } |
| 75 | }); |
| 76 | } |
| 77 | |
Leon Scroggins | a331556 | 2009-09-18 14:21:08 -0400 | [diff] [blame] | 78 | /** |
| 79 | * Special class to hold the close drawable. Its sole purpose is to allow |
| 80 | * the parent to be pressed without being pressed itself. This way the line |
| 81 | * of a tab can be pressed, but the close button itself is not. |
| 82 | */ |
| 83 | private static class CloseHolder extends ImageView { |
| 84 | public CloseHolder(Context context, AttributeSet attrs) { |
| 85 | super(context, attrs); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void setPressed(boolean pressed) { |
| 90 | // If the parent is pressed, do not set to pressed. |
| 91 | if (pressed && ((View) getParent()).isPressed()) { |
| 92 | return; |
| 93 | } |
| 94 | super.setPressed(pressed); |
| 95 | } |
| 96 | } |
| 97 | |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 98 | private class TabsListAdapter extends BaseAdapter { |
Patrick Scott | 49b11f9 | 2009-12-14 09:32:13 -0500 | [diff] [blame] | 99 | private boolean mNotified = true; |
| 100 | private int mReturnedCount; |
| 101 | private Handler mHandler = new Handler(); |
| 102 | |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 103 | public int getCount() { |
| 104 | int count = mControl.getTabCount(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 105 | if (mControl.canCreateNewTab()) { |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 106 | count += 2; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 107 | } |
Patrick Scott | 49b11f9 | 2009-12-14 09:32:13 -0500 | [diff] [blame] | 108 | // XXX: This is a workaround to be more like a real adapter. Most |
| 109 | // adapters call notifyDataSetChanged() whenever the internal data |
| 110 | // has changed. Since TabControl is our internal data, we don't |
| 111 | // know when that changes. |
| 112 | // |
| 113 | // Keep track of the last count we returned and whether we called |
| 114 | // notifyDataSetChanged(). If we did not initiate a data set |
| 115 | // change, and the count is different, send the notify and return |
| 116 | // the old count. |
| 117 | if (!mNotified && count != mReturnedCount) { |
| 118 | notifyChange(); |
| 119 | return mReturnedCount; |
| 120 | } |
| 121 | mReturnedCount = count; |
| 122 | mNotified = false; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 123 | return count; |
| 124 | } |
| 125 | public Object getItem(int position) { |
| 126 | return null; |
| 127 | } |
| 128 | public long getItemId(int position) { |
| 129 | return position; |
| 130 | } |
Patrick Scott | 555c580 | 2009-09-23 08:08:17 -0400 | [diff] [blame] | 131 | public int getViewTypeCount() { |
| 132 | return 2; |
| 133 | } |
| 134 | public int getItemViewType(int position) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 135 | if (mControl.canCreateNewTab()) { |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 136 | position -= 2; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 137 | } |
Patrick Scott | 555c580 | 2009-09-23 08:08:17 -0400 | [diff] [blame] | 138 | // Do not recycle the "add new tab" item. |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 139 | return position < 0 ? IGNORE_ITEM_VIEW_TYPE : 1; |
Patrick Scott | 555c580 | 2009-09-23 08:08:17 -0400 | [diff] [blame] | 140 | } |
| 141 | public View getView(int position, View convertView, ViewGroup parent) { |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 142 | final int tabCount = mControl.getTabCount(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 143 | if (mControl.canCreateNewTab()) { |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 144 | position -= 2; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 145 | } |
Patrick Scott | 555c580 | 2009-09-23 08:08:17 -0400 | [diff] [blame] | 146 | |
| 147 | if (convertView == null) { |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 148 | if (position == -2) { |
| 149 | convertView = mFactory.inflate(R.layout.tab_view_add_tab, null); |
| 150 | } else if (position == -1) { |
| 151 | convertView = mFactory.inflate(R.layout.tab_view_add_incognito_tab, null); |
| 152 | } else { |
| 153 | convertView = mFactory.inflate(R.layout.tab_view, null); |
| 154 | } |
Patrick Scott | 555c580 | 2009-09-23 08:08:17 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Elliott Slaughter | f0f0395 | 2010-07-14 18:10:36 -0700 | [diff] [blame] | 157 | if (position >= 0) { |
Patrick Scott | 555c580 | 2009-09-23 08:08:17 -0400 | [diff] [blame] | 158 | TextView title = |
| 159 | (TextView) convertView.findViewById(R.id.title); |
| 160 | TextView url = (TextView) convertView.findViewById(R.id.url); |
| 161 | ImageView favicon = |
| 162 | (ImageView) convertView.findViewById(R.id.favicon); |
| 163 | View close = convertView.findViewById(R.id.close); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 164 | Tab tab = mControl.getTab(position); |
Leon Scroggins | 70a153b | 2010-05-10 17:27:26 -0400 | [diff] [blame] | 165 | if (tab.getWebView() == null) { |
| 166 | // This means that populatePickerData will have to use the |
| 167 | // saved state. |
| 168 | Log.w(LOGTAG, "Tab " + position + " has a null WebView and " |
| 169 | + (tab.getSavedState() == null ? "null" : "non-null") |
| 170 | + " saved state "); |
| 171 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 172 | tab.populatePickerData(); |
Leon Scroggins | 70a153b | 2010-05-10 17:27:26 -0400 | [diff] [blame] | 173 | if (tab.getTitle() == null || tab.getTitle().length() == 0) { |
| 174 | Log.w(LOGTAG, "Tab " + position + " has no title. " |
| 175 | + "Check above in the Logs to see whether it has a " |
| 176 | + "null WebView or null WebHistoryItem"); |
| 177 | } |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 178 | title.setText(tab.getTitle()); |
| 179 | url.setText(tab.getUrl()); |
Patrick Scott | 555c580 | 2009-09-23 08:08:17 -0400 | [diff] [blame] | 180 | Bitmap icon = tab.getFavicon(); |
| 181 | if (icon != null) { |
| 182 | favicon.setImageBitmap(icon); |
| 183 | } else { |
| 184 | favicon.setImageResource(R.drawable.app_web_browser_sm); |
| 185 | } |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 186 | final int closePosition = position; |
| 187 | close.setOnClickListener(new View.OnClickListener() { |
| 188 | public void onClick(View v) { |
| 189 | mBrowserActivity.closeTab( |
| 190 | mControl.getTab(closePosition)); |
| 191 | if (tabCount == 1) { |
| 192 | mBrowserActivity.openTabToHomePage(); |
| 193 | mBrowserActivity.removeActiveTabPage(false); |
| 194 | } else { |
Patrick Scott | 49b11f9 | 2009-12-14 09:32:13 -0500 | [diff] [blame] | 195 | mNotified = true; |
| 196 | notifyDataSetChanged(); |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | }); |
| 200 | } |
| 201 | return convertView; |
| 202 | } |
Patrick Scott | 49b11f9 | 2009-12-14 09:32:13 -0500 | [diff] [blame] | 203 | |
| 204 | void notifyChange() { |
| 205 | mHandler.post(new Runnable() { |
| 206 | public void run() { |
| 207 | mNotified = true; |
| 208 | notifyDataSetChanged(); |
| 209 | } |
| 210 | }); |
| 211 | } |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 212 | } |
| 213 | } |