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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.content.Context; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 21 | import android.content.res.Configuration; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 22 | import android.view.LayoutInflater; |
| 23 | import android.view.Menu; |
| 24 | import android.view.MenuItem; |
| 25 | import android.view.View; |
| 26 | import android.view.View.OnClickListener; |
| 27 | import android.view.ViewGroup; |
| 28 | import android.webkit.WebView; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 29 | import android.widget.BaseAdapter; |
| 30 | import android.widget.FrameLayout; |
| 31 | import android.widget.Gallery; |
| 32 | import android.widget.ImageButton; |
| 33 | import android.widget.ImageView; |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 34 | import android.widget.LinearLayout; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 35 | import android.widget.ListPopupWindow; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 36 | import android.widget.RelativeLayout; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 37 | import android.widget.TextView; |
| 38 | |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 39 | import com.android.browser.view.Gallery.OnItemSelectedListener; |
| 40 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 41 | import java.util.ArrayList; |
| 42 | import java.util.List; |
| 43 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 44 | public class NavScreen extends RelativeLayout implements OnClickListener { |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 45 | |
| 46 | UiController mUiController; |
| 47 | PhoneUi mUi; |
| 48 | Tab mTab; |
| 49 | Activity mActivity; |
| 50 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 51 | ImageButton mRefresh; |
| 52 | ImageButton mForward; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 53 | ImageButton mBookmarks; |
| 54 | ImageButton mMore; |
| 55 | ImageButton mNewTab; |
| 56 | ImageButton mNewIncognito; |
| 57 | FrameLayout mHolder; |
| 58 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 59 | TextView mTitle; |
| 60 | ImageView mFavicon; |
| 61 | ImageButton mCloseTab; |
| 62 | |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 63 | NavTabGallery mScroller; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 64 | float mTabAspect = 0.66f; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 65 | int mTabWidth; |
| 66 | int mTabHeight; |
| 67 | TabAdapter mAdapter; |
| 68 | ListPopupWindow mPopup; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 69 | int mOrientation; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 70 | |
| 71 | public NavScreen(Activity activity, UiController ctl, PhoneUi ui) { |
| 72 | super(activity); |
| 73 | mActivity = activity; |
| 74 | mUiController = ctl; |
| 75 | mUi = ui; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 76 | mOrientation = activity.getResources().getConfiguration().orientation; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 77 | init(); |
| 78 | } |
| 79 | |
| 80 | protected Tab getSelectedTab() { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 81 | return (Tab) mScroller.getSelectedItem(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Michael Kolb | fedb492 | 2011-04-20 16:45:33 -0700 | [diff] [blame] | 84 | protected void showMenu() { |
| 85 | Menu menu = mUi.getMenu(); |
| 86 | menu.setGroupVisible(R.id.NAV_MENU, false); |
| 87 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 88 | MenuAdapter menuAdapter = new MenuAdapter(mContext); |
| 89 | menuAdapter.setMenu(menu); |
| 90 | ListPopupWindow popup = new ListPopupWindow(mContext); |
| 91 | popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED); |
| 92 | popup.setAdapter(menuAdapter); |
| 93 | popup.setModal(true); |
| 94 | popup.setAnchorView(mMore); |
| 95 | popup.setWidth((int) mContext.getResources().getDimension( |
| 96 | R.dimen.menu_width)); |
| 97 | popup.show(); |
| 98 | mPopup = popup; |
| 99 | } |
| 100 | |
John Reck | adc921f | 2011-04-27 10:11:03 -0700 | [diff] [blame] | 101 | protected float getToolbarHeight() { |
| 102 | return mActivity.getResources().getDimension(R.dimen.toolbar_height); |
| 103 | } |
| 104 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 105 | protected void dismissMenu() { |
| 106 | if (mPopup != null) { |
| 107 | mPopup.dismiss(); |
| 108 | } |
| 109 | } |
| 110 | |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 111 | // for configuration changes |
| 112 | @Override |
| 113 | protected void onConfigurationChanged(Configuration newconfig) { |
| 114 | if (newconfig.orientation != mOrientation) { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 115 | int selIx = mScroller.getSelectionIndex(); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 116 | removeAllViews(); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 117 | mOrientation = newconfig.orientation; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 118 | init(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 119 | mScroller.setSelection(selIx); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 120 | mAdapter.notifyDataSetChanged(); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 124 | private void init() { |
| 125 | LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 126 | mBookmarks = (ImageButton) findViewById(R.id.bookmarks); |
| 127 | mNewTab = (ImageButton) findViewById(R.id.newtab); |
| 128 | mNewIncognito = (ImageButton) findViewById(R.id.newincognito); |
| 129 | mMore = (ImageButton) findViewById(R.id.more); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 130 | mBookmarks.setOnClickListener(this); |
| 131 | mNewTab.setOnClickListener(this); |
| 132 | mNewIncognito.setOnClickListener(this); |
| 133 | mMore.setOnClickListener(this); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 134 | mScroller = (NavTabGallery) findViewById(R.id.scroller); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 135 | mAdapter = new TabAdapter(mContext, mUiController.getTabControl()); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 136 | mScroller.setAdapter(mAdapter); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 137 | mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE |
| 138 | ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 139 | // update state for active tab |
| 140 | mScroller.setSelection(mUiController.getTabControl().getTabPosition(mUi.getActiveTab())); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void onClick(View v) { |
| 145 | WebView web = (mTab != null) ? mTab.getWebView() : null; |
| 146 | if (web != null) { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 147 | if (mForward == v) { |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 148 | mUi.hideNavScreen(true); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 149 | web.goForward(); |
| 150 | } else if (mRefresh == v) { |
| 151 | mUi.hideNavScreen(true); |
| 152 | web.reload(); |
| 153 | } |
| 154 | } |
| 155 | if (mBookmarks == v) { |
| 156 | mUi.hideNavScreen(false); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 157 | switchToSelected(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 158 | mUiController.bookmarksOrHistoryPicker(false); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 159 | } else if (mNewTab == v) { |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 160 | openNewTab(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 161 | } else if (mMore == v) { |
Michael Kolb | fedb492 | 2011-04-20 16:45:33 -0700 | [diff] [blame] | 162 | showMenu(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 163 | } else if (mNewIncognito == v) { |
| 164 | mUi.hideNavScreen(true); |
Michael Kolb | 519d228 | 2011-05-09 17:03:19 -0700 | [diff] [blame] | 165 | mUiController.openIncognitoTab(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 166 | } else if (mTitle == v) { |
| 167 | mUi.getTitleBar().setSkipTitleBarAnimations(true); |
| 168 | close(false); |
| 169 | mUi.editUrl(false); |
| 170 | mUi.getTitleBar().setSkipTitleBarAnimations(false); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 174 | private void onCloseTab(Tab tab) { |
| 175 | if (tab != null) { |
Michael Kolb | 66af816 | 2011-06-09 11:33:34 -0700 | [diff] [blame] | 176 | switchToSelected(); |
| 177 | mUiController.closeCurrentTab(); |
| 178 | mAdapter.notifyDataSetChanged(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 181 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 182 | private void openNewTab() { |
| 183 | // need to call openTab explicitely with setactive false |
| 184 | Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(), |
| 185 | false, false, false); |
| 186 | mAdapter.notifyDataSetChanged(); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 187 | if (tab != null) { |
| 188 | // set tab as the selected in flipper, then hide |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 189 | final int tix = mUi.mTabControl.getTabPosition(tab); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 190 | mScroller.setSelection(tix); |
| 191 | postDelayed(new Runnable() { |
| 192 | @Override |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 193 | public void run() { |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 194 | mUi.hideNavScreen(true); |
| 195 | switchToSelected(); |
| 196 | } |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 197 | }, 100); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | private void switchToSelected() { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 202 | Tab tab = (Tab) mScroller.getSelectedItem(); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 203 | if (tab != mUi.getActiveTab()) { |
| 204 | mUiController.setActiveTab(tab); |
| 205 | } |
| 206 | } |
| 207 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 208 | protected void close() { |
| 209 | close(true); |
| 210 | } |
| 211 | |
| 212 | protected void close(boolean animate) { |
| 213 | mUi.hideNavScreen(animate); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | class TabGallery extends Gallery { |
| 217 | |
| 218 | public TabGallery(Context ctx) { |
| 219 | super(ctx); |
| 220 | setUnselectedAlpha(0.3f); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | protected ViewGroup.LayoutParams generateDefaultLayoutParams() { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 225 | return new Gallery.LayoutParams(mTabWidth, mTabHeight); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | @Override |
| 229 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) { |
| 230 | return generateDefaultLayoutParams(); |
| 231 | } |
| 232 | |
| 233 | } |
| 234 | |
| 235 | class TabAdapter extends BaseAdapter { |
| 236 | |
| 237 | Context context; |
| 238 | TabControl tabControl; |
| 239 | |
| 240 | public TabAdapter(Context ctx, TabControl tc) { |
| 241 | context = ctx; |
| 242 | tabControl = tc; |
| 243 | } |
| 244 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 245 | @Override |
| 246 | public int getCount() { |
| 247 | return tabControl.getTabCount(); |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public Tab getItem(int position) { |
| 252 | return tabControl.getTab(position); |
| 253 | } |
| 254 | |
| 255 | public long getItemId(int position) { |
| 256 | return position; |
| 257 | } |
| 258 | |
| 259 | @Override |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 260 | public View getView(final int position, View convertView, ViewGroup parent) { |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 261 | final NavTabView tabview = new NavTabView(mActivity); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 262 | final Tab tab = getItem(position); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 263 | final BrowserWebView web = (BrowserWebView) tab.getWebView(); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 264 | tabview.setWebView(mUi, tab); |
| 265 | tabview.setOnClickListener(new OnClickListener() { |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 266 | @Override |
| 267 | public void onClick(View v) { |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 268 | if (tabview.isClose(v)) { |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 269 | onCloseTab((Tab) (mScroller.getSelectedItem())); |
| 270 | } else if (tabview.isTitle(v)) { |
| 271 | mUi.getTitleBar().setSkipTitleBarAnimations(true); |
| 272 | close(false); |
| 273 | mUi.editUrl(false); |
| 274 | mUi.getTitleBar().setSkipTitleBarAnimations(false); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 275 | } else if (tabview.isWebView(v)) { |
| 276 | mScroller.setSelection(position); |
| 277 | close(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 278 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | }); |
| 282 | return tabview; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 283 | } |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 284 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | private class MenuAdapter extends BaseAdapter implements OnClickListener { |
| 288 | |
| 289 | List<MenuItem> mItems; |
| 290 | LayoutInflater mInflater; |
| 291 | |
| 292 | public MenuAdapter(Context ctx) { |
| 293 | mInflater = LayoutInflater.from(ctx); |
| 294 | mItems = new ArrayList<MenuItem>(); |
| 295 | } |
| 296 | |
| 297 | public void setMenu(Menu menu) { |
| 298 | mItems.clear(); |
| 299 | for (int i = 0; i < menu.size(); i++) { |
| 300 | MenuItem item = menu.getItem(i); |
| 301 | if (item.isEnabled() && item.isVisible()) { |
| 302 | mItems.add(item); |
| 303 | } |
| 304 | } |
| 305 | notifyDataSetChanged(); |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public int getCount() { |
| 310 | return mItems.size(); |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public MenuItem getItem(int position) { |
| 315 | return mItems.get(position); |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public long getItemId(int position) { |
| 320 | return position; |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public void onClick(View v) { |
| 325 | if (v.getTag() != null) { |
| 326 | dismissMenu(); |
| 327 | mActivity.closeOptionsMenu(); |
| 328 | mUi.hideNavScreen(false); |
| 329 | mUiController.onOptionsItemSelected((MenuItem) v.getTag()); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public View getView(int position, View convertView, ViewGroup parent) { |
| 335 | final MenuItem item = mItems.get(position); |
| 336 | View view = mInflater.inflate(R.layout.qc_menu_item, null); |
| 337 | TextView label = (TextView) view.findViewById(R.id.title); |
| 338 | label.setText(item.getTitle()); |
| 339 | label.setTag(item); |
| 340 | label.setOnClickListener(this); |
| 341 | return label; |
| 342 | } |
| 343 | |
| 344 | } |
| 345 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 346 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 347 | } |