blob: 1d2114e0079b8ad7d2ba9334a993daa517dcefca [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;
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 Kolba3194d02011-09-07 11:23:51 -070039import com.android.browser.NavTabScroller.OnLayoutListener;
40import com.android.browser.NavTabScroller.OnRemoveListener;
John Reck8ee633f2011-08-09 16:00:35 -070041import com.android.browser.TabControl.OnThumbnailUpdatedListener;
Michael Kolb315d5022011-10-13 12:47:11 -070042import com.android.browser.UI.ComboViews;
John Reck8ee633f2011-08-09 16:00:35 -070043
44import java.util.HashMap;
Michael Kolbdcd81d32011-07-22 10:30:49 -070045
Michael Kolb017ffab2011-07-11 15:26:47 -070046public class NavScreen extends RelativeLayout
John Reck8ee633f2011-08-09 16:00:35 -070047 implements OnClickListener, OnMenuItemClickListener, OnThumbnailUpdatedListener {
Michael Kolbf2055602011-04-09 17:20:03 -070048
Michael Kolbc3af0672011-08-09 10:24:41 -070049
Michael Kolbf2055602011-04-09 17:20:03 -070050 UiController mUiController;
51 PhoneUi mUi;
52 Tab mTab;
53 Activity mActivity;
54
Michael Kolbf2055602011-04-09 17:20:03 -070055 ImageButton mRefresh;
56 ImageButton mForward;
Michael Kolbf2055602011-04-09 17:20:03 -070057 ImageButton mBookmarks;
58 ImageButton mMore;
59 ImageButton mNewTab;
Michael Kolbf2055602011-04-09 17:20:03 -070060 FrameLayout mHolder;
61
Michael Kolb2814a362011-05-19 15:49:41 -070062 TextView mTitle;
63 ImageView mFavicon;
64 ImageButton mCloseTab;
65
Michael Kolba3194d02011-09-07 11:23:51 -070066 NavTabScroller mScroller;
Michael Kolbf2055602011-04-09 17:20:03 -070067 TabAdapter mAdapter;
Michael Kolba4261fd2011-05-05 11:27:37 -070068 int mOrientation;
Michael Kolb3ca12752011-07-20 13:52:25 -070069 boolean mNeedsMenu;
John Reck8ee633f2011-08-09 16:00:35 -070070 HashMap<Tab, View> mTabViews;
Michael Kolbf2055602011-04-09 17:20:03 -070071
72 public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
73 super(activity);
74 mActivity = activity;
75 mUiController = ctl;
76 mUi = ui;
Michael Kolba4261fd2011-05-05 11:27:37 -070077 mOrientation = activity.getResources().getConfiguration().orientation;
Michael Kolbf2055602011-04-09 17:20:03 -070078 init();
79 }
80
Michael Kolbfedb4922011-04-20 16:45:33 -070081 protected void showMenu() {
Michael Kolb017ffab2011-07-11 15:26:47 -070082 PopupMenu popup = new PopupMenu(mContext, mMore);
83 Menu menu = popup.getMenu();
84 popup.getMenuInflater().inflate(R.menu.browser, menu);
Michael Kolba3194d02011-09-07 11:23:51 -070085 mUiController.updateMenuState(mUiController.getCurrentTab(), menu);
Michael Kolb017ffab2011-07-11 15:26:47 -070086 popup.setOnMenuItemClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -070087 popup.show();
Michael Kolb017ffab2011-07-11 15:26:47 -070088 }
89
90 @Override
91 public boolean onMenuItemClick(MenuItem item) {
92 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 @Override
100 protected void onConfigurationChanged(Configuration newconfig) {
101 if (newconfig.orientation != mOrientation) {
Michael Kolba3194d02011-09-07 11:23:51 -0700102 int sv = mScroller.getScrollValue();
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 Kolba3194d02011-09-07 11:23:51 -0700106 mScroller.setScrollValue(sv);
Michael Kolb2814a362011-05-19 15:49:41 -0700107 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700108 }
109 }
110
John Reck9c5004e2011-10-07 16:00:12 -0700111 public void refreshAdapter() {
John Reck1adf0302011-10-11 10:58:12 -0700112 mScroller.handleDataChanged(
113 mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
John Reck9c5004e2011-10-07 16:00:12 -0700114 }
115
Michael Kolbf2055602011-04-09 17:20:03 -0700116 private void init() {
117 LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this);
Michael Kolb30adae62011-07-29 13:37:05 -0700118 setContentDescription(mContext.getResources().getString(
119 R.string.accessibility_transition_navscreen));
Michael Kolbf2055602011-04-09 17:20:03 -0700120 mBookmarks = (ImageButton) findViewById(R.id.bookmarks);
121 mNewTab = (ImageButton) findViewById(R.id.newtab);
Michael Kolbf2055602011-04-09 17:20:03 -0700122 mMore = (ImageButton) findViewById(R.id.more);
Michael Kolbf2055602011-04-09 17:20:03 -0700123 mBookmarks.setOnClickListener(this);
124 mNewTab.setOnClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -0700125 mMore.setOnClickListener(this);
Michael Kolba3194d02011-09-07 11:23:51 -0700126 mScroller = (NavTabScroller) findViewById(R.id.scroller);
John Reck8ee633f2011-08-09 16:00:35 -0700127 TabControl tc = mUiController.getTabControl();
128 mTabViews = new HashMap<Tab, View>(tc.getTabCount());
129 mAdapter = new TabAdapter(mContext, tc);
Michael Kolb9829b432011-06-04 13:29:00 -0700130 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
131 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700132 // update state for active tab
Michael Kolbc3af0672011-08-09 10:24:41 -0700133 mScroller.setAdapter(mAdapter,
134 mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbdcd81d32011-07-22 10:30:49 -0700135 mScroller.setOnRemoveListener(new OnRemoveListener() {
136 public void onRemovePosition(int pos) {
137 Tab tab = mAdapter.getItem(pos);
138 onCloseTab(tab);
139 }
140 });
Michael Kolb3ca12752011-07-20 13:52:25 -0700141 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
142 if (!mNeedsMenu) {
143 mMore.setVisibility(View.GONE);
144 }
Michael Kolbf2055602011-04-09 17:20:03 -0700145 }
146
147 @Override
148 public void onClick(View v) {
Michael Kolbf2055602011-04-09 17:20:03 -0700149 if (mBookmarks == v) {
Michael Kolb315d5022011-10-13 12:47:11 -0700150 mUiController.bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolbf2055602011-04-09 17:20:03 -0700151 } else if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700152 openNewTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700153 } else if (mMore == v) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700154 showMenu();
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 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
Michael Kolba3194d02011-09-07 11:23:51 -0700170 final Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
Michael Kolb2814a362011-05-19 15:49:41 -0700171 false, false, false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700172 if (tab != null) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700173 mUiController.setBlockEvents(true);
Michael Kolbc831b632011-05-11 09:30:34 -0700174 final int tix = mUi.mTabControl.getTabPosition(tab);
Michael Kolba3194d02011-09-07 11:23:51 -0700175 mScroller.setOnLayoutListener(new OnLayoutListener() {
176
Michael Kolb2814a362011-05-19 15:49:41 -0700177 @Override
Michael Kolba3194d02011-09-07 11:23:51 -0700178 public void onLayout(int l, int t, int r, int b) {
179 mUi.hideNavScreen(tix, true);
180 switchToTab(tab);
Michael Kolba4261fd2011-05-05 11:27:37 -0700181 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700182 });
Michael Kolba3194d02011-09-07 11:23:51 -0700183 mScroller.handleDataChanged(tix);
184 mUiController.setBlockEvents(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700185 }
186 }
187
Michael Kolba3194d02011-09-07 11:23:51 -0700188 private void switchToTab(Tab tab) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700189 if (tab != mUi.getActiveTab()) {
190 mUiController.setActiveTab(tab);
191 }
192 }
193
Michael Kolba3194d02011-09-07 11:23:51 -0700194 protected void close(int position) {
195 close(position, true);
Michael Kolbf2055602011-04-09 17:20:03 -0700196 }
197
Michael Kolba3194d02011-09-07 11:23:51 -0700198 protected void close(int position, boolean animate) {
199 mUi.hideNavScreen(position, animate);
200 }
201
202 protected NavTabView getTabView(int pos) {
203 return mScroller.getTabView(pos);
Michael Kolbf2055602011-04-09 17:20:03 -0700204 }
205
Michael Kolbf2055602011-04-09 17:20:03 -0700206 class TabAdapter extends BaseAdapter {
207
208 Context context;
209 TabControl tabControl;
210
211 public TabAdapter(Context ctx, TabControl tc) {
212 context = ctx;
213 tabControl = tc;
214 }
215
Michael Kolbf2055602011-04-09 17:20:03 -0700216 @Override
217 public int getCount() {
218 return tabControl.getTabCount();
219 }
220
221 @Override
222 public Tab getItem(int position) {
223 return tabControl.getTab(position);
224 }
225
226 public long getItemId(int position) {
227 return position;
228 }
229
230 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700231 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700232 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700233 final Tab tab = getItem(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700234 tabview.setWebView(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700235 mTabViews.put(tab, tabview.mImage);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700236 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700237 @Override
238 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700239 if (tabview.isClose(v)) {
Michael Kolbe76f7042011-07-27 15:16:32 -0700240 mScroller.animateOut(tabview);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700241 } else if (tabview.isTitle(v)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700242 switchToTab(tab);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700243 mUi.getTitleBar().setSkipTitleBarAnimations(true);
Michael Kolba3194d02011-09-07 11:23:51 -0700244 close(position, false);
Michael Kolb1f9b3562012-04-24 14:38:34 -0700245 mUi.editUrl(false, true);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700246 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700247 } else if (tabview.isWebView(v)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700248 close(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700249 }
250 }
251 });
252 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700253 }
Michael Kolb2814a362011-05-19 15:49:41 -0700254
Michael Kolbf2055602011-04-09 17:20:03 -0700255 }
256
John Reck8ee633f2011-08-09 16:00:35 -0700257 @Override
258 public void onThumbnailUpdated(Tab t) {
259 View v = mTabViews.get(t);
260 if (v != null) {
261 v.invalidate();
John Reck8ee633f2011-08-09 16:00:35 -0700262 }
263 }
264
Michael Kolbf2055602011-04-09 17:20:03 -0700265}