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.ui.widget; | 5 package org.chromium.ui.widget; |
6 | 6 |
7 import android.animation.AnimatorInflater; | 7 import android.animation.AnimatorInflater; |
8 import android.annotation.TargetApi; | 8 import android.annotation.TargetApi; |
9 import android.content.Context; | 9 import android.content.Context; |
10 import android.content.res.ColorStateList; | 10 import android.content.res.ColorStateList; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 74 |
75 getBackground().mutate(); | 75 getBackground().mutate(); |
76 setButtonColor(buttonColor); | 76 setButtonColor(buttonColor); |
77 | 77 |
78 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | 78 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
79 // Use the StateListAnimator from the Widget.Material.Button style t
o animate the | 79 // Use the StateListAnimator from the Widget.Material.Button style t
o animate the |
80 // elevation when the button is pressed. | 80 // elevation when the button is pressed. |
81 TypedArray a = getContext().obtainStyledAttributes(null, | 81 TypedArray a = getContext().obtainStyledAttributes(null, |
82 new int[]{android.R.attr.stateListAnimator}, 0, | 82 new int[]{android.R.attr.stateListAnimator}, 0, |
83 android.R.style.Widget_Material_Button); | 83 android.R.style.Widget_Material_Button); |
84 setStateListAnimator(AnimatorInflater.loadStateListAnimator(getConte
xt(), | 84 int stateListAnimatorId = a.getResourceId(0, 0); |
85 a.getResourceId(0, 0))); | |
86 a.recycle(); | 85 a.recycle(); |
| 86 // stateListAnimatorId could be 0 on custom or future builds of Andr
oid, or when using a |
| 87 // framework like Xposed. Handle these cases gracefully by simply no
t using a |
| 88 // StateListAnimator. |
| 89 if (stateListAnimatorId != 0) { |
| 90 setStateListAnimator(AnimatorInflater.loadStateListAnimator(getC
ontext(), |
| 91 stateListAnimatorId)); |
| 92 } |
87 } | 93 } |
88 } | 94 } |
89 | 95 |
90 /** | 96 /** |
91 * Sets the background color of the button. | 97 * Sets the background color of the button. |
92 */ | 98 */ |
93 public void setButtonColor(int color) { | 99 public void setButtonColor(int color) { |
94 if (color == mColor) return; | 100 if (color == mColor) return; |
95 mColor = color; | 101 mColor = color; |
96 | 102 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 return DISABLED_COLOR; | 150 return DISABLED_COLOR; |
145 } | 151 } |
146 | 152 |
147 private static int getColorFromAttributeSet(Context context, AttributeSet at
trs) { | 153 private static int getColorFromAttributeSet(Context context, AttributeSet at
trs) { |
148 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ButtonC
ompat, 0, 0); | 154 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ButtonC
ompat, 0, 0); |
149 int color = a.getColor(R.styleable.ButtonCompat_buttonColor, Color.WHITE
); | 155 int color = a.getColor(R.styleable.ButtonCompat_buttonColor, Color.WHITE
); |
150 a.recycle(); | 156 a.recycle(); |
151 return color; | 157 return color; |
152 } | 158 } |
153 } | 159 } |
OLD | NEW |