blob: 0190c1b12519f68a062e181d9f0c82586346b091 [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;
42
43import java.util.HashMap;
Michael Kolbdcd81d32011-07-22 10:30:49 -070044
Michael Kolb017ffab2011-07-11 15:26:47 -070045public class NavScreen extends RelativeLayout
John Reck8ee633f2011-08-09 16:00:35 -070046 implements OnClickListener, OnMenuItemClickListener, OnThumbnailUpdatedListener {
Michael Kolbf2055602011-04-09 17:20:03 -070047
48 UiController mUiController;
49 PhoneUi mUi;
50 Tab mTab;
51 Activity mActivity;
52
Michael Kolbf2055602011-04-09 17:20:03 -070053 ImageButton mRefresh;
54 ImageButton mForward;
Michael Kolbf2055602011-04-09 17:20:03 -070055 ImageButton mBookmarks;
56 ImageButton mMore;
57 ImageButton mNewTab;
Michael Kolbf2055602011-04-09 17:20:03 -070058 FrameLayout mHolder;
59
Michael Kolb2814a362011-05-19 15:49:41 -070060 TextView mTitle;
61 ImageView mFavicon;
62 ImageButton mCloseTab;
63
Michael Kolb9829b432011-06-04 13:29:00 -070064 NavTabGallery mScroller;
Michael Kolbf2055602011-04-09 17:20:03 -070065 TabAdapter mAdapter;
Michael Kolba4261fd2011-05-05 11:27:37 -070066 int mOrientation;
Michael Kolb3ca12752011-07-20 13:52:25 -070067 boolean mNeedsMenu;
John Reck8ee633f2011-08-09 16:00:35 -070068 HashMap<Tab, View> mTabViews;
Michael Kolbf2055602011-04-09 17:20:03 -070069
70 public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
71 super(activity);
72 mActivity = activity;
73 mUiController = ctl;
74 mUi = ui;
Michael Kolba4261fd2011-05-05 11:27:37 -070075 mOrientation = activity.getResources().getConfiguration().orientation;
Michael Kolbf2055602011-04-09 17:20:03 -070076 init();
77 }
78
79 protected Tab getSelectedTab() {
Michael Kolb2814a362011-05-19 15:49:41 -070080 return (Tab) mScroller.getSelectedItem();
Michael Kolbf2055602011-04-09 17:20:03 -070081 }
82
Michael Kolbfedb4922011-04-20 16:45:33 -070083 protected void showMenu() {
Michael Kolb017ffab2011-07-11 15:26:47 -070084 PopupMenu popup = new PopupMenu(mContext, mMore);
85 Menu menu = popup.getMenu();
86 popup.getMenuInflater().inflate(R.menu.browser, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -070087 mUiController.updateMenuState(mScroller.getSelectedItem(), menu);
Michael Kolb017ffab2011-07-11 15:26:47 -070088 popup.setOnMenuItemClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -070089 popup.show();
Michael Kolb017ffab2011-07-11 15:26:47 -070090 }
91
92 @Override
93 public boolean onMenuItemClick(MenuItem item) {
Michael Kolbadb0e602011-07-18 14:33:46 -070094 mUi.hideNavScreen(false);
Michael Kolb017ffab2011-07-11 15:26:47 -070095 return mUiController.onOptionsItemSelected(item);
Michael Kolbf2055602011-04-09 17:20:03 -070096 }
97
John Reckadc921f2011-04-27 10:11:03 -070098 protected float getToolbarHeight() {
99 return mActivity.getResources().getDimension(R.dimen.toolbar_height);
100 }
101
Michael Kolba4261fd2011-05-05 11:27:37 -0700102 // for configuration changes
103 @Override
104 protected void onConfigurationChanged(Configuration newconfig) {
105 if (newconfig.orientation != mOrientation) {
Michael Kolb2814a362011-05-19 15:49:41 -0700106 int selIx = mScroller.getSelectionIndex();
Michael Kolba4261fd2011-05-05 11:27:37 -0700107 removeAllViews();
Michael Kolb9829b432011-06-04 13:29:00 -0700108 mOrientation = newconfig.orientation;
Michael Kolba4261fd2011-05-05 11:27:37 -0700109 init();
Michael Kolb2814a362011-05-19 15:49:41 -0700110 mScroller.setSelection(selIx);
Michael Kolb2814a362011-05-19 15:49:41 -0700111 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700112 }
113 }
114
Michael Kolbf2055602011-04-09 17:20:03 -0700115 private void init() {
116 LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this);
Michael Kolb30adae62011-07-29 13:37:05 -0700117 setContentDescription(mContext.getResources().getString(
118 R.string.accessibility_transition_navscreen));
Michael Kolbf2055602011-04-09 17:20:03 -0700119 mBookmarks = (ImageButton) findViewById(R.id.bookmarks);
120 mNewTab = (ImageButton) findViewById(R.id.newtab);
Michael Kolbf2055602011-04-09 17:20:03 -0700121 mMore = (ImageButton) findViewById(R.id.more);
Michael Kolbf2055602011-04-09 17:20:03 -0700122 mBookmarks.setOnClickListener(this);
123 mNewTab.setOnClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -0700124 mMore.setOnClickListener(this);
Michael Kolb9829b432011-06-04 13:29:00 -0700125 mScroller = (NavTabGallery) findViewById(R.id.scroller);
John Reck8ee633f2011-08-09 16:00:35 -0700126 TabControl tc = mUiController.getTabControl();
127 mTabViews = new HashMap<Tab, View>(tc.getTabCount());
128 mAdapter = new TabAdapter(mContext, tc);
Michael Kolb2814a362011-05-19 15:49:41 -0700129 mScroller.setAdapter(mAdapter);
Michael Kolb9829b432011-06-04 13:29:00 -0700130 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
131 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700132 // update state for active tab
133 mScroller.setSelection(mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbdcd81d32011-07-22 10:30:49 -0700134 mScroller.setOnRemoveListener(new OnRemoveListener() {
135 public void onRemovePosition(int pos) {
136 Tab tab = mAdapter.getItem(pos);
137 onCloseTab(tab);
138 }
139 });
Michael Kolb3ca12752011-07-20 13:52:25 -0700140 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
141 if (!mNeedsMenu) {
142 mMore.setVisibility(View.GONE);
143 }
Michael Kolbf2055602011-04-09 17:20:03 -0700144 }
145
146 @Override
147 public void onClick(View v) {
148 WebView web = (mTab != null) ? mTab.getWebView() : null;
149 if (web != null) {
Michael Kolb2814a362011-05-19 15:49:41 -0700150 if (mForward == v) {
Michael Kolbf2055602011-04-09 17:20:03 -0700151 mUi.hideNavScreen(true);
John Reckef654f12011-07-12 16:42:08 -0700152 mTab.goForward();
Michael Kolbf2055602011-04-09 17:20:03 -0700153 } else if (mRefresh == v) {
154 mUi.hideNavScreen(true);
155 web.reload();
156 }
157 }
158 if (mBookmarks == v) {
159 mUi.hideNavScreen(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700160 switchToSelected();
Michael Kolbf2055602011-04-09 17:20:03 -0700161 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700162 } else if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700163 openNewTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700164 } else if (mMore == v) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700165 showMenu();
Michael Kolb2814a362011-05-19 15:49:41 -0700166 } else if (mTitle == v) {
167 mUi.getTitleBar().setSkipTitleBarAnimations(true);
168 close(false);
169 mUi.editUrl(false);
170 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700171 }
172 }
173
Michael Kolb2814a362011-05-19 15:49:41 -0700174 private void onCloseTab(Tab tab) {
175 if (tab != null) {
Michael Kolb308b3012011-07-13 14:50:56 -0700176 if (tab == mUiController.getCurrentTab()) {
177 mUiController.closeCurrentTab();
178 } else {
179 mUiController.closeTab(tab);
180 }
Michael Kolb66af8162011-06-09 11:33:34 -0700181 mAdapter.notifyDataSetChanged();
Michael Kolb2814a362011-05-19 15:49:41 -0700182 }
183 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700184
Michael Kolb2814a362011-05-19 15:49:41 -0700185 private void openNewTab() {
186 // need to call openTab explicitely with setactive false
187 Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
188 false, false, false);
189 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700190 if (tab != null) {
191 // set tab as the selected in flipper, then hide
Michael Kolbc831b632011-05-11 09:30:34 -0700192 final int tix = mUi.mTabControl.getTabPosition(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700193 mScroller.setSelection(tix);
194 postDelayed(new Runnable() {
195 @Override
Michael Kolba4261fd2011-05-05 11:27:37 -0700196 public void run() {
Michael Kolba4261fd2011-05-05 11:27:37 -0700197 mUi.hideNavScreen(true);
198 switchToSelected();
199 }
Michael Kolb2814a362011-05-19 15:49:41 -0700200 }, 100);
Michael Kolba4261fd2011-05-05 11:27:37 -0700201 }
202 }
203
204 private void switchToSelected() {
Michael Kolb2814a362011-05-19 15:49:41 -0700205 Tab tab = (Tab) mScroller.getSelectedItem();
Michael Kolba4261fd2011-05-05 11:27:37 -0700206 if (tab != mUi.getActiveTab()) {
207 mUiController.setActiveTab(tab);
208 }
209 }
210
Michael Kolbf2055602011-04-09 17:20:03 -0700211 protected void close() {
212 close(true);
213 }
214
215 protected void close(boolean animate) {
216 mUi.hideNavScreen(animate);
Michael Kolbf2055602011-04-09 17:20:03 -0700217 }
218
Michael Kolbf2055602011-04-09 17:20:03 -0700219 class TabAdapter extends BaseAdapter {
220
221 Context context;
222 TabControl tabControl;
223
224 public TabAdapter(Context ctx, TabControl tc) {
225 context = ctx;
226 tabControl = tc;
227 }
228
Michael Kolbf2055602011-04-09 17:20:03 -0700229 @Override
230 public int getCount() {
231 return tabControl.getTabCount();
232 }
233
234 @Override
235 public Tab getItem(int position) {
236 return tabControl.getTab(position);
237 }
238
239 public long getItemId(int position) {
240 return position;
241 }
242
243 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700244 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700245 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700246 final Tab tab = getItem(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700247 tabview.setWebView(mUi, tab);
John Reck8ee633f2011-08-09 16:00:35 -0700248 mTabViews.put(tab, tabview.mImage);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700249 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700250 @Override
251 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700252 if (tabview.isClose(v)) {
Michael Kolbe76f7042011-07-27 15:16:32 -0700253 mScroller.animateOut(tabview);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700254 } else if (tabview.isTitle(v)) {
Michael Kolb308b3012011-07-13 14:50:56 -0700255 mScroller.setSelection(position);
256 switchToSelected();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700257 mUi.getTitleBar().setSkipTitleBarAnimations(true);
258 close(false);
259 mUi.editUrl(false);
260 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700261 } else if (tabview.isWebView(v)) {
262 mScroller.setSelection(position);
263 close();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700264 }
265 }
266 });
267 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700268 }
Michael Kolb2814a362011-05-19 15:49:41 -0700269
Michael Kolbf2055602011-04-09 17:20:03 -0700270 }
271
John Reck8ee633f2011-08-09 16:00:35 -0700272 @Override
273 public void onThumbnailUpdated(Tab t) {
274 View v = mTabViews.get(t);
275 if (v != null) {
276 v.invalidate();
277 mScroller.invalidate();
278 }
279 }
280
Michael Kolbf2055602011-04-09 17:20:03 -0700281}