blob: eeca95a032178df2473cfe20718d70509531b0e0 [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;
27import android.view.ViewGroup;
28import android.webkit.WebView;
Michael Kolbf2055602011-04-09 17:20:03 -070029import android.widget.BaseAdapter;
30import android.widget.FrameLayout;
31import android.widget.Gallery;
32import android.widget.ImageButton;
33import android.widget.ImageView;
Michael Kolbf2055602011-04-09 17:20:03 -070034import android.widget.ListPopupWindow;
Michael Kolb2814a362011-05-19 15:49:41 -070035import android.widget.RelativeLayout;
Michael Kolbf2055602011-04-09 17:20:03 -070036import android.widget.TextView;
37
38import java.util.ArrayList;
39import java.util.List;
40
Michael Kolb2814a362011-05-19 15:49:41 -070041public class NavScreen extends RelativeLayout implements OnClickListener {
Michael Kolbf2055602011-04-09 17:20:03 -070042
43 UiController mUiController;
44 PhoneUi mUi;
45 Tab mTab;
46 Activity mActivity;
47
Michael Kolbf2055602011-04-09 17:20:03 -070048 ImageButton mRefresh;
49 ImageButton mForward;
Michael Kolbf2055602011-04-09 17:20:03 -070050 ImageButton mBookmarks;
51 ImageButton mMore;
52 ImageButton mNewTab;
53 ImageButton mNewIncognito;
54 FrameLayout mHolder;
55
Michael Kolb2814a362011-05-19 15:49:41 -070056 TextView mTitle;
57 ImageView mFavicon;
58 ImageButton mCloseTab;
59
60 NavTabScroller mScroller;
Michael Kolba4261fd2011-05-05 11:27:37 -070061 float mTabAspect = 0.66f;
Michael Kolbf2055602011-04-09 17:20:03 -070062 int mTabWidth;
63 int mTabHeight;
64 TabAdapter mAdapter;
65 ListPopupWindow mPopup;
Michael Kolba4261fd2011-05-05 11:27:37 -070066 int mOrientation;
Michael Kolbf2055602011-04-09 17:20:03 -070067
68 public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
69 super(activity);
70 mActivity = activity;
71 mUiController = ctl;
72 mUi = ui;
Michael Kolba4261fd2011-05-05 11:27:37 -070073 mOrientation = activity.getResources().getConfiguration().orientation;
Michael Kolbf2055602011-04-09 17:20:03 -070074 init();
75 }
76
77 protected Tab getSelectedTab() {
Michael Kolb2814a362011-05-19 15:49:41 -070078 return (Tab) mScroller.getSelectedItem();
Michael Kolbf2055602011-04-09 17:20:03 -070079 }
80
Michael Kolbfedb4922011-04-20 16:45:33 -070081 protected void showMenu() {
82 Menu menu = mUi.getMenu();
83 menu.setGroupVisible(R.id.NAV_MENU, false);
84
Michael Kolbf2055602011-04-09 17:20:03 -070085 MenuAdapter menuAdapter = new MenuAdapter(mContext);
86 menuAdapter.setMenu(menu);
87 ListPopupWindow popup = new ListPopupWindow(mContext);
88 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
89 popup.setAdapter(menuAdapter);
90 popup.setModal(true);
91 popup.setAnchorView(mMore);
92 popup.setWidth((int) mContext.getResources().getDimension(
93 R.dimen.menu_width));
94 popup.show();
95 mPopup = popup;
96 }
97
John Reckadc921f2011-04-27 10:11:03 -070098 protected float getToolbarHeight() {
99 return mActivity.getResources().getDimension(R.dimen.toolbar_height);
100 }
101
Michael Kolbf2055602011-04-09 17:20:03 -0700102 protected void dismissMenu() {
103 if (mPopup != null) {
104 mPopup.dismiss();
105 }
106 }
107
Michael Kolba4261fd2011-05-05 11:27:37 -0700108 // for configuration changes
109 @Override
110 protected void onConfigurationChanged(Configuration newconfig) {
111 if (newconfig.orientation != mOrientation) {
Michael Kolb2814a362011-05-19 15:49:41 -0700112 int selIx = mScroller.getSelectionIndex();
Michael Kolba4261fd2011-05-05 11:27:37 -0700113 removeAllViews();
114 init();
Michael Kolb2814a362011-05-19 15:49:41 -0700115 mScroller.setSelection(selIx);
Michael Kolba4261fd2011-05-05 11:27:37 -0700116 mOrientation = newconfig.orientation;
Michael Kolb2814a362011-05-19 15:49:41 -0700117 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700118 }
119 }
120
Michael Kolbf2055602011-04-09 17:20:03 -0700121 private void init() {
122 LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700123 mBookmarks = (ImageButton) findViewById(R.id.bookmarks);
124 mNewTab = (ImageButton) findViewById(R.id.newtab);
125 mNewIncognito = (ImageButton) findViewById(R.id.newincognito);
126 mMore = (ImageButton) findViewById(R.id.more);
Michael Kolbf2055602011-04-09 17:20:03 -0700127 mBookmarks.setOnClickListener(this);
128 mNewTab.setOnClickListener(this);
129 mNewIncognito.setOnClickListener(this);
130 mMore.setOnClickListener(this);
Michael Kolb2814a362011-05-19 15:49:41 -0700131 mScroller = (NavTabScroller) findViewById(R.id.scroller);
Michael Kolbf2055602011-04-09 17:20:03 -0700132 mAdapter = new TabAdapter(mContext, mUiController.getTabControl());
Michael Kolb2814a362011-05-19 15:49:41 -0700133 mScroller.setAdapter(mAdapter);
Michael Kolbf2055602011-04-09 17:20:03 -0700134
Michael Kolb2814a362011-05-19 15:49:41 -0700135 // update state for active tab
136 mScroller.setSelection(mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbf2055602011-04-09 17:20:03 -0700137 }
138
139 @Override
140 public void onClick(View v) {
141 WebView web = (mTab != null) ? mTab.getWebView() : null;
142 if (web != null) {
Michael Kolb2814a362011-05-19 15:49:41 -0700143 if (mForward == v) {
Michael Kolbf2055602011-04-09 17:20:03 -0700144 mUi.hideNavScreen(true);
Michael Kolbf2055602011-04-09 17:20:03 -0700145 web.goForward();
146 } else if (mRefresh == v) {
147 mUi.hideNavScreen(true);
148 web.reload();
149 }
150 }
151 if (mBookmarks == v) {
152 mUi.hideNavScreen(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700153 switchToSelected();
Michael Kolbf2055602011-04-09 17:20:03 -0700154 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700155 } else if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700156 openNewTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700157 } else if (mMore == v) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700158 showMenu();
Michael Kolbf2055602011-04-09 17:20:03 -0700159 } else if (mNewIncognito == v) {
160 mUi.hideNavScreen(true);
Michael Kolb519d2282011-05-09 17:03:19 -0700161 mUiController.openIncognitoTab();
Michael Kolb2814a362011-05-19 15:49:41 -0700162 } else if (mTitle == v) {
163 mUi.getTitleBar().setSkipTitleBarAnimations(true);
164 close(false);
165 mUi.editUrl(false);
166 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700167 }
168 }
169
Michael Kolb2814a362011-05-19 15:49:41 -0700170 private void onCloseTab(Tab tab) {
171 if (tab != null) {
Michael Kolb66af8162011-06-09 11:33:34 -0700172 switchToSelected();
173 mUiController.closeCurrentTab();
174 mAdapter.notifyDataSetChanged();
Michael Kolb2814a362011-05-19 15:49:41 -0700175 }
176 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700177
Michael Kolb2814a362011-05-19 15:49:41 -0700178 private void openNewTab() {
179 // need to call openTab explicitely with setactive false
180 Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
181 false, false, false);
182 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700183 if (tab != null) {
184 // set tab as the selected in flipper, then hide
Michael Kolbc831b632011-05-11 09:30:34 -0700185 final int tix = mUi.mTabControl.getTabPosition(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700186 mScroller.setSelection(tix);
187 postDelayed(new Runnable() {
188 @Override
Michael Kolba4261fd2011-05-05 11:27:37 -0700189 public void run() {
Michael Kolba4261fd2011-05-05 11:27:37 -0700190 mUi.hideNavScreen(true);
191 switchToSelected();
192 }
Michael Kolb2814a362011-05-19 15:49:41 -0700193 }, 100);
Michael Kolba4261fd2011-05-05 11:27:37 -0700194 }
195 }
196
197 private void switchToSelected() {
Michael Kolb2814a362011-05-19 15:49:41 -0700198 Tab tab = (Tab) mScroller.getSelectedItem();
Michael Kolba4261fd2011-05-05 11:27:37 -0700199 if (tab != mUi.getActiveTab()) {
200 mUiController.setActiveTab(tab);
201 }
202 }
203
Michael Kolbf2055602011-04-09 17:20:03 -0700204 protected void close() {
205 close(true);
206 }
207
208 protected void close(boolean animate) {
209 mUi.hideNavScreen(animate);
Michael Kolbf2055602011-04-09 17:20:03 -0700210 }
211
212 class TabGallery extends Gallery {
213
214 public TabGallery(Context ctx) {
215 super(ctx);
216 setUnselectedAlpha(0.3f);
217 }
218
219 @Override
220 protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
Michael Kolb2814a362011-05-19 15:49:41 -0700221 return new Gallery.LayoutParams(mTabWidth, mTabHeight);
Michael Kolbf2055602011-04-09 17:20:03 -0700222 }
223
224 @Override
225 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
226 return generateDefaultLayoutParams();
227 }
228
229 }
230
231 class TabAdapter extends BaseAdapter {
232
233 Context context;
234 TabControl tabControl;
235
236 public TabAdapter(Context ctx, TabControl tc) {
237 context = ctx;
238 tabControl = tc;
239 }
240
Michael Kolbf2055602011-04-09 17:20:03 -0700241 @Override
242 public int getCount() {
243 return tabControl.getTabCount();
244 }
245
246 @Override
247 public Tab getItem(int position) {
248 return tabControl.getTab(position);
249 }
250
251 public long getItemId(int position) {
252 return position;
253 }
254
255 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700256 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700257 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700258 final Tab tab = getItem(position);
Michael Kolb2814a362011-05-19 15:49:41 -0700259 final BrowserWebView web = (BrowserWebView) tab.getWebView();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700260 tabview.setWebView(mUi, tab);
261 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700262 @Override
263 public void onClick(View v) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700264 if (tabview.isRefresh(v)) {
265 mUi.hideNavScreen(true);
266 web.reload();
267 } else if (tabview.isClose(v)) {
268 onCloseTab((Tab) (mScroller.getSelectedItem()));
269 } else if (tabview.isTitle(v)) {
270 mUi.getTitleBar().setSkipTitleBarAnimations(true);
271 close(false);
272 mUi.editUrl(false);
273 mUi.getTitleBar().setSkipTitleBarAnimations(false);
274 } else if (tabview.isForward(v)) {
275 mUi.hideNavScreen(true);
276 web.goForward();
277 } else if (tabview.isWebView(v)) {
278 mScroller.setSelection(position);
279 close();
Michael Kolb2814a362011-05-19 15:49:41 -0700280
Michael Kolb4bd767d2011-05-27 11:33:55 -0700281 }
282 }
283 });
284 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700285 }
Michael Kolb2814a362011-05-19 15:49:41 -0700286
Michael Kolbf2055602011-04-09 17:20:03 -0700287 }
288
289 private class MenuAdapter extends BaseAdapter implements OnClickListener {
290
291 List<MenuItem> mItems;
292 LayoutInflater mInflater;
293
294 public MenuAdapter(Context ctx) {
295 mInflater = LayoutInflater.from(ctx);
296 mItems = new ArrayList<MenuItem>();
297 }
298
299 public void setMenu(Menu menu) {
300 mItems.clear();
301 for (int i = 0; i < menu.size(); i++) {
302 MenuItem item = menu.getItem(i);
303 if (item.isEnabled() && item.isVisible()) {
304 mItems.add(item);
305 }
306 }
307 notifyDataSetChanged();
308 }
309
310 @Override
311 public int getCount() {
312 return mItems.size();
313 }
314
315 @Override
316 public MenuItem getItem(int position) {
317 return mItems.get(position);
318 }
319
320 @Override
321 public long getItemId(int position) {
322 return position;
323 }
324
325 @Override
326 public void onClick(View v) {
327 if (v.getTag() != null) {
328 dismissMenu();
329 mActivity.closeOptionsMenu();
330 mUi.hideNavScreen(false);
331 mUiController.onOptionsItemSelected((MenuItem) v.getTag());
332 }
333 }
334
335 @Override
336 public View getView(int position, View convertView, ViewGroup parent) {
337 final MenuItem item = mItems.get(position);
338 View view = mInflater.inflate(R.layout.qc_menu_item, null);
339 TextView label = (TextView) view.findViewById(R.id.title);
340 label.setText(item.getTitle());
341 label.setTag(item);
342 label.setOnClickListener(this);
343 return label;
344 }
345
346 }
347
Michael Kolb2814a362011-05-19 15:49:41 -0700348
Michael Kolbf2055602011-04-09 17:20:03 -0700349}