blob: 9486a1957c3997f2535f64f104e945781c252db6 [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 Kolbfedb4922011-04-20 16:45:33 -070080 menu.setGroupVisible(R.id.NAV_MENU, false);
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);
Michael Kolbf2055602011-04-09 17:20:03 -0700132 web.goForward();
133 } 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 Kolb66af8162011-06-09 11:33:34 -0700159 switchToSelected();
160 mUiController.closeCurrentTab();
161 mAdapter.notifyDataSetChanged();
Michael Kolb2814a362011-05-19 15:49:41 -0700162 }
163 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700164
Michael Kolb2814a362011-05-19 15:49:41 -0700165 private void openNewTab() {
166 // need to call openTab explicitely with setactive false
167 Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
168 false, false, false);
169 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700170 if (tab != null) {
171 // set tab as the selected in flipper, then hide
Michael Kolbc831b632011-05-11 09:30:34 -0700172 final int tix = mUi.mTabControl.getTabPosition(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700173 mScroller.setSelection(tix);
174 postDelayed(new Runnable() {
175 @Override
Michael Kolba4261fd2011-05-05 11:27:37 -0700176 public void run() {
Michael Kolba4261fd2011-05-05 11:27:37 -0700177 mUi.hideNavScreen(true);
178 switchToSelected();
179 }
Michael Kolb2814a362011-05-19 15:49:41 -0700180 }, 100);
Michael Kolba4261fd2011-05-05 11:27:37 -0700181 }
182 }
183
184 private void switchToSelected() {
Michael Kolb2814a362011-05-19 15:49:41 -0700185 Tab tab = (Tab) mScroller.getSelectedItem();
Michael Kolba4261fd2011-05-05 11:27:37 -0700186 if (tab != mUi.getActiveTab()) {
187 mUiController.setActiveTab(tab);
188 }
189 }
190
Michael Kolbf2055602011-04-09 17:20:03 -0700191 protected void close() {
192 close(true);
193 }
194
195 protected void close(boolean animate) {
196 mUi.hideNavScreen(animate);
Michael Kolbf2055602011-04-09 17:20:03 -0700197 }
198
Michael Kolbf2055602011-04-09 17:20:03 -0700199 class TabAdapter extends BaseAdapter {
200
201 Context context;
202 TabControl tabControl;
203
204 public TabAdapter(Context ctx, TabControl tc) {
205 context = ctx;
206 tabControl = tc;
207 }
208
Michael Kolbf2055602011-04-09 17:20:03 -0700209 @Override
210 public int getCount() {
211 return tabControl.getTabCount();
212 }
213
214 @Override
215 public Tab getItem(int position) {
216 return tabControl.getTab(position);
217 }
218
219 public long getItemId(int position) {
220 return position;
221 }
222
223 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700224 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700225 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700226 final Tab tab = getItem(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700227 tabview.setWebView(mUi, tab);
228 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700229 @Override
230 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700231 if (tabview.isClose(v)) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700232 onCloseTab((Tab) (mScroller.getSelectedItem()));
233 } else if (tabview.isTitle(v)) {
234 mUi.getTitleBar().setSkipTitleBarAnimations(true);
235 close(false);
236 mUi.editUrl(false);
237 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700238 } else if (tabview.isWebView(v)) {
239 mScroller.setSelection(position);
240 close();
Michael Kolb2814a362011-05-19 15:49:41 -0700241
Michael Kolb4bd767d2011-05-27 11:33:55 -0700242 }
243 }
244 });
245 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700246 }
Michael Kolb2814a362011-05-19 15:49:41 -0700247
Michael Kolbf2055602011-04-09 17:20:03 -0700248 }
249
Michael Kolbf2055602011-04-09 17:20:03 -0700250}