blob: d77d31c1d385fbf456c95b4c3fa00782b55d4f0c [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;
Dave Tharp3e2896f2015-05-20 15:25:07 -070038import com.android.browser.mdm.IncognitoRestriction;
John Reck8ee633f2011-08-09 16:00:35 -070039
40import java.util.HashMap;
Michael Kolbdcd81d32011-07-22 10:30:49 -070041
Michael Kolb017ffab2011-07-11 15:26:47 -070042public class NavScreen extends RelativeLayout
Pankaj Garg79878492015-04-01 14:48:21 -070043 implements OnClickListener, OnMenuItemClickListener {
Michael Kolbf2055602011-04-09 17:20:03 -070044
Michael Kolbc3af0672011-08-09 10:24:41 -070045
Enrico Ros80c045a2014-11-24 15:23:12 -080046 private final UiController mUiController;
47 private final PhoneUi mUi;
48 private final Activity mActivity;
Michael Kolbf2055602011-04-09 17:20:03 -070049
Enrico Ros80c045a2014-11-24 15:23:12 -080050 private View mToolbarLayout;
51 private ImageButton mMore;
52 private ImageButton mNewTab;
53 private ImageButton mNewIncognitoTab;
Michael Kolb2814a362011-05-19 15:49:41 -070054
Enrico Ros80c045a2014-11-24 15:23:12 -080055 private NavTabScroller mScroller;
56 private TabAdapter mAdapter;
57 private int mOrientation;
58 private HashMap<Tab, View> mTabViews;
Michael Kolbf2055602011-04-09 17:20:03 -070059
60 public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
61 super(activity);
62 mActivity = activity;
63 mUiController = ctl;
64 mUi = ui;
Michael Kolba4261fd2011-05-05 11:27:37 -070065 mOrientation = activity.getResources().getConfiguration().orientation;
Michael Kolbf2055602011-04-09 17:20:03 -070066 init();
67 }
68
Enrico Ros80c045a2014-11-24 15:23:12 -080069 protected void showPopupMenu() {
Pankaj Garg49b79252014-11-07 17:33:41 -080070 if (mUiController instanceof Controller) {
71 PopupMenu popup = new PopupMenu(getContext(), mMore);
72 Menu menu = popup.getMenu();
73
74 Controller controller = (Controller) mUiController;
75 controller.onPrepareOptionsMenu(menu);
76 }
Michael Kolb017ffab2011-07-11 15:26:47 -070077 }
78
Enrico Ros80c045a2014-11-24 15:23:12 -080079 public NavTabScroller getScroller() {
80 return mScroller;
81 }
82
83 public ObjectAnimator createToolbarInAnimator() {
84 return ObjectAnimator.ofFloat(mToolbarLayout, "translationY",
85 -getResources().getDimensionPixelSize(R.dimen.toolbar_height), 0f);
86 }
87
Michael Kolb017ffab2011-07-11 15:26:47 -070088 @Override
89 public boolean onMenuItemClick(MenuItem item) {
90 return mUiController.onOptionsItemSelected(item);
Michael Kolbf2055602011-04-09 17:20:03 -070091 }
92
Michael Kolba4261fd2011-05-05 11:27:37 -070093 @Override
94 protected void onConfigurationChanged(Configuration newconfig) {
95 if (newconfig.orientation != mOrientation) {
Michael Kolb9829b432011-06-04 13:29:00 -070096 mOrientation = newconfig.orientation;
Enrico Ros80c045a2014-11-24 15:23:12 -080097
98 // the only thing we need to change is the orientation. see nav_screen.xml
99 //final int prevScroll = mScroller.getScrollValue();
100 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
101 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
102 mScroller.setScrollOnNextLayout();
Michael Kolba4261fd2011-05-05 11:27:37 -0700103 }
104 }
105
John Reck9c5004e2011-10-07 16:00:12 -0700106 public void refreshAdapter() {
John Reck1adf0302011-10-11 10:58:12 -0700107 mScroller.handleDataChanged(
108 mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
John Reck9c5004e2011-10-07 16:00:12 -0700109 }
110
Dave Tharp3e2896f2015-05-20 15:25:07 -0700111
112
Michael Kolbf2055602011-04-09 17:20:03 -0700113 private void init() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800114 LayoutInflater.from(getContext()).inflate(R.layout.nav_screen, this);
115 setContentDescription(getContext().getResources().getString(
Michael Kolb30adae62011-07-29 13:37:05 -0700116 R.string.accessibility_transition_navscreen));
Enrico Ros1f5a0952014-11-18 20:15:48 -0800117 mToolbarLayout = findViewById(R.id.nav_toolbar_animate);
118 mNewIncognitoTab = (ImageButton) findViewById(R.id.newincognitotab);
Dave Tharpdcad7b02015-05-28 07:38:32 -0700119 IncognitoRestriction.getInstance().registerControl(mNewIncognitoTab);
Michael Kolbf2055602011-04-09 17:20:03 -0700120 mNewTab = (ImageButton) findViewById(R.id.newtab);
Michael Kolbf2055602011-04-09 17:20:03 -0700121 mMore = (ImageButton) findViewById(R.id.more);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800122 mNewIncognitoTab.setOnClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -0700123 mNewTab.setOnClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -0700124 mMore.setOnClickListener(this);
Michael Kolba3194d02011-09-07 11:23:51 -0700125 mScroller = (NavTabScroller) findViewById(R.id.scroller);
John Reck8ee633f2011-08-09 16:00:35 -0700126 TabControl tc = mUiController.getTabControl();
127 mTabViews = new HashMap<Tab, View>(tc.getTabCount());
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800128 mAdapter = new TabAdapter(getContext(), tc);
Michael Kolb9829b432011-06-04 13:29:00 -0700129 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
130 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700131 // update state for active tab
Michael Kolbc3af0672011-08-09 10:24:41 -0700132 mScroller.setAdapter(mAdapter,
133 mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbdcd81d32011-07-22 10:30:49 -0700134 mScroller.setOnRemoveListener(new OnRemoveListener() {
135 public void onRemovePosition(int pos) {
136 Tab tab = mAdapter.getItem(pos);
137 onCloseTab(tab);
138 }
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) {
Dave Tharpdcad7b02015-05-28 07:38:32 -0700147 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) {
jrizzoli5bba72a2015-08-28 22:03:53 +0200241 if (tabview.isTitle(v)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700242 switchToTab(tab);
Michael Kolba3194d02011-09-07 11:23:51 -0700243 close(position, false);
Michael Kolb1f9b3562012-04-24 14:38:34 -0700244 mUi.editUrl(false, true);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700245 } else if (tabview.isWebView(v)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700246 close(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700247 }
248 }
249 });
250 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700251 }
Michael Kolb2814a362011-05-19 15:49:41 -0700252
Michael Kolbf2055602011-04-09 17:20:03 -0700253 }
Michael Kolbf2055602011-04-09 17:20:03 -0700254}