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