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 | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 35 | import android.widget.PopupMenu; |
| 36 | import android.widget.PopupMenu.OnMenuItemClickListener; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 37 | import android.widget.RelativeLayout; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 38 | import android.widget.TextView; |
| 39 | |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 40 | public class NavScreen extends RelativeLayout |
| 41 | implements OnClickListener, OnMenuItemClickListener { |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 42 | |
| 43 | UiController mUiController; |
| 44 | PhoneUi mUi; |
| 45 | Tab mTab; |
| 46 | Activity mActivity; |
| 47 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 48 | ImageButton mRefresh; |
| 49 | ImageButton mForward; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 50 | ImageButton mBookmarks; |
| 51 | ImageButton mMore; |
| 52 | ImageButton mNewTab; |
| 53 | ImageButton mNewIncognito; |
| 54 | FrameLayout mHolder; |
| 55 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 56 | TextView mTitle; |
| 57 | ImageView mFavicon; |
| 58 | ImageButton mCloseTab; |
| 59 | |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 60 | NavTabGallery mScroller; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 61 | float mTabAspect = 0.66f; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 62 | int mTabWidth; |
| 63 | int mTabHeight; |
| 64 | TabAdapter mAdapter; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 65 | int mOrientation; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 66 | |
| 67 | public NavScreen(Activity activity, UiController ctl, PhoneUi ui) { |
| 68 | super(activity); |
| 69 | mActivity = activity; |
| 70 | mUiController = ctl; |
| 71 | mUi = ui; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 72 | mOrientation = activity.getResources().getConfiguration().orientation; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 73 | init(); |
| 74 | } |
| 75 | |
| 76 | protected Tab getSelectedTab() { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 77 | return (Tab) mScroller.getSelectedItem(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Michael Kolb | fedb492 | 2011-04-20 16:45:33 -0700 | [diff] [blame] | 80 | protected void showMenu() { |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 81 | PopupMenu popup = new PopupMenu(mContext, mMore); |
| 82 | Menu menu = popup.getMenu(); |
| 83 | popup.getMenuInflater().inflate(R.menu.browser, menu); |
Michael Kolb | fedb492 | 2011-04-20 16:45:33 -0700 | [diff] [blame] | 84 | menu.setGroupVisible(R.id.NAV_MENU, false); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 85 | popup.setOnMenuItemClickListener(this); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 86 | popup.show(); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 87 | } |
| 88 | |
| 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 | |
John Reck | adc921f | 2011-04-27 10:11:03 -0700 | [diff] [blame] | 94 | protected float getToolbarHeight() { |
| 95 | return mActivity.getResources().getDimension(R.dimen.toolbar_height); |
| 96 | } |
| 97 | |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 98 | // for configuration changes |
| 99 | @Override |
| 100 | protected void onConfigurationChanged(Configuration newconfig) { |
| 101 | if (newconfig.orientation != mOrientation) { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 102 | int selIx = mScroller.getSelectionIndex(); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 103 | removeAllViews(); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 104 | mOrientation = newconfig.orientation; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 105 | init(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 106 | mScroller.setSelection(selIx); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 107 | mAdapter.notifyDataSetChanged(); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 111 | private void init() { |
| 112 | LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 113 | mBookmarks = (ImageButton) findViewById(R.id.bookmarks); |
| 114 | mNewTab = (ImageButton) findViewById(R.id.newtab); |
| 115 | mNewIncognito = (ImageButton) findViewById(R.id.newincognito); |
| 116 | mMore = (ImageButton) findViewById(R.id.more); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 117 | mBookmarks.setOnClickListener(this); |
| 118 | mNewTab.setOnClickListener(this); |
| 119 | mNewIncognito.setOnClickListener(this); |
| 120 | mMore.setOnClickListener(this); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 121 | mScroller = (NavTabGallery) findViewById(R.id.scroller); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 122 | mAdapter = new TabAdapter(mContext, mUiController.getTabControl()); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 123 | mScroller.setAdapter(mAdapter); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 124 | mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE |
| 125 | ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 126 | // update state for active tab |
| 127 | mScroller.setSelection(mUiController.getTabControl().getTabPosition(mUi.getActiveTab())); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void onClick(View v) { |
| 132 | WebView web = (mTab != null) ? mTab.getWebView() : null; |
| 133 | if (web != null) { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 134 | if (mForward == v) { |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 135 | mUi.hideNavScreen(true); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 136 | web.goForward(); |
| 137 | } else if (mRefresh == v) { |
| 138 | mUi.hideNavScreen(true); |
| 139 | web.reload(); |
| 140 | } |
| 141 | } |
| 142 | if (mBookmarks == v) { |
| 143 | mUi.hideNavScreen(false); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 144 | switchToSelected(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 145 | mUiController.bookmarksOrHistoryPicker(false); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 146 | } else if (mNewTab == v) { |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 147 | openNewTab(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 148 | } else if (mMore == v) { |
Michael Kolb | fedb492 | 2011-04-20 16:45:33 -0700 | [diff] [blame] | 149 | showMenu(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 150 | } else if (mNewIncognito == v) { |
| 151 | mUi.hideNavScreen(true); |
Michael Kolb | 519d228 | 2011-05-09 17:03:19 -0700 | [diff] [blame] | 152 | mUiController.openIncognitoTab(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 153 | } else if (mTitle == v) { |
| 154 | mUi.getTitleBar().setSkipTitleBarAnimations(true); |
| 155 | close(false); |
| 156 | mUi.editUrl(false); |
| 157 | mUi.getTitleBar().setSkipTitleBarAnimations(false); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 161 | private void onCloseTab(Tab tab) { |
| 162 | if (tab != null) { |
Michael Kolb | 66af816 | 2011-06-09 11:33:34 -0700 | [diff] [blame] | 163 | switchToSelected(); |
| 164 | mUiController.closeCurrentTab(); |
| 165 | mAdapter.notifyDataSetChanged(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 168 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 169 | private void openNewTab() { |
| 170 | // need to call openTab explicitely with setactive false |
| 171 | Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(), |
| 172 | false, false, false); |
| 173 | mAdapter.notifyDataSetChanged(); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 174 | if (tab != null) { |
| 175 | // set tab as the selected in flipper, then hide |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 176 | final int tix = mUi.mTabControl.getTabPosition(tab); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 177 | mScroller.setSelection(tix); |
| 178 | postDelayed(new Runnable() { |
| 179 | @Override |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 180 | public void run() { |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 181 | mUi.hideNavScreen(true); |
| 182 | switchToSelected(); |
| 183 | } |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 184 | }, 100); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | private void switchToSelected() { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 189 | Tab tab = (Tab) mScroller.getSelectedItem(); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 190 | if (tab != mUi.getActiveTab()) { |
| 191 | mUiController.setActiveTab(tab); |
| 192 | } |
| 193 | } |
| 194 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 195 | protected void close() { |
| 196 | close(true); |
| 197 | } |
| 198 | |
| 199 | protected void close(boolean animate) { |
| 200 | mUi.hideNavScreen(animate); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | class TabGallery extends Gallery { |
| 204 | |
| 205 | public TabGallery(Context ctx) { |
| 206 | super(ctx); |
| 207 | setUnselectedAlpha(0.3f); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | protected ViewGroup.LayoutParams generateDefaultLayoutParams() { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 212 | return new Gallery.LayoutParams(mTabWidth, mTabHeight); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
| 216 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) { |
| 217 | return generateDefaultLayoutParams(); |
| 218 | } |
| 219 | |
| 220 | } |
| 221 | |
| 222 | class TabAdapter extends BaseAdapter { |
| 223 | |
| 224 | Context context; |
| 225 | TabControl tabControl; |
| 226 | |
| 227 | public TabAdapter(Context ctx, TabControl tc) { |
| 228 | context = ctx; |
| 229 | tabControl = tc; |
| 230 | } |
| 231 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 232 | @Override |
| 233 | public int getCount() { |
| 234 | return tabControl.getTabCount(); |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public Tab getItem(int position) { |
| 239 | return tabControl.getTab(position); |
| 240 | } |
| 241 | |
| 242 | public long getItemId(int position) { |
| 243 | return position; |
| 244 | } |
| 245 | |
| 246 | @Override |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 247 | public View getView(final int position, View convertView, ViewGroup parent) { |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 248 | final NavTabView tabview = new NavTabView(mActivity); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 249 | final Tab tab = getItem(position); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 250 | final BrowserWebView web = (BrowserWebView) tab.getWebView(); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 251 | tabview.setWebView(mUi, tab); |
| 252 | tabview.setOnClickListener(new OnClickListener() { |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 253 | @Override |
| 254 | public void onClick(View v) { |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 255 | if (tabview.isClose(v)) { |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 256 | onCloseTab((Tab) (mScroller.getSelectedItem())); |
| 257 | } else if (tabview.isTitle(v)) { |
| 258 | mUi.getTitleBar().setSkipTitleBarAnimations(true); |
| 259 | close(false); |
| 260 | mUi.editUrl(false); |
| 261 | mUi.getTitleBar().setSkipTitleBarAnimations(false); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 262 | } else if (tabview.isWebView(v)) { |
| 263 | mScroller.setSelection(position); |
| 264 | close(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 265 | |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | }); |
| 269 | return tabview; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 270 | } |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 271 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 274 | } |