blob: 768f9ba2759254e3add83fd4029a12023b927141 [file] [log] [blame]
Michael Kolbf2055602011-04-09 17:20:03 -07001/*
2 * Copyright (C) 2011 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.app.Activity;
20import android.content.Context;
Michael Kolba4261fd2011-05-05 11:27:37 -070021import android.content.res.Configuration;
Michael Kolbf2055602011-04-09 17:20:03 -070022import android.view.LayoutInflater;
23import android.view.Menu;
24import android.view.MenuItem;
25import android.view.View;
26import android.view.View.OnClickListener;
Michael Kolbdcd81d32011-07-22 10:30:49 -070027import android.view.ViewConfiguration;
Michael Kolbf2055602011-04-09 17:20:03 -070028import android.view.ViewGroup;
29import android.webkit.WebView;
Michael Kolbf2055602011-04-09 17:20:03 -070030import android.widget.BaseAdapter;
31import android.widget.FrameLayout;
Michael Kolbf2055602011-04-09 17:20:03 -070032import android.widget.ImageButton;
33import android.widget.ImageView;
Michael Kolb9829b432011-06-04 13:29:00 -070034import android.widget.LinearLayout;
Michael Kolb017ffab2011-07-11 15:26:47 -070035import android.widget.PopupMenu;
36import android.widget.PopupMenu.OnMenuItemClickListener;
Michael Kolb2814a362011-05-19 15:49:41 -070037import android.widget.RelativeLayout;
Michael Kolbf2055602011-04-09 17:20:03 -070038import android.widget.TextView;
39
Michael Kolbdcd81d32011-07-22 10:30:49 -070040import com.android.browser.NavTabGallery.OnRemoveListener;
John Reck8ee633f2011-08-09 16:00:35 -070041import com.android.browser.TabControl.OnThumbnailUpdatedListener;
Michael Kolbc3af0672011-08-09 10:24:41 -070042import com.android.browser.view.Gallery.OnScrollFinishedListener;
John Reck8ee633f2011-08-09 16:00:35 -070043
44import java.util.HashMap;
Michael Kolbdcd81d32011-07-22 10:30:49 -070045
Michael Kolb017ffab2011-07-11 15:26:47 -070046public class NavScreen extends RelativeLayout
John Reck8ee633f2011-08-09 16:00:35 -070047 implements OnClickListener, OnMenuItemClickListener, OnThumbnailUpdatedListener {
Michael Kolbf2055602011-04-09 17:20:03 -070048
Michael Kolbc3af0672011-08-09 10:24:41 -070049
50 private static final int SCROLL_MIN = 200;
51 private static final int SCROLL_FACTOR = 20;
52
Michael Kolbf2055602011-04-09 17:20:03 -070053 UiController mUiController;
54 PhoneUi mUi;
55 Tab mTab;
56 Activity mActivity;
57
Michael Kolbf2055602011-04-09 17:20:03 -070058 ImageButton mRefresh;
59 ImageButton mForward;
Michael Kolbf2055602011-04-09 17:20:03 -070060 ImageButton mBookmarks;
61 ImageButton mMore;
62 ImageButton mNewTab;
Michael Kolbf2055602011-04-09 17:20:03 -070063 FrameLayout mHolder;
64
Michael Kolb2814a362011-05-19 15:49:41 -070065 TextView mTitle;
66 ImageView mFavicon;
67 ImageButton mCloseTab;
68
Michael Kolb9829b432011-06-04 13:29:00 -070069 NavTabGallery mScroller;
Michael Kolbf2055602011-04-09 17:20:03 -070070 TabAdapter mAdapter;
Michael Kolba4261fd2011-05-05 11:27:37 -070071 int mOrientation;
Michael Kolb3ca12752011-07-20 13:52:25 -070072 boolean mNeedsMenu;
John Reck8ee633f2011-08-09 16:00:35 -070073 HashMap<Tab, View> mTabViews;
Michael Kolbf2055602011-04-09 17:20:03 -070074
75 public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
76 super(activity);
77 mActivity = activity;
78 mUiController = ctl;
79 mUi = ui;
Michael Kolba4261fd2011-05-05 11:27:37 -070080 mOrientation = activity.getResources().getConfiguration().orientation;
Michael Kolbf2055602011-04-09 17:20:03 -070081 init();
82 }
83
84 protected Tab getSelectedTab() {
Michael Kolb2814a362011-05-19 15:49:41 -070085 return (Tab) mScroller.getSelectedItem();
Michael Kolbf2055602011-04-09 17:20:03 -070086 }
87
Michael Kolbfedb4922011-04-20 16:45:33 -070088 protected void showMenu() {
Michael Kolb017ffab2011-07-11 15:26:47 -070089 PopupMenu popup = new PopupMenu(mContext, mMore);
90 Menu menu = popup.getMenu();
91 popup.getMenuInflater().inflate(R.menu.browser, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -070092 mUiController.updateMenuState(mScroller.getSelectedItem(), menu);
Michael Kolb017ffab2011-07-11 15:26:47 -070093 popup.setOnMenuItemClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -070094 popup.show();
Michael Kolb017ffab2011-07-11 15:26:47 -070095 }
96
97 @Override
98 public boolean onMenuItemClick(MenuItem item) {
Michael Kolbadb0e602011-07-18 14:33:46 -070099 mUi.hideNavScreen(false);
Michael Kolb017ffab2011-07-11 15:26:47 -0700100 return mUiController.onOptionsItemSelected(item);
Michael Kolbf2055602011-04-09 17:20:03 -0700101 }
102
John Reckadc921f2011-04-27 10:11:03 -0700103 protected float getToolbarHeight() {
104 return mActivity.getResources().getDimension(R.dimen.toolbar_height);
105 }
106
Michael Kolba4261fd2011-05-05 11:27:37 -0700107 // for configuration changes
108 @Override
109 protected void onConfigurationChanged(Configuration newconfig) {
110 if (newconfig.orientation != mOrientation) {
Michael Kolb2814a362011-05-19 15:49:41 -0700111 int selIx = mScroller.getSelectionIndex();
Michael Kolba4261fd2011-05-05 11:27:37 -0700112 removeAllViews();
Michael Kolb9829b432011-06-04 13:29:00 -0700113 mOrientation = newconfig.orientation;
Michael Kolba4261fd2011-05-05 11:27:37 -0700114 init();
Michael Kolb2814a362011-05-19 15:49:41 -0700115 mScroller.setSelection(selIx);
Michael Kolb2814a362011-05-19 15:49:41 -0700116 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700117 }
118 }
119
Michael Kolbf2055602011-04-09 17:20:03 -0700120 private void init() {
121 LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this);
Michael Kolb30adae62011-07-29 13:37:05 -0700122 setContentDescription(mContext.getResources().getString(
123 R.string.accessibility_transition_navscreen));
Michael Kolbf2055602011-04-09 17:20:03 -0700124 mBookmarks = (ImageButton) findViewById(R.id.bookmarks);
125 mNewTab = (ImageButton) findViewById(R.id.newtab);
Michael Kolbf2055602011-04-09 17:20:03 -0700126 mMore = (ImageButton) findViewById(R.id.more);
Michael Kolbf2055602011-04-09 17:20:03 -0700127 mBookmarks.setOnClickListener(this);
128 mNewTab.setOnClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -0700129 mMore.setOnClickListener(this);
Michael Kolb9829b432011-06-04 13:29:00 -0700130 mScroller = (NavTabGallery) findViewById(R.id.scroller);
John Reck8ee633f2011-08-09 16:00:35 -0700131 TabControl tc = mUiController.getTabControl();
132 mTabViews = new HashMap<Tab, View>(tc.getTabCount());
133 mAdapter = new TabAdapter(mContext, tc);
Michael Kolb9829b432011-06-04 13:29:00 -0700134 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
135 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700136 // update state for active tab
Michael Kolbc3af0672011-08-09 10:24:41 -0700137 mScroller.setAdapter(mAdapter,
138 mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbdcd81d32011-07-22 10:30:49 -0700139 mScroller.setOnRemoveListener(new OnRemoveListener() {
140 public void onRemovePosition(int pos) {
141 Tab tab = mAdapter.getItem(pos);
142 onCloseTab(tab);
143 }
144 });
Michael Kolb3ca12752011-07-20 13:52:25 -0700145 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
146 if (!mNeedsMenu) {
147 mMore.setVisibility(View.GONE);
148 }
Michael Kolbf2055602011-04-09 17:20:03 -0700149 }
150
151 @Override
152 public void onClick(View v) {
153 WebView web = (mTab != null) ? mTab.getWebView() : null;
154 if (web != null) {
Michael Kolb2814a362011-05-19 15:49:41 -0700155 if (mForward == v) {
Michael Kolbf2055602011-04-09 17:20:03 -0700156 mUi.hideNavScreen(true);
John Reckef654f12011-07-12 16:42:08 -0700157 mTab.goForward();
Michael Kolbf2055602011-04-09 17:20:03 -0700158 } else if (mRefresh == v) {
159 mUi.hideNavScreen(true);
160 web.reload();
161 }
162 }
163 if (mBookmarks == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700164 switchToSelected();
Michael Kolbf2055602011-04-09 17:20:03 -0700165 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700166 } else if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700167 openNewTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700168 } else if (mMore == v) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700169 showMenu();
Michael Kolb2814a362011-05-19 15:49:41 -0700170 } else if (mTitle == v) {
171 mUi.getTitleBar().setSkipTitleBarAnimations(true);
172 close(false);
173 mUi.editUrl(false);
174 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700175 }
176 }
177
Michael Kolb2814a362011-05-19 15:49:41 -0700178 private void onCloseTab(Tab tab) {
179 if (tab != null) {
Michael Kolb308b3012011-07-13 14:50:56 -0700180 if (tab == mUiController.getCurrentTab()) {
181 mUiController.closeCurrentTab();
182 } else {
183 mUiController.closeTab(tab);
184 }
Michael Kolb66af8162011-06-09 11:33:34 -0700185 mAdapter.notifyDataSetChanged();
Michael Kolb2814a362011-05-19 15:49:41 -0700186 }
187 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700188
Michael Kolb2814a362011-05-19 15:49:41 -0700189 private void openNewTab() {
190 // need to call openTab explicitely with setactive false
191 Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
192 false, false, false);
Michael Kolbc3af0672011-08-09 10:24:41 -0700193 int duration = 0;
Michael Kolba4261fd2011-05-05 11:27:37 -0700194 if (tab != null) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700195 mUiController.setBlockEvents(true);
196 int oldsel = mScroller.getSelectedItemPosition();
Michael Kolbc831b632011-05-11 09:30:34 -0700197 final int tix = mUi.mTabControl.getTabPosition(tab);
Michael Kolbc3af0672011-08-09 10:24:41 -0700198 duration = SCROLL_MIN + SCROLL_FACTOR * Math.abs(oldsel - tix);
199 mScroller.handleDataChanged();
200 mScroller.smoothScrollToPosition(tix, duration, new OnScrollFinishedListener() {
Michael Kolb2814a362011-05-19 15:49:41 -0700201 @Override
Michael Kolbc3af0672011-08-09 10:24:41 -0700202 public void onScrollFinished() {
203 mUiController.setBlockEvents(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700204 mUi.hideNavScreen(true);
205 switchToSelected();
206 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700207 });
Michael Kolba4261fd2011-05-05 11:27:37 -0700208 }
209 }
210
Michael Kolbc3af0672011-08-09 10:24:41 -0700211 View getSelectedTabView() {
212 return mScroller.getSelectedTab();
213 }
214
Michael Kolba4261fd2011-05-05 11:27:37 -0700215 private void switchToSelected() {
Michael Kolb2814a362011-05-19 15:49:41 -0700216 Tab tab = (Tab) mScroller.getSelectedItem();
Michael Kolba4261fd2011-05-05 11:27:37 -0700217 if (tab != mUi.getActiveTab()) {
218 mUiController.setActiveTab(tab);
219 }
220 }
221
Michael Kolbf2055602011-04-09 17:20:03 -0700222 protected void close() {
223 close(true);
224 }
225
226 protected void close(boolean animate) {
227 mUi.hideNavScreen(animate);
Michael Kolbf2055602011-04-09 17:20:03 -0700228 }
229
Michael Kolbf2055602011-04-09 17:20:03 -0700230 class TabAdapter extends BaseAdapter {
231
232 Context context;
233 TabControl tabControl;
234
235 public TabAdapter(Context ctx, TabControl tc) {
236 context = ctx;
237 tabControl = tc;
238 }
239
Michael Kolbf2055602011-04-09 17:20:03 -0700240 @Override
241 public int getCount() {
242 return tabControl.getTabCount();
243 }
244
245 @Override
246 public Tab getItem(int position) {
247 return tabControl.getTab(position);
248 }
249
250 public long getItemId(int position) {
251 return position;
252 }
253
254 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700255 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700256 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700257 final Tab tab = getItem(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700258 tabview.setWebView(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700259 mTabViews.put(tab, tabview.mImage);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700260 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700261 @Override
262 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700263 if (tabview.isClose(v)) {
Michael Kolbe76f7042011-07-27 15:16:32 -0700264 mScroller.animateOut(tabview);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700265 } else if (tabview.isTitle(v)) {
Michael Kolb308b3012011-07-13 14:50:56 -0700266 mScroller.setSelection(position);
267 switchToSelected();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700268 mUi.getTitleBar().setSkipTitleBarAnimations(true);
269 close(false);
270 mUi.editUrl(false);
271 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700272 } else if (tabview.isWebView(v)) {
273 mScroller.setSelection(position);
274 close();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700275 }
276 }
277 });
278 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700279 }
Michael Kolb2814a362011-05-19 15:49:41 -0700280
Michael Kolbf2055602011-04-09 17:20:03 -0700281 }
282
John Reck8ee633f2011-08-09 16:00:35 -0700283 @Override
284 public void onThumbnailUpdated(Tab t) {
285 View v = mTabViews.get(t);
286 if (v != null) {
287 v.invalidate();
288 mScroller.invalidate();
289 }
290 }
291
Michael Kolbf2055602011-04-09 17:20:03 -0700292}