blob: 76d0703f7d9ca28b932f0540b759361993699c91 [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;
Dave Tharp3e2896f2015-05-20 15:25:07 -070036import android.widget.Toast;
Michael Kolbf2055602011-04-09 17:20:03 -070037
Bijan Amirzada41242f22014-03-21 12:12:18 -070038import com.android.browser.NavTabScroller.OnRemoveListener;
Dave Tharp3e2896f2015-05-20 15:25:07 -070039import com.android.browser.mdm.IncognitoRestriction;
John Reck8ee633f2011-08-09 16:00:35 -070040
41import java.util.HashMap;
Michael Kolbdcd81d32011-07-22 10:30:49 -070042
Michael Kolb017ffab2011-07-11 15:26:47 -070043public class NavScreen extends RelativeLayout
Pankaj Garg79878492015-04-01 14:48:21 -070044 implements OnClickListener, OnMenuItemClickListener {
Michael Kolbf2055602011-04-09 17:20:03 -070045
Michael Kolbc3af0672011-08-09 10:24:41 -070046
Enrico Ros80c045a2014-11-24 15:23:12 -080047 private final UiController mUiController;
48 private final PhoneUi mUi;
49 private final Activity mActivity;
Michael Kolbf2055602011-04-09 17:20:03 -070050
Enrico Ros80c045a2014-11-24 15:23:12 -080051 private View mToolbarLayout;
52 private ImageButton mMore;
53 private ImageButton mNewTab;
54 private ImageButton mNewIncognitoTab;
Michael Kolb2814a362011-05-19 15:49:41 -070055
Enrico Ros80c045a2014-11-24 15:23:12 -080056 private NavTabScroller mScroller;
57 private TabAdapter mAdapter;
58 private int mOrientation;
59 private HashMap<Tab, View> mTabViews;
Michael Kolbf2055602011-04-09 17:20:03 -070060
61 public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
62 super(activity);
63 mActivity = activity;
64 mUiController = ctl;
65 mUi = ui;
Michael Kolba4261fd2011-05-05 11:27:37 -070066 mOrientation = activity.getResources().getConfiguration().orientation;
Michael Kolbf2055602011-04-09 17:20:03 -070067 init();
68 }
69
Enrico Ros80c045a2014-11-24 15:23:12 -080070 protected void showPopupMenu() {
Pankaj Garg49b79252014-11-07 17:33:41 -080071 if (mUiController instanceof Controller) {
72 PopupMenu popup = new PopupMenu(getContext(), mMore);
73 Menu menu = popup.getMenu();
74
75 Controller controller = (Controller) mUiController;
76 controller.onPrepareOptionsMenu(menu);
77 }
Michael Kolb017ffab2011-07-11 15:26:47 -070078 }
79
Enrico Ros80c045a2014-11-24 15:23:12 -080080 public NavTabScroller getScroller() {
81 return mScroller;
82 }
83
84 public ObjectAnimator createToolbarInAnimator() {
85 return ObjectAnimator.ofFloat(mToolbarLayout, "translationY",
86 -getResources().getDimensionPixelSize(R.dimen.toolbar_height), 0f);
87 }
88
Michael Kolb017ffab2011-07-11 15:26:47 -070089 @Override
90 public boolean onMenuItemClick(MenuItem item) {
91 return mUiController.onOptionsItemSelected(item);
Michael Kolbf2055602011-04-09 17:20:03 -070092 }
93
Michael Kolba4261fd2011-05-05 11:27:37 -070094 @Override
95 protected void onConfigurationChanged(Configuration newconfig) {
96 if (newconfig.orientation != mOrientation) {
Michael Kolb9829b432011-06-04 13:29:00 -070097 mOrientation = newconfig.orientation;
Enrico Ros80c045a2014-11-24 15:23:12 -080098
99 // the only thing we need to change is the orientation. see nav_screen.xml
100 //final int prevScroll = mScroller.getScrollValue();
101 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
102 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
103 mScroller.setScrollOnNextLayout();
Michael Kolba4261fd2011-05-05 11:27:37 -0700104 }
105 }
106
John Reck9c5004e2011-10-07 16:00:12 -0700107 public void refreshAdapter() {
John Reck1adf0302011-10-11 10:58:12 -0700108 mScroller.handleDataChanged(
109 mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
John Reck9c5004e2011-10-07 16:00:12 -0700110 }
111
Dave Tharp3e2896f2015-05-20 15:25:07 -0700112
113
Michael Kolbf2055602011-04-09 17:20:03 -0700114 private void init() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800115 LayoutInflater.from(getContext()).inflate(R.layout.nav_screen, this);
116 setContentDescription(getContext().getResources().getString(
Michael Kolb30adae62011-07-29 13:37:05 -0700117 R.string.accessibility_transition_navscreen));
Enrico Ros1f5a0952014-11-18 20:15:48 -0800118 mToolbarLayout = findViewById(R.id.nav_toolbar_animate);
119 mNewIncognitoTab = (ImageButton) findViewById(R.id.newincognitotab);
Dave Tharp3e2896f2015-05-20 15:25:07 -0700120 IncognitoRestriction.getInstance().registerButton(mNewIncognitoTab);
Michael Kolbf2055602011-04-09 17:20:03 -0700121 mNewTab = (ImageButton) findViewById(R.id.newtab);
Michael Kolbf2055602011-04-09 17:20:03 -0700122 mMore = (ImageButton) findViewById(R.id.more);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800123 mNewIncognitoTab.setOnClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -0700124 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());
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800129 mAdapter = new TabAdapter(getContext(), 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 });
Enrico Ros80c045a2014-11-24 15:23:12 -0800141 boolean needsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
142 if (!needsMenu) {
Michael Kolb3ca12752011-07-20 13:52:25 -0700143 mMore.setVisibility(View.GONE);
144 }
Michael Kolbf2055602011-04-09 17:20:03 -0700145 }
146
147 @Override
148 public void onClick(View v) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800149 if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700150 openNewTab();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800151 } else if (mNewIncognitoTab == v) {
Dave Tharp3e2896f2015-05-20 15:25:07 -0700152 if (IncognitoRestriction.getInstance().isEnabled()) {
153 Toast.makeText(getContext(), R.string.mdm_managed_alert, Toast.LENGTH_SHORT).show();
154 } else {
155 openNewIncognitoTab();
156 }
Michael Kolbf2055602011-04-09 17:20:03 -0700157 } else if (mMore == v) {
Enrico Ros80c045a2014-11-24 15:23:12 -0800158 showPopupMenu();
Michael Kolbf2055602011-04-09 17:20:03 -0700159 }
160 }
161
Michael Kolb2814a362011-05-19 15:49:41 -0700162 private void onCloseTab(Tab tab) {
163 if (tab != null) {
Michael Kolb308b3012011-07-13 14:50:56 -0700164 if (tab == mUiController.getCurrentTab()) {
165 mUiController.closeCurrentTab();
166 } else {
167 mUiController.closeTab(tab);
168 }
Axesh R. Ajmera069f8002014-11-04 10:02:39 -0800169 mTabViews.remove(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700170 }
171 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700172
Enrico Ros1f5a0952014-11-18 20:15:48 -0800173 private void openNewIncognitoTab() {
174 final Tab tab = mUiController.openIncognitoTab();
175 if (tab != null) {
176 mUiController.setBlockEvents(true);
177 final int tix = mUi.mTabControl.getTabPosition(tab);
178 switchToTab(tab);
179 mUi.hideNavScreen(tix, true);
180 mScroller.handleDataChanged(tix);
181 mUiController.setBlockEvents(false);
182 }
183 }
184
Michael Kolb2814a362011-05-19 15:49:41 -0700185 private void openNewTab() {
186 // need to call openTab explicitely with setactive false
Michael Kolba3194d02011-09-07 11:23:51 -0700187 final Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
Michael Kolb2814a362011-05-19 15:49:41 -0700188 false, false, false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700189 if (tab != null) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700190 mUiController.setBlockEvents(true);
Michael Kolbc831b632011-05-11 09:30:34 -0700191 final int tix = mUi.mTabControl.getTabPosition(tab);
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700192 switchToTab(tab);
193 mUi.hideNavScreen(tix, true);
Michael Kolba3194d02011-09-07 11:23:51 -0700194 mScroller.handleDataChanged(tix);
195 mUiController.setBlockEvents(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700196 }
197 }
198
Michael Kolba3194d02011-09-07 11:23:51 -0700199 private void switchToTab(Tab tab) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700200 if (tab != mUi.getActiveTab()) {
201 mUiController.setActiveTab(tab);
202 }
203 }
204
Michael Kolba3194d02011-09-07 11:23:51 -0700205 protected void close(int position) {
206 close(position, true);
Michael Kolbf2055602011-04-09 17:20:03 -0700207 }
208
Michael Kolba3194d02011-09-07 11:23:51 -0700209 protected void close(int position, boolean animate) {
210 mUi.hideNavScreen(position, animate);
211 }
212
213 protected NavTabView getTabView(int pos) {
214 return mScroller.getTabView(pos);
Michael Kolbf2055602011-04-09 17:20:03 -0700215 }
216
Michael Kolbf2055602011-04-09 17:20:03 -0700217 class TabAdapter extends BaseAdapter {
218
219 Context context;
220 TabControl tabControl;
221
222 public TabAdapter(Context ctx, TabControl tc) {
223 context = ctx;
224 tabControl = tc;
225 }
226
Michael Kolbf2055602011-04-09 17:20:03 -0700227 @Override
228 public int getCount() {
229 return tabControl.getTabCount();
230 }
231
232 @Override
233 public Tab getItem(int position) {
234 return tabControl.getTab(position);
235 }
236
237 public long getItemId(int position) {
238 return position;
239 }
240
241 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700242 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700243 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700244 final Tab tab = getItem(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700245 tabview.setWebView(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700246 mTabViews.put(tab, tabview.mImage);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700247 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700248 @Override
249 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700250 if (tabview.isClose(v)) {
Michael Kolbe76f7042011-07-27 15:16:32 -0700251 mScroller.animateOut(tabview);
Axesh R. Ajmera069f8002014-11-04 10:02:39 -0800252 mTabViews.remove(tab);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700253 } else if (tabview.isTitle(v)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700254 switchToTab(tab);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700255 mUi.getTitleBar().setSkipTitleBarAnimations(true);
Michael Kolba3194d02011-09-07 11:23:51 -0700256 close(position, false);
Michael Kolb1f9b3562012-04-24 14:38:34 -0700257 mUi.editUrl(false, true);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700258 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700259 } else if (tabview.isWebView(v)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700260 close(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700261 }
262 }
263 });
264 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700265 }
Michael Kolb2814a362011-05-19 15:49:41 -0700266
Michael Kolbf2055602011-04-09 17:20:03 -0700267 }
Michael Kolbf2055602011-04-09 17:20:03 -0700268}