1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko;
import org.mozilla.gecko.animation.PropertyAnimator;
import org.mozilla.gecko.animation.ViewHelper;
import org.mozilla.gecko.gfx.ImmutableViewportMetrics;
import org.mozilla.gecko.gfx.LayerView;
import org.mozilla.gecko.menu.GeckoMenu;
import org.mozilla.gecko.menu.MenuPopup;
import org.mozilla.gecko.util.Clipboard;
import org.mozilla.gecko.util.StringUtils;
import org.mozilla.gecko.util.HardwareUtils;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.util.UiAsyncTask;
import org.mozilla.gecko.PrefsHelper;
import org.json.JSONObject;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.os.Handler;
import android.os.SystemClock;
import android.text.style.ForegroundColorSpan;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.Window;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AlphaAnimation;
import android.view.animation.Interpolator;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ViewSwitcher;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BrowserToolbar implements Tabs.OnTabsChangedListener,
GeckoMenu.ActionItemBarPresenter,
Animation.AnimationListener {
private static final String LOGTAG = "GeckoToolbar";
public static final String PREF_TITLEBAR_MODE = "browser.chrome.titlebarMode";
private GeckoRelativeLayout mLayout;
private LayoutParams mAwesomeBarParams;
private View mUrlDisplayContainer;
private View mAwesomeBarEntry;
private ImageView mAwesomeBarRightEdge;
private BrowserToolbarBackground mAddressBarBg;
private GeckoTextView mTitle;
private int mTitlePadding;
private boolean mSiteSecurityVisible;
private boolean mSwitchingTabs;
private ShapedButton mTabs;
private ImageButton mBack;
private ImageButton mForward;
public ImageButton mFavicon;
public ImageButton mStop;
public ImageButton mSiteSecurity;
public ImageButton mReader;
private AnimationDrawable mProgressSpinner;
private TabCounter mTabsCounter;
private ImageView mShadow;
private GeckoImageButton mMenu;
private GeckoImageView mMenuIcon;
private LinearLayout mActionItemBar;
private MenuPopup mMenuPopup;
private List<View> mFocusOrder;
final private BrowserApp mActivity;
private Handler mHandler;
private boolean mHasSoftMenuButton;
private boolean mShowSiteSecurity;
private boolean mShowReader;
private boolean mAnimatingEntry;
private AlphaAnimation mLockFadeIn;
private TranslateAnimation mTitleSlideLeft;
private TranslateAnimation mTitleSlideRight;
private int mAddressBarViewOffset;
private int mDefaultForwardMargin;
private PropertyAnimator mForwardAnim = null;
private int mFaviconSize;
private PropertyAnimator mVisibilityAnimator;
private static final Interpolator sButtonsInterpolator = new AccelerateInterpolator();
private static final int TABS_CONTRACTED = 1;
private static final int TABS_EXPANDED = 2;
private static final int FORWARD_ANIMATION_DURATION = 450;
private final ForegroundColorSpan mUrlColor;
private final ForegroundColorSpan mDomainColor;
private final ForegroundColorSpan mPrivateDomainColor;
private boolean mShowUrl;
private Integer mPrefObserverId;
public BrowserToolbar(BrowserApp activity) {
// BrowserToolbar is attached to BrowserApp only.
mActivity = activity;
Tabs.registerOnTabsChangedListener(this);
mSwitchingTabs = true;
mAnimatingEntry = false;
mShowUrl = false;
// listen to the title bar pref.
mPrefObserverId = PrefsHelper.getPref(PREF_TITLEBAR_MODE, new PrefsHelper.PrefHandlerBase() {
@Override
public void prefValue(String pref, String str) {
int value = Integer.parseInt(str);
boolean shouldShowUrl = (value == 1);
if (shouldShowUrl == mShowUrl) {
return;
}
mShowUrl = shouldShowUrl;
ThreadUtils.postToUiThread(new Runnable() {
@Override
public void run() {
updateTitle();
}
});
}
@Override
public boolean isObserver() {
// We want to be notified of changes to be able to switch mode
// without restarting.
return true;
}
});
Resources res = mActivity.getResources();
mUrlColor = new ForegroundColorSpan(res.getColor(R.color.url_bar_urltext));
mDomainColor = new ForegroundColorSpan(res.getColor(R.color.url_bar_domaintext));
mPrivateDomainColor = new ForegroundColorSpan(res.getColor(R.color.url_bar_domaintext_private));
}
public void from(RelativeLayout layout) {
if (mLayout != null) {
// make sure we retain the visibility property on rotation
layout.setVisibility(mLayout.getVisibility());
}
mLayout = (GeckoRelativeLayout) layout;
mLayout.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
mActivity.autoHideTabs();
onAwesomeBarSearch();
}
});
mLayout.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = mActivity.getMenuInflater();
inflater.inflate(R.menu.titlebar_contextmenu, menu);
String clipboard = Clipboard.getText();
if (TextUtils.isEmpty(clipboard)) {
menu.findItem(R.id.pasteandgo).setVisible(false);
menu.findItem(R.id.paste).setVisible(false);
}
Tab tab = Tabs.getInstance().getSelectedTab();
if (tab != null) {
String url = tab.getURL();
if (url == null) {
menu.findItem(R.id.copyurl).setVisible(false);
menu.findItem(R.id.share).setVisible(false);
menu.findItem(R.id.add_to_launcher).setVisible(false);
}
if (!tab.getFeedsEnabled()) {
menu.findItem(R.id.subscribe).setVisible(false);
}
} else {
// if there is no tab, remove anything tab dependent
menu.findItem(R.id.copyurl).setVisible(false);
menu.findItem(R.id.share).setVisible(false);
menu.findItem(R.id.add_to_launcher).setVisible(false);
menu.findItem(R.id.subscribe).setVisible(false);
}
}
});
mShowSiteSecurity = false;
mShowReader = false;
mAnimatingEntry = false;
mAddressBarBg = (BrowserToolbarBackground) mLayout.findViewById(R.id.address_bar_bg);
mAddressBarViewOffset = mActivity.getResources().getDimensionPixelSize(R.dimen.addressbar_offset_left);
mDefaultForwardMargin = mActivity.getResources().getDimensionPixelSize(R.dimen.forward_default_offset);
mUrlDisplayContainer = mLayout.findViewById(R.id.awesome_bar_display_container);
mAwesomeBarEntry = mLayout.findViewById(R.id.awesome_bar_entry);
// This will clip the right edge's image at half of its width
mAwesomeBarRightEdge = (ImageView) mLayout.findViewById(R.id.awesome_bar_right_edge);
if (mAwesomeBarRightEdge != null) {
mAwesomeBarRightEdge.getDrawable().setLevel(5000);
}
mTitle = (GeckoTextView) mLayout.findViewById(R.id.awesome_bar_title);
mTitlePadding = mTitle.getPaddingRight();
mTabs = (ShapedButton) mLayout.findViewById(R.id.tabs);
mTabs.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
toggleTabs();
}
});
mTabs.setImageLevel(0);
mTabsCounter = (TabCounter) mLayout.findViewById(R.id.tabs_counter);
mBack = (ImageButton) mLayout.findViewById(R.id.back);
mBack.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
Tabs.getInstance().getSelectedTab().doBack();
}
});
mBack.setOnLongClickListener(new Button.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return Tabs.getInstance().getSelectedTab().showBackHistory();
}
});
mForward = (ImageButton) mLayout.findViewById(R.id.forward);
mForward.setEnabled(false); // initialize the forward button to not be enabled
mForward.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
Tabs.getInstance().getSelectedTab().doForward();
}
});
mForward.setOnLongClickListener(new Button.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return Tabs.getInstance().getSelectedTab().showForwardHistory();
}
});
Button.OnClickListener faviconListener = new Button.OnClickListener() {
@Override
public void onClick(View view) {
if (mSiteSecurity.getVisibility() != View.VISIBLE)
return;
JSONObject identityData = Tabs.getInstance().getSelectedTab().getIdentityData();
if (identityData == null) {
Log.e(LOGTAG, "Selected tab has no identity data");
return;
}
SiteIdentityPopup siteIdentityPopup = mActivity.getSiteIdentityPopup();
siteIdentityPopup.updateIdentity(identityData);
siteIdentityPopup.show();
}
};
mFavicon = (ImageButton) mLayout.findViewById(R.id.favicon);
mFavicon.setOnClickListener(faviconListener);
if (Build.VERSION.SDK_INT >= 16)
mFavicon.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
mFaviconSize = Math.round(mActivity.getResources().getDimension(R.dimen.browser_toolbar_favicon_size));
mSiteSecurity = (ImageButton) mLayout.findViewById(R.id.site_security);
mSiteSecurity.setOnClickListener(faviconListener);
mSiteSecurityVisible = (mSiteSecurity.getVisibility() == View.VISIBLE);
mActivity.getSiteIdentityPopup().setAnchor(mSiteSecurity);
mProgressSpinner = (AnimationDrawable) mActivity.getResources().getDrawable(R.drawable.progress_spinner);
mStop = (ImageButton) mLayout.findViewById(R.id.stop);
mStop.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Tab tab = Tabs.getInstance().getSelectedTab();
if (tab != null)
tab.doStop();
setProgressVisibility(false);
}
});
mReader = (ImageButton) mLayout.findViewById(R.id.reader);
mReader.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
Tab tab = Tabs.getInstance().getSelectedTab();
if (tab != null) {
tab.toggleReaderMode();
}
}
});
mReader.setOnLongClickListener(new Button.OnLongClickListener() {
public boolean onLongClick(View v) {
Tab tab = Tabs.getInstance().getSelectedTab();
if (tab != null) {
tab.addToReadingList();
return true;
}
return false;
}
});
mShadow = (ImageView) mLayout.findViewById(R.id.shadow);
mShadow.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
}
});
if (Build.VERSION.SDK_INT >= 16) {
mShadow.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
}
mHandler = new Handler();
float slideWidth = mActivity.getResources().getDimension(R.dimen.browser_toolbar_lock_width);
LinearLayout.LayoutParams siteSecParams = (LinearLayout.LayoutParams) mSiteSecurity.getLayoutParams();
final float scale = mActivity.getResources().getDisplayMetrics().density;
slideWidth += (siteSecParams.leftMargin + siteSecParams.rightMargin) * scale + 0.5f;
mLockFadeIn = new AlphaAnimation(0.0f, 1.0f);
mLockFadeIn.setAnimationListener(this);
mTitleSlideLeft = new TranslateAnimation(slideWidth, 0, 0, 0);
mTitleSlideLeft.setAnimationListener(this);
mTitleSlideRight = new TranslateAnimation(-slideWidth, 0, 0, 0);
mTitleSlideRight.setAnimationListener(this);
final int lockAnimDuration = 300;
mLockFadeIn.setDuration(lockAnimDuration);
mTitleSlideLeft.setDuration(lockAnimDuration);
mTitleSlideRight.setDuration(lockAnimDuration);
mMenu = (GeckoImageButton) mLayout.findViewById(R.id.menu);
mMenuIcon = (GeckoImageView) mLayout.findViewById(R.id.menu_icon);
mActionItemBar = (LinearLayout) mLayout.findViewById(R.id.menu_items);
mHasSoftMenuButton = !HardwareUtils.hasMenuButton();
if (mHasSoftMenuButton) {
mMenu.setVisibility(View.VISIBLE);
mMenuIcon.setVisibility(View.VISIBLE);
mMenu.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
mActivity.openOptionsMenu();
}
});
}
if (!HardwareUtils.isTablet()) {
// Set a touch delegate to Tabs button, so the touch events on its tail
// are passed to the menu button.
mLayout.post(new Runnable() {
@Override
public void run() {
int height = mTabs.getHeight();
int width = mTabs.getWidth();
int tail = (width - height) / 2;
Rect bounds = new Rect(0, 0, tail, height);
TailTouchDelegate delegate = new TailTouchDelegate(bounds, mShadow);
mTabs.setTouchDelegate(delegate);
}
});
}
if (Build.VERSION.SDK_INT >= 11) {
View panel = mActivity.getMenuPanel();
// If panel is null, the app is starting up for the first time;
// add this to the popup only if we have a soft menu button.
// else, browser-toolbar is initialized on rotation,
// and we need to re-attach action-bar items.
if (panel == null) {
mActivity.onCreatePanelMenu(Window.FEATURE_OPTIONS_PANEL, null);
panel = mActivity.getMenuPanel();
if (mHasSoftMenuButton) {
mMenuPopup = new MenuPopup(mActivity);
mMenuPopup.setPanelView(panel);
mMenuPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
mActivity.onOptionsMenuClosed(null);
}
});
}
}
}
mFocusOrder = Arrays.asList(mBack, mForward, mLayout, mReader, mSiteSecurity, mStop, mTabs);
}
public View getLayout() {
return mLayout;
}
@Override
public void onTabChanged(Tab tab, Tabs.TabEvents msg, Object data) {
switch(msg) {
case TITLE:
if (Tabs.getInstance().isSelectedTab(tab)) {
updateTitle();
}
break;
case START:
if (Tabs.getInstance().isSelectedTab(tab)) {
updateBackButton(tab.canDoBack());
updateForwardButton(tab.canDoForward());
Boolean showProgress = (Boolean)data;
if (showProgress && tab.getState() == Tab.STATE_LOADING)
setProgressVisibility(true);
setSecurityMode(tab.getSecurityMode());
setReaderMode(tab.getReaderEnabled());
}
break;
case STOP:
if (Tabs.getInstance().isSelectedTab(tab)) {
updateBackButton(tab.canDoBack());
updateForwardButton(tab.canDoForward());
setProgressVisibility(false);
// Reset the title in case we haven't navigated to a new page yet.
updateTitle();
}
break;
case RESTORED:
// TabCount fixup after OOM
case SELECTED:
updateTabCount(Tabs.getInstance().getDisplayCount());
mSwitchingTabs = true;
// fall through
case LOCATION_CHANGE:
case LOAD_ERROR:
if (Tabs.getInstance().isSelectedTab(tab)) {
refresh();
}
mSwitchingTabs = false;
break;
case CLOSED:
case ADDED:
updateTabCount(Tabs.getInstance().getDisplayCount());
if (Tabs.getInstance().isSelectedTab(tab)) {
updateBackButton(tab.canDoBack());
updateForwardButton(tab.canDoForward());
}
break;
case FAVICON:
if (Tabs.getInstance().isSelectedTab(tab)) {
setFavicon(tab.getFavicon());
}
break;
case SECURITY_CHANGE:
if (Tabs.getInstance().isSelectedTab(tab)) {
setSecurityMode(tab.getSecurityMode());
}
break;
case READER_ENABLED:
if (Tabs.getInstance().isSelectedTab(tab)) {
setReaderMode(tab.getReaderEnabled());
}
break;
}
}
public boolean isVisible() {
return mLayout.getScrollY() == 0;
}
public void setNextFocusDownId(int nextId) {
mLayout.setNextFocusDownId(nextId);
mTabs.setNextFocusDownId(nextId);
mBack.setNextFocusDownId(nextId);
mForward.setNextFocusDownId(nextId);
mFavicon.setNextFocusDownId(nextId);
mStop.setNextFocusDownId(nextId);
mSiteSecurity.setNextFocusDownId(nextId);
mReader.setNextFocusDownId(nextId);
mMenu.setNextFocusDownId(nextId);
}
@Override
public void onAnimationStart(Animation animation) {
if (animation.equals(mLockFadeIn)) {
if (mSiteSecurityVisible)
mSiteSecurity.setVisibility(View.VISIBLE);
} else if (animation.equals(mTitleSlideLeft)) {
// These two animations may be scheduled to start while the forward
// animation is occurring. If we're showing the site security icon, make
// sure it doesn't take any space during the forward transition.
mSiteSecurity.setVisibility(View.GONE);
} else if (animation.equals(mTitleSlideRight)) {
// If we're hiding the icon, make sure that we keep its padding
// in place during the forward transition
mSiteSecurity.setVisibility(View.INVISIBLE);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (animation.equals(mTitleSlideRight)) {
mSiteSecurity.startAnimation(mLockFadeIn);
}
}
private int getAwesomeBarEntryTranslation() {
return mLayout.getWidth() - mAwesomeBarEntry.getRight();
}
private int getAwesomeBarCurveTranslation() {
return mLayout.getWidth() - mTabs.getLeft();
}
public void fromAwesomeBarSearch(String url) {
// Update the title with the url that was just entered. Don't update the title if
// the AwesomeBar activity was cancelled, or if the user entered an empty string.
if (url != null && url.length() > 0) {
setTitle(url);
}
if (HardwareUtils.isTablet() || Build.VERSION.SDK_INT < 11) {
return;
}
// If the awesomebar entry is not selected at this point, this means that
// we had to reinflate the toolbar layout for some reason (device rotation
// while in awesome screen, activity was killed in background, etc). In this
// case, we have to ensure the toolbar is in the correct initial state to
// shrink back.
if (!mLayout.isSelected()) {
// Keep the entry highlighted during the animation
mLayout.setSelected(true);
final int entryTranslation = getAwesomeBarEntryTranslation();
final int curveTranslation = getAwesomeBarCurveTranslation();
if (mAwesomeBarRightEdge != null) {
ViewHelper.setTranslationX(mAwesomeBarRightEdge, entryTranslation);
}
ViewHelper.setTranslationX(mTabs, curveTranslation);
ViewHelper.setTranslationX(mTabsCounter, curveTranslation);
ViewHelper.setTranslationX(mActionItemBar, curveTranslation);
if (mHasSoftMenuButton) {
ViewHelper.setTranslationX(mMenu, curveTranslation);
ViewHelper.setTranslationX(mMenuIcon, curveTranslation);
}
ViewHelper.setAlpha(mReader, 0);
ViewHelper.setAlpha(mStop, 0);
}
final PropertyAnimator contentAnimator = new PropertyAnimator(250);
contentAnimator.setUseHardwareLayer(false);
// Shrink the awesome entry back to its original size
if (mAwesomeBarRightEdge != null) {
contentAnimator.attach(mAwesomeBarRightEdge,
PropertyAnimator.Property.TRANSLATION_X,
0);
}
contentAnimator.attach(mTabs,
PropertyAnimator.Property.TRANSLATION_X,
0);
contentAnimator.attach(mTabsCounter,
PropertyAnimator.Property.TRANSLATION_X,
0);
contentAnimator.attach(mActionItemBar,
PropertyAnimator.Property.TRANSLATION_X,
0);
if (mHasSoftMenuButton) {
contentAnimator.attach(mMenu,
PropertyAnimator.Property.TRANSLATION_X,
0);
contentAnimator.attach(mMenuIcon,
PropertyAnimator.Property.TRANSLATION_X,
0);
}
contentAnimator.setPropertyAnimationListener(new PropertyAnimator.PropertyAnimationListener() {
@Override
public void onPropertyAnimationStart() {
}
@Override
public void onPropertyAnimationEnd() {
// Turn off selected state on the entry
mLayout.setSelected(false);
PropertyAnimator buttonsAnimator = new PropertyAnimator(300);
// Fade toolbar buttons (reader, stop) after the entry
// is schrunk back to its original size.
buttonsAnimator.attach(mReader,
PropertyAnimator.Property.ALPHA,
1);
buttonsAnimator.attach(mStop,
PropertyAnimator.Property.ALPHA,
1);
buttonsAnimator.start();
mAnimatingEntry = false;
// Trigger animation to update the tabs counter once the
// tabs button is back on screen.
updateTabCount(Tabs.getInstance().getDisplayCount());
}
});
mAnimatingEntry = true;
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
contentAnimator.start();
}
}, 500);
}
private void onAwesomeBarSearch() {
// This animation doesn't make much sense in a sidebar UI
if (HardwareUtils.isTablet() || Build.VERSION.SDK_INT < 11) {
mActivity.onSearchRequested();
return;
}
if (mAnimatingEntry)
return;
final PropertyAnimator contentAnimator = new PropertyAnimator(250);
contentAnimator.setUseHardwareLayer(false);
final int entryTranslation = getAwesomeBarEntryTranslation();
final int curveTranslation = getAwesomeBarCurveTranslation();
// Keep the entry highlighted during the animation
mLayout.setSelected(true);
// Hide stop/reader buttons immediately
ViewHelper.setAlpha(mReader, 0);
ViewHelper.setAlpha(mStop, 0);
// Slide the right side elements of the toolbar
if (mAwesomeBarRightEdge != null) {
contentAnimator.attach(mAwesomeBarRightEdge,
PropertyAnimator.Property.TRANSLATION_X,
entryTranslation);
}
contentAnimator.attach(mTabs,
PropertyAnimator.Property.TRANSLATION_X,
curveTranslation);
contentAnimator.attach(mTabsCounter,
PropertyAnimator.Property.TRANSLATION_X,
curveTranslation);
contentAnimator.attach(mActionItemBar,
PropertyAnimator.Property.TRANSLATION_X,
curveTranslation);
if (mHasSoftMenuButton) {
contentAnimator.attach(mMenu,
PropertyAnimator.Property.TRANSLATION_X,
curveTranslation);
contentAnimator.attach(mMenuIcon,
PropertyAnimator.Property.TRANSLATION_X,
curveTranslation);
}
contentAnimator.setPropertyAnimationListener(new PropertyAnimator.PropertyAnimationListener() {
@Override
public void onPropertyAnimationStart() {
}
@Override
public void onPropertyAnimationEnd() {
// Once the entry is fully expanded, start awesome screen
mActivity.onSearchRequested();
mAnimatingEntry = false;
}
});
mAnimatingEntry = true;
contentAnimator.start();
}
private void addTab() {
mActivity.addTab();
}
private void toggleTabs() {
if (mActivity.areTabsShown()) {
if (mActivity.hasTabsSideBar())
mActivity.hideTabs();
} else {
// hide the virtual keyboard
InputMethodManager imm =
(InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mTabs.getWindowToken(), 0);
Tab tab = Tabs.getInstance().getSelectedTab();
if (tab != null) {
if (!tab.isPrivate())
mActivity.showNormalTabs();
else
mActivity.showPrivateTabs();
}
}
}
public void updateTabCount(int count) {
// If toolbar is selected, this means the entry is expanded and the
// tabs button is translated offscreen. Don't trigger tabs counter
// updates until the tabs button is back on screen.
// See fromAwesomeBarSearch()
if (mLayout.isSelected()) {
return;
}
// Set TabCounter based on visibility
if (isVisible() && ViewHelper.getAlpha(mTabsCounter) != 0) {
mTabsCounter.setCountWithAnimation(count);
} else {
mTabsCounter.setCount(count);
}
// Update A11y information
mTabs.setContentDescription((count > 1) ?
mActivity.getString(R.string.num_tabs, count) :
mActivity.getString(R.string.one_tab));
}
public void setProgressVisibility(boolean visible) {
// The "Throbber start" and "Throbber stop" log messages in this method
// are needed by S1/S2 tests (http://mrcote.info/phonedash/#).
// See discussion in Bug 804457. Bug 805124 tracks paring these down.
if (visible) {
mFavicon.setImageDrawable(mProgressSpinner);
mProgressSpinner.start();
setPageActionVisibility(true);
Log.i(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - Throbber start");
} else {
mProgressSpinner.stop();
setPageActionVisibility(false);
Tab selectedTab = Tabs.getInstance().getSelectedTab();
if (selectedTab != null)
setFavicon(selectedTab.getFavicon());
Log.i(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - Throbber stop");
}
}
public void setPageActionVisibility(boolean isLoading) {
// Handle the loading mode page actions
mStop.setVisibility(isLoading ? View.VISIBLE : View.GONE);
// Handle the viewing mode page actions
setSiteSecurityVisibility(mShowSiteSecurity && !isLoading);
// Handle the readerMode image and visibility: We show the reader mode button if 1) you can
// enter reader mode for current page or 2) if you're already in reader mode,
// in which case we show the reader mode "close" (reader_active) icon.
boolean inReaderMode = false;
Tab tab = Tabs.getInstance().getSelectedTab();
if (tab != null)
inReaderMode = ReaderModeUtils.isAboutReader(tab.getURL());
mReader.setImageResource(inReaderMode ? R.drawable.reader_active : R.drawable.reader);
mReader.setVisibility(!isLoading && (mShowReader || inReaderMode) ? View.VISIBLE : View.GONE);
// We want title to fill the whole space available for it when there are icons
// being shown on the right side of the toolbar as the icons already have some
// padding in them. This is just to avoid wasting space when icons are shown.
mTitle.setPadding(0, 0, (!isLoading && !(mShowReader || inReaderMode) ? mTitlePadding : 0), 0);
updateFocusOrder();
}
private void setSiteSecurityVisibility(final boolean visible) {
if (visible == mSiteSecurityVisible)
return;
mSiteSecurityVisible = visible;
if (mSwitchingTabs) {
mSiteSecurity.setVisibility(visible ? View.VISIBLE : View.GONE);
return;
}
mTitle.clearAnimation();
mSiteSecurity.clearAnimation();
// If any of these animations were cancelled as a result of the
// clearAnimation() calls above, we need to reset them.
mLockFadeIn.reset();
mTitleSlideLeft.reset();
mTitleSlideRight.reset();
if (mForwardAnim != null) {
long delay = mForwardAnim.getRemainingTime();
mTitleSlideRight.setStartOffset(delay);
mTitleSlideLeft.setStartOffset(delay);
} else {
mTitleSlideRight.setStartOffset(0);
mTitleSlideLeft.setStartOffset(0);
}
mTitle.startAnimation(visible ? mTitleSlideRight : mTitleSlideLeft);
}
private void updateFocusOrder() {
View prevView = null;
for (View view : mFocusOrder) {
if (view.getVisibility() != View.VISIBLE)
continue;
if (prevView != null) {
view.setNextFocusLeftId(prevView.getId());
prevView.setNextFocusRightId(view.getId());
}
prevView = view;
}
}
public void setShadowVisibility(boolean visible) {
Tab tab = Tabs.getInstance().getSelectedTab();
if (tab == null) {
return;
}
String url = tab.getURL();
// Only set shadow to visible when not on about screens except about:blank.
visible &= !(url == null || (url.startsWith("about:") &&
!url.equals("about:blank")));
if ((mShadow.getVisibility() == View.VISIBLE) != visible) {
mShadow.setVisibility(visible ? View.VISIBLE : View.GONE);
}
}
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();
// Keep the title unchanged if there's no selected tab, or if the tab is entering reader mode.
if (tab == null || tab.isEnteringReaderMode()) {
return;
}
String url = tab.getURL();
// Setting a null title will ensure we just see the "Enter Search or Address" placeholder text.
if ("about:home".equals(url) || "about:privatebrowsing".equals(url)) {
setTitle(null);
return;
}
// If the pref to show the URL isn't set, just use the tab's display title.
if (!mShowUrl || url == null) {
setTitle(tab.getDisplayTitle());
return;
}
url = StringUtils.stripScheme(url);
CharSequence title = StringUtils.stripCommonSubdomains(url);
String baseDomain = tab.getBaseDomain();
if (!TextUtils.isEmpty(baseDomain)) {
SpannableStringBuilder builder = new SpannableStringBuilder(title);
int index = title.toString().indexOf(baseDomain);
if (index > -1) {
builder.setSpan(mUrlColor, 0, title.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
builder.setSpan(tab.isPrivate() ? mPrivateDomainColor : mDomainColor, index, index+baseDomain.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
title = builder;
}
}
setTitle(title);
}
private void setFavicon(Bitmap image) {
if (Tabs.getInstance().getSelectedTab().getState() == Tab.STATE_LOADING)
return;
if (image != null) {
image = Bitmap.createScaledBitmap(image, mFaviconSize, mFaviconSize, false);
mFavicon.setImageBitmap(image);
} else {
mFavicon.setImageResource(R.drawable.favicon);
}
}
private void setSecurityMode(String mode) {
int imageLevel = SiteIdentityPopup.getSecurityImageLevel(mode);
mSiteSecurity.setImageLevel(imageLevel);
mShowSiteSecurity = (imageLevel != SiteIdentityPopup.LEVEL_UKNOWN);
setPageActionVisibility(mStop.getVisibility() == View.VISIBLE);
}
private void setReaderMode(boolean showReader) {
mShowReader = showReader;
setPageActionVisibility(mStop.getVisibility() == View.VISIBLE);
}
public void requestFocusFromTouch() {
mLayout.requestFocusFromTouch();
}
public void prepareTabsAnimation(PropertyAnimator animator, boolean tabsAreShown) {
if (!tabsAreShown) {
PropertyAnimator buttonsAnimator =
new PropertyAnimator(animator.getDuration(), sButtonsInterpolator);
buttonsAnimator.attach(mTabsCounter,
PropertyAnimator.Property.ALPHA,
1.0f);
if (mHasSoftMenuButton && !HardwareUtils.isTablet()) {
buttonsAnimator.attach(mMenuIcon,
PropertyAnimator.Property.ALPHA,
1.0f);
}
buttonsAnimator.start();
return;
}
ViewHelper.setAlpha(mTabsCounter, 0.0f);
if (mHasSoftMenuButton && !HardwareUtils.isTablet()) {
ViewHelper.setAlpha(mMenuIcon, 0.0f);
}
}
public void updateBackButton(boolean enabled) {
Drawable drawable = mBack.getDrawable();
if (drawable != null)
drawable.setAlpha(enabled ? 255 : 77);
mBack.setEnabled(enabled);
}
public void updateForwardButton(final boolean enabled) {
if (mForward.isEnabled() == enabled)
return;
// Save the state on the forward button so that we can skip animations
// when there's nothing to change
mForward.setEnabled(enabled);
if (mForward.getVisibility() != View.VISIBLE)
return;
// We want the forward button to show immediately when switching tabs
mForwardAnim = new PropertyAnimator(mSwitchingTabs ? 10 : FORWARD_ANIMATION_DURATION);
final int width = mForward.getWidth() / 2;
mForwardAnim.setPropertyAnimationListener(new PropertyAnimator.PropertyAnimationListener() {
@Override
public void onPropertyAnimationStart() {
if (!enabled) {
// Set the margin before the transition when hiding the forward button. We
// have to do this so that the favicon isn't clipped during the transition
ViewGroup.MarginLayoutParams layoutParams =
(ViewGroup.MarginLayoutParams)mUrlDisplayContainer.getLayoutParams();
layoutParams.leftMargin = 0;
mUrlDisplayContainer.requestLayout();
// Note, we already translated the favicon, site security, and text field
// in prepareForwardAnimation, so they should appear to have not moved at
// all at this point.
}
}
@Override
public void onPropertyAnimationEnd() {
if (enabled) {
ViewGroup.MarginLayoutParams layoutParams =
(ViewGroup.MarginLayoutParams)mUrlDisplayContainer.getLayoutParams();
layoutParams.leftMargin = mAddressBarViewOffset;
ViewHelper.setTranslationX(mTitle, 0);
ViewHelper.setTranslationX(mFavicon, 0);
ViewHelper.setTranslationX(mSiteSecurity, 0);
}
ViewGroup.MarginLayoutParams layoutParams =
(ViewGroup.MarginLayoutParams)mForward.getLayoutParams();
layoutParams.leftMargin = mDefaultForwardMargin + (mForward.isEnabled() ? width : 0);
ViewHelper.setTranslationX(mForward, 0);
mUrlDisplayContainer.requestLayout();
mForwardAnim = null;
}
});
prepareForwardAnimation(mForwardAnim, enabled, width);
mForwardAnim.start();
}
private void prepareForwardAnimation(PropertyAnimator anim, boolean enabled, int width) {
if (!enabled) {
anim.attach(mForward,
PropertyAnimator.Property.TRANSLATION_X,
-width);
anim.attach(mForward,
PropertyAnimator.Property.ALPHA,
0);
anim.attach(mTitle,
PropertyAnimator.Property.TRANSLATION_X,
0);
anim.attach(mFavicon,
PropertyAnimator.Property.TRANSLATION_X,
0);
anim.attach(mSiteSecurity,
PropertyAnimator.Property.TRANSLATION_X,
0);
// We're hiding the forward button. We're going to reset the margin before
// the animation starts, so we shift these items to the right so that they don't
// appear to move initially.
ViewHelper.setTranslationX(mTitle, mAddressBarViewOffset);
ViewHelper.setTranslationX(mFavicon, mAddressBarViewOffset);
ViewHelper.setTranslationX(mSiteSecurity, mAddressBarViewOffset);
} else {
anim.attach(mForward,
PropertyAnimator.Property.TRANSLATION_X,
width);
anim.attach(mForward,
PropertyAnimator.Property.ALPHA,
1);
anim.attach(mTitle,
PropertyAnimator.Property.TRANSLATION_X,
mAddressBarViewOffset);
anim.attach(mFavicon,
PropertyAnimator.Property.TRANSLATION_X,
mAddressBarViewOffset);
anim.attach(mSiteSecurity,
PropertyAnimator.Property.TRANSLATION_X,
mAddressBarViewOffset);
}
}
@Override
public void addActionItem(View actionItem) {
mActionItemBar.addView(actionItem);
}
@Override
public void removeActionItem(View actionItem) {
mActionItemBar.removeView(actionItem);
}
public void show() {
mLayout.setVisibility(View.VISIBLE);
}
public void hide() {
mLayout.setVisibility(View.GONE);
}
public void refresh() {
Tab tab = Tabs.getInstance().getSelectedTab();
if (tab != null) {
updateTitle();
setFavicon(tab.getFavicon());
setProgressVisibility(tab.getState() == Tab.STATE_LOADING);
setSecurityMode(tab.getSecurityMode());
setReaderMode(tab.getReaderEnabled());
setShadowVisibility(true);
updateBackButton(tab.canDoBack());
updateForwardButton(tab.canDoForward());
final boolean isPrivate = tab.isPrivate();
mAddressBarBg.setPrivateMode(isPrivate);
mLayout.setPrivateMode(isPrivate);
mTabs.setPrivateMode(isPrivate);
mTitle.setPrivateMode(isPrivate);
mMenu.setPrivateMode(isPrivate);
mMenuIcon.setPrivateMode(isPrivate);
if (mBack instanceof BackButton)
((BackButton) mBack).setPrivateMode(isPrivate);
if (mForward instanceof ForwardButton)
((ForwardButton) mForward).setPrivateMode(isPrivate);
}
}
public void onDestroy() {
if (mPrefObserverId != null) {
PrefsHelper.removeObserver(mPrefObserverId);
mPrefObserverId = null;
}
Tabs.unregisterOnTabsChangedListener(this);
}
public boolean openOptionsMenu() {
if (!mHasSoftMenuButton)
return false;
GeckoAppShell.getGeckoInterface().invalidateOptionsMenu();
if (mMenuPopup != null && !mMenuPopup.isShowing())
mMenuPopup.showAsDropDown(mMenu);
return true;
}
public boolean closeOptionsMenu() {
if (!mHasSoftMenuButton)
return false;
if (mMenuPopup != null && mMenuPopup.isShowing())
mMenuPopup.dismiss();
return true;
}
}
|