blob: 0962449fbf22a1b42133de731aaca0faba269160 [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;
Michael Kolbf2055602011-04-09 17:20:03 -070030import android.widget.ImageButton;
Michael Kolb9829b432011-06-04 13:29:00 -070031import android.widget.LinearLayout;
Michael Kolb017ffab2011-07-11 15:26:47 -070032import android.widget.PopupMenu;
33import android.widget.PopupMenu.OnMenuItemClickListener;
Michael Kolb2814a362011-05-19 15:49:41 -070034import android.widget.RelativeLayout;
Michael Kolbf2055602011-04-09 17:20:03 -070035
Bijan Amirzada41242f22014-03-21 12:12:18 -070036import com.android.browser.NavTabScroller.OnRemoveListener;
37import com.android.browser.TabControl.OnThumbnailUpdatedListener;
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
John Reck8ee633f2011-08-09 16:00:35 -070042 implements OnClickListener, OnMenuItemClickListener, OnThumbnailUpdatedListener {
Michael Kolbf2055602011-04-09 17:20:03 -070043
Michael Kolbc3af0672011-08-09 10:24:41 -070044
Michael Kolbf2055602011-04-09 17:20:03 -070045 UiController mUiController;
46 PhoneUi mUi;
Michael Kolbf2055602011-04-09 17:20:03 -070047 Activity mActivity;
48
Enrico Ros1f5a0952014-11-18 20:15:48 -080049 View mToolbarLayout;
Michael Kolbf2055602011-04-09 17:20:03 -070050 ImageButton mMore;
51 ImageButton mNewTab;
Enrico Ros1f5a0952014-11-18 20:15:48 -080052 ImageButton mNewIncognitoTab;
Michael Kolb2814a362011-05-19 15:49:41 -070053
Michael Kolba3194d02011-09-07 11:23:51 -070054 NavTabScroller mScroller;
Michael Kolbf2055602011-04-09 17:20:03 -070055 TabAdapter mAdapter;
Michael Kolba4261fd2011-05-05 11:27:37 -070056 int mOrientation;
Michael Kolb3ca12752011-07-20 13:52:25 -070057 boolean mNeedsMenu;
John Reck8ee633f2011-08-09 16:00:35 -070058 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
Michael Kolbfedb4922011-04-20 16:45:33 -070069 protected void showMenu() {
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
79 @Override
80 public boolean onMenuItemClick(MenuItem item) {
81 return mUiController.onOptionsItemSelected(item);
Michael Kolbf2055602011-04-09 17:20:03 -070082 }
83
Michael Kolba4261fd2011-05-05 11:27:37 -070084 @Override
85 protected void onConfigurationChanged(Configuration newconfig) {
86 if (newconfig.orientation != mOrientation) {
Michael Kolba3194d02011-09-07 11:23:51 -070087 int sv = mScroller.getScrollValue();
Michael Kolba4261fd2011-05-05 11:27:37 -070088 removeAllViews();
Michael Kolb9829b432011-06-04 13:29:00 -070089 mOrientation = newconfig.orientation;
Michael Kolba4261fd2011-05-05 11:27:37 -070090 init();
Michael Kolba3194d02011-09-07 11:23:51 -070091 mScroller.setScrollValue(sv);
Michael Kolb2814a362011-05-19 15:49:41 -070092 mAdapter.notifyDataSetChanged();
Michael Kolba4261fd2011-05-05 11:27:37 -070093 }
94 }
95
John Reck9c5004e2011-10-07 16:00:12 -070096 public void refreshAdapter() {
John Reck1adf0302011-10-11 10:58:12 -070097 mScroller.handleDataChanged(
98 mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
John Reck9c5004e2011-10-07 16:00:12 -070099 }
100
Michael Kolbf2055602011-04-09 17:20:03 -0700101 private void init() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800102 LayoutInflater.from(getContext()).inflate(R.layout.nav_screen, this);
103 setContentDescription(getContext().getResources().getString(
Michael Kolb30adae62011-07-29 13:37:05 -0700104 R.string.accessibility_transition_navscreen));
Enrico Ros1f5a0952014-11-18 20:15:48 -0800105 mToolbarLayout = findViewById(R.id.nav_toolbar_animate);
106 mNewIncognitoTab = (ImageButton) findViewById(R.id.newincognitotab);
Michael Kolbf2055602011-04-09 17:20:03 -0700107 mNewTab = (ImageButton) findViewById(R.id.newtab);
Michael Kolbf2055602011-04-09 17:20:03 -0700108 mMore = (ImageButton) findViewById(R.id.more);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800109 mNewIncognitoTab.setOnClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -0700110 mNewTab.setOnClickListener(this);
Michael Kolbf2055602011-04-09 17:20:03 -0700111 mMore.setOnClickListener(this);
Michael Kolba3194d02011-09-07 11:23:51 -0700112 mScroller = (NavTabScroller) findViewById(R.id.scroller);
John Reck8ee633f2011-08-09 16:00:35 -0700113 TabControl tc = mUiController.getTabControl();
114 mTabViews = new HashMap<Tab, View>(tc.getTabCount());
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800115 mAdapter = new TabAdapter(getContext(), tc);
Michael Kolb9829b432011-06-04 13:29:00 -0700116 mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
117 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
Michael Kolb2814a362011-05-19 15:49:41 -0700118 // update state for active tab
Michael Kolbc3af0672011-08-09 10:24:41 -0700119 mScroller.setAdapter(mAdapter,
120 mUiController.getTabControl().getTabPosition(mUi.getActiveTab()));
Michael Kolbdcd81d32011-07-22 10:30:49 -0700121 mScroller.setOnRemoveListener(new OnRemoveListener() {
122 public void onRemovePosition(int pos) {
123 Tab tab = mAdapter.getItem(pos);
124 onCloseTab(tab);
125 }
126 });
Michael Kolb3ca12752011-07-20 13:52:25 -0700127 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
128 if (!mNeedsMenu) {
129 mMore.setVisibility(View.GONE);
130 }
Michael Kolbf2055602011-04-09 17:20:03 -0700131 }
132
133 @Override
134 public void onClick(View v) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800135 if (mNewTab == v) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700136 openNewTab();
Enrico Ros1f5a0952014-11-18 20:15:48 -0800137 } else if (mNewIncognitoTab == v) {
138 openNewIncognitoTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700139 } else if (mMore == v) {
Michael Kolbfedb4922011-04-20 16:45:33 -0700140 showMenu();
Michael Kolbf2055602011-04-09 17:20:03 -0700141 }
142 }
143
Michael Kolb2814a362011-05-19 15:49:41 -0700144 private void onCloseTab(Tab tab) {
145 if (tab != null) {
Michael Kolb308b3012011-07-13 14:50:56 -0700146 if (tab == mUiController.getCurrentTab()) {
147 mUiController.closeCurrentTab();
148 } else {
149 mUiController.closeTab(tab);
150 }
Axesh R. Ajmera069f8002014-11-04 10:02:39 -0800151 mTabViews.remove(tab);
Michael Kolb2814a362011-05-19 15:49:41 -0700152 }
153 }
Michael Kolba4261fd2011-05-05 11:27:37 -0700154
Enrico Ros1f5a0952014-11-18 20:15:48 -0800155 private void openNewIncognitoTab() {
156 final Tab tab = mUiController.openIncognitoTab();
157 if (tab != null) {
158 mUiController.setBlockEvents(true);
159 final int tix = mUi.mTabControl.getTabPosition(tab);
160 switchToTab(tab);
161 mUi.hideNavScreen(tix, true);
162 mScroller.handleDataChanged(tix);
163 mUiController.setBlockEvents(false);
164 }
165 }
166
Michael Kolb2814a362011-05-19 15:49:41 -0700167 private void openNewTab() {
168 // need to call openTab explicitely with setactive false
Michael Kolba3194d02011-09-07 11:23:51 -0700169 final Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(),
Michael Kolb2814a362011-05-19 15:49:41 -0700170 false, false, false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700171 if (tab != null) {
Michael Kolbc3af0672011-08-09 10:24:41 -0700172 mUiController.setBlockEvents(true);
Michael Kolbc831b632011-05-11 09:30:34 -0700173 final int tix = mUi.mTabControl.getTabPosition(tab);
Vivek Sekharc549e3a2014-06-05 16:19:26 -0700174 switchToTab(tab);
175 mUi.hideNavScreen(tix, true);
Michael Kolba3194d02011-09-07 11:23:51 -0700176 mScroller.handleDataChanged(tix);
177 mUiController.setBlockEvents(false);
Michael Kolba4261fd2011-05-05 11:27:37 -0700178 }
179 }
180
Michael Kolba3194d02011-09-07 11:23:51 -0700181 private void switchToTab(Tab tab) {
Michael Kolba4261fd2011-05-05 11:27:37 -0700182 if (tab != mUi.getActiveTab()) {
183 mUiController.setActiveTab(tab);
184 }
185 }
186
Michael Kolba3194d02011-09-07 11:23:51 -0700187 protected void close(int position) {
188 close(position, true);
Michael Kolbf2055602011-04-09 17:20:03 -0700189 }
190
Michael Kolba3194d02011-09-07 11:23:51 -0700191 protected void close(int position, boolean animate) {
192 mUi.hideNavScreen(position, animate);
193 }
194
195 protected NavTabView getTabView(int pos) {
196 return mScroller.getTabView(pos);
Michael Kolbf2055602011-04-09 17:20:03 -0700197 }
198
Michael Kolbf2055602011-04-09 17:20:03 -0700199 class TabAdapter extends BaseAdapter {
200
201 Context context;
202 TabControl tabControl;
203
204 public TabAdapter(Context ctx, TabControl tc) {
205 context = ctx;
206 tabControl = tc;
207 }
208
Michael Kolbf2055602011-04-09 17:20:03 -0700209 @Override
210 public int getCount() {
211 return tabControl.getTabCount();
212 }
213
214 @Override
215 public Tab getItem(int position) {
216 return tabControl.getTab(position);
217 }
218
219 public long getItemId(int position) {
220 return position;
221 }
222
223 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700224 public View getView(final int position, View convertView, ViewGroup parent) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700225 final NavTabView tabview = new NavTabView(mActivity);
Michael Kolbf2055602011-04-09 17:20:03 -0700226 final Tab tab = getItem(position);
Michael Kolbc3af0672011-08-09 10:24:41 -0700227 tabview.setWebView(tab);
John Reck8ee633f2011-08-09 16:00:35 -0700228 mTabViews.put(tab, tabview.mImage);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700229 tabview.setOnClickListener(new OnClickListener() {
Michael Kolbf2055602011-04-09 17:20:03 -0700230 @Override
231 public void onClick(View v) {
Michael Kolb9829b432011-06-04 13:29:00 -0700232 if (tabview.isClose(v)) {
Michael Kolbe76f7042011-07-27 15:16:32 -0700233 mScroller.animateOut(tabview);
Axesh R. Ajmera069f8002014-11-04 10:02:39 -0800234 mTabViews.remove(tab);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700235 } else if (tabview.isTitle(v)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700236 switchToTab(tab);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700237 mUi.getTitleBar().setSkipTitleBarAnimations(true);
Michael Kolba3194d02011-09-07 11:23:51 -0700238 close(position, false);
Michael Kolb1f9b3562012-04-24 14:38:34 -0700239 mUi.editUrl(false, true);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700240 mUi.getTitleBar().setSkipTitleBarAnimations(false);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700241 } else if (tabview.isWebView(v)) {
Michael Kolba3194d02011-09-07 11:23:51 -0700242 close(position);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700243 }
244 }
245 });
246 return tabview;
Michael Kolbf2055602011-04-09 17:20:03 -0700247 }
Michael Kolb2814a362011-05-19 15:49:41 -0700248
Michael Kolbf2055602011-04-09 17:20:03 -0700249 }
250
John Reck8ee633f2011-08-09 16:00:35 -0700251 @Override
252 public void onThumbnailUpdated(Tab t) {
253 View v = mTabViews.get(t);
254 if (v != null) {
255 v.invalidate();
John Reck8ee633f2011-08-09 16:00:35 -0700256 }
257 }
258
Michael Kolbf2055602011-04-09 17:20:03 -0700259}