blob: 42b35de968be90a204deefa0c0e00d5a023f8c65 [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolbf2055602011-04-09 17:20:03 -070018
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
Bijan Amirzada41242f22014-03-21 12:12:18 -070039import com.android.browser.R;
40import com.android.browser.NavTabScroller.OnLayoutListener;
41import com.android.browser.NavTabScroller.OnRemoveListener;
42import com.android.browser.TabControl.OnThumbnailUpdatedListener;
43import com.android.browser.UI.ComboViews;
John Reck8ee633f2011-08-09 16:00:35 -070044
45import java.util.HashMap;
Michael Kolbdcd81d32011-07-22 10:30:49 -070046
Michael Kolb017ffab2011-07-11 15:26:47 -070047public class NavScreen extends RelativeLayout
John Reck8ee633f2011-08-09 16:00:35 -070048 implements OnClickListener, OnMenuItemClickListener, OnThumbnailUpdatedListener {
Michael Kolbf2055602011-04-09 17:20:03 -070049
Michael Kolbc3af0672011-08-09 10:24:41 -070050
Michael Kolbf2055602011-04-09 17:20:03 -070051 UiController mUiController;
52 PhoneUi mUi;
53 Tab mTab;
54 Activity mActivity;
55
Michael Kolbf2055602011-04-09 17:20:03 -070056 ImageButton mRefresh;
57 ImageButton mForward;
Michael Kolbf2055602011-04-09 17:20:03 -070058 ImageButton mBookmarks;
59 ImageButton mMore;
60 ImageButton mNewTab;
Michael Kolbf2055602011-04-09 17:20:03 -070061 FrameLayout mHolder;
62
Michael Kolb2814a362011-05-19 15:49:41 -070063 TextView mTitle;
64 ImageView mFavicon;
65 ImageButton mCloseTab;
66
Michael Kolba3194d02011-09-07 11:23:51 -070067 NavTabScroller mScroller;
Michael Kolbf2055602011-04-09 17:20:03 -070068 TabAdapter mAdapter;
Michael Kolba4261fd2011-05-05 11:27:37 -070069 int mOrientation;
Michael Kolb3ca12752011-07-20 13:52:25 -070070 boolean mNeedsMenu;
John Reck8ee633f2011-08-09 16:00:35 -070071 HashMap<Tab, View> mTabViews;
Michael Kolbf2055602011-04-09 17:20:03 -070072
73 public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
74 super(activity);
75 mActivity = activity;
76 mUiController = ctl;
77 mUi = ui;
Michael Kolba4261fd2011-05-05 11:27:37 -070078 mOrientation = activity.getResources().getConfiguration().orientation;
Michael Kolbf2055602011-04-09 17:20:03 -070079 init();
80 }
81
Michael Kolbfedb4922011-04-20 16:45:33 -070082 protected void showMenu() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080083 PopupMenu popup = new PopupMenu(getContext(), mMore);
Michael Kolb017ffab2011-07-11 15:26:47 -070084 Menu menu = popup.getMenu();
85 popup.getMenuInflater().inflate(R.menu.browser, menu);
Michael Kolba3194d02011-09-07 11:23:51 -070086 mUiController.updateMenuState(mUiController.getCurrentTab(), menu);
Michael Kolb017ffab2011-07-11 15:26:47 -070087 popup.setOnMenuItemClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -070088 popup.show();
Michael Kolb017ffab2011-07-11 15:26:47 -070089 }
90
91 @Override
92 public boolean onMenuItemClick(MenuItem item) {
93 return mUiController.onOptionsItemSelected(item);
Michael Kolbf2055602011-04-09 17:20:03 -070094 }
95
John Reckadc921f2011-04-27 10:11:03 -070096 protected float getToolbarHeight() {
97 return mActivity.getResources().getDimension(R.dimen.toolbar_height);
98 }
99
Michael Kolba4261fd2011-05-05 11:27:37 -0700100 @Override
101 protected void onConfigurationChanged(Configuration newconfig) {
102 if (newconfig.orientation != mOrientation) {
Michael Kolba3194d02011-09-07 11:23:51 -0700103 int sv = mScroller.getScrollValue();
Michael Kolba4261fd2011-05-05 11:27:37 -0700104 removeAllViews();
Michael Kolb9829b432011-06-04 13:29:00 -0700105 mOrientation = newconfig.orientation;
Michael Kolba4261fd2011-05-05 11:27:37 -0700106 init();
Michael Kolba3194d02011-09-07 11:23:51 -0700107 mScroller.setScrollValue(sv);
Michael Kolb2814a362011-05-19 15:49:41 -0700108 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -0700109 }
110 }
111
John Reck9c5004e2011-10-07 16:00:12 -0700112 public void refreshAdapter() {
John Reck1adf0302011-10-11 10:58:12 -0700113 mScroller.handleDataChanged(
114 mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
John Reck9c5004e2011-10-07 16:00:12 -0700115 }
116
Michael Kolbf2055602011-04-09 17:20:03 -0700117 private void init() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800118 LayoutInflater.from(getContext()).inflate(R.layout.nav_screen, this);
119 setContentDescription(getContext().getResources().getString(
Michael Kolb30adae62011-07-29 13:37:05 -0700120 R.string.accessibility_transition_navscreen));
Michael Kolbf2055602011-04-09 17:20:03 -0700121 mBookmarks = (ImageButton) findViewById(R.id.bookmarks);
122 mNewTab = (ImageButton) findViewById(R.id.newtab);
Michael Kolbf2055602011-04-09 17:20:03 -0700123 mMore = (ImageButton) findViewById(R.id.more);
Michael Kolbf2055602011-04-09 17:20:03 -0700124 mBookmarks.setOnClickListener(this);
125 mNewTab.setOnClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -0700126 mMore.setOnClickListener(this);
Michael Kolba3194d02011-09-07 11:23:51 -0700127 mScroller = (NavTabScroller) findViewById(R.id.scroller);
John Reck8ee633f2011-08-09 16:00:35 -0700128 TabControl tc = mUiController.getTabControl();
129 mTabViews = new HashMap<Tab, View>(tc.getTabCount());
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800130 mAdapter = new TabAdapter(getContext(), tc);
Michael Kolb9829b432011-06-04 13:29:00 -0700131 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
132 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700133 // update state for active tab
Michael Kolbc3af0672011-08-09 10:24:41 -0700134 mScroller.setAdapter(mAdapter,
135 mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbdcd81d32011-07-22 10:30:49 -0700136 mScroller.setOnRemoveListener(new OnRemoveListener() {
137 public void onRemovePosition(int pos) {
138 Tab tab = mAdapter.getItem(pos);
139 onCloseTab(tab);
140 }
141 });
Michael Kolb3ca12752011-07-20 13:52:25 -0700142 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
143 if (!mNeedsMenu) {
144 mMore.setVisibility(View.GONE);
145 }
Michael Kolbf2055602011-04-09 17:20:03 -0700146 }
147
148 @Override
149 public void onClick(View v) {
Michael Kolbf2055602011-04-09 17:20:03 -0700150 if (mBookmarks == v) {
Michael Kolb315d5022011-10-13 12:47:11 -0700151 mUiController.bookmarksOrHistoryPicker(ComboViews.Bookmarks);
Michael Kolbf2055602011-04-09 17:20:03 -0700152 } else if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700153 openNewTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700154 } else if (mMore == v) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700155 showMenu();
Michael Kolbf2055602011-04-09 17:20:03 -0700156 }
157 }
158
Michael Kolb2814a362011-05-19 15:49:41 -0700159 private void onCloseTab(Tab tab) {
160 if (tab != null) {
Michael Kolb308b3012011-07-13 14:50:56 -0700161 if (tab == mUiController.getCurrentTab()) {
162 mUiController.closeCurrentTab();
163 } else {
164 mUiController.closeTab(tab);
165 }
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
Michael Kolba3194d02011-09-07 11:23:51 -0700171 final Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
Michael Kolb2814a362011-05-19 15:49:41 -0700172 false, false, false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700173 if (tab != null) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700174 mUiController.setBlockEvents(true);
Michael Kolbc831b632011-05-11 09:30:34 -0700175 final int tix = mUi.mTabControl.getTabPosition(tab);
Michael Kolba3194d02011-09-07 11:23:51 -0700176 mScroller.setOnLayoutListener(new OnLayoutListener() {
177
Michael Kolb2814a362011-05-19 15:49:41 -0700178 @Override
Michael Kolba3194d02011-09-07 11:23:51 -0700179 public void onLayout(int l, int t, int r, int b) {
180 mUi.hideNavScreen(tix, true);
181 switchToTab(tab);
Michael Kolba4261fd2011-05-05 11:27:37 -0700182 }
Michael Kolbc3af0672011-08-09 10:24:41 -0700183 });
Michael Kolba3194d02011-09-07 11:23:51 -0700184 mScroller.handleDataChanged(tix);
185 mUiController.setBlockEvents(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700186 }
187 }
188
Michael Kolba3194d02011-09-07 11:23:51 -0700189 private void switchToTab(Tab tab) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700190 if (tab != mUi.getActiveTab()) {
191 mUiController.setActiveTab(tab);
192 }
193 }
194
Michael Kolba3194d02011-09-07 11:23:51 -0700195 protected void close(int position) {
196 close(position, true);
Michael Kolbf2055602011-04-09 17:20:03 -0700197 }
198
Michael Kolba3194d02011-09-07 11:23:51 -0700199 protected void close(int position, boolean animate) {
200 mUi.hideNavScreen(position, animate);
201 }
202
203 protected NavTabView getTabView(int pos) {
204 return mScroller.getTabView(pos);
Michael Kolbf2055602011-04-09 17:20:03 -0700205 }
206
Michael Kolbf2055602011-04-09 17:20:03 -0700207 class TabAdapter extends BaseAdapter {
208
209 Context context;
210 TabControl tabControl;
211
212 public TabAdapter(Context ctx, TabControl tc) {
213 context = ctx;
214 tabControl = tc;
215 }
216
Michael Kolbf2055602011-04-09 17:20:03 -0700217 @Override
218 public int getCount() {
219 return tabControl.getTabCount();
220 }
221
222 @Override
223 public Tab getItem(int position) {
224 return tabControl.getTab(position);
225 }
226
227 public long getItemId(int position) {
228 return position;
229 }
230
231 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700232 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700233 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700234 final Tab tab = getItem(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700235 tabview.setWebView(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700236 mTabViews.put(tab, tabview.mImage);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700237 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700238 @Override
239 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700240 if (tabview.isClose(v)) {
Michael Kolbe76f7042011-07-27 15:16:32 -0700241 mScroller.animateOut(tabview);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700242 } else if (tabview.isTitle(v)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700243 switchToTab(tab);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700244 mUi.getTitleBar().setSkipTitleBarAnimations(true);
Michael Kolba3194d02011-09-07 11:23:51 -0700245 close(position, false);
Michael Kolb1f9b3562012-04-24 14:38:34 -0700246 mUi.editUrl(false, true);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700247 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700248 } else if (tabview.isWebView(v)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700249 close(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700250 }
251 }
252 });
253 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700254 }
Michael Kolb2814a362011-05-19 15:49:41 -0700255
Michael Kolbf2055602011-04-09 17:20:03 -0700256 }
257
John Reck8ee633f2011-08-09 16:00:35 -0700258 @Override
259 public void onThumbnailUpdated(Tab t) {
260 View v = mTabViews.get(t);
261 if (v != null) {
262 v.invalidate();
John Reck8ee633f2011-08-09 16:00:35 -0700263 }
264 }
265
Michael Kolbf2055602011-04-09 17:20:03 -0700266}