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.widget.emptybackground; |
| 6 |
| 7 import android.animation.Animator; |
| 8 import android.animation.AnimatorListenerAdapter; |
| 9 import android.animation.ObjectAnimator; |
| 10 import android.content.Context; |
| 11 import android.content.res.TypedArray; |
| 12 import android.util.AttributeSet; |
| 13 import android.view.View; |
| 14 import android.widget.FrameLayout; |
| 15 import android.widget.ImageButton; |
| 16 |
| 17 import com.google.android.apps.chrome.R; |
| 18 |
| 19 import org.chromium.base.metrics.RecordUserAction; |
| 20 import org.chromium.chrome.browser.appmenu.AppMenuButtonHelper; |
| 21 import org.chromium.chrome.browser.appmenu.AppMenuHandler; |
| 22 import org.chromium.chrome.browser.tabmodel.ChromeTabCreator; |
| 23 import org.chromium.chrome.browser.tabmodel.TabModelSelector; |
| 24 import org.chromium.chrome.browser.widget.incognitotoggle.IncognitoToggleButtonT
ablet; |
| 25 import org.chromium.ui.UiUtils; |
| 26 |
| 27 /** |
| 28 * Represents the background screen that shows when no tabs are visible. This {
@link View} |
| 29 * handles making itself invisible or not automatically depending on the state o
f the system. |
| 30 */ |
| 31 public class EmptyBackgroundViewTablet extends FrameLayout { |
| 32 private static final int ANIMATE_DURATION_MS = 200; |
| 33 |
| 34 private TabModelSelector mTabModelSelector; |
| 35 private ChromeTabCreator mTabCreator; |
| 36 |
| 37 private Animator mCurrentTransitionAnimation; |
| 38 |
| 39 private Animator mAnimateInAnimation; |
| 40 private Animator mAnimateOutAnimation; |
| 41 |
| 42 /** |
| 43 * Creates an instance of {@link EmptyBackgroundViewTablet}. |
| 44 * @param context The {@link Context} to create this {@link View} under. |
| 45 * @param attrs An {@link AttributeSet} that contains information on how to
build this |
| 46 * {@link View}. |
| 47 */ |
| 48 public EmptyBackgroundViewTablet(Context context, AttributeSet attrs) { |
| 49 super(context, attrs); |
| 50 } |
| 51 |
| 52 @Override |
| 53 public void onFinishInflate() { |
| 54 super.onFinishInflate(); |
| 55 |
| 56 View newTabButton = findViewById(R.id.empty_new_tab_button); |
| 57 newTabButton.setOnClickListener(new OnClickListener() { |
| 58 @Override |
| 59 public void onClick(View v) { |
| 60 if (mTabCreator == null) return; |
| 61 mTabCreator.launchNTP(); |
| 62 } |
| 63 }); |
| 64 |
| 65 buildAnimatorSets(); |
| 66 } |
| 67 |
| 68 /** |
| 69 * Sets the {@link TabModelSelector} that will be queried for information ab
out the state of |
| 70 * the system. |
| 71 * @param tabModelSelector A {@link TabModelSelector} that represents the st
ate of the system. |
| 72 */ |
| 73 public void setTabModelSelector(TabModelSelector tabModelSelector) { |
| 74 mTabModelSelector = tabModelSelector; |
| 75 |
| 76 IncognitoToggleButtonTablet incognitoToggle = (IncognitoToggleButtonTabl
et) findViewById( |
| 77 R.id.empty_incognito_toggle_button); |
| 78 |
| 79 incognitoToggle.setTabModelSelector(mTabModelSelector); |
| 80 } |
| 81 |
| 82 /** |
| 83 * Sets the {@link ChromeTabCreator} that will be used to open the New Tab P
age. |
| 84 * @param tabCreator A {@link ChromeTabCreator} to open the New Tab Page. |
| 85 */ |
| 86 public void setTabCreator(ChromeTabCreator tabCreator) { |
| 87 mTabCreator = tabCreator; |
| 88 } |
| 89 |
| 90 /** |
| 91 * Creates an on touch listener for the menu button using the given menu han
dler. |
| 92 * @param menuHandler The menu handler to be used for showing the pop up men
u. |
| 93 */ |
| 94 public void setMenuOnTouchListener(final AppMenuHandler menuHandler) { |
| 95 final ImageButton menuBtn = (ImageButton) findViewById(R.id.empty_menu_b
utton); |
| 96 final AppMenuButtonHelper menuPopupButtonHelper = new AppMenuButtonHelpe
r(menuHandler); |
| 97 menuBtn.setOnTouchListener(menuPopupButtonHelper); |
| 98 menuPopupButtonHelper.setOnAppMenuShownListener(new Runnable() { |
| 99 @Override |
| 100 public void run() { |
| 101 RecordUserAction.record("MobileToolbarShowMenu"); |
| 102 } |
| 103 }); |
| 104 } |
| 105 |
| 106 public void setEmptyContainerState(boolean shouldShow) { |
| 107 Animator nextAnimator = null; |
| 108 |
| 109 if (shouldShow && getVisibility() != View.VISIBLE |
| 110 && mCurrentTransitionAnimation != mAnimateInAnimation) { |
| 111 nextAnimator = mAnimateInAnimation; |
| 112 UiUtils.hideKeyboard(this); |
| 113 } else if (!shouldShow && getVisibility() != View.GONE |
| 114 && mCurrentTransitionAnimation != mAnimateOutAnimation) { |
| 115 nextAnimator = mAnimateOutAnimation; |
| 116 } |
| 117 |
| 118 if (nextAnimator != null) { |
| 119 if (mCurrentTransitionAnimation != null) mCurrentTransitionAnimation
.cancel(); |
| 120 mCurrentTransitionAnimation = nextAnimator; |
| 121 mCurrentTransitionAnimation.start(); |
| 122 } |
| 123 } |
| 124 |
| 125 private void buildAnimatorSets() { |
| 126 TypedArray a = getContext().getTheme().obtainStyledAttributes(R.style.To
olbarButton, |
| 127 new int[] {android.R.attr.layout_height}); |
| 128 int viewHeight = a.getDimensionPixelSize(0, 0); |
| 129 a.recycle(); |
| 130 View view = findViewById(R.id.empty_layout_button_container); |
| 131 |
| 132 // Build the in animation |
| 133 mAnimateInAnimation = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, -
viewHeight, 0.f); |
| 134 mAnimateInAnimation.setDuration(ANIMATE_DURATION_MS); |
| 135 |
| 136 mAnimateInAnimation.addListener(new AnimatorListenerAdapter() { |
| 137 @Override |
| 138 public void onAnimationStart(Animator animation) { |
| 139 setVisibility(View.VISIBLE); |
| 140 } |
| 141 |
| 142 @Override |
| 143 public void onAnimationEnd(Animator animation) { |
| 144 mCurrentTransitionAnimation = null; |
| 145 getRootView().findViewById(R.id.control_container).setVisibility
(INVISIBLE); |
| 146 } |
| 147 }); |
| 148 |
| 149 // Build the out animation |
| 150 mAnimateOutAnimation = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y,
0.f, -viewHeight); |
| 151 mAnimateOutAnimation.setDuration(ANIMATE_DURATION_MS); |
| 152 |
| 153 mAnimateOutAnimation.addListener(new AnimatorListenerAdapter() { |
| 154 @Override |
| 155 public void onAnimationStart(Animator animation) { |
| 156 setVisibility(View.VISIBLE); |
| 157 getRootView().findViewById(R.id.control_container).setVisibility
(VISIBLE); |
| 158 } |
| 159 |
| 160 @Override |
| 161 public void onAnimationEnd(Animator animation) { |
| 162 setVisibility(View.GONE); |
| 163 mCurrentTransitionAnimation = null; |
| 164 } |
| 165 }); |
| 166 } |
| 167 } |
OLD | NEW |