/Honor5C-7.0/src/main/java/android/graphics/drawable/StateListDrawable.java

https://github.com/dstmath/HWFramework · Java · 262 lines · 231 code · 31 blank · 0 comment · 41 complexity · 3a81a245580c2f9390c9ecdf3c94f656 MD5 · raw file

  1. package android.graphics.drawable;
  2. import android.content.res.Resources;
  3. import android.content.res.Resources.Theme;
  4. import android.content.res.TypedArray;
  5. import android.graphics.drawable.DrawableContainer.DrawableContainerState;
  6. import android.hwtheme.HwThemeManager;
  7. import android.speech.tts.TextToSpeech;
  8. import android.util.AttributeSet;
  9. import android.util.StateSet;
  10. import com.android.internal.R;
  11. import java.io.IOException;
  12. import org.xmlpull.v1.XmlPullParser;
  13. import org.xmlpull.v1.XmlPullParserException;
  14. public class StateListDrawable extends DrawableContainer {
  15. private static final boolean DEBUG = false;
  16. private static final String TAG = "StateListDrawable";
  17. private boolean mMutated;
  18. private StateListState mStateListState;
  19. static class StateListState extends DrawableContainerState {
  20. int[][] mStateSets;
  21. int[] mThemeAttrs;
  22. StateListState(StateListState orig, StateListDrawable owner, Resources res) {
  23. super(orig, owner, res);
  24. if (orig != null) {
  25. this.mThemeAttrs = orig.mThemeAttrs;
  26. this.mStateSets = orig.mStateSets;
  27. return;
  28. }
  29. this.mThemeAttrs = null;
  30. this.mStateSets = new int[getCapacity()][];
  31. }
  32. void mutate() {
  33. int[] iArr;
  34. if (this.mThemeAttrs != null) {
  35. iArr = (int[]) this.mThemeAttrs.clone();
  36. } else {
  37. iArr = null;
  38. }
  39. this.mThemeAttrs = iArr;
  40. int[][] stateSets = new int[this.mStateSets.length][];
  41. for (int i = this.mStateSets.length - 1; i >= 0; i--) {
  42. if (this.mStateSets[i] != null) {
  43. iArr = (int[]) this.mStateSets[i].clone();
  44. } else {
  45. iArr = null;
  46. }
  47. stateSets[i] = iArr;
  48. }
  49. this.mStateSets = stateSets;
  50. }
  51. int addStateSet(int[] stateSet, Drawable drawable) {
  52. int pos = addChild(drawable);
  53. this.mStateSets[pos] = stateSet;
  54. return pos;
  55. }
  56. int indexOfStateSet(int[] stateSet) {
  57. int[][] stateSets = this.mStateSets;
  58. int N = getChildCount();
  59. for (int i = 0; i < N; i++) {
  60. if (StateSet.stateSetMatches(stateSets[i], stateSet)) {
  61. return i;
  62. }
  63. }
  64. return -1;
  65. }
  66. public Drawable newDrawable() {
  67. return new StateListDrawable();
  68. }
  69. public Drawable newDrawable(Resources res) {
  70. return new StateListDrawable(res, null);
  71. }
  72. public boolean canApplyTheme() {
  73. return this.mThemeAttrs == null ? super.canApplyTheme() : true;
  74. }
  75. public void growArray(int oldSize, int newSize) {
  76. super.growArray(oldSize, newSize);
  77. int[][] newStateSets = new int[newSize][];
  78. System.arraycopy(this.mStateSets, 0, newStateSets, 0, oldSize);
  79. this.mStateSets = newStateSets;
  80. }
  81. }
  82. public StateListDrawable() {
  83. this(null, null);
  84. }
  85. public void addState(int[] stateSet, Drawable drawable) {
  86. if (drawable != null) {
  87. this.mStateListState.addStateSet(stateSet, drawable);
  88. onStateChange(getState());
  89. }
  90. }
  91. public boolean isStateful() {
  92. return true;
  93. }
  94. protected boolean onStateChange(int[] stateSet) {
  95. boolean changed = super.onStateChange(stateSet);
  96. int idx = this.mStateListState.indexOfStateSet(stateSet);
  97. if (idx < 0) {
  98. idx = this.mStateListState.indexOfStateSet(StateSet.WILD_CARD);
  99. }
  100. return !selectDrawable(idx) ? changed : true;
  101. }
  102. public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme) throws XmlPullParserException, IOException {
  103. TypedArray a = Drawable.obtainAttributes(r, theme, attrs, R.styleable.StateListDrawable);
  104. super.inflateWithAttributes(r, parser, a, 1);
  105. updateStateFromTypedArray(a);
  106. updateDensity(r);
  107. a.recycle();
  108. inflateChildElements(r, parser, attrs, theme);
  109. onStateChange(getState());
  110. }
  111. private void updateStateFromTypedArray(TypedArray a) {
  112. StateListState state = this.mStateListState;
  113. state.mChangingConfigurations |= a.getChangingConfigurations();
  114. state.mThemeAttrs = a.extractThemeAttrs();
  115. state.mVariablePadding = a.getBoolean(2, state.mVariablePadding);
  116. state.mConstantSize = a.getBoolean(3, state.mConstantSize);
  117. state.mEnterFadeDuration = a.getInt(4, state.mEnterFadeDuration);
  118. state.mExitFadeDuration = a.getInt(5, state.mExitFadeDuration);
  119. state.mDither = a.getBoolean(0, state.mDither);
  120. state.mAutoMirrored = a.getBoolean(6, state.mAutoMirrored);
  121. }
  122. private void inflateChildElements(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme) throws XmlPullParserException, IOException {
  123. StateListState state = this.mStateListState;
  124. int innerDepth = parser.getDepth() + 1;
  125. while (true) {
  126. int type = parser.next();
  127. if (type != 1) {
  128. int depth = parser.getDepth();
  129. if (depth < innerDepth && type == 3) {
  130. return;
  131. }
  132. if (type == 2 && depth <= innerDepth && parser.getName().equals(HwThemeManager.TAG_ITEM)) {
  133. TypedArray a = Drawable.obtainAttributes(r, theme, attrs, R.styleable.StateListDrawableItem);
  134. Drawable dr = a.getDrawable(0);
  135. a.recycle();
  136. int[] states = extractStateSet(attrs);
  137. if (dr == null) {
  138. do {
  139. type = parser.next();
  140. } while (type == 4);
  141. if (type != 2) {
  142. break;
  143. }
  144. dr = Drawable.createFromXmlInner(r, parser, attrs, theme);
  145. }
  146. state.addStateSet(states, dr);
  147. }
  148. } else {
  149. return;
  150. }
  151. }
  152. throw new XmlPullParserException(parser.getPositionDescription() + ": <item> tag requires a 'drawable' attribute or " + "child tag defining a drawable");
  153. }
  154. int[] extractStateSet(AttributeSet attrs) {
  155. int numAttrs = attrs.getAttributeCount();
  156. int[] states = new int[numAttrs];
  157. int i = 0;
  158. int j = 0;
  159. while (i < numAttrs) {
  160. int j2;
  161. int stateResId = attrs.getAttributeNameResource(i);
  162. switch (stateResId) {
  163. case TextToSpeech.SUCCESS /*0*/:
  164. j2 = j;
  165. break;
  166. case android.R.attr.id /*16842960*/:
  167. case android.R.attr.drawable /*16843161*/:
  168. j2 = j;
  169. break;
  170. default:
  171. j2 = j + 1;
  172. if (!attrs.getAttributeBooleanValue(i, DEBUG)) {
  173. stateResId = -stateResId;
  174. }
  175. states[j] = stateResId;
  176. break;
  177. }
  178. i++;
  179. j = j2;
  180. }
  181. return StateSet.trimStateSet(states, j);
  182. }
  183. StateListState getStateListState() {
  184. return this.mStateListState;
  185. }
  186. public int getStateCount() {
  187. return this.mStateListState.getChildCount();
  188. }
  189. public int[] getStateSet(int index) {
  190. return this.mStateListState.mStateSets[index];
  191. }
  192. public Drawable getStateDrawable(int index) {
  193. return this.mStateListState.getChild(index);
  194. }
  195. public int getStateDrawableIndex(int[] stateSet) {
  196. return this.mStateListState.indexOfStateSet(stateSet);
  197. }
  198. public Drawable mutate() {
  199. if (!this.mMutated && super.mutate() == this) {
  200. this.mStateListState.mutate();
  201. this.mMutated = true;
  202. }
  203. return this;
  204. }
  205. StateListState cloneConstantState() {
  206. return new StateListState(this.mStateListState, this, null);
  207. }
  208. public void clearMutated() {
  209. super.clearMutated();
  210. this.mMutated = DEBUG;
  211. }
  212. public void applyTheme(Theme theme) {
  213. super.applyTheme(theme);
  214. onStateChange(getState());
  215. }
  216. protected void setConstantState(DrawableContainerState state) {
  217. super.setConstantState(state);
  218. if (state instanceof StateListState) {
  219. this.mStateListState = (StateListState) state;
  220. }
  221. }
  222. private StateListDrawable(StateListState state, Resources res) {
  223. setConstantState(new StateListState(state, this, res));
  224. onStateChange(getState());
  225. }
  226. StateListDrawable(StateListState state) {
  227. if (state != null) {
  228. setConstantState(state);
  229. }
  230. }
  231. }