blob: ee205350779324bdcc4dee7fcdd5a72e04894adb [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;
Michael Kolb3ca12752011-07-20 13:52:25 -070026import android.view.ViewConfiguration;
Michael Kolbf2055602011-04-09 17:20:03 -070027import android.view.View.OnClickListener;
28import 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 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 Kolbf2055602011-04-09 17:20:03 -070061 TabAdapter mAdapter;
Michael Kolba4261fd2011-05-05 11:27:37 -070062 int mOrientation;
Michael Kolb3ca12752011-07-20 13:52:25 -070063 boolean mNeedsMenu;
Michael Kolbf2055602011-04-09 17:20:03 -070064
65 public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
66 super(activity);
67 mActivity = activity;
68 mUiController = ctl;
69 mUi = ui;
Michael Kolba4261fd2011-05-05 11:27:37 -070070 mOrientation = activity.getResources().getConfiguration().orientation;
Michael Kolbf2055602011-04-09 17:20:03 -070071 init();
72 }
73
74 protected Tab getSelectedTab() {
Michael Kolb2814a362011-05-19 15:49:41 -070075 return (Tab) mScroller.getSelectedItem();
Michael Kolbf2055602011-04-09 17:20:03 -070076 }
77
Michael Kolbfedb4922011-04-20 16:45:33 -070078 protected void showMenu() {
Michael Kolb017ffab2011-07-11 15:26:47 -070079 PopupMenu popup = new PopupMenu(mContext, mMore);
80 Menu menu = popup.getMenu();
81 popup.getMenuInflater().inflate(R.menu.browser, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -070082 mUiController.updateMenuState(mScroller.getSelectedItem(), menu);
Michael Kolb017ffab2011-07-11 15:26:47 -070083 popup.setOnMenuItemClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -070084 popup.show();
Michael Kolb017ffab2011-07-11 15:26:47 -070085 }
86
87 @Override
88 public boolean onMenuItemClick(MenuItem item) {
Michael Kolbadb0e602011-07-18 14:33:46 -070089 mUi.hideNavScreen(false);
Michael Kolb017ffab2011-07-11 15:26:47 -070090 return mUiController.onOptionsItemSelected(item);
Michael Kolbf2055602011-04-09 17:20:03 -070091 }
92
John Reckadc921f2011-04-27 10:11:03 -070093 protected float getToolbarHeight() {
94 return mActivity.getResources().getDimension(R.dimen.toolbar_height);
95 }
96
Michael Kolba4261fd2011-05-05 11:27:37 -070097 // for configuration changes
98 @Override
99 protected void onConfigurationChanged(Configuration newconfig) {
100 if (newconfig.orientation != mOrientation) {
Michael Kolb2814a362011-05-19 15:49:41 -0700101 int selIx = mScroller.getSelectionIndex();
Michael Kolba4261fd2011-05-05 11:27:37 -0700102 removeAllViews();
Michael Kolb9829b432011-06-04 13:29:00 -0700103 mOrientation = newconfig.orientation;
Michael Kolba4261fd2011-05-05 11:27:37 -0700104 init();
Michael Kolb2814a362011-05-19 15:49:41 -0700105 mScroller.setSelection(selIx);
Michael Kolb2814a362011-05-19 15:49:41 -0700106 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700107 }
108 }
109
Michael Kolbf2055602011-04-09 17:20:03 -0700110 private void init() {
111 LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700112 mBookmarks = (ImageButton) findViewById(R.id.bookmarks);
113 mNewTab = (ImageButton) findViewById(R.id.newtab);
114 mNewIncognito = (ImageButton) findViewById(R.id.newincognito);
115 mMore = (ImageButton) findViewById(R.id.more);
Michael Kolbf2055602011-04-09 17:20:03 -0700116 mBookmarks.setOnClickListener(this);
117 mNewTab.setOnClickListener(this);
118 mNewIncognito.setOnClickListener(this);
119 mMore.setOnClickListener(this);
Michael Kolb9829b432011-06-04 13:29:00 -0700120 mScroller = (NavTabGallery) findViewById(R.id.scroller);
Michael Kolbf2055602011-04-09 17:20:03 -0700121 mAdapter = new TabAdapter(mContext, mUiController.getTabControl());
Michael Kolb2814a362011-05-19 15:49:41 -0700122 mScroller.setAdapter(mAdapter);
Michael Kolb9829b432011-06-04 13:29:00 -0700123 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
124 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700125 // update state for active tab
126 mScroller.setSelection(mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolb3ca12752011-07-20 13:52:25 -0700127 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
128 if (!mNeedsMenu) {
129 mMore.setVisibility(View.GONE);
130 }
Michael Kolbf2055602011-04-09 17:20:03 -0700131 }
132
133 @Override
134 public void onClick(View v) {
135 WebView web = (mTab != null) ? mTab.getWebView() : null;
136 if (web != null) {
Michael Kolb2814a362011-05-19 15:49:41 -0700137 if (mForward == v) {
Michael Kolbf2055602011-04-09 17:20:03 -0700138 mUi.hideNavScreen(true);
John Reckef654f12011-07-12 16:42:08 -0700139 mTab.goForward();
Michael Kolbf2055602011-04-09 17:20:03 -0700140 } else if (mRefresh == v) {
141 mUi.hideNavScreen(true);
142 web.reload();
143 }
144 }
145 if (mBookmarks == v) {
146 mUi.hideNavScreen(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700147 switchToSelected();
Michael Kolbf2055602011-04-09 17:20:03 -0700148 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700149 } else if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700150 openNewTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700151 } else if (mMore == v) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700152 showMenu();
Michael Kolbf2055602011-04-09 17:20:03 -0700153 } else if (mNewIncognito == v) {
154 mUi.hideNavScreen(true);
Michael Kolb519d2282011-05-09 17:03:19 -0700155 mUiController.openIncognitoTab();
Michael Kolb2814a362011-05-19 15:49:41 -0700156 } else if (mTitle == v) {
157 mUi.getTitleBar().setSkipTitleBarAnimations(true);
158 close(false);
159 mUi.editUrl(false);
160 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700161 }
162 }
163
Michael Kolb2814a362011-05-19 15:49:41 -0700164 private void onCloseTab(Tab tab) {
165 if (tab != null) {
Michael Kolb308b3012011-07-13 14:50:56 -0700166 if (tab == mUiController.getCurrentTab()) {
167 mUiController.closeCurrentTab();
168 } else {
169 mUiController.closeTab(tab);
170 }
Michael Kolb66af8162011-06-09 11:33:34 -0700171 mAdapter.notifyDataSetChanged();
Michael Kolb2814a362011-05-19 15:49:41 -0700172 }
173 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700174
Michael Kolb2814a362011-05-19 15:49:41 -0700175 private void openNewTab() {
176 // need to call openTab explicitely with setactive false
177 Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
178 false, false, false);
179 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700180 if (tab != null) {
181 // set tab as the selected in flipper, then hide
Michael Kolbc831b632011-05-11 09:30:34 -0700182 final int tix = mUi.mTabControl.getTabPosition(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700183 mScroller.setSelection(tix);
184 postDelayed(new Runnable() {
185 @Override
Michael Kolba4261fd2011-05-05 11:27:37 -0700186 public void run() {
Michael Kolba4261fd2011-05-05 11:27:37 -0700187 mUi.hideNavScreen(true);
188 switchToSelected();
189 }
Michael Kolb2814a362011-05-19 15:49:41 -0700190 }, 100);
Michael Kolba4261fd2011-05-05 11:27:37 -0700191 }
192 }
193
194 private void switchToSelected() {
Michael Kolb2814a362011-05-19 15:49:41 -0700195 Tab tab = (Tab) mScroller.getSelectedItem();
Michael Kolba4261fd2011-05-05 11:27:37 -0700196 if (tab != mUi.getActiveTab()) {
197 mUiController.setActiveTab(tab);
198 }
199 }
200
Michael Kolbf2055602011-04-09 17:20:03 -0700201 protected void close() {
202 close(true);
203 }
204
205 protected void close(boolean animate) {
206 mUi.hideNavScreen(animate);
Michael Kolbf2055602011-04-09 17:20:03 -0700207 }
208
Michael Kolbf2055602011-04-09 17:20:03 -0700209 class TabAdapter extends BaseAdapter {
210
211 Context context;
212 TabControl tabControl;
213
214 public TabAdapter(Context ctx, TabControl tc) {
215 context = ctx;
216 tabControl = tc;
217 }
218
Michael Kolbf2055602011-04-09 17:20:03 -0700219 @Override
220 public int getCount() {
221 return tabControl.getTabCount();
222 }
223
224 @Override
225 public Tab getItem(int position) {
226 return tabControl.getTab(position);
227 }
228
229 public long getItemId(int position) {
230 return position;
231 }
232
233 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700234 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700235 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700236 final Tab tab = getItem(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700237 tabview.setWebView(mUi, tab);
238 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700239 @Override
240 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700241 if (tabview.isClose(v)) {
Michael Kolb308b3012011-07-13 14:50:56 -0700242 onCloseTab(tab);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700243 } else if (tabview.isTitle(v)) {
Michael Kolb308b3012011-07-13 14:50:56 -0700244 mScroller.setSelection(position);
245 switchToSelected();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700246 mUi.getTitleBar().setSkipTitleBarAnimations(true);
247 close(false);
248 mUi.editUrl(false);
249 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700250 } else if (tabview.isWebView(v)) {
251 mScroller.setSelection(position);
252 close();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700253 }
254 }
255 });
256 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700257 }
Michael Kolb2814a362011-05-19 15:49:41 -0700258
Michael Kolbf2055602011-04-09 17:20:03 -0700259 }
260
Michael Kolbf2055602011-04-09 17:20:03 -0700261}