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; 21 import android.graphics.Bitmap; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.View.OnClickListener; 25 import android.view.ViewGroup; 26 import android.view.ViewGroup.LayoutParams; 27 import android.webkit.WebView; 28 import android.widget.BaseAdapter; 29 import android.widget.FrameLayout; 30 import android.widget.ImageView; 31 import android.widget.ImageView.ScaleType; 32 import android.widget.TextView; 33 34 import com.android.browser.UI.ComboViews; 35 import com.android.browser.view.PieItem; 36 import com.android.browser.view.PieMenu; 37 import com.android.browser.view.PieMenu.PieView.OnLayoutListener; 38 import com.android.browser.view.PieStackView; 39 import com.android.browser.view.PieStackView.OnCurrentListener; 40 41 import java.util.ArrayList; 42 import java.util.List; 43 44 /** 45 * Controller for Quick Controls pie menu 46 */ 47 public class PieControl implements PieMenu.PieController, OnClickListener { 48 49 protected Activity mActivity; 50 protected UiController mUiController; 51 protected PieMenu mPie; 52 protected int mItemSize; 53 protected TextView mTabsCount; 54 private BaseUi mUi; 55 private PieItem mBack; 56 private PieItem mForward; 57 private PieItem mRefresh; 58 private PieItem mUrl; 59 private PieItem mOptions; 60 private PieItem mBookmarks; 61 private PieItem mHistory; 62 private PieItem mAddBookmark; 63 private PieItem mNewTab; 64 private PieItem mIncognito; 65 private PieItem mClose; 66 private PieItem mShowTabs; 67 private PieItem mInfo; 68 private PieItem mFind; 69 private PieItem mShare; 70 private PieItem mRDS; 71 private TabAdapter mTabAdapter; 72 73 public PieControl(Activity activity, UiController controller, BaseUi ui) { 74 mActivity = activity; 75 mUiController = controller; 76 mItemSize = (int) activity.getResources().getDimension(R.dimen.qc_item_size); 77 mUi = ui; 78 } 79 80 public void stopEditingUrl() { 81 mUi.stopEditingUrl(); 82 } 83 84 protected void attachToContainer(FrameLayout container) { 85 if (mPie == null) { 86 mPie = new PieMenu(mActivity); 87 LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 88 LayoutParams.MATCH_PARENT); 89 mPie.setLayoutParams(lp); 90 populateMenu(); 91 mPie.setController(this); 92 } 93 container.addView(mPie); 94 } 95 96 protected void removeFromContainer(FrameLayout container) { 97 container.removeView(mPie); 98 } 99 100 protected void forceToTop(FrameLayout container) { 101 if (mPie.getParent() != null) { 102 container.removeView(mPie); 103 container.addView(mPie); 104 } 105 } 106 107 protected void setClickListener(OnClickListener listener, PieItem... items) { 108 for (PieItem item : items) { 109 item.getView().setOnClickListener(listener); 110 } 111 } 112 113 @Override 114 public boolean onOpen() { 115 int n = mUiController.getTabControl().getTabCount(); 116 mTabsCount.setText(Integer.toString(n)); 117 Tab tab = mUiController.getCurrentTab(); 118 if (tab != null) { 119 mForward.setEnabled(tab.canGoForward()); 120 } 121 WebView view = mUiController.getCurrentWebView(); 122 if (view != null) { 123 ImageView icon = (ImageView) mRDS.getView(); 124 if (mUiController.getSettings().hasDesktopUseragent(view)) { 125 icon.setImageResource(R.drawable.ic_mobile); 126 } else { 127 icon.setImageResource(R.drawable.ic_desktop_holo_dark); 128 } 129 } 130 return true; 131 } 132 133 protected void populateMenu() { 134 mBack = makeItem(R.drawable.ic_back_holo_dark, 1); 135 mUrl = makeItem(R.drawable.ic_web_holo_dark, 1); 136 mBookmarks = makeItem(R.drawable.ic_bookmarks_holo_dark, 1); 137 mHistory = makeItem(R.drawable.ic_history_holo_dark, 1); 138 mAddBookmark = makeItem(R.drawable.ic_bookmark_on_holo_dark, 1); 139 mRefresh = makeItem(R.drawable.ic_refresh_holo_dark, 1); 140 mForward = makeItem(R.drawable.ic_forward_holo_dark, 1); 141 mNewTab = makeItem(R.drawable.ic_new_window_holo_dark, 1); 142 mIncognito = makeItem(R.drawable.ic_new_incognito_holo_dark, 1); 143 mClose = makeItem(R.drawable.ic_close_window_holo_dark, 1); 144 mInfo = makeItem(android.R.drawable.ic_menu_info_details, 1); 145 mFind = makeItem(R.drawable.ic_search_holo_dark, 1); 146 mShare = makeItem(R.drawable.ic_share_holo_dark, 1); 147 View tabs = makeTabsView(); 148 mShowTabs = new PieItem(tabs, 1); 149 mOptions = makeItem(R.drawable.ic_settings_holo_dark, 1); 150 mRDS = makeItem(R.drawable.ic_desktop_holo_dark, 1); 151 mTabAdapter = new TabAdapter(mActivity, mUiController); 152 PieStackView stack = new PieStackView(mActivity); 153 stack.setLayoutListener(new OnLayoutListener() { 154 @Override 155 public void onLayout(int ax, int ay, boolean left) { 156 buildTabs(); 157 } 158 }); 159 stack.setOnCurrentListener(mTabAdapter); 160 stack.setAdapter(mTabAdapter); 161 mShowTabs.setPieView(stack); 162 setClickListener(this, mBack, mRefresh, mForward, mUrl, mFind, mInfo, 163 mShare, mBookmarks, mNewTab, mIncognito, mClose, mHistory, 164 mAddBookmark, mOptions, mRDS); 165 if (!BrowserActivity.isTablet(mActivity)) { 166 mShowTabs.getView().setOnClickListener(this); 167 } 168 // level 1 169 mPie.addItem(mOptions); 170 mOptions.addItem(mRDS); 171 mOptions.addItem(makeFiller()); 172 mOptions.addItem(makeFiller()); 173 mOptions.addItem(makeFiller()); 174 mPie.addItem(mBack); 175 mBack.addItem(mRefresh); 176 mBack.addItem(mForward); 177 mBack.addItem(makeFiller()); 178 mBack.addItem(makeFiller()); 179 mPie.addItem(mUrl); 180 mUrl.addItem(mFind); 181 mUrl.addItem(mShare); 182 mUrl.addItem(makeFiller()); 183 mUrl.addItem(makeFiller()); 184 mPie.addItem(mShowTabs); 185 mShowTabs.addItem(mClose); 186 mShowTabs.addItem(mIncognito); 187 mShowTabs.addItem(mNewTab); 188 mShowTabs.addItem(makeFiller()); 189 mPie.addItem(mBookmarks); 190 mBookmarks.addItem(makeFiller()); 191 mBookmarks.addItem(makeFiller()); 192 mBookmarks.addItem(mAddBookmark); 193 mBookmarks.addItem(mHistory); 194 } 195 196 @Override 197 public void onClick(View v) { 198 Tab tab = mUiController.getTabControl().getCurrentTab(); 199 WebView web = tab.getWebView(); 200 if (mBack.getView() == v) { 201 tab.goBack(); 202 } else if (mForward.getView() == v) { 203 tab.goForward(); 204 } else if (mRefresh.getView() == v) { 205 if (tab.inPageLoad()) { 206 web.stopLoading(); 207 } else { 208 web.reload(); 209 } 210 } else if (mUrl.getView() == v) { 211 mUi.editUrl(false, true); 212 } else if (mBookmarks.getView() == v) { 213 mUiController.bookmarksOrHistoryPicker(ComboViews.Bookmarks); 214 } else if (mHistory.getView() == v) { 215 mUiController.bookmarksOrHistoryPicker(ComboViews.History); 216 } else if (mAddBookmark.getView() == v) { 217 mUiController.bookmarkCurrentPage(); 218 } else if (mNewTab.getView() == v) { 219 mUiController.openTabToHomePage(); 220 mUi.editUrl(false, true); 221 } else if (mIncognito.getView() == v) { 222 mUiController.openIncognitoTab(); 223 mUi.editUrl(false, true); 224 } else if (mClose.getView() == v) { 225 mUiController.closeCurrentTab(); 226 } else if (mOptions.getView() == v) { 227 mUiController.openPreferences(); 228 } else if (mShare.getView() == v) { 229 mUiController.shareCurrentPage(); 230 } else if (mInfo.getView() == v) { 231 mUiController.showPageInfo(); 232 } else if (mFind.getView() == v) { 233 mUiController.findOnPage(); 234 } else if (mRDS.getView() == v) { 235 mUiController.toggleUserAgent(); 236 } else if (mShowTabs.getView() == v) { 237 ((PhoneUi) mUi).showNavScreen(); 238 } 239 } 240 241 private void buildTabs() { 242 final List<Tab> tabs = mUiController.getTabs(); 243 mUi.getActiveTab().capture(); 244 mTabAdapter.setTabs(tabs); 245 PieStackView sym = (PieStackView) mShowTabs.getPieView(); 246 sym.setCurrent(mUiController.getTabControl().getCurrentPosition()); 247 } 248 249 protected PieItem makeItem(int image, int l) { 250 ImageView view = new ImageView(mActivity); 251 view.setImageResource(image); 252 view.setMinimumWidth(mItemSize); 253 view.setMinimumHeight(mItemSize); 254 view.setScaleType(ScaleType.CENTER); 255 LayoutParams lp = new LayoutParams(mItemSize, mItemSize); 256 view.setLayoutParams(lp); 257 return new PieItem(view, l); 258 } 259 260 protected PieItem makeFiller() { 261 return new PieItem(null, 1); 262 } 263 264 protected View makeTabsView() { 265 View v = mActivity.getLayoutInflater().inflate(R.layout.qc_tabs_view, null); 266 mTabsCount = (TextView) v.findViewById(R.id.label); 267 mTabsCount.setText("1"); 268 ImageView image = (ImageView) v.findViewById(R.id.icon); 269 image.setImageResource(R.drawable.ic_windows_holo_dark); 270 image.setScaleType(ScaleType.CENTER); 271 LayoutParams lp = new LayoutParams(mItemSize, mItemSize); 272 v.setLayoutParams(lp); 273 return v; 274 } 275 276 static class TabAdapter extends BaseAdapter implements OnCurrentListener { 277 278 LayoutInflater mInflater; 279 UiController mUiController; 280 private List<Tab> mTabs; 281 private int mCurrent; 282 283 public TabAdapter(Context ctx, UiController ctl) { 284 mInflater = LayoutInflater.from(ctx); 285 mUiController = ctl; 286 mTabs = new ArrayList<Tab>(); 287 mCurrent = -1; 288 } 289 290 public void setTabs(List<Tab> tabs) { 291 mTabs = tabs; 292 notifyDataSetChanged(); 293 } 294 295 @Override 296 public int getCount() { 297 return mTabs.size(); 298 } 299 300 @Override 301 public Tab getItem(int position) { 302 return mTabs.get(position); 303 } 304 305 @Override 306 public long getItemId(int position) { 307 return position; 308 } 309 310 @Override 311 public View getView(int position, View convertView, ViewGroup parent) { 312 final Tab tab = mTabs.get(position); 313 View view = mInflater.inflate(R.layout.qc_tab, 314 null); 315 ImageView thumb = (ImageView) view.findViewById(R.id.thumb); 316 TextView title1 = (TextView) view.findViewById(R.id.title1); 317 TextView title2 = (TextView) view.findViewById(R.id.title2); 318 Bitmap b = tab.getScreenshot(); 319 if (b != null) { 320 thumb.setImageBitmap(b); 321 } 322 if (position > mCurrent) { 323 title1.setVisibility(View.GONE); 324 title2.setText(tab.getTitle()); 325 } else { 326 title2.setVisibility(View.GONE); 327 title1.setText(tab.getTitle()); 328 } 329 view.setOnClickListener(new OnClickListener() { 330 @Override 331 public void onClick(View v) { 332 mUiController.switchToTab(tab); 333 } 334 }); 335 return view; 336 } 337 338 @Override 339 public void onSetCurrent(int index) { 340 mCurrent = index; 341 } 342 343 } 344 345 } 346