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; |
Leon Scroggins | a331556 | 2009-09-18 14:21:08 -0400 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 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; |
Leon Scroggins | a331556 | 2009-09-18 14:21:08 -0400 | [diff] [blame] | 28 | import android.widget.ImageView; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 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 | |
| 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 Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame^] | 52 | if (mControl.canCreateNewTab()) { |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 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 | |
Leon Scroggins | a331556 | 2009-09-18 14:21:08 -0400 | [diff] [blame] | 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 { |
| 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 Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 92 | private class TabsListAdapter extends BaseAdapter { |
| 93 | public int getCount() { |
| 94 | int count = mControl.getTabCount(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame^] | 95 | if (mControl.canCreateNewTab()) { |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 96 | 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 Scott | 555c580 | 2009-09-23 08:08:17 -0400 | [diff] [blame] | 106 | public int getViewTypeCount() { |
| 107 | return 2; |
| 108 | } |
| 109 | public int getItemViewType(int position) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame^] | 110 | if (mControl.canCreateNewTab()) { |
Patrick Scott | 555c580 | 2009-09-23 08:08:17 -0400 | [diff] [blame] | 111 | position--; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 112 | } |
Patrick Scott | 555c580 | 2009-09-23 08:08:17 -0400 | [diff] [blame] | 113 | // 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 Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 117 | final int tabCount = mControl.getTabCount(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame^] | 118 | if (mControl.canCreateNewTab()) { |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 119 | position--; |
| 120 | } |
Patrick Scott | 555c580 | 2009-09-23 08:08:17 -0400 | [diff] [blame] | 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); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame^] | 134 | Tab tab = mControl.getTab(position); |
| 135 | tab.populatePickerData(); |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 136 | title.setText(tab.getTitle()); |
| 137 | url.setText(tab.getUrl()); |
Patrick Scott | 555c580 | 2009-09-23 08:08:17 -0400 | [diff] [blame] | 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 | } |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 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 | } |