diff options
Diffstat (limited to 'core/java/android/util/StateSet.java')
| -rw-r--r-- | core/java/android/util/StateSet.java | 83 |
1 files changed, 82 insertions, 1 deletions
diff --git a/core/java/android/util/StateSet.java b/core/java/android/util/StateSet.java index 2623638d781b..83dfc4705d09 100644 --- a/core/java/android/util/StateSet.java +++ b/core/java/android/util/StateSet.java @@ -36,7 +36,88 @@ import com.android.internal.R; */ public class StateSet { - /** @hide */ public StateSet() {} + /** + * The order here is very important to + * {@link android.view.View#getDrawableState()} + */ + private static final int[][] VIEW_STATE_SETS; + + /** @hide */ + public static final int VIEW_STATE_WINDOW_FOCUSED = 1; + /** @hide */ + public static final int VIEW_STATE_SELECTED = 1 << 1; + /** @hide */ + public static final int VIEW_STATE_FOCUSED = 1 << 2; + /** @hide */ + public static final int VIEW_STATE_ENABLED = 1 << 3; + /** @hide */ + public static final int VIEW_STATE_PRESSED = 1 << 4; + /** @hide */ + public static final int VIEW_STATE_ACTIVATED = 1 << 5; + /** @hide */ + public static final int VIEW_STATE_ACCELERATED = 1 << 6; + /** @hide */ + public static final int VIEW_STATE_HOVERED = 1 << 7; + /** @hide */ + public static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8; + /** @hide */ + public static final int VIEW_STATE_DRAG_HOVERED = 1 << 9; + + static final int[] VIEW_STATE_IDS = new int[] { + R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED, + R.attr.state_selected, VIEW_STATE_SELECTED, + R.attr.state_focused, VIEW_STATE_FOCUSED, + R.attr.state_enabled, VIEW_STATE_ENABLED, + R.attr.state_pressed, VIEW_STATE_PRESSED, + R.attr.state_activated, VIEW_STATE_ACTIVATED, + R.attr.state_accelerated, VIEW_STATE_ACCELERATED, + R.attr.state_hovered, VIEW_STATE_HOVERED, + R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT, + R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED + }; + + static { + if ((VIEW_STATE_IDS.length / 2) != R.styleable.ViewDrawableStates.length) { + throw new IllegalStateException( + "VIEW_STATE_IDs array length does not match ViewDrawableStates style array"); + } + + final int[] orderedIds = new int[VIEW_STATE_IDS.length]; + for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) { + final int viewState = R.styleable.ViewDrawableStates[i]; + for (int j = 0; j < VIEW_STATE_IDS.length; j += 2) { + if (VIEW_STATE_IDS[j] == viewState) { + orderedIds[i * 2] = viewState; + orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1]; + } + } + } + + final int NUM_BITS = VIEW_STATE_IDS.length / 2; + VIEW_STATE_SETS = new int[1 << NUM_BITS][]; + for (int i = 0; i < VIEW_STATE_SETS.length; i++) { + final int numBits = Integer.bitCount(i); + final int[] set = new int[numBits]; + int pos = 0; + for (int j = 0; j < orderedIds.length; j += 2) { + if ((i & orderedIds[j + 1]) != 0) { + set[pos++] = orderedIds[j]; + } + } + VIEW_STATE_SETS[i] = set; + } + } + + /** @hide */ + public static int[] get(int mask) { + if (mask >= VIEW_STATE_SETS.length) { + throw new IllegalArgumentException("Invalid state set mask"); + } + return VIEW_STATE_SETS[mask]; + } + + /** @hide */ + public StateSet() {} public static final int[] WILD_CARD = new int[0]; public static final int[] NOTHING = new int[] { 0 }; |
