blob: 22f62570562ae4f996494f5d107497d80a26a35c [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) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700159 switchToSelected();
Michael Kolbf2055602011-04-09 17:20:03 -0700160 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700161 } else if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700162 openNewTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700163 } else if (mMore == v) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700164 showMenu();
Michael Kolb2814a362011-05-19 15:49:41 -0700165 } else if (mTitle == v) {
166 mUi.getTitleBar().setSkipTitleBarAnimations(true);
167 close(false);
168 mUi.editUrl(false);
169 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700170 }
171 }
172
Michael Kolb2814a362011-05-19 15:49:41 -0700173 private void onCloseTab(Tab tab) {
174 if (tab != null) {
Michael Kolb308b3012011-07-13 14:50:56 -0700175 if (tab == mUiController.getCurrentTab()) {
176 mUiController.closeCurrentTab();
177 } else {
178 mUiController.closeTab(tab);
179 }
Michael Kolb66af8162011-06-09 11:33:34 -0700180 mAdapter.notifyDataSetChanged();
Michael Kolb2814a362011-05-19 15:49:41 -0700181 }
182 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700183
Michael Kolb2814a362011-05-19 15:49:41 -0700184 private void openNewTab() {
185 // need to call openTab explicitely with setactive false
186 Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
187 false, false, false);
188 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700189 if (tab != null) {
190 // set tab as the selected in flipper, then hide
Michael Kolbc831b632011-05-11 09:30:34 -0700191 final int tix = mUi.mTabControl.getTabPosition(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700192 mScroller.setSelection(tix);
193 postDelayed(new Runnable() {
194 @Override
Michael Kolba4261fd2011-05-05 11:27:37 -0700195 public void run() {
Michael Kolba4261fd2011-05-05 11:27:37 -0700196 mUi.hideNavScreen(true);
197 switchToSelected();
198 }
Michael Kolb2814a362011-05-19 15:49:41 -0700199 }, 100);
Michael Kolba4261fd2011-05-05 11:27:37 -0700200 }
201 }
202
203 private void switchToSelected() {
Michael Kolb2814a362011-05-19 15:49:41 -0700204 Tab tab = (Tab) mScroller.getSelectedItem();
Michael Kolba4261fd2011-05-05 11:27:37 -0700205 if (tab != mUi.getActiveTab()) {
206 mUiController.setActiveTab(tab);
207 }
208 }
209
Michael Kolbf2055602011-04-09 17:20:03 -0700210 protected void close() {
211 close(true);
212 }
213
214 protected void close(boolean animate) {
215 mUi.hideNavScreen(animate);
Michael Kolbf2055602011-04-09 17:20:03 -0700216 }
217
Michael Kolbf2055602011-04-09 17:20:03 -0700218 class TabAdapter extends BaseAdapter {
219
220 Context context;
221 TabControl tabControl;
222
223 public TabAdapter(Context ctx, TabControl tc) {
224 context = ctx;
225 tabControl = tc;
226 }
227
Michael Kolbf2055602011-04-09 17:20:03 -0700228 @Override
229 public int getCount() {
230 return tabControl.getTabCount();
231 }
232
233 @Override
234 public Tab getItem(int position) {
235 return tabControl.getTab(position);
236 }
237
238 public long getItemId(int position) {
239 return position;
240 }
241
242 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700243 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700244 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700245 final Tab tab = getItem(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700246 tabview.setWebView(mUi, tab);
John Reck8ee633f2011-08-09 16:00:35 -0700247 mTabViews.put(tab, tabview.mImage);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700248 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700249 @Override
250 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700251 if (tabview.isClose(v)) {
Michael Kolbe76f7042011-07-27 15:16:32 -0700252 mScroller.animateOut(tabview);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700253 } else if (tabview.isTitle(v)) {
Michael Kolb308b3012011-07-13 14:50:56 -0700254 mScroller.setSelection(position);
255 switchToSelected();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700256 mUi.getTitleBar().setSkipTitleBarAnimations(true);
257 close(false);
258 mUi.editUrl(false);
259 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700260 } else if (tabview.isWebView(v)) {
261 mScroller.setSelection(position);
262 close();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700263 }
264 }
265 });
266 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700267 }
Michael Kolb2814a362011-05-19 15:49:41 -0700268
Michael Kolbf2055602011-04-09 17:20:03 -0700269 }
270
John Reck8ee633f2011-08-09 16:00:35 -0700271 @Override
272 public void onThumbnailUpdated(Tab t) {
273 View v = mTabViews.get(t);
274 if (v != null) {
275 v.invalidate();
276 mScroller.invalidate();
277 }
278 }
279
Michael Kolbf2055602011-04-09 17:20:03 -0700280}