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