blob: d03da6baa5ff68967093dab4ffadeee80919e0f9 [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) {
87 return mUiController.onOptionsItemSelected(item);
Michael Kolbf2055602011-04-09 17:20:03 -070088 }
89
John Reckadc921f2011-04-27 10:11:03 -070090 protected float getToolbarHeight() {
91 return mActivity.getResources().getDimension(R.dimen.toolbar_height);
92 }
93
Michael Kolba4261fd2011-05-05 11:27:37 -070094 // for configuration changes
95 @Override
96 protected void onConfigurationChanged(Configuration newconfig) {
97 if (newconfig.orientation != mOrientation) {
Michael Kolb2814a362011-05-19 15:49:41 -070098 int selIx = mScroller.getSelectionIndex();
Michael Kolba4261fd2011-05-05 11:27:37 -070099 removeAllViews();
Michael Kolb9829b432011-06-04 13:29:00 -0700100 mOrientation = newconfig.orientation;
Michael Kolba4261fd2011-05-05 11:27:37 -0700101 init();
Michael Kolb2814a362011-05-19 15:49:41 -0700102 mScroller.setSelection(selIx);
Michael Kolb2814a362011-05-19 15:49:41 -0700103 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700104 }
105 }
106
Michael Kolbf2055602011-04-09 17:20:03 -0700107 private void init() {
108 LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700109 mBookmarks = (ImageButton) findViewById(R.id.bookmarks);
110 mNewTab = (ImageButton) findViewById(R.id.newtab);
111 mNewIncognito = (ImageButton) findViewById(R.id.newincognito);
112 mMore = (ImageButton) findViewById(R.id.more);
Michael Kolbf2055602011-04-09 17:20:03 -0700113 mBookmarks.setOnClickListener(this);
114 mNewTab.setOnClickListener(this);
115 mNewIncognito.setOnClickListener(this);
116 mMore.setOnClickListener(this);
Michael Kolb9829b432011-06-04 13:29:00 -0700117 mScroller = (NavTabGallery) findViewById(R.id.scroller);
Michael Kolbf2055602011-04-09 17:20:03 -0700118 mAdapter = new TabAdapter(mContext, mUiController.getTabControl());
Michael Kolb2814a362011-05-19 15:49:41 -0700119 mScroller.setAdapter(mAdapter);
Michael Kolb9829b432011-06-04 13:29:00 -0700120 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
121 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700122 // update state for active tab
123 mScroller.setSelection(mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbf2055602011-04-09 17:20:03 -0700124 }
125
126 @Override
127 public void onClick(View v) {
128 WebView web = (mTab != null) ? mTab.getWebView() : null;
129 if (web != null) {
Michael Kolb2814a362011-05-19 15:49:41 -0700130 if (mForward == v) {
Michael Kolbf2055602011-04-09 17:20:03 -0700131 mUi.hideNavScreen(true);
John Reckef654f12011-07-12 16:42:08 -0700132 mTab.goForward();
Michael Kolbf2055602011-04-09 17:20:03 -0700133 } else if (mRefresh == v) {
134 mUi.hideNavScreen(true);
135 web.reload();
136 }
137 }
138 if (mBookmarks == v) {
139 mUi.hideNavScreen(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700140 switchToSelected();
Michael Kolbf2055602011-04-09 17:20:03 -0700141 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700142 } else if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700143 openNewTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700144 } else if (mMore == v) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700145 showMenu();
Michael Kolbf2055602011-04-09 17:20:03 -0700146 } else if (mNewIncognito == v) {
147 mUi.hideNavScreen(true);
Michael Kolb519d2282011-05-09 17:03:19 -0700148 mUiController.openIncognitoTab();
Michael Kolb2814a362011-05-19 15:49:41 -0700149 } else if (mTitle == v) {
150 mUi.getTitleBar().setSkipTitleBarAnimations(true);
151 close(false);
152 mUi.editUrl(false);
153 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700154 }
155 }
156
Michael Kolb2814a362011-05-19 15:49:41 -0700157 private void onCloseTab(Tab tab) {
158 if (tab != null) {
Michael Kolb308b3012011-07-13 14:50:56 -0700159 if (tab == mUiController.getCurrentTab()) {
160 mUiController.closeCurrentTab();
161 } else {
162 mUiController.closeTab(tab);
163 }
Michael Kolb66af8162011-06-09 11:33:34 -0700164 mAdapter.notifyDataSetChanged();
Michael Kolb2814a362011-05-19 15:49:41 -0700165 }
166 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700167
Michael Kolb2814a362011-05-19 15:49:41 -0700168 private void openNewTab() {
169 // need to call openTab explicitely with setactive false
170 Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
171 false, false, false);
172 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700173 if (tab != null) {
174 // set tab as the selected in flipper, then hide
Michael Kolbc831b632011-05-11 09:30:34 -0700175 final int tix = mUi.mTabControl.getTabPosition(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700176 mScroller.setSelection(tix);
177 postDelayed(new Runnable() {
178 @Override
Michael Kolba4261fd2011-05-05 11:27:37 -0700179 public void run() {
Michael Kolba4261fd2011-05-05 11:27:37 -0700180 mUi.hideNavScreen(true);
181 switchToSelected();
182 }
Michael Kolb2814a362011-05-19 15:49:41 -0700183 }, 100);
Michael Kolba4261fd2011-05-05 11:27:37 -0700184 }
185 }
186
187 private void switchToSelected() {
Michael Kolb2814a362011-05-19 15:49:41 -0700188 Tab tab = (Tab) mScroller.getSelectedItem();
Michael Kolba4261fd2011-05-05 11:27:37 -0700189 if (tab != mUi.getActiveTab()) {
190 mUiController.setActiveTab(tab);
191 }
192 }
193
Michael Kolbf2055602011-04-09 17:20:03 -0700194 protected void close() {
195 close(true);
196 }
197
198 protected void close(boolean animate) {
199 mUi.hideNavScreen(animate);
Michael Kolbf2055602011-04-09 17:20:03 -0700200 }
201
Michael Kolbf2055602011-04-09 17:20:03 -0700202 class TabAdapter extends BaseAdapter {
203
204 Context context;
205 TabControl tabControl;
206
207 public TabAdapter(Context ctx, TabControl tc) {
208 context = ctx;
209 tabControl = tc;
210 }
211
Michael Kolbf2055602011-04-09 17:20:03 -0700212 @Override
213 public int getCount() {
214 return tabControl.getTabCount();
215 }
216
217 @Override
218 public Tab getItem(int position) {
219 return tabControl.getTab(position);
220 }
221
222 public long getItemId(int position) {
223 return position;
224 }
225
226 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700227 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700228 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700229 final Tab tab = getItem(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700230 tabview.setWebView(mUi, tab);
231 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700232 @Override
233 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700234 if (tabview.isClose(v)) {
Michael Kolb308b3012011-07-13 14:50:56 -0700235 onCloseTab(tab);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700236 } else if (tabview.isTitle(v)) {
Michael Kolb308b3012011-07-13 14:50:56 -0700237 mScroller.setSelection(position);
238 switchToSelected();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700239 mUi.getTitleBar().setSkipTitleBarAnimations(true);
240 close(false);
241 mUi.editUrl(false);
242 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700243 } else if (tabview.isWebView(v)) {
244 mScroller.setSelection(position);
245 close();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700246 }
247 }
248 });
249 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700250 }
Michael Kolb2814a362011-05-19 15:49:41 -0700251
Michael Kolbf2055602011-04-09 17:20:03 -0700252 }
253
Michael Kolbf2055602011-04-09 17:20:03 -0700254}