blob: 6ef759fee90f3983b290ad5d966c171331c953b1 [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) {
172 mUiController.closeTab(tab);
173 if (mUiController.getTabControl().getTabCount() == 0) {
174 openNewTab();
175 } else {
176 mAdapter.notifyDataSetChanged();
177 }
178 }
179 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700180
Michael Kolb2814a362011-05-19 15:49:41 -0700181
182 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 Kolb4bd767d2011-05-27 11:33:55 -0700268 if (tabview.isRefresh(v)) {
269 mUi.hideNavScreen(true);
270 web.reload();
271 } else if (tabview.isClose(v)) {
272 onCloseTab((Tab) (mScroller.getSelectedItem()));
273 } else if (tabview.isTitle(v)) {
274 mUi.getTitleBar().setSkipTitleBarAnimations(true);
275 close(false);
276 mUi.editUrl(false);
277 mUi.getTitleBar().setSkipTitleBarAnimations(false);
278 } else if (tabview.isForward(v)) {
279 mUi.hideNavScreen(true);
280 web.goForward();
281 } else if (tabview.isWebView(v)) {
282 mScroller.setSelection(position);
283 close();
Michael Kolb2814a362011-05-19 15:49:41 -0700284
Michael Kolb4bd767d2011-05-27 11:33:55 -0700285 }
286 }
287 });
288 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700289 }
Michael Kolb2814a362011-05-19 15:49:41 -0700290
Michael Kolbf2055602011-04-09 17:20:03 -0700291 }
292
293 private class MenuAdapter extends BaseAdapter implements OnClickListener {
294
295 List<MenuItem> mItems;
296 LayoutInflater mInflater;
297
298 public MenuAdapter(Context ctx) {
299 mInflater = LayoutInflater.from(ctx);
300 mItems = new ArrayList<MenuItem>();
301 }
302
303 public void setMenu(Menu menu) {
304 mItems.clear();
305 for (int i = 0; i < menu.size(); i++) {
306 MenuItem item = menu.getItem(i);
307 if (item.isEnabled() && item.isVisible()) {
308 mItems.add(item);
309 }
310 }
311 notifyDataSetChanged();
312 }
313
314 @Override
315 public int getCount() {
316 return mItems.size();
317 }
318
319 @Override
320 public MenuItem getItem(int position) {
321 return mItems.get(position);
322 }
323
324 @Override
325 public long getItemId(int position) {
326 return position;
327 }
328
329 @Override
330 public void onClick(View v) {
331 if (v.getTag() != null) {
332 dismissMenu();
333 mActivity.closeOptionsMenu();
334 mUi.hideNavScreen(false);
335 mUiController.onOptionsItemSelected((MenuItem) v.getTag());
336 }
337 }
338
339 @Override
340 public View getView(int position, View convertView, ViewGroup parent) {
341 final MenuItem item = mItems.get(position);
342 View view = mInflater.inflate(R.layout.qc_menu_item, null);
343 TextView label = (TextView) view.findViewById(R.id.title);
344 label.setText(item.getTitle());
345 label.setTag(item);
346 label.setOnClickListener(this);
347 return label;
348 }
349
350 }
351
Michael Kolb2814a362011-05-19 15:49:41 -0700352
Michael Kolbf2055602011-04-09 17:20:03 -0700353}