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