blob: d7f389a290375f00c3de2fe5a45304db6503f42e [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;
38import com.android.browser.TabControl.OnThumbnailUpdatedListener;
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
John Reck8ee633f2011-08-09 16:00:35 -070043 implements OnClickListener, OnMenuItemClickListener, OnThumbnailUpdatedListener {
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
Michael Kolbf2055602011-04-09 17:20:03 -0700111 private void init() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800112 LayoutInflater.from(getContext()).inflate(R.layout.nav_screen, this);
113 setContentDescription(getContext().getResources().getString(
Michael Kolb30adae62011-07-29 13:37:05 -0700114 R.string.accessibility_transition_navscreen));
Enrico Ros1f5a0952014-11-18 20:15:48 -0800115 mToolbarLayout = findViewById(R.id.nav_toolbar_animate);
116 mNewIncognitoTab = (ImageButton) findViewById(R.id.newincognitotab);
Michael Kolbf2055602011-04-09 17:20:03 -0700117 mNewTab = (ImageButton) findViewById(R.id.newtab);
Michael Kolbf2055602011-04-09 17:20:03 -0700118 mMore = (ImageButton) findViewById(R.id.more);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800119 mNewIncognitoTab.setOnClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -0700120 mNewTab.setOnClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -0700121 mMore.setOnClickListener(this);
Michael Kolba3194d02011-09-07 11:23:51 -0700122 mScroller = (NavTabScroller) findViewById(R.id.scroller);
John Reck8ee633f2011-08-09 16:00:35 -0700123 TabControl tc = mUiController.getTabControl();
124 mTabViews = new HashMap<Tab, View>(tc.getTabCount());
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800125 mAdapter = new TabAdapter(getContext(), tc);
Michael Kolb9829b432011-06-04 13:29:00 -0700126 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
127 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700128 // update state for active tab
Michael Kolbc3af0672011-08-09 10:24:41 -0700129 mScroller.setAdapter(mAdapter,
130 mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbdcd81d32011-07-22 10:30:49 -0700131 mScroller.setOnRemoveListener(new OnRemoveListener() {
132 public void onRemovePosition(int pos) {
133 Tab tab = mAdapter.getItem(pos);
134 onCloseTab(tab);
135 }
136 });
Enrico Ros80c045a2014-11-24 15:23:12 -0800137 boolean needsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
138 if (!needsMenu) {
Michael Kolb3ca12752011-07-20 13:52:25 -0700139 mMore.setVisibility(View.GONE);
140 }
Michael Kolbf2055602011-04-09 17:20:03 -0700141 }
142
143 @Override
144 public void onClick(View v) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800145 if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700146 openNewTab();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800147 } else if (mNewIncognitoTab == v) {
148 openNewIncognitoTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700149 } else if (mMore == v) {
Enrico Ros80c045a2014-11-24 15:23:12 -0800150 showPopupMenu();
Michael Kolbf2055602011-04-09 17:20:03 -0700151 }
152 }
153
Michael Kolb2814a362011-05-19 15:49:41 -0700154 private void onCloseTab(Tab tab) {
155 if (tab != null) {
Michael Kolb308b3012011-07-13 14:50:56 -0700156 if (tab == mUiController.getCurrentTab()) {
157 mUiController.closeCurrentTab();
158 } else {
159 mUiController.closeTab(tab);
160 }
Axesh R. Ajmera069f8002014-11-04 10:02:39 -0800161 mTabViews.remove(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700162 }
163 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700164
Enrico Ros1f5a0952014-11-18 20:15:48 -0800165 private void openNewIncognitoTab() {
166 final Tab tab = mUiController.openIncognitoTab();
167 if (tab != null) {
168 mUiController.setBlockEvents(true);
169 final int tix = mUi.mTabControl.getTabPosition(tab);
170 switchToTab(tab);
171 mUi.hideNavScreen(tix, true);
172 mScroller.handleDataChanged(tix);
173 mUiController.setBlockEvents(false);
174 }
175 }
176
Michael Kolb2814a362011-05-19 15:49:41 -0700177 private void openNewTab() {
178 // need to call openTab explicitely with setactive false
Michael Kolba3194d02011-09-07 11:23:51 -0700179 final Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
Michael Kolb2814a362011-05-19 15:49:41 -0700180 false, false, false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700181 if (tab != null) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700182 mUiController.setBlockEvents(true);
Michael Kolbc831b632011-05-11 09:30:34 -0700183 final int tix = mUi.mTabControl.getTabPosition(tab);
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700184 switchToTab(tab);
185 mUi.hideNavScreen(tix, true);
Michael Kolba3194d02011-09-07 11:23:51 -0700186 mScroller.handleDataChanged(tix);
187 mUiController.setBlockEvents(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700188 }
189 }
190
Michael Kolba3194d02011-09-07 11:23:51 -0700191 private void switchToTab(Tab tab) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700192 if (tab != mUi.getActiveTab()) {
193 mUiController.setActiveTab(tab);
194 }
195 }
196
Michael Kolba3194d02011-09-07 11:23:51 -0700197 protected void close(int position) {
198 close(position, true);
Michael Kolbf2055602011-04-09 17:20:03 -0700199 }
200
Michael Kolba3194d02011-09-07 11:23:51 -0700201 protected void close(int position, boolean animate) {
202 mUi.hideNavScreen(position, animate);
203 }
204
205 protected NavTabView getTabView(int pos) {
206 return mScroller.getTabView(pos);
Michael Kolbf2055602011-04-09 17:20:03 -0700207 }
208
Michael Kolbf2055602011-04-09 17:20:03 -0700209 class TabAdapter extends BaseAdapter {
210
211 Context context;
212 TabControl tabControl;
213
214 public TabAdapter(Context ctx, TabControl tc) {
215 context = ctx;
216 tabControl = tc;
217 }
218
Michael Kolbf2055602011-04-09 17:20:03 -0700219 @Override
220 public int getCount() {
221 return tabControl.getTabCount();
222 }
223
224 @Override
225 public Tab getItem(int position) {
226 return tabControl.getTab(position);
227 }
228
229 public long getItemId(int position) {
230 return position;
231 }
232
233 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700234 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700235 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700236 final Tab tab = getItem(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700237 tabview.setWebView(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700238 mTabViews.put(tab, tabview.mImage);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700239 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700240 @Override
241 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700242 if (tabview.isClose(v)) {
Michael Kolbe76f7042011-07-27 15:16:32 -0700243 mScroller.animateOut(tabview);
Axesh R. Ajmera069f8002014-11-04 10:02:39 -0800244 mTabViews.remove(tab);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700245 } else if (tabview.isTitle(v)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700246 switchToTab(tab);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700247 mUi.getTitleBar().setSkipTitleBarAnimations(true);
Michael Kolba3194d02011-09-07 11:23:51 -0700248 close(position, false);
Michael Kolb1f9b3562012-04-24 14:38:34 -0700249 mUi.editUrl(false, true);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700250 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700251 } else if (tabview.isWebView(v)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700252 close(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700253 }
254 }
255 });
256 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700257 }
Michael Kolb2814a362011-05-19 15:49:41 -0700258
Michael Kolbf2055602011-04-09 17:20:03 -0700259 }
260
John Reck8ee633f2011-08-09 16:00:35 -0700261 @Override
262 public void onThumbnailUpdated(Tab t) {
263 View v = mTabViews.get(t);
264 if (v != null) {
265 v.invalidate();
John Reck8ee633f2011-08-09 16:00:35 -0700266 }
267 }
268
Michael Kolbf2055602011-04-09 17:20:03 -0700269}