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