blob: 43a38e0bafc2b986b072fdd2bc16a738e672cbdc [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 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 Kolb017ffab2011-07-11 15:26:47 -070040public class NavScreen extends RelativeLayout
41 implements OnClickListener, OnMenuItemClickListener {
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
Michael Kolb9829b432011-06-04 13:29:00 -070060 NavTabGallery 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;
Michael Kolba4261fd2011-05-05 11:27:37 -070065 int mOrientation;
Michael Kolbf2055602011-04-09 17:20:03 -070066
67 public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
68 super(activity);
69 mActivity = activity;
70 mUiController = ctl;
71 mUi = ui;
Michael Kolba4261fd2011-05-05 11:27:37 -070072 mOrientation = activity.getResources().getConfiguration().orientation;
Michael Kolbf2055602011-04-09 17:20:03 -070073 init();
74 }
75
76 protected Tab getSelectedTab() {
Michael Kolb2814a362011-05-19 15:49:41 -070077 return (Tab) mScroller.getSelectedItem();
Michael Kolbf2055602011-04-09 17:20:03 -070078 }
79
Michael Kolbfedb4922011-04-20 16:45:33 -070080 protected void showMenu() {
Michael Kolb017ffab2011-07-11 15:26:47 -070081 PopupMenu popup = new PopupMenu(mContext, mMore);
82 Menu menu = popup.getMenu();
83 popup.getMenuInflater().inflate(R.menu.browser, menu);
Michael Kolbfedb4922011-04-20 16:45:33 -070084 menu.setGroupVisible(R.id.NAV_MENU, false);
Michael Kolb017ffab2011-07-11 15:26:47 -070085 popup.setOnMenuItemClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -070086 popup.show();
Michael Kolb017ffab2011-07-11 15:26:47 -070087 }
88
89 @Override
90 public boolean onMenuItemClick(MenuItem item) {
91 return mUiController.onOptionsItemSelected(item);
Michael Kolbf2055602011-04-09 17:20:03 -070092 }
93
John Reckadc921f2011-04-27 10:11:03 -070094 protected float getToolbarHeight() {
95 return mActivity.getResources().getDimension(R.dimen.toolbar_height);
96 }
97
Michael Kolba4261fd2011-05-05 11:27:37 -070098 // for configuration changes
99 @Override
100 protected void onConfigurationChanged(Configuration newconfig) {
101 if (newconfig.orientation != mOrientation) {
Michael Kolb2814a362011-05-19 15:49:41 -0700102 int selIx = mScroller.getSelectionIndex();
Michael Kolba4261fd2011-05-05 11:27:37 -0700103 removeAllViews();
Michael Kolb9829b432011-06-04 13:29:00 -0700104 mOrientation = newconfig.orientation;
Michael Kolba4261fd2011-05-05 11:27:37 -0700105 init();
Michael Kolb2814a362011-05-19 15:49:41 -0700106 mScroller.setSelection(selIx);
Michael Kolb2814a362011-05-19 15:49:41 -0700107 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700108 }
109 }
110
Michael Kolbf2055602011-04-09 17:20:03 -0700111 private void init() {
112 LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700113 mBookmarks = (ImageButton) findViewById(R.id.bookmarks);
114 mNewTab = (ImageButton) findViewById(R.id.newtab);
115 mNewIncognito = (ImageButton) findViewById(R.id.newincognito);
116 mMore = (ImageButton) findViewById(R.id.more);
Michael Kolbf2055602011-04-09 17:20:03 -0700117 mBookmarks.setOnClickListener(this);
118 mNewTab.setOnClickListener(this);
119 mNewIncognito.setOnClickListener(this);
120 mMore.setOnClickListener(this);
Michael Kolb9829b432011-06-04 13:29:00 -0700121 mScroller = (NavTabGallery) findViewById(R.id.scroller);
Michael Kolbf2055602011-04-09 17:20:03 -0700122 mAdapter = new TabAdapter(mContext, mUiController.getTabControl());
Michael Kolb2814a362011-05-19 15:49:41 -0700123 mScroller.setAdapter(mAdapter);
Michael Kolb9829b432011-06-04 13:29:00 -0700124 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
125 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700126 // update state for active tab
127 mScroller.setSelection(mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbf2055602011-04-09 17:20:03 -0700128 }
129
130 @Override
131 public void onClick(View v) {
132 WebView web = (mTab != null) ? mTab.getWebView() : null;
133 if (web != null) {
Michael Kolb2814a362011-05-19 15:49:41 -0700134 if (mForward == v) {
Michael Kolbf2055602011-04-09 17:20:03 -0700135 mUi.hideNavScreen(true);
Michael Kolbf2055602011-04-09 17:20:03 -0700136 web.goForward();
137 } else if (mRefresh == v) {
138 mUi.hideNavScreen(true);
139 web.reload();
140 }
141 }
142 if (mBookmarks == v) {
143 mUi.hideNavScreen(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700144 switchToSelected();
Michael Kolbf2055602011-04-09 17:20:03 -0700145 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700146 } else if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700147 openNewTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700148 } else if (mMore == v) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700149 showMenu();
Michael Kolbf2055602011-04-09 17:20:03 -0700150 } else if (mNewIncognito == v) {
151 mUi.hideNavScreen(true);
Michael Kolb519d2282011-05-09 17:03:19 -0700152 mUiController.openIncognitoTab();
Michael Kolb2814a362011-05-19 15:49:41 -0700153 } else if (mTitle == v) {
154 mUi.getTitleBar().setSkipTitleBarAnimations(true);
155 close(false);
156 mUi.editUrl(false);
157 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700158 }
159 }
160
Michael Kolb2814a362011-05-19 15:49:41 -0700161 private void onCloseTab(Tab tab) {
162 if (tab != null) {
Michael Kolb66af8162011-06-09 11:33:34 -0700163 switchToSelected();
164 mUiController.closeCurrentTab();
165 mAdapter.notifyDataSetChanged();
Michael Kolb2814a362011-05-19 15:49:41 -0700166 }
167 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700168
Michael Kolb2814a362011-05-19 15:49:41 -0700169 private void openNewTab() {
170 // need to call openTab explicitely with setactive false
171 Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
172 false, false, false);
173 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700174 if (tab != null) {
175 // set tab as the selected in flipper, then hide
Michael Kolbc831b632011-05-11 09:30:34 -0700176 final int tix = mUi.mTabControl.getTabPosition(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700177 mScroller.setSelection(tix);
178 postDelayed(new Runnable() {
179 @Override
Michael Kolba4261fd2011-05-05 11:27:37 -0700180 public void run() {
Michael Kolba4261fd2011-05-05 11:27:37 -0700181 mUi.hideNavScreen(true);
182 switchToSelected();
183 }
Michael Kolb2814a362011-05-19 15:49:41 -0700184 }, 100);
Michael Kolba4261fd2011-05-05 11:27:37 -0700185 }
186 }
187
188 private void switchToSelected() {
Michael Kolb2814a362011-05-19 15:49:41 -0700189 Tab tab = (Tab) mScroller.getSelectedItem();
Michael Kolba4261fd2011-05-05 11:27:37 -0700190 if (tab != mUi.getActiveTab()) {
191 mUiController.setActiveTab(tab);
192 }
193 }
194
Michael Kolbf2055602011-04-09 17:20:03 -0700195 protected void close() {
196 close(true);
197 }
198
199 protected void close(boolean animate) {
200 mUi.hideNavScreen(animate);
Michael Kolbf2055602011-04-09 17:20:03 -0700201 }
202
203 class TabGallery extends Gallery {
204
205 public TabGallery(Context ctx) {
206 super(ctx);
207 setUnselectedAlpha(0.3f);
208 }
209
210 @Override
211 protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
Michael Kolb2814a362011-05-19 15:49:41 -0700212 return new Gallery.LayoutParams(mTabWidth, mTabHeight);
Michael Kolbf2055602011-04-09 17:20:03 -0700213 }
214
215 @Override
216 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
217 return generateDefaultLayoutParams();
218 }
219
220 }
221
222 class TabAdapter extends BaseAdapter {
223
224 Context context;
225 TabControl tabControl;
226
227 public TabAdapter(Context ctx, TabControl tc) {
228 context = ctx;
229 tabControl = tc;
230 }
231
Michael Kolbf2055602011-04-09 17:20:03 -0700232 @Override
233 public int getCount() {
234 return tabControl.getTabCount();
235 }
236
237 @Override
238 public Tab getItem(int position) {
239 return tabControl.getTab(position);
240 }
241
242 public long getItemId(int position) {
243 return position;
244 }
245
246 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700247 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700248 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700249 final Tab tab = getItem(position);
Michael Kolb2814a362011-05-19 15:49:41 -0700250 final BrowserWebView web = (BrowserWebView) tab.getWebView();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700251 tabview.setWebView(mUi, tab);
252 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700253 @Override
254 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700255 if (tabview.isClose(v)) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700256 onCloseTab((Tab) (mScroller.getSelectedItem()));
257 } else if (tabview.isTitle(v)) {
258 mUi.getTitleBar().setSkipTitleBarAnimations(true);
259 close(false);
260 mUi.editUrl(false);
261 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700262 } else if (tabview.isWebView(v)) {
263 mScroller.setSelection(position);
264 close();
Michael Kolb2814a362011-05-19 15:49:41 -0700265
Michael Kolb4bd767d2011-05-27 11:33:55 -0700266 }
267 }
268 });
269 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700270 }
Michael Kolb2814a362011-05-19 15:49:41 -0700271
Michael Kolbf2055602011-04-09 17:20:03 -0700272 }
273
Michael Kolbf2055602011-04-09 17:20:03 -0700274}