blob: 03e309d76623e6dcc572002906848d0ed38a64f7 [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;
21import android.graphics.drawable.Drawable;
22import android.util.AttributeSet;
23import android.util.Log;
24import android.view.LayoutInflater;
25import android.view.MotionEvent;
26import android.view.View;
27import android.view.ViewGroup;
28import android.webkit.WebView;
29import android.widget.AdapterView;
30import android.widget.Gallery;
31import android.widget.SpinnerAdapter;
32
33import java.util.Vector;
34
35/**
36 * The TitleBarSet holds a TitleBar for each open "tab" in the browser.
37 */
38public class TitleBarSet extends Gallery
39 implements AdapterView.OnItemSelectedListener {
40 private Vector<TitleBar> mTitleBars;
41 private BrowserActivity mBrowserActivity;
42 private View mNewButton;
43 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;
56 // Now create the Plus button that goes on the right.
57 LayoutInflater factory = LayoutInflater.from(context);
58 mNewButton = factory.inflate(R.layout.new_window_button, null);
59 mTitleAdapter = new TitleAdapter();
60 setAdapter(mTitleAdapter);
61 setCallbackDuringFling(false);
62 setCallbackOnUnselectedItemClick(true);
63 setSpacing(0);
64 setOnItemSelectedListener(this);
65 }
66
67 /**
68 * Add a tab/titlebar to our set. Called when BrowserActivity adds a new
69 * Tab to its TabControl.
70 * @param view WebView associated with this tab. Used to determine whether
71 * updates are going to the correct place.
72 * @param selected Whether to set the new tab to be selected.
73 */
74 /* package */ void addTab(WebView view, boolean selected) {
75 if (TabControl.MAX_TABS == mCount) {
76 return;
77 }
78 int newSelection = mCount;
79 TitleBar titleBar = new TitleBar(getContext(), view);
80 mTitleBars.add(titleBar);
81 mCount++;
82 if (TabControl.MAX_TABS == mCount) {
83 mNewButton.setEnabled(false);
84 }
85 // Need to refresh our list
86 setAdapter(mTitleAdapter);
87 mIgnoreSelectedListener = true;
88 // No need to call onItemSelected, since the Tab in BrowserActivity has
89 // already been changed.
90 if (selected) {
91 setSelection(newSelection);
92 }
93 mIgnoreSelectedListener = false;
94 }
95
96 /**
97 * Convenience method to get a particular title bar.
98 */
99 private TitleBar getTitleBarAt(int position) {
100 if (position < 0 || position >= mCount) {
101 return null;
102 }
103 return (TitleBar) mTitleBars.elementAt(position);
104 }
105
106 /**
107 * Implementation for OnItemSelectedListener
108 */
109 public void onItemSelected(AdapterView<?> parent, View view, int position,
110 long id) {
111 if (mIgnoreSelectedListener || !(view instanceof TitleBar)) {
112 return;
113 }
114 mBrowserActivity.switchToTab(position);
115 // In case the WebView finished loading while this TitleBar was out of
116 // focus, make sure all its data is up to date
117 TitleBar titleBar = getTitleBarAt(position);
118 WebView webview = titleBar.getWebView();
119 if (webview == null) {
120 // FIXME: Possible that the tab needs to be restored.
121 return;
122 }
123 if (webview.getProgress() == 100) {
124 titleBar.setProgress(100);
125 titleBar.setTitleAndUrl(webview.getTitle(), webview.getUrl());
126 // FIXME: Pass in a bitmap, so we can always update the bitmap
127 // properly
128 //titleBar.setFavicon(webview.getFavicon());
129 }
130 }
131
132 /**
133 * Implementation for OnItemSelectedListener
134 */
135 public void onNothingSelected(AdapterView<?> parent) {
136 // do nothing
137 }
138
139 /**
140 * Override from GestureDetector.OnGestureListener. Store the MotionEvent
141 * so performItemClick can know how to handle the click.
142 */
143 public boolean onSingleTapUp(MotionEvent e) {
144 mLastTouchUp = e;
145 // super.onSingleTapUp will call performItemClick
146 boolean result = super.onSingleTapUp(e);
147 mLastTouchUp = null;
148 return result;
149 }
150
151 /**
152 * Override from View to ensure that the TitleBars get resized to match
153 * the new screen width
154 */
155 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
156 super.onSizeChanged(w, h, oldw, oldh);
157 // Need to make sure getView gets called again
158 // FIXME: This didn't seem to work
159 setAdapter(mTitleAdapter);
160 }
161
162 /**
163 * Override from AdapterView. Using simple OnClickListeners overrides
164 * the GestureDetector.OnGestureListener, so we handle it here.
165 */
166 public boolean performItemClick(View view, int position, long id) {
167 if (!(view instanceof TitleBar)) {
168 // For new window button
169 return super.performItemClick(view, position, id);
170 }
171 // If we have no mLastTouchUp, this was not called from onSingleTapUp,
172 // so ignore it.
173 if (null == mLastTouchUp) {
174 return false;
175 }
176 TitleBar titleBar = (TitleBar) view;
177 // If the user clicks on a view which is not selected, the Gallery will
178 // take care of making it selected.
179 if (titleBar != getTitleBarAt(position)) {
180 return false;
181 }
182 if (titleBar.hitRightButton((int) mLastTouchUp.getX() - mScrollX,
183 (int) mLastTouchUp.getY() - mScrollY)) {
184 if (titleBar.isInLoad()) {
185 WebView webView = titleBar.getWebView();
186 if (null == webView) {
187 // FIXME: How did we get into this situation?
188 return false;
189 }
190 webView.stopLoading();
191 } else {
192 mBrowserActivity.closeCurrentWindow();
193 }
194 } else {
Leon Scroggins7f19a5e2009-08-14 13:39:05 -0400195 mBrowserActivity.bookmarksOrHistoryPicker(false);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400196 }
197 return true;
198 }
199
200 /**
201 * Remove the tab at the given position.
202 */
203 /* package */ void removeTab(int position) {
204 if (TabControl.MAX_TABS == mCount) {
205 mNewButton.setEnabled(true);
206 }
207 mTitleBars.remove(position);
208 mCount--;
209 // Need to refresh our list
210 setAdapter(mTitleAdapter);
211 }
212
213 /**
214 * Convenience method to get the currently selected title bar.
215 */
216 private TitleBar selectedTitleBar() {
217 return getTitleBarAt(getSelectedItemPosition());
218 }
219
220 /**
221 * Set the owning BrowserActivity. Necessary so that we can call methods
222 * on it.
223 */
224 /* package */ void setBrowserActivity(final BrowserActivity ba) {
225 mBrowserActivity = ba;
226 View.OnClickListener listener = new View.OnClickListener() {
227 public void onClick(View v) {
228 ba.openTabAndShow(BrowserActivity.EMPTY_URL_DATA, false, null);
229 }
230 };
231 mNewButton.findViewById(R.id.button).setOnClickListener(listener);
232 }
233
234 /**
235 * Change to the tab at the new position.
236 */
237 /* package */ void setCurrentTab(int position) {
238 mIgnoreSelectedListener = true;
239 setSelection(position);
240 mIgnoreSelectedListener = false;
241 }
242
243 /**
244 * Update the Favicon of the currently selected tab.
245 * @param d The new Drawable for the Favicon
246 * @param topWindow The WebView which posted the update. If it does not
247 * match the WebView of the currently selected tab, do
248 * nothing, since that tab is not being displayed.
249 */
250 /* package */ void setFavicon(Drawable d, WebView topWindow) {
251 TitleBar current = selectedTitleBar();
252 if (current != null && current.getWebView() == topWindow) {
253 current.setFavicon(d);
254 }
255 }
256
257 /**
258 * Update the lock icon of the currently selected tab.
259 * @param d The new Drawable for the lock icon
260 * @param topWindow The WebView which posted the update. If it does not
261 * match the WebView of the currently selected tab, do
262 * nothing, since that tab is not being displayed.
263 */
264 /* package */ void setLock(Drawable d, WebView topWindow) {
265 TitleBar current = selectedTitleBar();
266 if (current != null && current.getWebView() == topWindow) {
267 current.setLock(d);
268 }
269 }
270 /**
271 * Update the progress of the currently selected tab.
272 * @param newProgress The progress, between 0 and 100, of the current tab.
273 * @param topWindow The WebView which posted the update. If it does not
274 * match the WebView of the currently selected tab, do
275 * nothing, since that tab is not being displayed.
276 */
277 /* package */ void setProgress(int newProgress, WebView topWindow) {
278 TitleBar current = selectedTitleBar();
279 if (current != null && current.getWebView() == topWindow) {
280 current.setProgress(newProgress);
281 }
282 }
283 /**
284 * Update the title and URL of the currently selected tab.
285 * @param title The title of the webpage
286 * @param url The URL of the webpage
287 * @param topWindow The WebView which posted the update. If it does not
288 * match the WebView of the currently selected tab, do
289 * nothing, since that tab is not being displayed.
290 */
291 /* package */ void setTitleAndUrl(CharSequence title, CharSequence url,
292 WebView topWindow) {
293 TitleBar current = selectedTitleBar();
294 if (current != null && current.getWebView() == topWindow) {
295 current.setTitleAndUrl(title, url);
296 }
297 }
298
299 // FIXME: Remove
300 /* package */ void setToTabPicker() {
301 TitleBar current = selectedTitleBar();
302 if (current != null) {
303 current.setToTabPicker();
304 }
305 }
306
307 /**
308 * Custom adapter which provides the TitleBars and the NewButton to the
309 * Gallery.
310 */
311 private class TitleAdapter implements SpinnerAdapter {
312 public View getDropDownView(int position, View convertView,
313 ViewGroup parent) {
314 return null;
315 }
316 public void registerDataSetObserver(DataSetObserver observer) {}
317 public void unregisterDataSetObserver(DataSetObserver observer) {}
318 public int getCount() {
319 // To account for new window
320 return mCount + 1;
321 }
322 public Object getItem(int position) {
323 return null;
324 }
325 public long getItemId(int position) {
326 return position;
327 }
328 public boolean hasStableIds() {
329 return true;
330 }
331 public View getView(int position, View convertView, ViewGroup parent) {
332 if (mCount == position) {
333 return mNewButton;
334 }
335 TitleBar titleBar = getTitleBarAt(position);
336 Gallery.LayoutParams lp;
337 int desiredWidth = TitleBarSet.this.getWidth()
338 - (2 * mNewButton.getWidth());
339 ViewGroup.LayoutParams old = titleBar.getLayoutParams();
340 if (old == null || !(old instanceof Gallery.LayoutParams)) {
341 lp = new Gallery.LayoutParams(desiredWidth,
342 ViewGroup.LayoutParams.WRAP_CONTENT);
343 titleBar.setLayoutParams(lp);
344 } else {
345 lp = (Gallery.LayoutParams) old;
346 if (lp.width != desiredWidth) {
347 lp.width = desiredWidth;
348 titleBar.setLayoutParams(lp);
349 requestLayout();
350 }
351 }
352 return titleBar;
353 }
354 public int getItemViewType(int position) {
355 // We are managing our own views.
356 return AdapterView.ITEM_VIEW_TYPE_IGNORE;
357 }
358 public int getViewTypeCount() {
359 return 1;
360 }
361 public boolean isEmpty() {
362 // Will never be empty, because the NewButton is always there
363 // (though sometimes disabled).
364 return false;
365 }
366 }
367}