OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.toolbar; |
| 6 |
| 7 import android.animation.Animator; |
| 8 import android.animation.ObjectAnimator; |
| 9 import android.annotation.SuppressLint; |
| 10 import android.content.Context; |
| 11 import android.content.res.ColorStateList; |
| 12 import android.graphics.Bitmap; |
| 13 import android.graphics.drawable.BitmapDrawable; |
| 14 import android.graphics.drawable.ColorDrawable; |
| 15 import android.text.TextUtils; |
| 16 import android.util.AttributeSet; |
| 17 import android.util.Pair; |
| 18 import android.view.KeyEvent; |
| 19 import android.view.MotionEvent; |
| 20 import android.view.View; |
| 21 import android.widget.ImageButton; |
| 22 import android.widget.ImageView; |
| 23 |
| 24 import com.google.android.apps.chrome.R; |
| 25 |
| 26 import org.chromium.base.ApiCompatibilityUtils; |
| 27 import org.chromium.chrome.browser.ContextualMenuBar.ActionBarDelegate; |
| 28 import org.chromium.chrome.browser.CustomSelectionActionModeCallback; |
| 29 import org.chromium.chrome.browser.WindowDelegate; |
| 30 import org.chromium.chrome.browser.appmenu.AppMenuButtonHelper; |
| 31 import org.chromium.chrome.browser.document.BrandColorUtils; |
| 32 import org.chromium.chrome.browser.dom_distiller.DomDistillerServiceFactory; |
| 33 import org.chromium.chrome.browser.dom_distiller.DomDistillerTabUtils; |
| 34 import org.chromium.chrome.browser.ntp.NativePageFactory; |
| 35 import org.chromium.chrome.browser.ntp.NewTabPage; |
| 36 import org.chromium.chrome.browser.omnibox.LocationBar; |
| 37 import org.chromium.chrome.browser.omnibox.LocationBarLayout; |
| 38 import org.chromium.chrome.browser.omnibox.UrlBar; |
| 39 import org.chromium.chrome.browser.omnibox.UrlContainer; |
| 40 import org.chromium.chrome.browser.omnibox.UrlFocusChangeListener; |
| 41 import org.chromium.chrome.browser.profiles.Profile; |
| 42 import org.chromium.chrome.browser.ssl.ConnectionSecurityHelperSecurityLevel; |
| 43 import org.chromium.chrome.browser.tab.ChromeTab; |
| 44 import org.chromium.chrome.browser.widget.TintedImageButton; |
| 45 import org.chromium.components.dom_distiller.core.DomDistillerService; |
| 46 import org.chromium.components.dom_distiller.core.DomDistillerUrlUtils; |
| 47 import org.chromium.ui.base.WindowAndroid; |
| 48 |
| 49 /** |
| 50 * The Toolbar layout to be used for hosted mode. This is used for both phone an
d tablet UIs. |
| 51 */ |
| 52 public class HostedToolbar extends ToolbarLayout implements LocationBar { |
| 53 private UrlBar mUrlBar; |
| 54 private ImageView mSecurityButton; |
| 55 private ImageButton mCustomActionButton; |
| 56 private int mSecurityIconType; |
| 57 private boolean mUseDarkColors; |
| 58 private UrlContainer mUrlContainer; |
| 59 private TintedImageButton mBackButton; |
| 60 private Animator mSecurityButtonShowAnimator; |
| 61 private boolean mBackgroundColorSet; |
| 62 |
| 63 /** |
| 64 * Constructor for getting this class inflated from an xml layout file. |
| 65 */ |
| 66 public HostedToolbar(Context context, AttributeSet attrs) { |
| 67 super(context, attrs); |
| 68 } |
| 69 |
| 70 @Override |
| 71 protected void onFinishInflate() { |
| 72 super.onFinishInflate(); |
| 73 setBackground(new ColorDrawable(getResources().getColor(R.color.default_
primary_color))); |
| 74 mUrlBar = (UrlBar) findViewById(R.id.url_bar); |
| 75 mUrlBar.setHint(""); |
| 76 mUrlBar.setDelegate(this); |
| 77 mUrlBar.setEnabled(false); |
| 78 mUrlContainer = (UrlContainer) findViewById(R.id.url_container); |
| 79 mSecurityButton = (ImageButton) findViewById(R.id.security_button); |
| 80 mSecurityIconType = ConnectionSecurityHelperSecurityLevel.NONE; |
| 81 mCustomActionButton = (ImageButton) findViewById(R.id.action_button); |
| 82 mBackButton = (TintedImageButton) findViewById(R.id.back_button); |
| 83 mSecurityButtonShowAnimator = ObjectAnimator.ofFloat(mSecurityButton, AL
PHA, 1); |
| 84 mSecurityButtonShowAnimator |
| 85 .setDuration(ToolbarPhone.URL_FOCUS_CHANGE_ANIMATION_DURATION_MS
); |
| 86 } |
| 87 |
| 88 @Override |
| 89 public void initialize(ToolbarDataProvider toolbarDataProvider, |
| 90 ToolbarTabController tabController, AppMenuButtonHelper appMenuButto
nHelper) { |
| 91 super.initialize(toolbarDataProvider, tabController, appMenuButtonHelper
); |
| 92 updateVisualsForState(); |
| 93 } |
| 94 |
| 95 @Override |
| 96 public void setHostedBackClickHandler(OnClickListener listener) { |
| 97 mBackButton.setOnClickListener(listener); |
| 98 } |
| 99 |
| 100 @Override |
| 101 public void addCustomActionButton(Bitmap buttonSource, OnClickListener liste
ner) { |
| 102 mCustomActionButton.setImageDrawable(new BitmapDrawable(getResources(),
buttonSource)); |
| 103 mCustomActionButton.setOnClickListener(listener); |
| 104 mCustomActionButton.setVisibility(VISIBLE); |
| 105 } |
| 106 |
| 107 @Override |
| 108 public ChromeTab getCurrentTab() { |
| 109 return ChromeTab.fromTab(getToolbarDataProvider().getTab()); |
| 110 } |
| 111 |
| 112 @Override |
| 113 public boolean showingOriginalUrlForPreview() { |
| 114 return false; |
| 115 } |
| 116 |
| 117 @Override |
| 118 public boolean shouldEmphasizeHttpsScheme() { |
| 119 return !mUseDarkColors; |
| 120 } |
| 121 |
| 122 @Override |
| 123 public void setUrlToPageUrl() { |
| 124 if (getCurrentTab() == null) { |
| 125 mUrlContainer.setUrlText(null, null, ""); |
| 126 return; |
| 127 } |
| 128 |
| 129 String url = getCurrentTab().getUrl().trim(); |
| 130 |
| 131 if (NativePageFactory.isNativePageUrl(url, getCurrentTab().isIncognito()
)) { |
| 132 // Don't show anything for Chrome URLs. |
| 133 mUrlContainer.setUrlText(null, null, ""); |
| 134 return; |
| 135 } |
| 136 String displayText = getToolbarDataProvider().getText(); |
| 137 Pair<String, String> urlText = LocationBarLayout.splitPathFromUrlDisplay
Text(displayText); |
| 138 displayText = urlText.first; |
| 139 String path = urlText.second; |
| 140 |
| 141 if (DomDistillerUrlUtils.isDistilledPage(url)) { |
| 142 if (isStoredArticle(url)) { |
| 143 Profile profile = getCurrentTab().getProfile(); |
| 144 DomDistillerService domDistillerService = |
| 145 DomDistillerServiceFactory.getForProfile(profile); |
| 146 String originalUrl = domDistillerService.getUrlForEntry( |
| 147 DomDistillerUrlUtils.getValueForKeyInUrl(url, "entry_id"
)); |
| 148 displayText = |
| 149 DomDistillerTabUtils.getFormattedUrlFromOriginalDistille
rUrl(originalUrl); |
| 150 } else if (DomDistillerUrlUtils.getOriginalUrlFromDistillerUrl(url)
!= null) { |
| 151 String originalUrl = DomDistillerUrlUtils.getOriginalUrlFromDist
illerUrl(url); |
| 152 displayText = |
| 153 DomDistillerTabUtils.getFormattedUrlFromOriginalDistille
rUrl(originalUrl); |
| 154 } |
| 155 } |
| 156 |
| 157 if (mUrlContainer.setUrlText(displayText, path, url)) { |
| 158 mUrlBar.deEmphasizeUrl(); |
| 159 mUrlBar.emphasizeUrl(); |
| 160 } |
| 161 } |
| 162 |
| 163 private boolean isStoredArticle(String url) { |
| 164 DomDistillerService domDistillerService = |
| 165 DomDistillerServiceFactory.getForProfile(Profile.getLastUsedProf
ile()); |
| 166 String entryIdFromUrl = DomDistillerUrlUtils.getValueForKeyInUrl(url, "e
ntry_id"); |
| 167 if (TextUtils.isEmpty(entryIdFromUrl)) return false; |
| 168 return domDistillerService.hasEntry(entryIdFromUrl); |
| 169 } |
| 170 |
| 171 @Override |
| 172 public void updateLoadingState(boolean updateUrl) { |
| 173 updateSecurityIcon(getSecurityLevel()); |
| 174 } |
| 175 |
| 176 @Override |
| 177 public void updateVisualsForState() { |
| 178 updateSecurityIcon(getSecurityLevel()); |
| 179 ColorStateList colorStateList = getResources().getColorStateList(mUseDar
kColors |
| 180 ? R.color.dark_mode_tint : R.color.light_mode_tint); |
| 181 mMenuButton.setTint(colorStateList); |
| 182 mBackButton.setTint(colorStateList); |
| 183 mUrlContainer.setUseDarkTextColors(mUseDarkColors); |
| 184 |
| 185 if (getProgressBar() != null) { |
| 186 int progressBarResource = !mUseDarkColors |
| 187 ? R.drawable.progress_bar_white : R.drawable.progress_bar; |
| 188 getProgressBar().setProgressDrawable( |
| 189 ApiCompatibilityUtils.getDrawable(getResources(), progressBa
rResource)); |
| 190 } |
| 191 } |
| 192 |
| 193 @Override |
| 194 public void setMenuButtonHelper(final AppMenuButtonHelper helper) { |
| 195 mMenuButton.setOnTouchListener(new OnTouchListener() { |
| 196 @SuppressLint("ClickableViewAccessibility") |
| 197 @Override |
| 198 public boolean onTouch(View v, MotionEvent event) { |
| 199 return helper.onTouch(v, event); |
| 200 } |
| 201 }); |
| 202 mMenuButton.setOnKeyListener(new OnKeyListener() { |
| 203 @Override |
| 204 public boolean onKey(View view, int keyCode, KeyEvent event) { |
| 205 if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == Ke
yEvent.ACTION_UP) { |
| 206 return helper.onEnterKeyPress(view); |
| 207 } |
| 208 return false; |
| 209 } |
| 210 }); |
| 211 } |
| 212 |
| 213 @Override |
| 214 public View getMenuAnchor() { |
| 215 return mMenuButton; |
| 216 } |
| 217 |
| 218 @Override |
| 219 public ColorDrawable getBackground() { |
| 220 return (ColorDrawable) super.getBackground(); |
| 221 } |
| 222 |
| 223 @Override |
| 224 public void initializeControls(WindowDelegate windowDelegate, ActionBarDeleg
ate delegate, |
| 225 WindowAndroid windowAndroid) { |
| 226 } |
| 227 |
| 228 private int getSecurityLevel() { |
| 229 if (getCurrentTab() == null) return ConnectionSecurityHelperSecurityLeve
l.NONE; |
| 230 return getCurrentTab().getSecurityLevel(); |
| 231 } |
| 232 |
| 233 @Override |
| 234 public void updateSecurityIcon(int securityLevel) { |
| 235 // ImageView#setImageResource is no-op if given resource is the current
one. |
| 236 mSecurityButton.setImageResource(LocationBarLayout.getSecurityIconResour
ce( |
| 237 securityLevel, !shouldEmphasizeHttpsScheme())); |
| 238 |
| 239 if (mSecurityIconType == securityLevel) return; |
| 240 mSecurityIconType = securityLevel; |
| 241 |
| 242 if (securityLevel == ConnectionSecurityHelperSecurityLevel.NONE) { |
| 243 // TODO(yusufo): Add an animator for hiding as well. |
| 244 mSecurityButton.setVisibility(GONE); |
| 245 } else { |
| 246 if (mSecurityButtonShowAnimator.isRunning()) mSecurityButtonShowAnim
ator.cancel(); |
| 247 mSecurityButton.setVisibility(VISIBLE); |
| 248 mSecurityButtonShowAnimator.start(); |
| 249 mUrlBar.deEmphasizeUrl(); |
| 250 } |
| 251 mUrlBar.emphasizeUrl(); |
| 252 mUrlBar.invalidate(); |
| 253 } |
| 254 |
| 255 /** |
| 256 * For extending classes to override and carry out the changes related with
the primary color |
| 257 * for the current tab changing. |
| 258 */ |
| 259 @Override |
| 260 protected void onPrimaryColorChanged() { |
| 261 if (mBackgroundColorSet) return; |
| 262 int primaryColor = getToolbarDataProvider().getPrimaryColor(); |
| 263 getBackground().setColor(primaryColor); |
| 264 mUseDarkColors = !BrandColorUtils.shouldUseLightDrawablesForToolbar(prim
aryColor); |
| 265 updateVisualsForState(); |
| 266 mBackgroundColorSet = true; |
| 267 } |
| 268 |
| 269 @Override |
| 270 protected void onNavigatedToDifferentPage() { |
| 271 super.onNavigatedToDifferentPage(); |
| 272 mUrlContainer.setTrailingTextVisible(true); |
| 273 } |
| 274 |
| 275 @Override |
| 276 public void setLoadProgress(int progress) { |
| 277 super.setLoadProgress(progress); |
| 278 if (progress == 100) mUrlContainer.setTrailingTextVisible(false); |
| 279 } |
| 280 |
| 281 @Override |
| 282 public View getContainerView() { |
| 283 return this; |
| 284 } |
| 285 |
| 286 @Override |
| 287 public void setDefaultTextEditActionModeCallback(CustomSelectionActionModeCa
llback callback) { |
| 288 mUrlBar.setCustomSelectionActionModeCallback(callback); |
| 289 } |
| 290 |
| 291 private void updateLayoutParams() { |
| 292 int startMargin = 0; |
| 293 int urlContainerChildIndex = -1; |
| 294 for (int i = 0; i < getChildCount(); i++) { |
| 295 View childView = getChildAt(i); |
| 296 if (childView.getVisibility() != GONE) { |
| 297 LayoutParams childLayoutParams = (LayoutParams) childView.getLay
outParams(); |
| 298 if (ApiCompatibilityUtils.getMarginStart(childLayoutParams) != s
tartMargin) { |
| 299 ApiCompatibilityUtils.setMarginStart(childLayoutParams, star
tMargin); |
| 300 childView.setLayoutParams(childLayoutParams); |
| 301 } |
| 302 if (childView == mUrlContainer) { |
| 303 urlContainerChildIndex = i; |
| 304 break; |
| 305 } |
| 306 int widthMeasureSpec; |
| 307 int heightMeasureSpec; |
| 308 if (childLayoutParams.width == LayoutParams.WRAP_CONTENT) { |
| 309 widthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 310 getMeasuredWidth(), MeasureSpec.AT_MOST); |
| 311 } else if (childLayoutParams.width == LayoutParams.MATCH_PARENT)
{ |
| 312 widthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 313 getMeasuredWidth(), MeasureSpec.EXACTLY); |
| 314 } else { |
| 315 widthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 316 childLayoutParams.width, MeasureSpec.EXACTLY); |
| 317 } |
| 318 if (childLayoutParams.height == LayoutParams.WRAP_CONTENT) { |
| 319 heightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 320 getMeasuredHeight(), MeasureSpec.AT_MOST); |
| 321 } else if (childLayoutParams.height == LayoutParams.MATCH_PARENT
) { |
| 322 heightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 323 getMeasuredHeight(), MeasureSpec.EXACTLY); |
| 324 } else { |
| 325 heightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 326 childLayoutParams.height, MeasureSpec.EXACTLY); |
| 327 } |
| 328 childView.measure(widthMeasureSpec, heightMeasureSpec); |
| 329 startMargin += childView.getMeasuredWidth(); |
| 330 } |
| 331 } |
| 332 |
| 333 assert urlContainerChildIndex != -1; |
| 334 int urlContainerMarginEnd = 0; |
| 335 for (int i = urlContainerChildIndex + 1; i < getChildCount(); i++) { |
| 336 View childView = getChildAt(i); |
| 337 if (childView.getVisibility() != GONE) { |
| 338 urlContainerMarginEnd += childView.getMeasuredWidth(); |
| 339 } |
| 340 } |
| 341 LayoutParams urlLayoutParams = (LayoutParams) mUrlContainer.getLayoutPar
ams(); |
| 342 |
| 343 if (ApiCompatibilityUtils.getMarginEnd(urlLayoutParams) != urlContainerM
arginEnd) { |
| 344 ApiCompatibilityUtils.setMarginEnd(urlLayoutParams, urlContainerMarg
inEnd); |
| 345 mUrlContainer.setLayoutParams(urlLayoutParams); |
| 346 } |
| 347 } |
| 348 |
| 349 @Override |
| 350 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 351 updateLayoutParams(); |
| 352 super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 353 } |
| 354 |
| 355 @Override |
| 356 public LocationBar getLocationBar() { |
| 357 return this; |
| 358 } |
| 359 |
| 360 // Toolbar and LocationBar calls that are not relevant here. |
| 361 |
| 362 @Override |
| 363 public void setToolbarDataProvider(ToolbarDataProvider model) { |
| 364 assert model.equals(getToolbarDataProvider()); |
| 365 } |
| 366 |
| 367 @Override |
| 368 public void onUrlPreFocusChanged(boolean gainFocus) { |
| 369 } |
| 370 |
| 371 @Override |
| 372 public void setUrlFocusChangeListener(UrlFocusChangeListener listener) { } |
| 373 |
| 374 @Override |
| 375 public void setUrlBarFocus(boolean shouldBeFocused) { } |
| 376 |
| 377 @Override |
| 378 public long getFirstUrlBarFocusTime() { |
| 379 return 0; |
| 380 } |
| 381 |
| 382 @Override |
| 383 public void setIgnoreURLBarModification(boolean ignore) { |
| 384 } |
| 385 |
| 386 @Override |
| 387 public void hideSuggestions() { |
| 388 } |
| 389 |
| 390 @Override |
| 391 public void updateMicButtonState() { |
| 392 } |
| 393 |
| 394 @Override |
| 395 public void onTabLoadingNTP(NewTabPage ntp) { |
| 396 } |
| 397 |
| 398 @Override |
| 399 public void setAutocompleteProfile(Profile profile) { |
| 400 } |
| 401 |
| 402 @Override |
| 403 public void backKeyPressed() { } |
| 404 } |
OLD | NEW |