blob: 9c12ccfe9a24b042898bd859885b98fa47c09461 [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;
Michael Kolbdcd81d32011-07-22 10:30:49 -070027import android.view.ViewConfiguration;
Michael Kolbf2055602011-04-09 17:20:03 -070028import 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 Kolbdcd81d32011-07-22 10:30:49 -070040import com.android.browser.NavTabGallery.OnRemoveListener;
41
Michael Kolb017ffab2011-07-11 15:26:47 -070042public class NavScreen extends RelativeLayout
43 implements OnClickListener, OnMenuItemClickListener {
Michael Kolbf2055602011-04-09 17:20:03 -070044
45 UiController mUiController;
46 PhoneUi mUi;
47 Tab mTab;
48 Activity mActivity;
49
Michael Kolbf2055602011-04-09 17:20:03 -070050 ImageButton mRefresh;
51 ImageButton mForward;
Michael Kolbf2055602011-04-09 17:20:03 -070052 ImageButton mBookmarks;
53 ImageButton mMore;
54 ImageButton mNewTab;
55 ImageButton mNewIncognito;
56 FrameLayout mHolder;
57
Michael Kolb2814a362011-05-19 15:49:41 -070058 TextView mTitle;
59 ImageView mFavicon;
60 ImageButton mCloseTab;
61
Michael Kolb9829b432011-06-04 13:29:00 -070062 NavTabGallery mScroller;
Michael Kolbf2055602011-04-09 17:20:03 -070063 TabAdapter mAdapter;
Michael Kolba4261fd2011-05-05 11:27:37 -070064 int mOrientation;
Michael Kolb3ca12752011-07-20 13:52:25 -070065 boolean mNeedsMenu;
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 Kolb4bf79712011-07-14 14:18:12 -070084 mUiController.updateMenuState(mScroller.getSelectedItem(), menu);
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) {
Michael Kolbadb0e602011-07-18 14:33:46 -070091 mUi.hideNavScreen(false);
Michael Kolb017ffab2011-07-11 15:26:47 -070092 return mUiController.onOptionsItemSelected(item);
Michael Kolbf2055602011-04-09 17:20:03 -070093 }
94
John Reckadc921f2011-04-27 10:11:03 -070095 protected float getToolbarHeight() {
96 return mActivity.getResources().getDimension(R.dimen.toolbar_height);
97 }
98
Michael Kolba4261fd2011-05-05 11:27:37 -070099 // for configuration changes
100 @Override
101 protected void onConfigurationChanged(Configuration newconfig) {
102 if (newconfig.orientation != mOrientation) {
Michael Kolb2814a362011-05-19 15:49:41 -0700103 int selIx = mScroller.getSelectionIndex();
Michael Kolba4261fd2011-05-05 11:27:37 -0700104 removeAllViews();
Michael Kolb9829b432011-06-04 13:29:00 -0700105 mOrientation = newconfig.orientation;
Michael Kolba4261fd2011-05-05 11:27:37 -0700106 init();
Michael Kolb2814a362011-05-19 15:49:41 -0700107 mScroller.setSelection(selIx);
Michael Kolb2814a362011-05-19 15:49:41 -0700108 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700109 }
110 }
111
Michael Kolbf2055602011-04-09 17:20:03 -0700112 private void init() {
113 LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this);
Michael Kolb30adae62011-07-29 13:37:05 -0700114 setContentDescription(mContext.getResources().getString(
115 R.string.accessibility_transition_navscreen));
Michael Kolbf2055602011-04-09 17:20:03 -0700116 mBookmarks = (ImageButton) findViewById(R.id.bookmarks);
117 mNewTab = (ImageButton) findViewById(R.id.newtab);
118 mNewIncognito = (ImageButton) findViewById(R.id.newincognito);
119 mMore = (ImageButton) findViewById(R.id.more);
Michael Kolbf2055602011-04-09 17:20:03 -0700120 mBookmarks.setOnClickListener(this);
121 mNewTab.setOnClickListener(this);
122 mNewIncognito.setOnClickListener(this);
123 mMore.setOnClickListener(this);
Michael Kolb9829b432011-06-04 13:29:00 -0700124 mScroller = (NavTabGallery) findViewById(R.id.scroller);
Michael Kolbf2055602011-04-09 17:20:03 -0700125 mAdapter = new TabAdapter(mContext, mUiController.getTabControl());
Michael Kolb2814a362011-05-19 15:49:41 -0700126 mScroller.setAdapter(mAdapter);
Michael Kolb9829b432011-06-04 13:29:00 -0700127 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
128 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700129 // update state for active tab
130 mScroller.setSelection(mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbdcd81d32011-07-22 10:30:49 -0700131 mScroller.setOnRemoveListener(new OnRemoveListener() {
132 public void onRemovePosition(int pos) {
133 Tab tab = mAdapter.getItem(pos);
134 onCloseTab(tab);
135 }
136 });
Michael Kolb3ca12752011-07-20 13:52:25 -0700137 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
138 if (!mNeedsMenu) {
139 mMore.setVisibility(View.GONE);
140 }
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);
John Reckef654f12011-07-12 16:42:08 -0700149 mTab.goForward();
Michael Kolbf2055602011-04-09 17:20:03 -0700150 } 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 Kolb308b3012011-07-13 14:50:56 -0700176 if (tab == mUiController.getCurrentTab()) {
177 mUiController.closeCurrentTab();
178 } else {
179 mUiController.closeTab(tab);
180 }
Michael Kolb66af8162011-06-09 11:33:34 -0700181 mAdapter.notifyDataSetChanged();
Michael Kolb2814a362011-05-19 15:49:41 -0700182 }
183 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700184
Michael Kolb2814a362011-05-19 15:49:41 -0700185 private void openNewTab() {
186 // need to call openTab explicitely with setactive false
187 Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
188 false, false, false);
189 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700190 if (tab != null) {
191 // set tab as the selected in flipper, then hide
Michael Kolbc831b632011-05-11 09:30:34 -0700192 final int tix = mUi.mTabControl.getTabPosition(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700193 mScroller.setSelection(tix);
194 postDelayed(new Runnable() {
195 @Override
Michael Kolba4261fd2011-05-05 11:27:37 -0700196 public void run() {
Michael Kolba4261fd2011-05-05 11:27:37 -0700197 mUi.hideNavScreen(true);
198 switchToSelected();
199 }
Michael Kolb2814a362011-05-19 15:49:41 -0700200 }, 100);
Michael Kolba4261fd2011-05-05 11:27:37 -0700201 }
202 }
203
204 private void switchToSelected() {
Michael Kolb2814a362011-05-19 15:49:41 -0700205 Tab tab = (Tab) mScroller.getSelectedItem();
Michael Kolba4261fd2011-05-05 11:27:37 -0700206 if (tab != mUi.getActiveTab()) {
207 mUiController.setActiveTab(tab);
208 }
209 }
210
Michael Kolbf2055602011-04-09 17:20:03 -0700211 protected void close() {
212 close(true);
213 }
214
215 protected void close(boolean animate) {
216 mUi.hideNavScreen(animate);
Michael Kolbf2055602011-04-09 17:20:03 -0700217 }
218
Michael Kolbf2055602011-04-09 17:20:03 -0700219 class TabAdapter extends BaseAdapter {
220
221 Context context;
222 TabControl tabControl;
223
224 public TabAdapter(Context ctx, TabControl tc) {
225 context = ctx;
226 tabControl = tc;
227 }
228
Michael Kolbf2055602011-04-09 17:20:03 -0700229 @Override
230 public int getCount() {
231 return tabControl.getTabCount();
232 }
233
234 @Override
235 public Tab getItem(int position) {
236 return tabControl.getTab(position);
237 }
238
239 public long getItemId(int position) {
240 return position;
241 }
242
243 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700244 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700245 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700246 final Tab tab = getItem(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700247 tabview.setWebView(mUi, tab);
248 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700249 @Override
250 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700251 if (tabview.isClose(v)) {
Michael Kolbe76f7042011-07-27 15:16:32 -0700252 mScroller.animateOut(tabview);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700253 } else if (tabview.isTitle(v)) {
Michael Kolb308b3012011-07-13 14:50:56 -0700254 mScroller.setSelection(position);
255 switchToSelected();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700256 mUi.getTitleBar().setSkipTitleBarAnimations(true);
257 close(false);
258 mUi.editUrl(false);
259 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700260 } else if (tabview.isWebView(v)) {
261 mScroller.setSelection(position);
262 close();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700263 }
264 }
265 });
266 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700267 }
Michael Kolb2814a362011-05-19 15:49:41 -0700268
Michael Kolbf2055602011-04-09 17:20:03 -0700269 }
270
Michael Kolbf2055602011-04-09 17:20:03 -0700271}