Bug 868342 - (Part 4) Refactor BrowserToolbar.setTitle. r=wesj

This commit is contained in:
Margaret Leibovic 2013-05-14 16:19:37 -07:00
parent dffc4af192
commit 162728676c

View File

@ -151,10 +151,7 @@ public class BrowserToolbar implements Tabs.OnTabsChangedListener,
ThreadUtils.postToUiThread(new Runnable() { ThreadUtils.postToUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
Tab tab = Tabs.getInstance().getSelectedTab(); updateTitle();
if (tab != null) {
setTitle(tab.getDisplayTitle());
}
} }
}); });
} }
@ -446,7 +443,7 @@ public class BrowserToolbar implements Tabs.OnTabsChangedListener,
switch(msg) { switch(msg) {
case TITLE: case TITLE:
if (Tabs.getInstance().isSelectedTab(tab)) { if (Tabs.getInstance().isSelectedTab(tab)) {
setTitle(tab.getDisplayTitle()); updateTitle();
} }
break; break;
case START: case START:
@ -466,7 +463,7 @@ public class BrowserToolbar implements Tabs.OnTabsChangedListener,
updateForwardButton(tab.canDoForward()); updateForwardButton(tab.canDoForward());
setProgressVisibility(false); setProgressVisibility(false);
// Reset the title in case we haven't navigated to a new page yet. // Reset the title in case we haven't navigated to a new page yet.
setTitle(tab.getDisplayTitle()); updateTitle();
} }
break; break;
case RESTORED: case RESTORED:
@ -898,43 +895,46 @@ public class BrowserToolbar implements Tabs.OnTabsChangedListener,
} }
private void setTitle(CharSequence title) { private void setTitle(CharSequence title) {
mTitle.setText(title);
mLayout.setContentDescription(title != null ? title : mTitle.getHint());
}
// Sets the toolbar title according to the selected tab, obeying the mShowUrl prference.
private void updateTitle() {
Tab tab = Tabs.getInstance().getSelectedTab(); Tab tab = Tabs.getInstance().getSelectedTab();
// Keep the title unchanged if there's no selected tab, or if the tab is entering reader mode.
if (tab == null || tab.isEnteringReaderMode()) {
return;
}
if (tab != null) { String url = tab.getURL();
// Keep the title unchanged if the tab is entering reader mode // Setting a null title will ensure we just see the "Enter Search or Address" placeholder text.
if (tab.isEnteringReaderMode()) { if ("about:home".equals(url) || "about:privatebrowsing".equals(url)) {
return; setTitle(null);
} return;
}
// Setting a null title will ensure we just see the "Enter Search or Address" // If the pref to show the URL isn't set, just use the tab's display title.
// placeholder text. Because "about:home" and "about:privatebrowsing" don't if (!mShowUrl || url == null) {
// have titles, their display titles will always match their URLs. setTitle(tab.getDisplayTitle());
if ("about:home".equals(title) || "about:privatebrowsing".equals(title)) { return;
title = null; }
}
String url = tab.getURL(); url = StringUtils.stripScheme(url);
if (mShowUrl && title != null && url != null) { CharSequence title = StringUtils.stripCommonSubdomains(url);
url = StringUtils.stripScheme(url);
title = StringUtils.stripCommonSubdomains(url);
// highlight the domain name if we find one String baseDomain = tab.getBaseDomain();
String baseDomain = tab.getBaseDomain(); if (!TextUtils.isEmpty(baseDomain)) {
if (!TextUtils.isEmpty(baseDomain)) { SpannableStringBuilder builder = new SpannableStringBuilder(title);
SpannableStringBuilder builder = new SpannableStringBuilder(title); int index = title.toString().indexOf(baseDomain);
int index = title.toString().indexOf(baseDomain); if (index > -1) {
if (index > -1) { builder.setSpan(mUrlColor, 0, title.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
builder.setSpan(mUrlColor, 0, title.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); builder.setSpan(tab.isPrivate() ? mPrivateDomainColor : mDomainColor, index, index+baseDomain.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
builder.setSpan(tab.isPrivate() ? mPrivateDomainColor : mDomainColor, index, index+baseDomain.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); title = builder;
title = builder;
}
}
} }
} }
mTitle.setText(title); setTitle(title);
mLayout.setContentDescription(title != null ? title : mTitle.getHint());
} }
private void setFavicon(Bitmap image) { private void setFavicon(Bitmap image) {
@ -1143,8 +1143,7 @@ public class BrowserToolbar implements Tabs.OnTabsChangedListener,
public void refresh() { public void refresh() {
Tab tab = Tabs.getInstance().getSelectedTab(); Tab tab = Tabs.getInstance().getSelectedTab();
if (tab != null) { if (tab != null) {
String url = tab.getURL(); updateTitle();
setTitle(tab.getDisplayTitle());
setFavicon(tab.getFavicon()); setFavicon(tab.getFavicon());
setProgressVisibility(tab.getState() == Tab.STATE_LOADING); setProgressVisibility(tab.getState() == Tab.STATE_LOADING);
setSecurityMode(tab.getSecurityMode()); setSecurityMode(tab.getSecurityMode());