blob: a84198969b78648ec7c1dc2ab1a638aedf477e69 [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 Kolb9829b432011-06-04 13:29:00 -070034import android.widget.LinearLayout;
Michael Kolbf2055602011-04-09 17:20:03 -070035import android.widget.ListPopupWindow;
Michael Kolb2814a362011-05-19 15:49:41 -070036import android.widget.RelativeLayout;
Michael Kolbf2055602011-04-09 17:20:03 -070037import android.widget.TextView;
38
Michael Kolb9829b432011-06-04 13:29:00 -070039import com.android.browser.view.Gallery.OnItemSelectedListener;
40
Michael Kolbf2055602011-04-09 17:20:03 -070041import java.util.ArrayList;
42import java.util.List;
43
Michael Kolb2814a362011-05-19 15:49:41 -070044public class NavScreen extends RelativeLayout implements OnClickListener {
Michael Kolbf2055602011-04-09 17:20:03 -070045
46 UiController mUiController;
47 PhoneUi mUi;
48 Tab mTab;
49 Activity mActivity;
50
Michael Kolbf2055602011-04-09 17:20:03 -070051 ImageButton mRefresh;
52 ImageButton mForward;
Michael Kolbf2055602011-04-09 17:20:03 -070053 ImageButton mBookmarks;
54 ImageButton mMore;
55 ImageButton mNewTab;
56 ImageButton mNewIncognito;
57 FrameLayout mHolder;
58
Michael Kolb2814a362011-05-19 15:49:41 -070059 TextView mTitle;
60 ImageView mFavicon;
61 ImageButton mCloseTab;
62
Michael Kolb9829b432011-06-04 13:29:00 -070063 NavTabGallery mScroller;
Michael Kolba4261fd2011-05-05 11:27:37 -070064 float mTabAspect = 0.66f;
Michael Kolbf2055602011-04-09 17:20:03 -070065 int mTabWidth;
66 int mTabHeight;
67 TabAdapter mAdapter;
68 ListPopupWindow mPopup;
Michael Kolba4261fd2011-05-05 11:27:37 -070069 int mOrientation;
Michael Kolbf2055602011-04-09 17:20:03 -070070
71 public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
72 super(activity);
73 mActivity = activity;
74 mUiController = ctl;
75 mUi = ui;
Michael Kolba4261fd2011-05-05 11:27:37 -070076 mOrientation = activity.getResources().getConfiguration().orientation;
Michael Kolbf2055602011-04-09 17:20:03 -070077 init();
78 }
79
80 protected Tab getSelectedTab() {
Michael Kolb2814a362011-05-19 15:49:41 -070081 return (Tab) mScroller.getSelectedItem();
Michael Kolbf2055602011-04-09 17:20:03 -070082 }
83
Michael Kolbfedb4922011-04-20 16:45:33 -070084 protected void showMenu() {
85 Menu menu = mUi.getMenu();
86 menu.setGroupVisible(R.id.NAV_MENU, false);
87
Michael Kolbf2055602011-04-09 17:20:03 -070088 MenuAdapter menuAdapter = new MenuAdapter(mContext);
89 menuAdapter.setMenu(menu);
90 ListPopupWindow popup = new ListPopupWindow(mContext);
91 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
92 popup.setAdapter(menuAdapter);
93 popup.setModal(true);
94 popup.setAnchorView(mMore);
95 popup.setWidth((int) mContext.getResources().getDimension(
96 R.dimen.menu_width));
97 popup.show();
98 mPopup = popup;
99 }
100
John Reckadc921f2011-04-27 10:11:03 -0700101 protected float getToolbarHeight() {
102 return mActivity.getResources().getDimension(R.dimen.toolbar_height);
103 }
104
Michael Kolbf2055602011-04-09 17:20:03 -0700105 protected void dismissMenu() {
106 if (mPopup != null) {
107 mPopup.dismiss();
108 }
109 }
110
Michael Kolba4261fd2011-05-05 11:27:37 -0700111 // for configuration changes
112 @Override
113 protected void onConfigurationChanged(Configuration newconfig) {
114 if (newconfig.orientation != mOrientation) {
Michael Kolb2814a362011-05-19 15:49:41 -0700115 int selIx = mScroller.getSelectionIndex();
Michael Kolba4261fd2011-05-05 11:27:37 -0700116 removeAllViews();
Michael Kolb9829b432011-06-04 13:29:00 -0700117 mOrientation = newconfig.orientation;
Michael Kolba4261fd2011-05-05 11:27:37 -0700118 init();
Michael Kolb2814a362011-05-19 15:49:41 -0700119 mScroller.setSelection(selIx);
Michael Kolb2814a362011-05-19 15:49:41 -0700120 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700121 }
122 }
123
Michael Kolbf2055602011-04-09 17:20:03 -0700124 private void init() {
125 LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700126 mBookmarks = (ImageButton) findViewById(R.id.bookmarks);
127 mNewTab = (ImageButton) findViewById(R.id.newtab);
128 mNewIncognito = (ImageButton) findViewById(R.id.newincognito);
129 mMore = (ImageButton) findViewById(R.id.more);
Michael Kolbf2055602011-04-09 17:20:03 -0700130 mBookmarks.setOnClickListener(this);
131 mNewTab.setOnClickListener(this);
132 mNewIncognito.setOnClickListener(this);
133 mMore.setOnClickListener(this);
Michael Kolb9829b432011-06-04 13:29:00 -0700134 mScroller = (NavTabGallery) findViewById(R.id.scroller);
Michael Kolbf2055602011-04-09 17:20:03 -0700135 mAdapter = new TabAdapter(mContext, mUiController.getTabControl());
Michael Kolb2814a362011-05-19 15:49:41 -0700136 mScroller.setAdapter(mAdapter);
Michael Kolb9829b432011-06-04 13:29:00 -0700137 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
138 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700139 // update state for active tab
140 mScroller.setSelection(mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbf2055602011-04-09 17:20:03 -0700141 }
142
143 @Override
144 public void onClick(View v) {
145 WebView web = (mTab != null) ? mTab.getWebView() : null;
146 if (web != null) {
Michael Kolb2814a362011-05-19 15:49:41 -0700147 if (mForward == v) {
Michael Kolbf2055602011-04-09 17:20:03 -0700148 mUi.hideNavScreen(true);
Michael Kolbf2055602011-04-09 17:20:03 -0700149 web.goForward();
150 } else if (mRefresh == v) {
151 mUi.hideNavScreen(true);
152 web.reload();
153 }
154 }
155 if (mBookmarks == v) {
156 mUi.hideNavScreen(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700157 switchToSelected();
Michael Kolbf2055602011-04-09 17:20:03 -0700158 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700159 } else if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700160 openNewTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700161 } else if (mMore == v) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700162 showMenu();
Michael Kolbf2055602011-04-09 17:20:03 -0700163 } else if (mNewIncognito == v) {
164 mUi.hideNavScreen(true);
Michael Kolb519d2282011-05-09 17:03:19 -0700165 mUiController.openIncognitoTab();
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 Kolb66af8162011-06-09 11:33:34 -0700176 switchToSelected();
177 mUiController.closeCurrentTab();
178 mAdapter.notifyDataSetChanged();
Michael Kolb2814a362011-05-19 15:49:41 -0700179 }
180 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700181
Michael Kolb2814a362011-05-19 15:49:41 -0700182 private void openNewTab() {
183 // need to call openTab explicitely with setactive false
184 Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
185 false, false, false);
186 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700187 if (tab != null) {
188 // set tab as the selected in flipper, then hide
Michael Kolbc831b632011-05-11 09:30:34 -0700189 final int tix = mUi.mTabControl.getTabPosition(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700190 mScroller.setSelection(tix);
191 postDelayed(new Runnable() {
192 @Override
Michael Kolba4261fd2011-05-05 11:27:37 -0700193 public void run() {
Michael Kolba4261fd2011-05-05 11:27:37 -0700194 mUi.hideNavScreen(true);
195 switchToSelected();
196 }
Michael Kolb2814a362011-05-19 15:49:41 -0700197 }, 100);
Michael Kolba4261fd2011-05-05 11:27:37 -0700198 }
199 }
200
201 private void switchToSelected() {
Michael Kolb2814a362011-05-19 15:49:41 -0700202 Tab tab = (Tab) mScroller.getSelectedItem();
Michael Kolba4261fd2011-05-05 11:27:37 -0700203 if (tab != mUi.getActiveTab()) {
204 mUiController.setActiveTab(tab);
205 }
206 }
207
Michael Kolbf2055602011-04-09 17:20:03 -0700208 protected void close() {
209 close(true);
210 }
211
212 protected void close(boolean animate) {
213 mUi.hideNavScreen(animate);
Michael Kolbf2055602011-04-09 17:20:03 -0700214 }
215
216 class TabGallery extends Gallery {
217
218 public TabGallery(Context ctx) {
219 super(ctx);
220 setUnselectedAlpha(0.3f);
221 }
222
223 @Override
224 protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
Michael Kolb2814a362011-05-19 15:49:41 -0700225 return new Gallery.LayoutParams(mTabWidth, mTabHeight);
Michael Kolbf2055602011-04-09 17:20:03 -0700226 }
227
228 @Override
229 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
230 return generateDefaultLayoutParams();
231 }
232
233 }
234
235 class TabAdapter extends BaseAdapter {
236
237 Context context;
238 TabControl tabControl;
239
240 public TabAdapter(Context ctx, TabControl tc) {
241 context = ctx;
242 tabControl = tc;
243 }
244
Michael Kolbf2055602011-04-09 17:20:03 -0700245 @Override
246 public int getCount() {
247 return tabControl.getTabCount();
248 }
249
250 @Override
251 public Tab getItem(int position) {
252 return tabControl.getTab(position);
253 }
254
255 public long getItemId(int position) {
256 return position;
257 }
258
259 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700260 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700261 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700262 final Tab tab = getItem(position);
Michael Kolb2814a362011-05-19 15:49:41 -0700263 final BrowserWebView web = (BrowserWebView) tab.getWebView();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700264 tabview.setWebView(mUi, tab);
265 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700266 @Override
267 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700268 if (tabview.isClose(v)) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700269 onCloseTab((Tab) (mScroller.getSelectedItem()));
270 } else if (tabview.isTitle(v)) {
271 mUi.getTitleBar().setSkipTitleBarAnimations(true);
272 close(false);
273 mUi.editUrl(false);
274 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700275 } else if (tabview.isWebView(v)) {
276 mScroller.setSelection(position);
277 close();
Michael Kolb2814a362011-05-19 15:49:41 -0700278
Michael Kolb4bd767d2011-05-27 11:33:55 -0700279 }
280 }
281 });
282 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700283 }
Michael Kolb2814a362011-05-19 15:49:41 -0700284
Michael Kolbf2055602011-04-09 17:20:03 -0700285 }
286
287 private class MenuAdapter extends BaseAdapter implements OnClickListener {
288
289 List<MenuItem> mItems;
290 LayoutInflater mInflater;
291
292 public MenuAdapter(Context ctx) {
293 mInflater = LayoutInflater.from(ctx);
294 mItems = new ArrayList<MenuItem>();
295 }
296
297 public void setMenu(Menu menu) {
298 mItems.clear();
299 for (int i = 0; i < menu.size(); i++) {
300 MenuItem item = menu.getItem(i);
301 if (item.isEnabled() && item.isVisible()) {
302 mItems.add(item);
303 }
304 }
305 notifyDataSetChanged();
306 }
307
308 @Override
309 public int getCount() {
310 return mItems.size();
311 }
312
313 @Override
314 public MenuItem getItem(int position) {
315 return mItems.get(position);
316 }
317
318 @Override
319 public long getItemId(int position) {
320 return position;
321 }
322
323 @Override
324 public void onClick(View v) {
325 if (v.getTag() != null) {
326 dismissMenu();
327 mActivity.closeOptionsMenu();
328 mUi.hideNavScreen(false);
329 mUiController.onOptionsItemSelected((MenuItem) v.getTag());
330 }
331 }
332
333 @Override
334 public View getView(int position, View convertView, ViewGroup parent) {
335 final MenuItem item = mItems.get(position);
336 View view = mInflater.inflate(R.layout.qc_menu_item, null);
337 TextView label = (TextView) view.findViewById(R.id.title);
338 label.setText(item.getTitle());
339 label.setTag(item);
340 label.setOnClickListener(this);
341 return label;
342 }
343
344 }
345
Michael Kolb2814a362011-05-19 15:49:41 -0700346
Michael Kolbf2055602011-04-09 17:20:03 -0700347}