blob: a361136bc7f7285fb8aac44ef9a54913c02be883 [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 Kolbf2055602011-04-09 17:20:03 -0700114 mBookmarks = (ImageButton) findViewById(R.id.bookmarks);
115 mNewTab = (ImageButton) findViewById(R.id.newtab);
116 mNewIncognito = (ImageButton) findViewById(R.id.newincognito);
117 mMore = (ImageButton) findViewById(R.id.more);
Michael Kolbf2055602011-04-09 17:20:03 -0700118 mBookmarks.setOnClickListener(this);
119 mNewTab.setOnClickListener(this);
120 mNewIncognito.setOnClickListener(this);
121 mMore.setOnClickListener(this);
Michael Kolb9829b432011-06-04 13:29:00 -0700122 mScroller = (NavTabGallery) findViewById(R.id.scroller);
Michael Kolbf2055602011-04-09 17:20:03 -0700123 mAdapter = new TabAdapter(mContext, mUiController.getTabControl());
Michael Kolb2814a362011-05-19 15:49:41 -0700124 mScroller.setAdapter(mAdapter);
Michael Kolb9829b432011-06-04 13:29:00 -0700125 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
126 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700127 // update state for active tab
128 mScroller.setSelection(mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbdcd81d32011-07-22 10:30:49 -0700129 mScroller.setOnRemoveListener(new OnRemoveListener() {
130 public void onRemovePosition(int pos) {
131 Tab tab = mAdapter.getItem(pos);
132 onCloseTab(tab);
133 }
134 });
Michael Kolb3ca12752011-07-20 13:52:25 -0700135 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
136 if (!mNeedsMenu) {
137 mMore.setVisibility(View.GONE);
138 }
Michael Kolbf2055602011-04-09 17:20:03 -0700139 }
140
141 @Override
142 public void onClick(View v) {
143 WebView web = (mTab != null) ? mTab.getWebView() : null;
144 if (web != null) {
Michael Kolb2814a362011-05-19 15:49:41 -0700145 if (mForward == v) {
Michael Kolbf2055602011-04-09 17:20:03 -0700146 mUi.hideNavScreen(true);
John Reckef654f12011-07-12 16:42:08 -0700147 mTab.goForward();
Michael Kolbf2055602011-04-09 17:20:03 -0700148 } else if (mRefresh == v) {
149 mUi.hideNavScreen(true);
150 web.reload();
151 }
152 }
153 if (mBookmarks == v) {
154 mUi.hideNavScreen(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700155 switchToSelected();
Michael Kolbf2055602011-04-09 17:20:03 -0700156 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700157 } else if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700158 openNewTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700159 } else if (mMore == v) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700160 showMenu();
Michael Kolbf2055602011-04-09 17:20:03 -0700161 } else if (mNewIncognito == v) {
162 mUi.hideNavScreen(true);
Michael Kolb519d2282011-05-09 17:03:19 -0700163 mUiController.openIncognitoTab();
Michael Kolb2814a362011-05-19 15:49:41 -0700164 } else if (mTitle == v) {
165 mUi.getTitleBar().setSkipTitleBarAnimations(true);
166 close(false);
167 mUi.editUrl(false);
168 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700169 }
170 }
171
Michael Kolb2814a362011-05-19 15:49:41 -0700172 private void onCloseTab(Tab tab) {
173 if (tab != null) {
Michael Kolb308b3012011-07-13 14:50:56 -0700174 if (tab == mUiController.getCurrentTab()) {
175 mUiController.closeCurrentTab();
176 } else {
177 mUiController.closeTab(tab);
178 }
Michael Kolb66af8162011-06-09 11:33:34 -0700179 mAdapter.notifyDataSetChanged();
Michael Kolb2814a362011-05-19 15:49:41 -0700180 }
181 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700182
Michael Kolb2814a362011-05-19 15:49:41 -0700183 private void openNewTab() {
184 // need to call openTab explicitely with setactive false
185 Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
186 false, false, false);
187 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700188 if (tab != null) {
189 // set tab as the selected in flipper, then hide
Michael Kolbc831b632011-05-11 09:30:34 -0700190 final int tix = mUi.mTabControl.getTabPosition(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700191 mScroller.setSelection(tix);
192 postDelayed(new Runnable() {
193 @Override
Michael Kolba4261fd2011-05-05 11:27:37 -0700194 public void run() {
Michael Kolba4261fd2011-05-05 11:27:37 -0700195 mUi.hideNavScreen(true);
196 switchToSelected();
197 }
Michael Kolb2814a362011-05-19 15:49:41 -0700198 }, 100);
Michael Kolba4261fd2011-05-05 11:27:37 -0700199 }
200 }
201
202 private void switchToSelected() {
Michael Kolb2814a362011-05-19 15:49:41 -0700203 Tab tab = (Tab) mScroller.getSelectedItem();
Michael Kolba4261fd2011-05-05 11:27:37 -0700204 if (tab != mUi.getActiveTab()) {
205 mUiController.setActiveTab(tab);
206 }
207 }
208
Michael Kolbf2055602011-04-09 17:20:03 -0700209 protected void close() {
210 close(true);
211 }
212
213 protected void close(boolean animate) {
214 mUi.hideNavScreen(animate);
Michael Kolbf2055602011-04-09 17:20:03 -0700215 }
216
Michael Kolbf2055602011-04-09 17:20:03 -0700217 class TabAdapter extends BaseAdapter {
218
219 Context context;
220 TabControl tabControl;
221
222 public TabAdapter(Context ctx, TabControl tc) {
223 context = ctx;
224 tabControl = tc;
225 }
226
Michael Kolbf2055602011-04-09 17:20:03 -0700227 @Override
228 public int getCount() {
229 return tabControl.getTabCount();
230 }
231
232 @Override
233 public Tab getItem(int position) {
234 return tabControl.getTab(position);
235 }
236
237 public long getItemId(int position) {
238 return position;
239 }
240
241 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700242 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700243 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700244 final Tab tab = getItem(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700245 tabview.setWebView(mUi, tab);
246 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700247 @Override
248 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700249 if (tabview.isClose(v)) {
Michael Kolbe76f7042011-07-27 15:16:32 -0700250 mScroller.animateOut(tabview);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700251 } else if (tabview.isTitle(v)) {
Michael Kolb308b3012011-07-13 14:50:56 -0700252 mScroller.setSelection(position);
253 switchToSelected();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700254 mUi.getTitleBar().setSkipTitleBarAnimations(true);
255 close(false);
256 mUi.editUrl(false);
257 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700258 } else if (tabview.isWebView(v)) {
259 mScroller.setSelection(position);
260 close();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700261 }
262 }
263 });
264 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700265 }
Michael Kolb2814a362011-05-19 15:49:41 -0700266
Michael Kolbf2055602011-04-09 17:20:03 -0700267 }
268
Michael Kolbf2055602011-04-09 17:20:03 -0700269}