Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 1 | /* |
| 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 Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 18 | |
Enrico Ros | 80c045a | 2014-11-24 15:23:12 -0800 | [diff] [blame] | 19 | import android.animation.ObjectAnimator; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 20 | import android.app.Activity; |
| 21 | import android.content.Context; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 22 | import android.content.res.Configuration; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 23 | import android.view.LayoutInflater; |
| 24 | import android.view.Menu; |
| 25 | import android.view.MenuItem; |
| 26 | import android.view.View; |
| 27 | import android.view.View.OnClickListener; |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 28 | import android.view.ViewConfiguration; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 29 | import android.view.ViewGroup; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 30 | import android.widget.BaseAdapter; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 31 | import android.widget.ImageButton; |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 32 | import android.widget.LinearLayout; |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 33 | import android.widget.PopupMenu; |
| 34 | import android.widget.PopupMenu.OnMenuItemClickListener; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 35 | import android.widget.RelativeLayout; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 36 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 37 | import com.android.browser.NavTabScroller.OnRemoveListener; |
Dave Tharp | 3e2896f | 2015-05-20 15:25:07 -0700 | [diff] [blame] | 38 | import com.android.browser.mdm.IncognitoRestriction; |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 39 | |
| 40 | import java.util.HashMap; |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 41 | |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 42 | public class NavScreen extends RelativeLayout |
Pankaj Garg | 7987849 | 2015-04-01 14:48:21 -0700 | [diff] [blame] | 43 | implements OnClickListener, OnMenuItemClickListener { |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 44 | |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 45 | |
Enrico Ros | 80c045a | 2014-11-24 15:23:12 -0800 | [diff] [blame] | 46 | private final UiController mUiController; |
| 47 | private final PhoneUi mUi; |
| 48 | private final Activity mActivity; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 49 | |
Enrico Ros | 80c045a | 2014-11-24 15:23:12 -0800 | [diff] [blame] | 50 | private View mToolbarLayout; |
| 51 | private ImageButton mMore; |
| 52 | private ImageButton mNewTab; |
| 53 | private ImageButton mNewIncognitoTab; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 54 | |
Enrico Ros | 80c045a | 2014-11-24 15:23:12 -0800 | [diff] [blame] | 55 | private NavTabScroller mScroller; |
| 56 | private TabAdapter mAdapter; |
| 57 | private int mOrientation; |
| 58 | private HashMap<Tab, View> mTabViews; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 59 | |
| 60 | public NavScreen(Activity activity, UiController ctl, PhoneUi ui) { |
| 61 | super(activity); |
| 62 | mActivity = activity; |
| 63 | mUiController = ctl; |
| 64 | mUi = ui; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 65 | mOrientation = activity.getResources().getConfiguration().orientation; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 66 | init(); |
| 67 | } |
| 68 | |
Enrico Ros | 80c045a | 2014-11-24 15:23:12 -0800 | [diff] [blame] | 69 | protected void showPopupMenu() { |
Pankaj Garg | 49b7925 | 2014-11-07 17:33:41 -0800 | [diff] [blame] | 70 | 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 Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Enrico Ros | 80c045a | 2014-11-24 15:23:12 -0800 | [diff] [blame] | 79 | 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 Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public boolean onMenuItemClick(MenuItem item) { |
| 90 | return mUiController.onOptionsItemSelected(item); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 93 | @Override |
| 94 | protected void onConfigurationChanged(Configuration newconfig) { |
| 95 | if (newconfig.orientation != mOrientation) { |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 96 | mOrientation = newconfig.orientation; |
Enrico Ros | 80c045a | 2014-11-24 15:23:12 -0800 | [diff] [blame] | 97 | |
| 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 Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
John Reck | 9c5004e | 2011-10-07 16:00:12 -0700 | [diff] [blame] | 106 | public void refreshAdapter() { |
John Reck | 1adf030 | 2011-10-11 10:58:12 -0700 | [diff] [blame] | 107 | mScroller.handleDataChanged( |
| 108 | mUiController.getTabControl().getTabPosition(mUi.getActiveTab())); |
John Reck | 9c5004e | 2011-10-07 16:00:12 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Dave Tharp | 3e2896f | 2015-05-20 15:25:07 -0700 | [diff] [blame] | 111 | |
| 112 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 113 | private void init() { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 114 | LayoutInflater.from(getContext()).inflate(R.layout.nav_screen, this); |
| 115 | setContentDescription(getContext().getResources().getString( |
Michael Kolb | 30adae6 | 2011-07-29 13:37:05 -0700 | [diff] [blame] | 116 | R.string.accessibility_transition_navscreen)); |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 117 | mToolbarLayout = findViewById(R.id.nav_toolbar_animate); |
| 118 | mNewIncognitoTab = (ImageButton) findViewById(R.id.newincognitotab); |
Dave Tharp | dcad7b0 | 2015-05-28 07:38:32 -0700 | [diff] [blame] | 119 | IncognitoRestriction.getInstance().registerControl(mNewIncognitoTab); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 120 | mNewTab = (ImageButton) findViewById(R.id.newtab); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 121 | mMore = (ImageButton) findViewById(R.id.more); |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 122 | mNewIncognitoTab.setOnClickListener(this); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 123 | mNewTab.setOnClickListener(this); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 124 | mMore.setOnClickListener(this); |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 125 | mScroller = (NavTabScroller) findViewById(R.id.scroller); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 126 | TabControl tc = mUiController.getTabControl(); |
| 127 | mTabViews = new HashMap<Tab, View>(tc.getTabCount()); |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 128 | mAdapter = new TabAdapter(getContext(), tc); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 129 | mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE |
| 130 | ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 131 | // update state for active tab |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 132 | mScroller.setAdapter(mAdapter, |
| 133 | mUiController.getTabControl().getTabPosition(mUi.getActiveTab())); |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 134 | mScroller.setOnRemoveListener(new OnRemoveListener() { |
| 135 | public void onRemovePosition(int pos) { |
| 136 | Tab tab = mAdapter.getItem(pos); |
| 137 | onCloseTab(tab); |
| 138 | } |
| 139 | }); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void onClick(View v) { |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 144 | if (mNewTab == v) { |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 145 | openNewTab(); |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 146 | } else if (mNewIncognitoTab == v) { |
Dave Tharp | dcad7b0 | 2015-05-28 07:38:32 -0700 | [diff] [blame] | 147 | openNewIncognitoTab(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 148 | } else if (mMore == v) { |
Enrico Ros | 80c045a | 2014-11-24 15:23:12 -0800 | [diff] [blame] | 149 | showPopupMenu(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 153 | private void onCloseTab(Tab tab) { |
| 154 | if (tab != null) { |
Michael Kolb | 308b301 | 2011-07-13 14:50:56 -0700 | [diff] [blame] | 155 | if (tab == mUiController.getCurrentTab()) { |
| 156 | mUiController.closeCurrentTab(); |
| 157 | } else { |
| 158 | mUiController.closeTab(tab); |
| 159 | } |
Axesh R. Ajmera | 069f800 | 2014-11-04 10:02:39 -0800 | [diff] [blame] | 160 | mTabViews.remove(tab); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 163 | |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 164 | 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 Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 176 | private void openNewTab() { |
| 177 | // need to call openTab explicitely with setactive false |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 178 | final Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(), |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 179 | false, false, false); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 180 | if (tab != null) { |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 181 | mUiController.setBlockEvents(true); |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 182 | final int tix = mUi.mTabControl.getTabPosition(tab); |
Vivek Sekhar | c549e3a | 2014-06-05 16:19:26 -0700 | [diff] [blame] | 183 | switchToTab(tab); |
| 184 | mUi.hideNavScreen(tix, true); |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 185 | mScroller.handleDataChanged(tix); |
| 186 | mUiController.setBlockEvents(false); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 190 | private void switchToTab(Tab tab) { |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 191 | if (tab != mUi.getActiveTab()) { |
| 192 | mUiController.setActiveTab(tab); |
| 193 | } |
| 194 | } |
| 195 | |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 196 | protected void close(int position) { |
| 197 | close(position, true); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 200 | 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 Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 208 | 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 Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 218 | @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 Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 233 | public View getView(final int position, View convertView, ViewGroup parent) { |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 234 | final NavTabView tabview = new NavTabView(mActivity); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 235 | final Tab tab = getItem(position); |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 236 | tabview.setWebView(tab); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 237 | mTabViews.put(tab, tabview.mImage); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 238 | tabview.setOnClickListener(new OnClickListener() { |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 239 | @Override |
| 240 | public void onClick(View v) { |
jrizzoli | 5bba72a | 2015-08-28 22:03:53 +0200 | [diff] [blame] | 241 | if (tabview.isTitle(v)) { |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 242 | switchToTab(tab); |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 243 | close(position, false); |
Michael Kolb | 1f9b356 | 2012-04-24 14:38:34 -0700 | [diff] [blame] | 244 | mUi.editUrl(false, true); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 245 | } else if (tabview.isWebView(v)) { |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 246 | close(position); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | }); |
| 250 | return tabview; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 251 | } |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 252 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 253 | } |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 254 | } |