blob: 9e32f49c48fce771c3970730f78ee280f8dfde48 [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
Enrico Ros80c045a2014-11-24 15:23:12 -080019import android.animation.ObjectAnimator;
Michael Kolbf2055602011-04-09 17:20:03 -070020import android.app.Activity;
21import android.content.Context;
Michael Kolba4261fd2011-05-05 11:27:37 -070022import android.content.res.Configuration;
Michael Kolbf2055602011-04-09 17:20:03 -070023import android.view.LayoutInflater;
24import android.view.Menu;
25import android.view.MenuItem;
26import android.view.View;
27import android.view.View.OnClickListener;
Michael Kolbdcd81d32011-07-22 10:30:49 -070028import android.view.ViewConfiguration;
Michael Kolbf2055602011-04-09 17:20:03 -070029import android.view.ViewGroup;
Michael Kolbf2055602011-04-09 17:20:03 -070030import android.widget.BaseAdapter;
Michael Kolbf2055602011-04-09 17:20:03 -070031import android.widget.ImageButton;
Michael Kolb9829b432011-06-04 13:29:00 -070032import android.widget.LinearLayout;
Michael Kolb017ffab2011-07-11 15:26:47 -070033import android.widget.PopupMenu;
34import android.widget.PopupMenu.OnMenuItemClickListener;
Michael Kolb2814a362011-05-19 15:49:41 -070035import android.widget.RelativeLayout;
Michael Kolbf2055602011-04-09 17:20:03 -070036
Bijan Amirzada41242f22014-03-21 12:12:18 -070037import com.android.browser.NavTabScroller.OnRemoveListener;
John Reck8ee633f2011-08-09 16:00:35 -070038
39import java.util.HashMap;
Michael Kolbdcd81d32011-07-22 10:30:49 -070040
Michael Kolb017ffab2011-07-11 15:26:47 -070041public class NavScreen extends RelativeLayout
Pankaj Garg79878492015-04-01 14:48:21 -070042 implements OnClickListener, OnMenuItemClickListener {
Michael Kolbf2055602011-04-09 17:20:03 -070043
Michael Kolbc3af0672011-08-09 10:24:41 -070044
Enrico Ros80c045a2014-11-24 15:23:12 -080045 private final UiController mUiController;
46 private final PhoneUi mUi;
47 private final Activity mActivity;
Michael Kolbf2055602011-04-09 17:20:03 -070048
Enrico Ros80c045a2014-11-24 15:23:12 -080049 private View mToolbarLayout;
50 private ImageButton mMore;
51 private ImageButton mNewTab;
52 private ImageButton mNewIncognitoTab;
Michael Kolb2814a362011-05-19 15:49:41 -070053
Enrico Ros80c045a2014-11-24 15:23:12 -080054 private NavTabScroller mScroller;
55 private TabAdapter mAdapter;
56 private int mOrientation;
57 private HashMap<Tab, View> mTabViews;
Michael Kolbf2055602011-04-09 17:20:03 -070058
59 public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
60 super(activity);
61 mActivity = activity;
62 mUiController = ctl;
63 mUi = ui;
Michael Kolba4261fd2011-05-05 11:27:37 -070064 mOrientation = activity.getResources().getConfiguration().orientation;
Michael Kolbf2055602011-04-09 17:20:03 -070065 init();
66 }
67
Enrico Ros80c045a2014-11-24 15:23:12 -080068 protected void showPopupMenu() {
Pankaj Garg49b79252014-11-07 17:33:41 -080069 if (mUiController instanceof Controller) {
70 PopupMenu popup = new PopupMenu(getContext(), mMore);
71 Menu menu = popup.getMenu();
72
73 Controller controller = (Controller) mUiController;
74 controller.onPrepareOptionsMenu(menu);
75 }
Michael Kolb017ffab2011-07-11 15:26:47 -070076 }
77
Enrico Ros80c045a2014-11-24 15:23:12 -080078 public NavTabScroller getScroller() {
79 return mScroller;
80 }
81
82 public ObjectAnimator createToolbarInAnimator() {
83 return ObjectAnimator.ofFloat(mToolbarLayout, "translationY",
84 -getResources().getDimensionPixelSize(R.dimen.toolbar_height), 0f);
85 }
86
Michael Kolb017ffab2011-07-11 15:26:47 -070087 @Override
88 public boolean onMenuItemClick(MenuItem item) {
89 return mUiController.onOptionsItemSelected(item);
Michael Kolbf2055602011-04-09 17:20:03 -070090 }
91
Michael Kolba4261fd2011-05-05 11:27:37 -070092 @Override
93 protected void onConfigurationChanged(Configuration newconfig) {
94 if (newconfig.orientation != mOrientation) {
Michael Kolb9829b432011-06-04 13:29:00 -070095 mOrientation = newconfig.orientation;
Enrico Ros80c045a2014-11-24 15:23:12 -080096
97 // the only thing we need to change is the orientation. see nav_screen.xml
98 //final int prevScroll = mScroller.getScrollValue();
99 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
100 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
101 mScroller.setScrollOnNextLayout();
Michael Kolba4261fd2011-05-05 11:27:37 -0700102 }
103 }
104
John Reck9c5004e2011-10-07 16:00:12 -0700105 public void refreshAdapter() {
John Reck1adf0302011-10-11 10:58:12 -0700106 mScroller.handleDataChanged(
107 mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
John Reck9c5004e2011-10-07 16:00:12 -0700108 }
109
Michael Kolbf2055602011-04-09 17:20:03 -0700110 private void init() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800111 LayoutInflater.from(getContext()).inflate(R.layout.nav_screen, this);
112 setContentDescription(getContext().getResources().getString(
Michael Kolb30adae62011-07-29 13:37:05 -0700113 R.string.accessibility_transition_navscreen));
Enrico Ros1f5a0952014-11-18 20:15:48 -0800114 mToolbarLayout = findViewById(R.id.nav_toolbar_animate);
115 mNewIncognitoTab = (ImageButton) findViewById(R.id.newincognitotab);
Michael Kolbf2055602011-04-09 17:20:03 -0700116 mNewTab = (ImageButton) findViewById(R.id.newtab);
Michael Kolbf2055602011-04-09 17:20:03 -0700117 mMore = (ImageButton) findViewById(R.id.more);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800118 mNewIncognitoTab.setOnClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -0700119 mNewTab.setOnClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -0700120 mMore.setOnClickListener(this);
Michael Kolba3194d02011-09-07 11:23:51 -0700121 mScroller = (NavTabScroller) findViewById(R.id.scroller);
John Reck8ee633f2011-08-09 16:00:35 -0700122 TabControl tc = mUiController.getTabControl();
123 mTabViews = new HashMap<Tab, View>(tc.getTabCount());
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800124 mAdapter = new TabAdapter(getContext(), tc);
Michael Kolb9829b432011-06-04 13:29:00 -0700125 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
126 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700127 // update state for active tab
Michael Kolbc3af0672011-08-09 10:24:41 -0700128 mScroller.setAdapter(mAdapter,
129 mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbdcd81d32011-07-22 10:30:49 -0700130 mScroller.setOnRemoveListener(new OnRemoveListener() {
131 public void onRemovePosition(int pos) {
132 Tab tab = mAdapter.getItem(pos);
133 onCloseTab(tab);
134 }
135 });
Enrico Ros80c045a2014-11-24 15:23:12 -0800136 boolean needsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
137 if (!needsMenu) {
Michael Kolb3ca12752011-07-20 13:52:25 -0700138 mMore.setVisibility(View.GONE);
139 }
Michael Kolbf2055602011-04-09 17:20:03 -0700140 }
141
142 @Override
143 public void onClick(View v) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800144 if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700145 openNewTab();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800146 } else if (mNewIncognitoTab == v) {
147 openNewIncognitoTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700148 } else if (mMore == v) {
Enrico Ros80c045a2014-11-24 15:23:12 -0800149 showPopupMenu();
Michael Kolbf2055602011-04-09 17:20:03 -0700150 }
151 }
152
Michael Kolb2814a362011-05-19 15:49:41 -0700153 private void onCloseTab(Tab tab) {
154 if (tab != null) {
Michael Kolb308b3012011-07-13 14:50:56 -0700155 if (tab == mUiController.getCurrentTab()) {
156 mUiController.closeCurrentTab();
157 } else {
158 mUiController.closeTab(tab);
159 }
Axesh R. Ajmera069f8002014-11-04 10:02:39 -0800160 mTabViews.remove(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700161 }
162 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700163
Enrico Ros1f5a0952014-11-18 20:15:48 -0800164 private void openNewIncognitoTab() {
165 final Tab tab = mUiController.openIncognitoTab();
166 if (tab != null) {
167 mUiController.setBlockEvents(true);
168 final int tix = mUi.mTabControl.getTabPosition(tab);
169 switchToTab(tab);
170 mUi.hideNavScreen(tix, true);
171 mScroller.handleDataChanged(tix);
172 mUiController.setBlockEvents(false);
173 }
174 }
175
Michael Kolb2814a362011-05-19 15:49:41 -0700176 private void openNewTab() {
177 // need to call openTab explicitely with setactive false
Michael Kolba3194d02011-09-07 11:23:51 -0700178 final Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
Michael Kolb2814a362011-05-19 15:49:41 -0700179 false, false, false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700180 if (tab != null) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700181 mUiController.setBlockEvents(true);
Michael Kolbc831b632011-05-11 09:30:34 -0700182 final int tix = mUi.mTabControl.getTabPosition(tab);
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700183 switchToTab(tab);
184 mUi.hideNavScreen(tix, true);
Michael Kolba3194d02011-09-07 11:23:51 -0700185 mScroller.handleDataChanged(tix);
186 mUiController.setBlockEvents(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700187 }
188 }
189
Michael Kolba3194d02011-09-07 11:23:51 -0700190 private void switchToTab(Tab tab) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700191 if (tab != mUi.getActiveTab()) {
192 mUiController.setActiveTab(tab);
193 }
194 }
195
Michael Kolba3194d02011-09-07 11:23:51 -0700196 protected void close(int position) {
197 close(position, true);
Michael Kolbf2055602011-04-09 17:20:03 -0700198 }
199
Michael Kolba3194d02011-09-07 11:23:51 -0700200 protected void close(int position, boolean animate) {
201 mUi.hideNavScreen(position, animate);
202 }
203
204 protected NavTabView getTabView(int pos) {
205 return mScroller.getTabView(pos);
Michael Kolbf2055602011-04-09 17:20:03 -0700206 }
207
Michael Kolbf2055602011-04-09 17:20:03 -0700208 class TabAdapter extends BaseAdapter {
209
210 Context context;
211 TabControl tabControl;
212
213 public TabAdapter(Context ctx, TabControl tc) {
214 context = ctx;
215 tabControl = tc;
216 }
217
Michael Kolbf2055602011-04-09 17:20:03 -0700218 @Override
219 public int getCount() {
220 return tabControl.getTabCount();
221 }
222
223 @Override
224 public Tab getItem(int position) {
225 return tabControl.getTab(position);
226 }
227
228 public long getItemId(int position) {
229 return position;
230 }
231
232 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700233 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700234 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700235 final Tab tab = getItem(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700236 tabview.setWebView(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700237 mTabViews.put(tab, tabview.mImage);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700238 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700239 @Override
240 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700241 if (tabview.isClose(v)) {
Michael Kolbe76f7042011-07-27 15:16:32 -0700242 mScroller.animateOut(tabview);
Axesh R. Ajmera069f8002014-11-04 10:02:39 -0800243 mTabViews.remove(tab);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700244 } else if (tabview.isTitle(v)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700245 switchToTab(tab);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700246 mUi.getTitleBar().setSkipTitleBarAnimations(true);
Michael Kolba3194d02011-09-07 11:23:51 -0700247 close(position, false);
Michael Kolb1f9b3562012-04-24 14:38:34 -0700248 mUi.editUrl(false, true);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700249 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700250 } else if (tabview.isWebView(v)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700251 close(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700252 }
253 }
254 });
255 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700256 }
Michael Kolb2814a362011-05-19 15:49:41 -0700257
Michael Kolbf2055602011-04-09 17:20:03 -0700258 }
Michael Kolbf2055602011-04-09 17:20:03 -0700259}