blob: 9cfce86501851c5cf3989320d13a6c81e289b642 [file] [log] [blame]
Leon Scroggins1f005d32009-08-10 17:36:42 -04001/*
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
17package com.android.browser;
18
19import android.content.Context;
20import android.database.DataSetObserver;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040021import android.graphics.Bitmap;
Leon Scroggins1f005d32009-08-10 17:36:42 -040022import android.graphics.drawable.Drawable;
23import android.util.AttributeSet;
24import android.util.Log;
25import android.view.LayoutInflater;
26import android.view.MotionEvent;
27import android.view.View;
28import android.view.ViewGroup;
29import android.webkit.WebView;
30import android.widget.AdapterView;
31import android.widget.Gallery;
32import android.widget.SpinnerAdapter;
33
34import java.util.Vector;
35
36/**
37 * The TitleBarSet holds a TitleBar for each open "tab" in the browser.
38 */
39public class TitleBarSet extends Gallery
40 implements AdapterView.OnItemSelectedListener {
41 private Vector<TitleBar> mTitleBars;
42 private BrowserActivity mBrowserActivity;
Leon Scroggins1f005d32009-08-10 17:36:42 -040043 private int mCount;
44 private TitleAdapter mTitleAdapter;
45 private boolean mIgnoreSelectedListener;
46 private MotionEvent mLastTouchUp;
47
48 public TitleBarSet(Context context) {
49 this(context, null);
50 }
51
52 public TitleBarSet(Context context, AttributeSet attrs) {
53 super(context, attrs);
54 mTitleBars = new Vector<TitleBar>(TabControl.MAX_TABS);
55 mCount = 0;
Leon Scroggins1f005d32009-08-10 17:36:42 -040056 mTitleAdapter = new TitleAdapter();
57 setAdapter(mTitleAdapter);
58 setCallbackDuringFling(false);
59 setCallbackOnUnselectedItemClick(true);
60 setSpacing(0);
61 setOnItemSelectedListener(this);
Leon Scroggins62e8f942009-09-03 15:08:54 -040062 setBackgroundResource(R.drawable.tab_browser_unselected);
63 setPadding(0,0,0,0);
Leon Scroggins1f005d32009-08-10 17:36:42 -040064 }
65
66 /**
67 * Add a tab/titlebar to our set. Called when BrowserActivity adds a new
68 * Tab to its TabControl.
69 * @param view WebView associated with this tab. Used to determine whether
70 * updates are going to the correct place.
71 * @param selected Whether to set the new tab to be selected.
72 */
73 /* package */ void addTab(WebView view, boolean selected) {
74 if (TabControl.MAX_TABS == mCount) {
75 return;
76 }
77 int newSelection = mCount;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040078 TitleBar titleBar = new TitleBar(mBrowserActivity, view);
Leon Scroggins1f005d32009-08-10 17:36:42 -040079 mTitleBars.add(titleBar);
80 mCount++;
Leon Scroggins1f005d32009-08-10 17:36:42 -040081 // Need to refresh our list
82 setAdapter(mTitleAdapter);
83 mIgnoreSelectedListener = true;
84 // No need to call onItemSelected, since the Tab in BrowserActivity has
85 // already been changed.
86 if (selected) {
87 setSelection(newSelection);
88 }
89 mIgnoreSelectedListener = false;
90 }
91
92 /**
93 * Convenience method to get a particular title bar.
94 */
95 private TitleBar getTitleBarAt(int position) {
96 if (position < 0 || position >= mCount) {
97 return null;
98 }
99 return (TitleBar) mTitleBars.elementAt(position);
100 }
101
102 /**
103 * Implementation for OnItemSelectedListener
104 */
105 public void onItemSelected(AdapterView<?> parent, View view, int position,
106 long id) {
107 if (mIgnoreSelectedListener || !(view instanceof TitleBar)) {
108 return;
109 }
110 mBrowserActivity.switchToTab(position);
111 // In case the WebView finished loading while this TitleBar was out of
112 // focus, make sure all its data is up to date
113 TitleBar titleBar = getTitleBarAt(position);
114 WebView webview = titleBar.getWebView();
115 if (webview == null) {
116 // FIXME: Possible that the tab needs to be restored.
117 return;
118 }
119 if (webview.getProgress() == 100) {
120 titleBar.setProgress(100);
121 titleBar.setTitleAndUrl(webview.getTitle(), webview.getUrl());
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400122 // FIXME: BrowserActivity looks at the back forward list. Is this
123 // better?
124 titleBar.setFavicon(webview.getFavicon());
125 mBrowserActivity.updateLockIconToLatest();
Leon Scroggins1f005d32009-08-10 17:36:42 -0400126 }
127 }
128
129 /**
130 * Implementation for OnItemSelectedListener
131 */
132 public void onNothingSelected(AdapterView<?> parent) {
133 // do nothing
134 }
135
136 /**
137 * Override from GestureDetector.OnGestureListener. Store the MotionEvent
138 * so performItemClick can know how to handle the click.
139 */
140 public boolean onSingleTapUp(MotionEvent e) {
141 mLastTouchUp = e;
142 // super.onSingleTapUp will call performItemClick
143 boolean result = super.onSingleTapUp(e);
144 mLastTouchUp = null;
145 return result;
146 }
147
148 /**
149 * Override from View to ensure that the TitleBars get resized to match
150 * the new screen width
151 */
152 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
153 super.onSizeChanged(w, h, oldw, oldh);
Leon Scroggins5a7e6c62009-08-14 16:32:43 -0400154 int selection = getSelectedItemPosition();
Leon Scroggins1f005d32009-08-10 17:36:42 -0400155 // Need to make sure getView gets called again
Leon Scroggins1f005d32009-08-10 17:36:42 -0400156 setAdapter(mTitleAdapter);
Leon Scroggins5a7e6c62009-08-14 16:32:43 -0400157 // Stay on the same tab
158 setCurrentTab(selection);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400159 }
160
161 /**
162 * Override from AdapterView. Using simple OnClickListeners overrides
163 * the GestureDetector.OnGestureListener, so we handle it here.
164 */
165 public boolean performItemClick(View view, int position, long id) {
166 if (!(view instanceof TitleBar)) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400167 return super.performItemClick(view, position, id);
168 }
169 // If we have no mLastTouchUp, this was not called from onSingleTapUp,
170 // so ignore it.
171 if (null == mLastTouchUp) {
172 return false;
173 }
174 TitleBar titleBar = (TitleBar) view;
175 // If the user clicks on a view which is not selected, the Gallery will
176 // take care of making it selected.
177 if (titleBar != getTitleBarAt(position)) {
178 return false;
179 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400180 mBrowserActivity.onSearchRequested();
Leon Scroggins1f005d32009-08-10 17:36:42 -0400181 return true;
182 }
183
184 /**
185 * Remove the tab at the given position.
186 */
187 /* package */ void removeTab(int position) {
Leon Scroggins53fe5812009-09-01 12:33:07 -0400188 int selection = getSelectedItemPosition();
Leon Scroggins1f005d32009-08-10 17:36:42 -0400189 mTitleBars.remove(position);
190 mCount--;
191 // Need to refresh our list
192 setAdapter(mTitleAdapter);
Leon Scroggins53fe5812009-09-01 12:33:07 -0400193 setCurrentTab(selection);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400194 }
195
196 /**
197 * Convenience method to get the currently selected title bar.
198 */
199 private TitleBar selectedTitleBar() {
200 return getTitleBarAt(getSelectedItemPosition());
201 }
202
203 /**
204 * Set the owning BrowserActivity. Necessary so that we can call methods
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400205 * on it. Only called once before adding any title bars.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400206 */
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400207 /* package */ void init(final BrowserActivity ba) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400208 mBrowserActivity = ba;
Leon Scroggins1f005d32009-08-10 17:36:42 -0400209 }
210
211 /**
212 * Change to the tab at the new position.
213 */
214 /* package */ void setCurrentTab(int position) {
Leon Scroggins53fe5812009-09-01 12:33:07 -0400215 if (position < 0 || position >= mCount) return;
Leon Scroggins1f005d32009-08-10 17:36:42 -0400216 mIgnoreSelectedListener = true;
217 setSelection(position);
218 mIgnoreSelectedListener = false;
219 }
220
221 /**
222 * Update the Favicon of the currently selected tab.
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400223 * @param icon The new bitmap for the favicon
Leon Scroggins1f005d32009-08-10 17:36:42 -0400224 * @param topWindow The WebView which posted the update. If it does not
225 * match the WebView of the currently selected tab, do
226 * nothing, since that tab is not being displayed.
227 */
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400228 /* package */ void setFavicon(Bitmap icon, WebView topWindow) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400229 TitleBar current = selectedTitleBar();
230 if (current != null && current.getWebView() == topWindow) {
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400231 current.setFavicon(icon);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400232 }
233 }
234
235 /**
236 * Update the lock icon of the currently selected tab.
237 * @param d The new Drawable for the lock icon
238 * @param topWindow The WebView which posted the update. If it does not
239 * match the WebView of the currently selected tab, do
240 * nothing, since that tab is not being displayed.
241 */
242 /* package */ void setLock(Drawable d, WebView topWindow) {
243 TitleBar current = selectedTitleBar();
244 if (current != null && current.getWebView() == topWindow) {
245 current.setLock(d);
246 }
247 }
248 /**
249 * Update the progress of the currently selected tab.
250 * @param newProgress The progress, between 0 and 100, of the current tab.
251 * @param topWindow The WebView which posted the update. If it does not
252 * match the WebView of the currently selected tab, do
253 * nothing, since that tab is not being displayed.
254 */
255 /* package */ void setProgress(int newProgress, WebView topWindow) {
256 TitleBar current = selectedTitleBar();
257 if (current != null && current.getWebView() == topWindow) {
258 current.setProgress(newProgress);
259 }
260 }
261 /**
262 * Update the title and URL of the currently selected tab.
263 * @param title The title of the webpage
264 * @param url The URL of the webpage
265 * @param topWindow The WebView which posted the update. If it does not
266 * match the WebView of the currently selected tab, do
267 * nothing, since that tab is not being displayed.
268 */
269 /* package */ void setTitleAndUrl(CharSequence title, CharSequence url,
270 WebView topWindow) {
271 TitleBar current = selectedTitleBar();
272 if (current != null && current.getWebView() == topWindow) {
273 current.setTitleAndUrl(title, url);
274 }
275 }
276
277 // FIXME: Remove
278 /* package */ void setToTabPicker() {
279 TitleBar current = selectedTitleBar();
280 if (current != null) {
281 current.setToTabPicker();
282 }
283 }
284
285 /**
286 * Custom adapter which provides the TitleBars and the NewButton to the
287 * Gallery.
288 */
289 private class TitleAdapter implements SpinnerAdapter {
290 public View getDropDownView(int position, View convertView,
291 ViewGroup parent) {
292 return null;
293 }
294 public void registerDataSetObserver(DataSetObserver observer) {}
295 public void unregisterDataSetObserver(DataSetObserver observer) {}
296 public int getCount() {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400297 return mCount;
Leon Scroggins1f005d32009-08-10 17:36:42 -0400298 }
299 public Object getItem(int position) {
300 return null;
301 }
302 public long getItemId(int position) {
303 return position;
304 }
305 public boolean hasStableIds() {
306 return true;
307 }
308 public View getView(int position, View convertView, ViewGroup parent) {
Leon Scroggins1f005d32009-08-10 17:36:42 -0400309 TitleBar titleBar = getTitleBarAt(position);
310 Gallery.LayoutParams lp;
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400311 int desiredWidth = TitleBarSet.this.getWidth();
Leon Scroggins1f005d32009-08-10 17:36:42 -0400312 ViewGroup.LayoutParams old = titleBar.getLayoutParams();
313 if (old == null || !(old instanceof Gallery.LayoutParams)) {
314 lp = new Gallery.LayoutParams(desiredWidth,
315 ViewGroup.LayoutParams.WRAP_CONTENT);
316 titleBar.setLayoutParams(lp);
317 } else {
318 lp = (Gallery.LayoutParams) old;
319 if (lp.width != desiredWidth) {
320 lp.width = desiredWidth;
321 titleBar.setLayoutParams(lp);
322 requestLayout();
323 }
324 }
325 return titleBar;
326 }
327 public int getItemViewType(int position) {
328 // We are managing our own views.
329 return AdapterView.ITEM_VIEW_TYPE_IGNORE;
330 }
331 public int getViewTypeCount() {
332 return 1;
333 }
334 public boolean isEmpty() {
335 // Will never be empty, because the NewButton is always there
336 // (though sometimes disabled).
337 return false;
338 }
339 }
340}