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