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 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 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; |
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 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 39 | import com.android.browser.R; |
| 40 | import com.android.browser.NavTabScroller.OnLayoutListener; |
| 41 | import com.android.browser.NavTabScroller.OnRemoveListener; |
| 42 | import com.android.browser.TabControl.OnThumbnailUpdatedListener; |
| 43 | import com.android.browser.UI.ComboViews; |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 44 | |
| 45 | import java.util.HashMap; |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 46 | |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 47 | public class NavScreen extends RelativeLayout |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 48 | implements OnClickListener, OnMenuItemClickListener, OnThumbnailUpdatedListener { |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 49 | |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 50 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 51 | UiController mUiController; |
| 52 | PhoneUi mUi; |
| 53 | Tab mTab; |
| 54 | Activity mActivity; |
| 55 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 56 | ImageButton mRefresh; |
| 57 | ImageButton mForward; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 58 | ImageButton mBookmarks; |
| 59 | ImageButton mMore; |
| 60 | ImageButton mNewTab; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 61 | FrameLayout mHolder; |
| 62 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 63 | TextView mTitle; |
| 64 | ImageView mFavicon; |
| 65 | ImageButton mCloseTab; |
| 66 | |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 67 | NavTabScroller mScroller; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 68 | TabAdapter mAdapter; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 69 | int mOrientation; |
Michael Kolb | 3ca1275 | 2011-07-20 13:52:25 -0700 | [diff] [blame] | 70 | boolean mNeedsMenu; |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 71 | HashMap<Tab, View> mTabViews; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 72 | |
| 73 | public NavScreen(Activity activity, UiController ctl, PhoneUi ui) { |
| 74 | super(activity); |
| 75 | mActivity = activity; |
| 76 | mUiController = ctl; |
| 77 | mUi = ui; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 78 | mOrientation = activity.getResources().getConfiguration().orientation; |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 79 | init(); |
| 80 | } |
| 81 | |
Michael Kolb | fedb492 | 2011-04-20 16:45:33 -0700 | [diff] [blame] | 82 | protected void showMenu() { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 83 | PopupMenu popup = new PopupMenu(getContext(), mMore); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 84 | Menu menu = popup.getMenu(); |
| 85 | popup.getMenuInflater().inflate(R.menu.browser, menu); |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 86 | mUiController.updateMenuState(mUiController.getCurrentTab(), menu); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 87 | popup.setOnMenuItemClickListener(this); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 88 | popup.show(); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean onMenuItemClick(MenuItem item) { |
| 93 | return mUiController.onOptionsItemSelected(item); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 94 | } |
| 95 | |
John Reck | adc921f | 2011-04-27 10:11:03 -0700 | [diff] [blame] | 96 | protected float getToolbarHeight() { |
| 97 | return mActivity.getResources().getDimension(R.dimen.toolbar_height); |
| 98 | } |
| 99 | |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 100 | @Override |
| 101 | protected void onConfigurationChanged(Configuration newconfig) { |
| 102 | if (newconfig.orientation != mOrientation) { |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 103 | int sv = mScroller.getScrollValue(); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 104 | removeAllViews(); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 105 | mOrientation = newconfig.orientation; |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 106 | init(); |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 107 | mScroller.setScrollValue(sv); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 108 | mAdapter.notifyDataSetChanged(); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
John Reck | 9c5004e | 2011-10-07 16:00:12 -0700 | [diff] [blame] | 112 | public void refreshAdapter() { |
John Reck | 1adf030 | 2011-10-11 10:58:12 -0700 | [diff] [blame] | 113 | mScroller.handleDataChanged( |
| 114 | mUiController.getTabControl().getTabPosition(mUi.getActiveTab())); |
John Reck | 9c5004e | 2011-10-07 16:00:12 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 117 | private void init() { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 118 | LayoutInflater.from(getContext()).inflate(R.layout.nav_screen, this); |
| 119 | setContentDescription(getContext().getResources().getString( |
Michael Kolb | 30adae6 | 2011-07-29 13:37:05 -0700 | [diff] [blame] | 120 | R.string.accessibility_transition_navscreen)); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 121 | mBookmarks = (ImageButton) findViewById(R.id.bookmarks); |
| 122 | mNewTab = (ImageButton) findViewById(R.id.newtab); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 123 | mMore = (ImageButton) findViewById(R.id.more); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 124 | mBookmarks.setOnClickListener(this); |
| 125 | mNewTab.setOnClickListener(this); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 126 | mMore.setOnClickListener(this); |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 127 | mScroller = (NavTabScroller) findViewById(R.id.scroller); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 128 | TabControl tc = mUiController.getTabControl(); |
| 129 | mTabViews = new HashMap<Tab, View>(tc.getTabCount()); |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 130 | mAdapter = new TabAdapter(getContext(), tc); |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 131 | mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE |
| 132 | ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 133 | // update state for active tab |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 134 | mScroller.setAdapter(mAdapter, |
| 135 | mUiController.getTabControl().getTabPosition(mUi.getActiveTab())); |
Michael Kolb | dcd81d3 | 2011-07-22 10:30:49 -0700 | [diff] [blame] | 136 | mScroller.setOnRemoveListener(new OnRemoveListener() { |
| 137 | public void onRemovePosition(int pos) { |
| 138 | Tab tab = mAdapter.getItem(pos); |
| 139 | onCloseTab(tab); |
| 140 | } |
| 141 | }); |
Michael Kolb | 3ca1275 | 2011-07-20 13:52:25 -0700 | [diff] [blame] | 142 | mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey(); |
| 143 | if (!mNeedsMenu) { |
| 144 | mMore.setVisibility(View.GONE); |
| 145 | } |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void onClick(View v) { |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 150 | if (mBookmarks == v) { |
Michael Kolb | 315d502 | 2011-10-13 12:47:11 -0700 | [diff] [blame] | 151 | mUiController.bookmarksOrHistoryPicker(ComboViews.Bookmarks); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 152 | } else if (mNewTab == v) { |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 153 | openNewTab(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 154 | } else if (mMore == v) { |
Michael Kolb | fedb492 | 2011-04-20 16:45:33 -0700 | [diff] [blame] | 155 | showMenu(); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 159 | private void onCloseTab(Tab tab) { |
| 160 | if (tab != null) { |
Michael Kolb | 308b301 | 2011-07-13 14:50:56 -0700 | [diff] [blame] | 161 | if (tab == mUiController.getCurrentTab()) { |
| 162 | mUiController.closeCurrentTab(); |
| 163 | } else { |
| 164 | mUiController.closeTab(tab); |
| 165 | } |
Axesh R. Ajmera | 069f800 | 2014-11-04 10:02:39 -0800 | [diff] [blame] | 166 | mTabViews.remove(tab); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 169 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 170 | private void openNewTab() { |
| 171 | // need to call openTab explicitely with setactive false |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 172 | final Tab tab = mUiController.openTab(BrowserSettings.getInstance().getHomePage(), |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 173 | false, false, false); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 174 | if (tab != null) { |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 175 | mUiController.setBlockEvents(true); |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 176 | final int tix = mUi.mTabControl.getTabPosition(tab); |
Vivek Sekhar | c549e3a | 2014-06-05 16:19:26 -0700 | [diff] [blame] | 177 | switchToTab(tab); |
| 178 | mUi.hideNavScreen(tix, true); |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 179 | mScroller.handleDataChanged(tix); |
| 180 | mUiController.setBlockEvents(false); |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 184 | private void switchToTab(Tab tab) { |
Michael Kolb | a4261fd | 2011-05-05 11:27:37 -0700 | [diff] [blame] | 185 | if (tab != mUi.getActiveTab()) { |
| 186 | mUiController.setActiveTab(tab); |
| 187 | } |
| 188 | } |
| 189 | |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 190 | protected void close(int position) { |
| 191 | close(position, true); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 194 | protected void close(int position, boolean animate) { |
| 195 | mUi.hideNavScreen(position, animate); |
| 196 | } |
| 197 | |
| 198 | protected NavTabView getTabView(int pos) { |
| 199 | return mScroller.getTabView(pos); |
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 | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 230 | tabview.setWebView(tab); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 231 | mTabViews.put(tab, tabview.mImage); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 232 | tabview.setOnClickListener(new OnClickListener() { |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 233 | @Override |
| 234 | public void onClick(View v) { |
Michael Kolb | 9829b43 | 2011-06-04 13:29:00 -0700 | [diff] [blame] | 235 | if (tabview.isClose(v)) { |
Michael Kolb | e76f704 | 2011-07-27 15:16:32 -0700 | [diff] [blame] | 236 | mScroller.animateOut(tabview); |
Axesh R. Ajmera | 069f800 | 2014-11-04 10:02:39 -0800 | [diff] [blame] | 237 | mTabViews.remove(tab); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 238 | } else if (tabview.isTitle(v)) { |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 239 | switchToTab(tab); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 240 | mUi.getTitleBar().setSkipTitleBarAnimations(true); |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 241 | close(position, false); |
Michael Kolb | 1f9b356 | 2012-04-24 14:38:34 -0700 | [diff] [blame] | 242 | mUi.editUrl(false, true); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 243 | mUi.getTitleBar().setSkipTitleBarAnimations(false); |
Michael Kolb | 4bd767d | 2011-05-27 11:33:55 -0700 | [diff] [blame] | 244 | } else if (tabview.isWebView(v)) { |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 245 | close(position); |
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 | |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 254 | @Override |
| 255 | public void onThumbnailUpdated(Tab t) { |
| 256 | View v = mTabViews.get(t); |
| 257 | if (v != null) { |
| 258 | v.invalidate(); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 262 | } |