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