blob: face39e3d697c5b5ae93055cc57243cbcd061f54 [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;
Michael Kolbf2055602011-04-09 17:20:03 -070055 FrameLayout mHolder;
56
Michael Kolb2814a362011-05-19 15:49:41 -070057 TextView mTitle;
58 ImageView mFavicon;
59 ImageButton mCloseTab;
60
Michael Kolb9829b432011-06-04 13:29:00 -070061 NavTabGallery mScroller;
Michael Kolbf2055602011-04-09 17:20:03 -070062 TabAdapter mAdapter;
Michael Kolba4261fd2011-05-05 11:27:37 -070063 int mOrientation;
Michael Kolb3ca12752011-07-20 13:52:25 -070064 boolean mNeedsMenu;
Michael Kolbf2055602011-04-09 17:20:03 -070065
66 public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
67 super(activity);
68 mActivity = activity;
69 mUiController = ctl;
70 mUi = ui;
Michael Kolba4261fd2011-05-05 11:27:37 -070071 mOrientation = activity.getResources().getConfiguration().orientation;
Michael Kolbf2055602011-04-09 17:20:03 -070072 init();
73 }
74
75 protected Tab getSelectedTab() {
Michael Kolb2814a362011-05-19 15:49:41 -070076 return (Tab) mScroller.getSelectedItem();
Michael Kolbf2055602011-04-09 17:20:03 -070077 }
78
Michael Kolbfedb4922011-04-20 16:45:33 -070079 protected void showMenu() {
Michael Kolb017ffab2011-07-11 15:26:47 -070080 PopupMenu popup = new PopupMenu(mContext, mMore);
81 Menu menu = popup.getMenu();
82 popup.getMenuInflater().inflate(R.menu.browser, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -070083 mUiController.updateMenuState(mScroller.getSelectedItem(), menu);
Michael Kolb017ffab2011-07-11 15:26:47 -070084 popup.setOnMenuItemClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -070085 popup.show();
Michael Kolb017ffab2011-07-11 15:26:47 -070086 }
87
88 @Override
89 public boolean onMenuItemClick(MenuItem item) {
Michael Kolbadb0e602011-07-18 14:33:46 -070090 mUi.hideNavScreen(false);
Michael Kolb017ffab2011-07-11 15:26:47 -070091 return mUiController.onOptionsItemSelected(item);
Michael Kolbf2055602011-04-09 17:20:03 -070092 }
93
John Reckadc921f2011-04-27 10:11:03 -070094 protected float getToolbarHeight() {
95 return mActivity.getResources().getDimension(R.dimen.toolbar_height);
96 }
97
Michael Kolba4261fd2011-05-05 11:27:37 -070098 // for configuration changes
99 @Override
100 protected void onConfigurationChanged(Configuration newconfig) {
101 if (newconfig.orientation != mOrientation) {
Michael Kolb2814a362011-05-19 15:49:41 -0700102 int selIx = mScroller.getSelectionIndex();
Michael Kolba4261fd2011-05-05 11:27:37 -0700103 removeAllViews();
Michael Kolb9829b432011-06-04 13:29:00 -0700104 mOrientation = newconfig.orientation;
Michael Kolba4261fd2011-05-05 11:27:37 -0700105 init();
Michael Kolb2814a362011-05-19 15:49:41 -0700106 mScroller.setSelection(selIx);
Michael Kolb2814a362011-05-19 15:49:41 -0700107 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700108 }
109 }
110
Michael Kolbf2055602011-04-09 17:20:03 -0700111 private void init() {
112 LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this);
Michael Kolb30adae62011-07-29 13:37:05 -0700113 setContentDescription(mContext.getResources().getString(
114 R.string.accessibility_transition_navscreen));
Michael Kolbf2055602011-04-09 17:20:03 -0700115 mBookmarks = (ImageButton) findViewById(R.id.bookmarks);
116 mNewTab = (ImageButton) findViewById(R.id.newtab);
Michael Kolbf2055602011-04-09 17:20:03 -0700117 mMore = (ImageButton) findViewById(R.id.more);
Michael Kolbf2055602011-04-09 17:20:03 -0700118 mBookmarks.setOnClickListener(this);
119 mNewTab.setOnClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -0700120 mMore.setOnClickListener(this);
Michael Kolb9829b432011-06-04 13:29:00 -0700121 mScroller = (NavTabGallery) findViewById(R.id.scroller);
Michael Kolbf2055602011-04-09 17:20:03 -0700122 mAdapter = new TabAdapter(mContext, mUiController.getTabControl());
Michael Kolb2814a362011-05-19 15:49:41 -0700123 mScroller.setAdapter(mAdapter);
Michael Kolb9829b432011-06-04 13:29:00 -0700124 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
125 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700126 // update state for active tab
127 mScroller.setSelection(mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbdcd81d32011-07-22 10:30:49 -0700128 mScroller.setOnRemoveListener(new OnRemoveListener() {
129 public void onRemovePosition(int pos) {
130 Tab tab = mAdapter.getItem(pos);
131 onCloseTab(tab);
132 }
133 });
Michael Kolb3ca12752011-07-20 13:52:25 -0700134 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
135 if (!mNeedsMenu) {
136 mMore.setVisibility(View.GONE);
137 }
Michael Kolbf2055602011-04-09 17:20:03 -0700138 }
139
140 @Override
141 public void onClick(View v) {
142 WebView web = (mTab != null) ? mTab.getWebView() : null;
143 if (web != null) {
Michael Kolb2814a362011-05-19 15:49:41 -0700144 if (mForward == v) {
Michael Kolbf2055602011-04-09 17:20:03 -0700145 mUi.hideNavScreen(true);
John Reckef654f12011-07-12 16:42:08 -0700146 mTab.goForward();
Michael Kolbf2055602011-04-09 17:20:03 -0700147 } else if (mRefresh == v) {
148 mUi.hideNavScreen(true);
149 web.reload();
150 }
151 }
152 if (mBookmarks == v) {
153 mUi.hideNavScreen(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700154 switchToSelected();
Michael Kolbf2055602011-04-09 17:20:03 -0700155 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700156 } else if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700157 openNewTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700158 } else if (mMore == v) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700159 showMenu();
Michael Kolb2814a362011-05-19 15:49:41 -0700160 } else if (mTitle == v) {
161 mUi.getTitleBar().setSkipTitleBarAnimations(true);
162 close(false);
163 mUi.editUrl(false);
164 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolbf2055602011-04-09 17:20:03 -0700165 }
166 }
167
Michael Kolb2814a362011-05-19 15:49:41 -0700168 private void onCloseTab(Tab tab) {
169 if (tab != null) {
Michael Kolb308b3012011-07-13 14:50:56 -0700170 if (tab == mUiController.getCurrentTab()) {
171 mUiController.closeCurrentTab();
172 } else {
173 mUiController.closeTab(tab);
174 }
Michael Kolb66af8162011-06-09 11:33:34 -0700175 mAdapter.notifyDataSetChanged();
Michael Kolb2814a362011-05-19 15:49:41 -0700176 }
177 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700178
Michael Kolb2814a362011-05-19 15:49:41 -0700179 private void openNewTab() {
180 // need to call openTab explicitely with setactive false
181 Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
182 false, false, false);
183 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700184 if (tab != null) {
185 // set tab as the selected in flipper, then hide
Michael Kolbc831b632011-05-11 09:30:34 -0700186 final int tix = mUi.mTabControl.getTabPosition(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700187 mScroller.setSelection(tix);
188 postDelayed(new Runnable() {
189 @Override
Michael Kolba4261fd2011-05-05 11:27:37 -0700190 public void run() {
Michael Kolba4261fd2011-05-05 11:27:37 -0700191 mUi.hideNavScreen(true);
192 switchToSelected();
193 }
Michael Kolb2814a362011-05-19 15:49:41 -0700194 }, 100);
Michael Kolba4261fd2011-05-05 11:27:37 -0700195 }
196 }
197
198 private void switchToSelected() {
Michael Kolb2814a362011-05-19 15:49:41 -0700199 Tab tab = (Tab) mScroller.getSelectedItem();
Michael Kolba4261fd2011-05-05 11:27:37 -0700200 if (tab != mUi.getActiveTab()) {
201 mUiController.setActiveTab(tab);
202 }
203 }
204
Michael Kolbf2055602011-04-09 17:20:03 -0700205 protected void close() {
206 close(true);
207 }
208
209 protected void close(boolean animate) {
210 mUi.hideNavScreen(animate);
Michael Kolbf2055602011-04-09 17:20:03 -0700211 }
212
Michael Kolbf2055602011-04-09 17:20:03 -0700213 class TabAdapter extends BaseAdapter {
214
215 Context context;
216 TabControl tabControl;
217
218 public TabAdapter(Context ctx, TabControl tc) {
219 context = ctx;
220 tabControl = tc;
221 }
222
Michael Kolbf2055602011-04-09 17:20:03 -0700223 @Override
224 public int getCount() {
225 return tabControl.getTabCount();
226 }
227
228 @Override
229 public Tab getItem(int position) {
230 return tabControl.getTab(position);
231 }
232
233 public long getItemId(int position) {
234 return position;
235 }
236
237 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700238 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700239 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700240 final Tab tab = getItem(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700241 tabview.setWebView(mUi, tab);
242 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700243 @Override
244 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700245 if (tabview.isClose(v)) {
Michael Kolbe76f7042011-07-27 15:16:32 -0700246 mScroller.animateOut(tabview);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700247 } else if (tabview.isTitle(v)) {
Michael Kolb308b3012011-07-13 14:50:56 -0700248 mScroller.setSelection(position);
249 switchToSelected();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700250 mUi.getTitleBar().setSkipTitleBarAnimations(true);
251 close(false);
252 mUi.editUrl(false);
253 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700254 } else if (tabview.isWebView(v)) {
255 mScroller.setSelection(position);
256 close();
Michael Kolb4bd767d2011-05-27 11:33:55 -0700257 }
258 }
259 });
260 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700261 }
Michael Kolb2814a362011-05-19 15:49:41 -0700262
Michael Kolbf2055602011-04-09 17:20:03 -0700263 }
264
Michael Kolbf2055602011-04-09 17:20:03 -0700265}