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.LayoutInflater; |
| 23 | import android.view.View; |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 24 | import android.view.View.OnClickListener; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 25 | import android.view.ViewGroup; |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 26 | import android.widget.AbsListView; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 27 | import android.widget.AdapterView; |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 28 | import android.widget.AdapterView.OnItemClickListener; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 29 | import android.widget.BaseAdapter; |
Leon Scroggins | a331556 | 2009-09-18 14:21:08 -0400 | [diff] [blame] | 30 | import android.widget.ImageView; |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 31 | import android.widget.ImageView.ScaleType; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 32 | import android.widget.LinearLayout; |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 33 | import android.widget.TextView; |
| 34 | |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 35 | interface OnCloseTab { |
| 36 | void onCloseTab(int position); |
| 37 | } |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 38 | |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 39 | public class ActiveTabsPage extends LinearLayout implements OnClickListener, |
| 40 | OnItemClickListener, OnCloseTab { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 41 | |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 42 | private Context mContext; |
| 43 | private UiController mController; |
| 44 | private TabControl mTabControl; |
| 45 | private View mNewTab, mNewIncognitoTab; |
| 46 | private TabAdapter mAdapter; |
| 47 | private AbsListView mTabsList; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 48 | |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 49 | public ActiveTabsPage(Context context, UiController controller) { |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 50 | super(context); |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 51 | mContext = context; |
| 52 | mController = controller; |
| 53 | mTabControl = mController.getTabControl(); |
| 54 | setOrientation(VERTICAL); |
| 55 | setBackgroundResource(R.drawable.bg_browser); |
| 56 | LayoutInflater inflate = LayoutInflater.from(mContext); |
| 57 | inflate.inflate(R.layout.active_tabs, this, true); |
| 58 | mNewTab = findViewById(R.id.new_tab); |
| 59 | mNewIncognitoTab = findViewById(R.id.new_incognito_tab); |
| 60 | mNewTab.setOnClickListener(this); |
| 61 | mNewIncognitoTab.setOnClickListener(this); |
| 62 | int visibility = mTabControl.canCreateNewTab() ? View.VISIBLE : View.GONE; |
| 63 | mNewTab.setVisibility(visibility); |
| 64 | mNewIncognitoTab.setVisibility(visibility); |
| 65 | mTabsList = (AbsListView) findViewById(android.R.id.list); |
| 66 | mAdapter = new TabAdapter(mContext, mTabControl); |
| 67 | mAdapter.setOnCloseListener(this); |
| 68 | mTabsList.setAdapter(mAdapter); |
| 69 | mTabsList.setOnItemClickListener(this); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void onClick(View v) { |
| 74 | if (v == mNewTab) { |
| 75 | mController.openTabToHomePage(); |
| 76 | } else if (v == mNewIncognitoTab) { |
| 77 | mController.openIncognitoTab(); |
| 78 | } |
| 79 | mController.removeActiveTabsPage(false); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onItemClick( |
| 84 | AdapterView<?> parent, View view, int position, long id) { |
| 85 | boolean needToAttach = !mController.switchToTab(position); |
| 86 | mController.removeActiveTabsPage(needToAttach); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void onCloseTab(int position) { |
| 91 | Tab tab = mTabControl.getTab(position); |
| 92 | if (tab != null) { |
| 93 | mController.closeTab(tab); |
| 94 | if (mTabControl.getTabCount() == 0) { |
| 95 | mController.openTabToHomePage(); |
| 96 | mController.removeActiveTabsPage(false); |
| 97 | } else { |
| 98 | mAdapter.notifyDataSetChanged(); |
| 99 | } |
| 100 | } |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 101 | } |
| 102 | |
Leon Scroggins | a331556 | 2009-09-18 14:21:08 -0400 | [diff] [blame] | 103 | /** |
| 104 | * Special class to hold the close drawable. Its sole purpose is to allow |
| 105 | * the parent to be pressed without being pressed itself. This way the line |
| 106 | * of a tab can be pressed, but the close button itself is not. |
| 107 | */ |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 108 | public static class CloseHolder extends ImageView { |
Leon Scroggins | a331556 | 2009-09-18 14:21:08 -0400 | [diff] [blame] | 109 | public CloseHolder(Context context, AttributeSet attrs) { |
| 110 | super(context, attrs); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void setPressed(boolean pressed) { |
| 115 | // If the parent is pressed, do not set to pressed. |
| 116 | if (pressed && ((View) getParent()).isPressed()) { |
| 117 | return; |
| 118 | } |
| 119 | super.setPressed(pressed); |
| 120 | } |
| 121 | } |
| 122 | |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 123 | static class TabAdapter extends BaseAdapter implements OnClickListener { |
Patrick Scott | 49b11f9 | 2009-12-14 09:32:13 -0500 | [diff] [blame] | 124 | |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 125 | LayoutInflater mInflater; |
| 126 | OnCloseTab mCloseListener; |
| 127 | TabControl mTabControl; |
| 128 | |
| 129 | TabAdapter(Context context, TabControl tabs) { |
| 130 | mInflater = LayoutInflater.from(context); |
| 131 | mTabControl = tabs; |
| 132 | } |
| 133 | |
| 134 | void setOnCloseListener(OnCloseTab listener) { |
| 135 | mCloseListener = listener; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public View getView(int position, View view, ViewGroup parent) { |
| 140 | if (view == null) { |
| 141 | view = mInflater.inflate(R.layout.tab_view, parent, false); |
| 142 | } |
| 143 | ImageView favicon = (ImageView) view.findViewById(R.id.favicon); |
| 144 | TextView title = (TextView) view.findViewById(R.id.title); |
| 145 | TextView url = (TextView) view.findViewById(R.id.url); |
| 146 | Tab tab = getItem(position); |
| 147 | |
| 148 | title.setText(tab.getTitle()); |
| 149 | url.setText(tab.getUrl()); |
| 150 | Bitmap faviconBitmap = tab.getFavicon(); |
| 151 | if (tab.isPrivateBrowsingEnabled()) { |
| 152 | favicon.setImageResource(R.drawable.ic_incognito_holo_dark); |
| 153 | favicon.setBackgroundDrawable(null); |
| 154 | } else { |
| 155 | if (faviconBitmap == null) { |
| 156 | favicon.setImageResource(R.drawable.app_web_browser_sm); |
| 157 | } else { |
| 158 | favicon.setImageBitmap(faviconBitmap); |
| 159 | } |
| 160 | favicon.setBackgroundResource(R.drawable.bookmark_list_favicon_bg); |
| 161 | } |
| 162 | View close = view.findViewById(R.id.close); |
| 163 | close.setTag(position); |
| 164 | close.setOnClickListener(this); |
| 165 | return view; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void onClick(View v) { |
| 170 | int position = (Integer) v.getTag(); |
| 171 | if (mCloseListener != null) { |
| 172 | mCloseListener.onCloseTab(position); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | @Override |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 177 | public int getCount() { |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 178 | return mTabControl.getTabCount(); |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 179 | } |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 180 | |
| 181 | @Override |
| 182 | public Tab getItem(int position) { |
| 183 | return mTabControl.getTab(position); |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 184 | } |
John Reck | 18e93f7 | 2011-03-21 11:46:09 -0700 | [diff] [blame] | 185 | |
| 186 | @Override |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 187 | public long getItemId(int position) { |
| 188 | return position; |
| 189 | } |
Leon Scroggins | 0a64ba5 | 2009-09-08 15:35:33 -0400 | [diff] [blame] | 190 | } |
| 191 | } |