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; |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 27 | import android.view.ViewConfiguration; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 28 | import android.view.ViewGroup; |
| 29 | import android.webkit.WebView; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 30 | import android.widget.BaseAdapter; |
| 31 | import android.widget.FrameLayout; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 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 | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 40 | import com.android.browser.NavTabGallery.OnRemoveListener; |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 41 | import com.android.browser.TabControl.OnThumbnailUpdatedListener; |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 42 | import com.android.browser.view.Gallery.OnScrollFinishedListener; |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 43 | |
| 44 | import java.util.HashMap; |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 45 | |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 46 | public class NavScreen extends RelativeLayout |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 47 | implements OnClickListener, OnMenuItemClickListener, OnThumbnailUpdatedListener { |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 48 | |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 49 | |
| 50 | private static final int SCROLL_MIN = 200; |
| 51 | private static final int SCROLL_FACTOR = 20; |
| 52 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 53 | UiController mUiController; |
| 54 | PhoneUi mUi; |
| 55 | Tab mTab; |
| 56 | Activity mActivity; |
| 57 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 58 | ImageButton mRefresh; |
| 59 | ImageButton mForward; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 60 | ImageButton mBookmarks; |
| 61 | ImageButton mMore; |
| 62 | ImageButton mNewTab; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 63 | FrameLayout mHolder; |
| 64 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 65 | TextView mTitle; |
| 66 | ImageView mFavicon; |
| 67 | ImageButton mCloseTab; |
| 68 | |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 69 | NavTabGallery mScroller; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 70 | TabAdapter mAdapter; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 71 | int mOrientation; |
Michael Kolb | 3ca1275 | 2011-07-20 13:52:25 -0700 | [diff] [blame] | 72 | boolean mNeedsMenu; |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 73 | HashMap<Tab, View> mTabViews; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 74 | |
| 75 | public NavScreen(Activity activity, UiController ctl, PhoneUi ui) { |
| 76 | super(activity); |
| 77 | mActivity = activity; |
| 78 | mUiController = ctl; |
| 79 | mUi = ui; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 80 | mOrientation = activity.getResources().getConfiguration().orientation; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 81 | init(); |
| 82 | } |
| 83 | |
| 84 | protected Tab getSelectedTab() { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 85 | return (Tab) mScroller.getSelectedItem(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Michael Kolb | fedb492 | 2011-04-20 16:45:33 -0700 | [diff] [blame] | 88 | protected void showMenu() { |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 89 | PopupMenu popup = new PopupMenu(mContext, mMore); |
| 90 | Menu menu = popup.getMenu(); |
| 91 | popup.getMenuInflater().inflate(R.menu.browser, menu); |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 92 | mUiController.updateMenuState(mScroller.getSelectedItem(), menu); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 93 | popup.setOnMenuItemClickListener(this); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 94 | popup.show(); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public boolean onMenuItemClick(MenuItem item) { |
Michael Kolb | adb0e60 | 2011-07-18 14:33:46 -0700 | [diff] [blame] | 99 | mUi.hideNavScreen(false); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 100 | return mUiController.onOptionsItemSelected(item); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 101 | } |
| 102 | |
John Reck | adc921f | 2011-04-27 10:11:03 -0700 | [diff] [blame] | 103 | protected float getToolbarHeight() { |
| 104 | return mActivity.getResources().getDimension(R.dimen.toolbar_height); |
| 105 | } |
| 106 | |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 107 | // for configuration changes |
| 108 | @Override |
| 109 | protected void onConfigurationChanged(Configuration newconfig) { |
| 110 | if (newconfig.orientation != mOrientation) { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 111 | int selIx = mScroller.getSelectionIndex(); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 112 | removeAllViews(); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 113 | mOrientation = newconfig.orientation; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 114 | init(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 115 | mScroller.setSelection(selIx); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 116 | mAdapter.notifyDataSetChanged(); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 120 | private void init() { |
| 121 | LayoutInflater.from(mContext).inflate(R.layout.nav_screen, this); |
Michael Kolb | 30adae6 | 2011-07-29 13:37:05 -0700 | [diff] [blame] | 122 | setContentDescription(mContext.getResources().getString( |
| 123 | R.string.accessibility_transition_navscreen)); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 124 | mBookmarks = (ImageButton) findViewById(R.id.bookmarks); |
| 125 | mNewTab = (ImageButton) findViewById(R.id.newtab); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 126 | mMore = (ImageButton) findViewById(R.id.more); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 127 | mBookmarks.setOnClickListener(this); |
| 128 | mNewTab.setOnClickListener(this); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 129 | mMore.setOnClickListener(this); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 130 | mScroller = (NavTabGallery) findViewById(R.id.scroller); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 131 | TabControl tc = mUiController.getTabControl(); |
| 132 | mTabViews = new HashMap<Tab, View>(tc.getTabCount()); |
| 133 | mAdapter = new TabAdapter(mContext, tc); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 134 | mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE |
| 135 | ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 136 | // update state for active tab |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 137 | mScroller.setAdapter(mAdapter, |
| 138 | mUiController.getTabControl().getTabPosition(mUi.getActiveTab())); |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 139 | mScroller.setOnRemoveListener(new OnRemoveListener() { |
| 140 | public void onRemovePosition(int pos) { |
| 141 | Tab tab = mAdapter.getItem(pos); |
| 142 | onCloseTab(tab); |
| 143 | } |
| 144 | }); |
Michael Kolb | 3ca1275 | 2011-07-20 13:52:25 -0700 | [diff] [blame] | 145 | mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey(); |
| 146 | if (!mNeedsMenu) { |
| 147 | mMore.setVisibility(View.GONE); |
| 148 | } |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void onClick(View v) { |
| 153 | WebView web = (mTab != null) ? mTab.getWebView() : null; |
| 154 | if (web != null) { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 155 | if (mForward == v) { |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 156 | mUi.hideNavScreen(true); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 157 | mTab.goForward(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 158 | } else if (mRefresh == v) { |
| 159 | mUi.hideNavScreen(true); |
| 160 | web.reload(); |
| 161 | } |
| 162 | } |
| 163 | if (mBookmarks == v) { |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 164 | switchToSelected(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 165 | mUiController.bookmarksOrHistoryPicker(false); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 166 | } else if (mNewTab == v) { |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 167 | openNewTab(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 168 | } else if (mMore == v) { |
Michael Kolb | fedb492 | 2011-04-20 16:45:33 -0700 | [diff] [blame] | 169 | showMenu(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 170 | } else if (mTitle == v) { |
| 171 | mUi.getTitleBar().setSkipTitleBarAnimations(true); |
| 172 | close(false); |
| 173 | mUi.editUrl(false); |
| 174 | mUi.getTitleBar().setSkipTitleBarAnimations(false); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 178 | private void onCloseTab(Tab tab) { |
| 179 | if (tab != null) { |
Michael Kolb | 308b301 | 2011-07-13 14:50:56 -0700 | [diff] [blame] | 180 | if (tab == mUiController.getCurrentTab()) { |
| 181 | mUiController.closeCurrentTab(); |
| 182 | } else { |
| 183 | mUiController.closeTab(tab); |
| 184 | } |
Michael Kolb | 66af816 | 2011-06-09 11:33:34 -0700 | [diff] [blame] | 185 | mAdapter.notifyDataSetChanged(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 188 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 189 | private void openNewTab() { |
| 190 | // need to call openTab explicitely with setactive false |
| 191 | Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(), |
| 192 | false, false, false); |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 193 | int duration = 0; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 194 | if (tab != null) { |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 195 | mUiController.setBlockEvents(true); |
| 196 | int oldsel = mScroller.getSelectedItemPosition(); |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 197 | final int tix = mUi.mTabControl.getTabPosition(tab); |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 198 | duration = SCROLL_MIN + SCROLL_FACTOR * Math.abs(oldsel - tix); |
| 199 | mScroller.handleDataChanged(); |
| 200 | mScroller.smoothScrollToPosition(tix, duration, new OnScrollFinishedListener() { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 201 | @Override |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 202 | public void onScrollFinished() { |
| 203 | mUiController.setBlockEvents(false); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 204 | mUi.hideNavScreen(true); |
| 205 | switchToSelected(); |
| 206 | } |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 207 | }); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 211 | View getSelectedTabView() { |
| 212 | return mScroller.getSelectedTab(); |
| 213 | } |
| 214 | |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 215 | private void switchToSelected() { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 216 | Tab tab = (Tab) mScroller.getSelectedItem(); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 217 | if (tab != mUi.getActiveTab()) { |
| 218 | mUiController.setActiveTab(tab); |
| 219 | } |
| 220 | } |
| 221 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 222 | protected void close() { |
| 223 | close(true); |
| 224 | } |
| 225 | |
| 226 | protected void close(boolean animate) { |
| 227 | mUi.hideNavScreen(animate); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 230 | class TabAdapter extends BaseAdapter { |
| 231 | |
| 232 | Context context; |
| 233 | TabControl tabControl; |
| 234 | |
| 235 | public TabAdapter(Context ctx, TabControl tc) { |
| 236 | context = ctx; |
| 237 | tabControl = tc; |
| 238 | } |
| 239 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 240 | @Override |
| 241 | public int getCount() { |
| 242 | return tabControl.getTabCount(); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public Tab getItem(int position) { |
| 247 | return tabControl.getTab(position); |
| 248 | } |
| 249 | |
| 250 | public long getItemId(int position) { |
| 251 | return position; |
| 252 | } |
| 253 | |
| 254 | @Override |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 255 | public View getView(final int position, View convertView, ViewGroup parent) { |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 256 | final NavTabView tabview = new NavTabView(mActivity); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 257 | final Tab tab = getItem(position); |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 258 | tabview.setWebView(tab); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 259 | mTabViews.put(tab, tabview.mImage); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 260 | tabview.setOnClickListener(new OnClickListener() { |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 261 | @Override |
| 262 | public void onClick(View v) { |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 263 | if (tabview.isClose(v)) { |
Michael Kolb | e76f704 | 2011-07-27 15:16:32 -0700 | [diff] [blame] | 264 | mScroller.animateOut(tabview); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 265 | } else if (tabview.isTitle(v)) { |
Michael Kolb | 308b301 | 2011-07-13 14:50:56 -0700 | [diff] [blame] | 266 | mScroller.setSelection(position); |
| 267 | switchToSelected(); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 268 | mUi.getTitleBar().setSkipTitleBarAnimations(true); |
| 269 | close(false); |
| 270 | mUi.editUrl(false); |
| 271 | mUi.getTitleBar().setSkipTitleBarAnimations(false); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 272 | } else if (tabview.isWebView(v)) { |
| 273 | mScroller.setSelection(position); |
| 274 | close(); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | }); |
| 278 | return tabview; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 279 | } |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 280 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 281 | } |
| 282 | |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 283 | @Override |
| 284 | public void onThumbnailUpdated(Tab t) { |
| 285 | View v = mTabViews.get(t); |
| 286 | if (v != null) { |
| 287 | v.invalidate(); |
| 288 | mScroller.invalidate(); |
| 289 | } |
| 290 | } |
| 291 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 292 | } |