blob: 23317b58426284d484ab3109e6b773bfda18ed4e [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;
Michael Kolbf2055602011-04-09 17:20:03 -070031import android.widget.ImageButton;
32import android.widget.ImageView;
Michael Kolb9829b432011-06-04 13:29:00 -070033import android.widget.LinearLayout;
Michael Kolb017ffab2011-07-11 15:26:47 -070034import android.widget.PopupMenu;
35import android.widget.PopupMenu.OnMenuItemClickListener;
Michael Kolb2814a362011-05-19 15:49:41 -070036import android.widget.RelativeLayout;
Michael Kolbf2055602011-04-09 17:20:03 -070037import android.widget.TextView;
38
Michael Kolb017ffab2011-07-11 15:26:47 -070039public class NavScreen extends RelativeLayout
40 implements OnClickListener, OnMenuItemClickListener {
Michael Kolbf2055602011-04-09 17:20:03 -070041
42 UiController mUiController;
43 PhoneUi mUi;
44 Tab mTab;
45 Activity mActivity;
46
Michael Kolbf2055602011-04-09 17:20:03 -070047 ImageButton mRefresh;
48 ImageButton mForward;
Michael Kolbf2055602011-04-09 17:20:03 -070049 ImageButton mBookmarks;
50 ImageButton mMore;
51 ImageButton mNewTab;
52 ImageButton mNewIncognito;
53 FrameLayout mHolder;
54
Michael Kolb2814a362011-05-19 15:49:41 -070055 TextView mTitle;
56 ImageView mFavicon;
57 ImageButton mCloseTab;
58
Michael Kolb9829b432011-06-04 13:29:00 -070059 NavTabGallery mScroller;
Michael Kolbf2055602011-04-09 17:20:03 -070060 TabAdapter mAdapter;
Michael Kolba4261fd2011-05-05 11:27:37 -070061 int mOrientation;
Michael Kolbf2055602011-04-09 17:20:03 -070062
63 public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
64 super(activity);
65 mActivity = activity;
66 mUiController = ctl;
67 mUi = ui;
Michael Kolba4261fd2011-05-05 11:27:37 -070068 mOrientation = activity.getResources().getConfiguration().orientation;
Michael Kolbf2055602011-04-09 17:20:03 -070069 init();
70 }
71
72 protected Tab getSelectedTab() {
Michael Kolb2814a362011-05-19 15:49:41 -070073 return (Tab) mScroller.getSelectedItem();
Michael Kolbf2055602011-04-09 17:20:03 -070074 }
75
Michael Kolbfedb4922011-04-20 16:45:33 -070076 protected void showMenu() {
Michael Kolb017ffab2011-07-11 15:26:47 -070077 PopupMenu popup = new PopupMenu(mContext, mMore);
78 Menu menu = popup.getMenu();
79 popup.getMenuInflater().inflate(R.menu.browser, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -070080 mUiController.updateMenuState(mScroller.getSelectedItem(), menu);
Michael Kolb017ffab2011-07-11 15:26:47 -070081 popup.setOnMenuItemClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -070082 popup.show();
Michael Kolb017ffab2011-07-11 15:26:47 -070083 }
84
85 @Override
86 public boolean onMenuItemClick(MenuItem item) {
Michael Kolbadb0e602011-07-18 14:33:46 -070087 mUi.hideNavScreen(false);
Michael Kolb017ffab2011-07-11 15:26:47 -070088 return mUiController.onOptionsItemSelected(item);
Michael Kolbf2055602011-04-09 17:20:03 -070089 }
90
John Reckadc921f2011-04-27 10:11:03 -070091 protected float getToolbarHeight() {
92 return mActivity.getResources().getDimension(R.dimen.toolbar_height);
93 }
94
Michael Kolba4261fd2011-05-05 11:27:37 -070095 // for configuration changes
96 @Override
97 protected void onConfigurationChanged(Configuration newconfig) {
98 if (newconfig.orientation != mOrientation) {
Michael Kolb2814a362011-05-19 15:49:41 -070099 int selIx = mScroller.getSelectionIndex();
Michael Kolba4261fd2011-05-05 11:27:37 -0700100 removeAllViews();
Michael Kolb9829b432011-06-04 13:29:00 -0700101 mOrientation = newconfig.orientation;
Michael Kolba4261fd2011-05-05 11:27:37 -0700102 init();
Michael Kolb2814a362011-05-19 15:49:41 -0700103 mScroller.setSelection(selIx);
Michael Kolb2814a362011-05-19 15:49:41 -0700104 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700105 }
106 }
107
Michael Kolbf2055602011-04-09 17:20:03 -0700108 private void init() {
109 LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700110 mBookmarks = (ImageButton) findViewById(R.id.bookmarks);
111 mNewTab = (ImageButton) findViewById(R.id.newtab);
112 mNewIncognito = (ImageButton) findViewById(R.id.newincognito);
113 mMore = (ImageButton) findViewById(R.id.more);
Michael Kolbf2055602011-04-09 17:20:03 -0700114 mBookmarks.setOnClickListener(this);
115 mNewTab.setOnClickListener(this);
116 mNewIncognito.setOnClickListener(this);
117 mMore.setOnClickListener(this);
Michael Kolb9829b432011-06-04 13:29:00 -0700118 mScroller = (NavTabGallery) findViewById(R.id.scroller);
Michael Kolbf2055602011-04-09 17:20:03 -0700119 mAdapter = new TabAdapter(mContext, mUiController.getTabControl());
Michael Kolb2814a362011-05-19 15:49:41 -0700120 mScroller.setAdapter(mAdapter);
Michael Kolb9829b432011-06-04 13:29:00 -0700121 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
122 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700123 // update state for active tab
124 mScroller.setSelection(mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbf2055602011-04-09 17:20:03 -0700125 }
126
127 @Override
128 public void onClick(View v) {
129 WebView web = (mTab != null) ? mTab.getWebView() : null;
130 if (web != null) {
Michael Kolb2814a362011-05-19 15:49:41 -0700131 if (mForward == v) {
Michael Kolbf2055602011-04-09 17:20:03 -0700132 mUi.hideNavScreen(true);
John Reckef654f12011-07-12 16:42:08 -0700133 mTab.goForward();
Michael Kolbf2055602011-04-09 17:20:03 -0700134 } else if (mRefresh == v) {
135 mUi.hideNavScreen(true);
136 web.reload();
137 }
138 }
139 if (mBookmarks == v) {
140 mUi.hideNavScreen(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700141 switchToSelected();
Michael Kolbf2055602011-04-09 17:20:03 -0700142 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700143 } else if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700144 openNewTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700145 } else if (mMore == v) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700146 showMenu();
Michael Kolbf2055602011-04-09 17:20:03 -0700147 } else if (mNewIncognito == v) {
148 mUi.hideNavScreen(true);
Michael Kolb519d2282011-05-09 17:03:19 -0700149 mUiController.openIncognitoTab();
Michael Kolb2814a362011-05-19 15:49:41 -0700150 } else if (mTitle == v) {
151 mUi.getTitleBar().setSkipTitleBarAnimations(true);
152 close(false);
153 mUi.editUrl(false);
154 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700155 }
156 }
157
Michael Kolb2814a362011-05-19 15:49:41 -0700158 private void onCloseTab(Tab tab) {
159 if (tab != null) {
Michael Kolb308b3012011-07-13 14:50:56 -0700160 if (tab == mUiController.getCurrentTab()) {
161 mUiController.closeCurrentTab();
162 } else {
163 mUiController.closeTab(tab);
164 }
Michael Kolb66af8162011-06-09 11:33:34 -0700165 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
Michael Kolbf2055602011-04-09 17:20:03 -0700203 class TabAdapter extends BaseAdapter {
204
205 Context context;
206 TabControl tabControl;
207
208 public TabAdapter(Context ctx, TabControl tc) {
209 context = ctx;
210 tabControl = tc;
211 }
212
Michael Kolbf2055602011-04-09 17:20:03 -0700213 @Override
214 public int getCount() {
215 return tabControl.getTabCount();
216 }
217
218 @Override
219 public Tab getItem(int position) {
220 return tabControl.getTab(position);
221 }
222
223 public long getItemId(int position) {
224 return position;
225 }
226
227 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700228 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700229 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700230 final Tab tab = getItem(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700231 tabview.setWebView(mUi, tab);
232 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700233 @Override
234 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700235 if (tabview.isClose(v)) {
Michael Kolb308b3012011-07-13 14:50:56 -0700236 onCloseTab(tab);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700237 } else if (tabview.isTitle(v)) {
Michael Kolb308b3012011-07-13 14:50:56 -0700238 mScroller.setSelection(position);
239 switchToSelected();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700240 mUi.getTitleBar().setSkipTitleBarAnimations(true);
241 close(false);
242 mUi.editUrl(false);
243 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700244 } else if (tabview.isWebView(v)) {
245 mScroller.setSelection(position);
246 close();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700247 }
248 }
249 });
250 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700251 }
Michael Kolb2814a362011-05-19 15:49:41 -0700252
Michael Kolbf2055602011-04-09 17:20:03 -0700253 }
254
Michael Kolbf2055602011-04-09 17:20:03 -0700255}