OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser; | 5 package org.chromium.chrome.browser.toolbar; |
6 | 6 |
7 import android.animation.Animator; | 7 import android.animation.Animator; |
8 import android.animation.AnimatorListenerAdapter; | 8 import android.animation.AnimatorListenerAdapter; |
9 import android.animation.ObjectAnimator; | 9 import android.animation.ObjectAnimator; |
10 import android.animation.ValueAnimator; | 10 import android.animation.ValueAnimator; |
11 import android.content.Context; | 11 import android.content.Context; |
12 import android.content.res.TypedArray; | 12 import android.content.res.TypedArray; |
13 import android.support.v7.app.ActionBar; | 13 import android.support.v7.app.ActionBar; |
14 import android.util.Property; | 14 import android.util.Property; |
15 | 15 |
16 import org.chromium.chrome.R; | 16 import org.chromium.chrome.R; |
17 | 17 |
18 /** | 18 /** |
19 * This class handles the animation for the contextual menu bar and adds a custo m | 19 * This class controls the how toolbar animates while the action mode bar is bei ng shown. It also |
20 * ActionMode.Callback to the menu bar. | 20 * manages a {@link ToolbarActionModeCallback}. |
21 */ | 21 */ |
22 public class ContextualMenuBar { | 22 public class ActionModeController { |
23 | 23 |
24 private static final int SLIDE_DURATION_MS = 200; | 24 private static final int SLIDE_DURATION_MS = 200; |
25 | 25 |
26 private CustomSelectionActionModeCallback mCustomSelectionActionModeCallback ; | 26 private ToolbarActionModeCallback mCustomSelectionActionModeCallback; |
David Trainor- moved to gerrit
2015/08/18 18:32:50
change variable name too?
Ian Wen
2015/08/19 18:35:36
Done.
| |
27 private ObjectAnimator mCurrentAnimation = null; | 27 private ObjectAnimator mCurrentAnimation = null; |
28 private boolean mShowingContextualActionBar = false; | 28 private boolean mShowingActionMode = false; |
29 private float mTabStripHeight; | 29 private float mTabStripHeight; |
30 private final Context mContext; | 30 private final Context mContext; |
31 private final ActionBarDelegate mActionBarDelegate; | 31 private final ActionBarDelegate mActionBarDelegate; |
32 | 32 |
33 /** Property for animating the top margin of ActionBarDelegate. */ | 33 /** Property for animating the top margin of ActionBarDelegate. */ |
34 public static final Property<ActionBarDelegate, Integer> TOP_MARGIN_ANIM_PRO PERTY = | 34 public static final Property<ActionBarDelegate, Integer> TOP_MARGIN_ANIM_PRO PERTY = |
35 new Property<ActionBarDelegate, Integer>(Integer.class, "controlTopM argin") { | 35 new Property<ActionBarDelegate, Integer>(Integer.class, "controlTopM argin") { |
36 @Override | 36 @Override |
37 public Integer get(ActionBarDelegate delegate) { | 37 public Integer get(ActionBarDelegate delegate) { |
38 return delegate.getControlTopMargin(); | 38 return delegate.getControlTopMargin(); |
39 } | 39 } |
40 @Override | 40 @Override |
41 public void set(ActionBarDelegate delegate, Integer value) { | 41 public void set(ActionBarDelegate delegate, Integer value) { |
42 delegate.setControlTopMargin(value); | 42 delegate.setControlTopMargin(value); |
43 } | 43 } |
44 }; | 44 }; |
45 | 45 |
46 /** | 46 /** |
47 * This is an interface for objects that the contextualMenuBar can use for a nimating the action | 47 * This is an interface for to let the controller animate the position of {@ link Toolbar}, while |
48 * bar. | 48 * action mode is showing. |
49 */ | 49 */ |
50 public interface ActionBarDelegate { | 50 public interface ActionBarDelegate { |
51 | 51 |
52 /** | 52 /** |
53 * Sets the top margin of the control container. | 53 * Sets the top margin of the control container. |
54 * @param margin The new top margin of the control container. | 54 * @param margin The new top margin of the control container. |
55 */ | 55 */ |
56 public void setControlTopMargin(int margin); | 56 public void setControlTopMargin(int margin); |
57 | 57 |
58 /** | 58 /** |
59 * @return The top margin of the control container. | 59 * @return The top margin of the control container. |
60 */ | 60 */ |
61 public int getControlTopMargin(); | 61 public int getControlTopMargin(); |
62 | 62 |
63 /** | 63 /** |
64 * @return The action bar that will be animated in and out. | 64 * @return The action bar that will be animated in and out. |
65 */ | 65 */ |
66 public ActionBar getSupportActionBar(); | 66 public ActionBar getSupportActionBar(); |
67 | 67 |
68 /** | 68 /** |
69 * Change the background visibility for the action bar. | 69 * Change the background visibility for the action bar. |
70 * @param visible Whether the background should be visible. | 70 * @param visible Whether the background should be visible. |
71 */ | 71 */ |
72 public void setActionBarBackgroundVisibility(boolean visible); | 72 public void setActionBarBackgroundVisibility(boolean visible); |
73 } | 73 } |
74 | 74 |
75 /** | 75 /** |
76 * Creates the contextual menu bar and ties it to an action bar using the gi ven action bar | 76 * Creates the {@link ActionModeController} and ties it to an action bar usi ng the given action |
77 * delegate. | 77 * bar delegate. |
78 * @param context The context which the contextual action menu bar should be using for accessing | 78 * @param actionBarDelegate The delegate for communicating with toolbar for animation. |
79 * resources. | |
80 * @param actionBarDelegate The delegate for communicating with the action b ar while animating | |
81 * it. | |
82 */ | 79 */ |
83 public ContextualMenuBar(Context context, ActionBarDelegate actionBarDelegat e) { | 80 public ActionModeController(Context context, ActionBarDelegate actionBarDele gate) { |
84 mActionBarDelegate = actionBarDelegate; | 81 mActionBarDelegate = actionBarDelegate; |
85 mContext = context; | 82 mContext = context; |
86 mTabStripHeight = mContext.getResources().getDimension(R.dimen.tab_strip _height); | 83 mTabStripHeight = mContext.getResources().getDimension(R.dimen.tab_strip _height); |
87 } | 84 } |
88 | 85 |
89 /** | 86 /** |
90 * Overrides the preset height of the tab strip. | 87 * Overrides the preset height of the tab strip. |
91 */ | 88 */ |
92 public void setTabStripHeight(int tabStripHeight) { | 89 public void setTabStripHeight(int tabStripHeight) { |
93 mTabStripHeight = tabStripHeight; | 90 mTabStripHeight = tabStripHeight; |
94 } | 91 } |
95 | 92 |
96 /** | 93 /** |
97 * @return The delegate handling action bar positioning for the contextual m enu bar. | 94 * @return The delegate handling action bar positioning for the custom actio n bar. |
98 */ | 95 */ |
99 public ActionBarDelegate getActionBarDelegate() { | 96 public ActionBarDelegate getActionBarDelegate() { |
100 return mActionBarDelegate; | 97 return mActionBarDelegate; |
101 } | 98 } |
102 | 99 |
103 /** | 100 /** |
104 * Sets the custom ActionMode.Callback | 101 * Sets the custom ActionMode.Callback |
105 * @param customSelectionActionModeCallback | 102 * @param customSelectionActionModeCallback |
106 */ | 103 */ |
107 public void setCustomSelectionActionModeCallback( | 104 public void setCustomSelectionActionModeCallback( |
108 CustomSelectionActionModeCallback customSelectionActionModeCallback) { | 105 ToolbarActionModeCallback customSelectionActionModeCallback) { |
David Trainor- moved to gerrit
2015/08/18 18:32:50
Change parameter name too?
Ian Wen
2015/08/19 18:35:36
Done.
| |
109 if (customSelectionActionModeCallback.equals(mCustomSelectionActionModeC allback)) return; | 106 if (customSelectionActionModeCallback.equals(mCustomSelectionActionModeC allback)) return; |
110 mCustomSelectionActionModeCallback = customSelectionActionModeCallback; | 107 mCustomSelectionActionModeCallback = customSelectionActionModeCallback; |
111 mCustomSelectionActionModeCallback.setContextualMenuBar(this); | 108 mCustomSelectionActionModeCallback.setActionModeController(this); |
112 } | 109 } |
113 | 110 |
114 /** | 111 /** |
115 * @return The custom ActionMode.Callback. | 112 * @return The custom ActionMode.Callback. |
116 */ | 113 */ |
117 public CustomSelectionActionModeCallback getCustomSelectionActionModeCallbac k() { | 114 public ToolbarActionModeCallback getActionModeCallback() { |
118 return mCustomSelectionActionModeCallback; | 115 return mCustomSelectionActionModeCallback; |
119 } | 116 } |
120 | 117 |
121 /** | 118 /** |
122 * @return The current action bar height. | 119 * @return The current action bar height. |
123 */ | 120 */ |
124 private int queryCurrentActionBarHeight() { | 121 private int queryCurrentActionBarHeight() { |
125 ActionBar actionBar = mActionBarDelegate.getSupportActionBar(); | 122 ActionBar actionBar = mActionBarDelegate.getSupportActionBar(); |
126 if (actionBar != null) return actionBar.getHeight(); | 123 if (actionBar != null) return actionBar.getHeight(); |
127 | 124 |
128 TypedArray styledAttributes = | 125 TypedArray styledAttributes = |
129 mContext.obtainStyledAttributes(new int[] {R.attr.actionBarSize} ); | 126 mContext.obtainStyledAttributes(new int[] {R.attr.actionBarSize} ); |
130 int height = styledAttributes.getDimensionPixelSize(0, 0); | 127 int height = styledAttributes.getDimensionPixelSize(0, 0); |
131 styledAttributes.recycle(); | 128 styledAttributes.recycle(); |
132 return height; | 129 return height; |
133 } | 130 } |
134 | 131 |
135 /** | 132 /** |
136 * Show controls after orientation change if previously visible. | 133 * Show controls after orientation change if previously visible. |
137 */ | 134 */ |
138 public void showControlsOnOrientationChange() { | 135 public void showControlsOnOrientationChange() { |
139 if (mShowingContextualActionBar && mCurrentAnimation == null) { | 136 if (mShowingActionMode && mCurrentAnimation == null) { |
140 showControls(); | 137 startShowAnimation(); |
141 } | 138 } |
142 } | 139 } |
143 | 140 |
144 /** | 141 /** |
145 * Animation for the textview if the action bar is visible. | 142 * Animation for the textview if the action bar is visible. |
146 */ | 143 */ |
147 public void showControls() { | 144 public void startShowAnimation() { |
148 if (mCurrentAnimation != null) mCurrentAnimation.cancel(); | 145 if (mCurrentAnimation != null) mCurrentAnimation.cancel(); |
149 | 146 |
150 mCurrentAnimation = ObjectAnimator.ofInt(mActionBarDelegate, TOP_MARGIN_ ANIM_PROPERTY, | 147 mCurrentAnimation = ObjectAnimator.ofInt(mActionBarDelegate, TOP_MARGIN_ ANIM_PROPERTY, |
151 (int) (Math.max(0, queryCurrentActionBarHeight() - mTabStripHeig ht))).setDuration( | 148 (int) (Math.max(0, queryCurrentActionBarHeight() - mTabStripHeig ht))).setDuration( |
152 SLIDE_DURATION_MS); | 149 SLIDE_DURATION_MS); |
153 | 150 |
154 mCurrentAnimation.addListener(new AnimatorListenerAdapter() { | 151 mCurrentAnimation.addListener(new AnimatorListenerAdapter() { |
155 @Override | 152 @Override |
156 public void onAnimationEnd(Animator animation) { | 153 public void onAnimationEnd(Animator animation) { |
157 mCurrentAnimation = null; | 154 mCurrentAnimation = null; |
158 } | 155 } |
159 }); | 156 }); |
160 | 157 |
161 mCurrentAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateList ener() { | 158 mCurrentAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateList ener() { |
162 @Override | 159 @Override |
163 public void onAnimationUpdate(ValueAnimator animation) { | 160 public void onAnimationUpdate(ValueAnimator animation) { |
164 ActionBar actionBar = mActionBarDelegate.getSupportActionBar(); | 161 ActionBar actionBar = mActionBarDelegate.getSupportActionBar(); |
165 if (actionBar != null) { | 162 if (actionBar != null) { |
166 animation.setIntValues((int) (Math.max(0, | 163 animation.setIntValues((int) (Math.max(0, |
167 queryCurrentActionBarHeight() - mTabStripHeight))); | 164 queryCurrentActionBarHeight() - mTabStripHeight))); |
168 } | 165 } |
169 } | 166 } |
170 }); | 167 }); |
171 | 168 |
172 mActionBarDelegate.setActionBarBackgroundVisibility(true); | 169 mActionBarDelegate.setActionBarBackgroundVisibility(true); |
173 mCurrentAnimation.start(); | 170 mCurrentAnimation.start(); |
174 mShowingContextualActionBar = true; | 171 mShowingActionMode = true; |
175 } | 172 } |
176 | 173 |
177 /** | 174 /** |
178 * Hide animation for the textview if the action bar is not visible. | 175 * Hide animation for the textview if the action bar is not visible. |
179 */ | 176 */ |
180 public void hideControls() { | 177 public void startHideAnimation() { |
181 if (mCurrentAnimation != null) mCurrentAnimation.cancel(); | 178 if (mCurrentAnimation != null) mCurrentAnimation.cancel(); |
182 | 179 |
183 mCurrentAnimation = ObjectAnimator.ofInt(mActionBarDelegate, TOP_MARGIN_ ANIM_PROPERTY, | 180 mCurrentAnimation = ObjectAnimator.ofInt(mActionBarDelegate, TOP_MARGIN_ ANIM_PROPERTY, |
184 0).setDuration(SLIDE_DURATION_MS); | 181 0).setDuration(SLIDE_DURATION_MS); |
185 | 182 |
186 mCurrentAnimation.addListener(new AnimatorListenerAdapter() { | 183 mCurrentAnimation.addListener(new AnimatorListenerAdapter() { |
187 @Override | 184 @Override |
188 public void onAnimationEnd(Animator animation) { | 185 public void onAnimationEnd(Animator animation) { |
189 mCurrentAnimation = null; | 186 mCurrentAnimation = null; |
190 mActionBarDelegate.setActionBarBackgroundVisibility(false); | 187 mActionBarDelegate.setActionBarBackgroundVisibility(false); |
191 } | 188 } |
192 }); | 189 }); |
193 | 190 |
194 mCurrentAnimation.start(); | 191 mCurrentAnimation.start(); |
195 mShowingContextualActionBar = false; | 192 mShowingActionMode = false; |
196 } | 193 } |
197 } | 194 } |
OLD | NEW |