/** * Returns a {@link Map} that holds a mapping from the existing state-lists in the background * {@link Drawable}. * * @param background A {@link Drawable} background ( {@link StateListDrawable}). * @return A {@link Map}. An empty map in case the background <code>null</code>, or is not a * {@link StateListDrawable}. */ public static Map<int[], Drawable> getExistingStates(Drawable background) { LinkedHashMap<int[], Drawable> map = new LinkedHashMap<int[], Drawable>(); if (background instanceof StateListDrawable) { // Grab the existing states. Note that the API hides some of the // public functionality with the @hide tag, so we have to access // those through reflection... StateListDrawable stateList = (StateListDrawable) background; DrawableContainerState containerState = (DrawableContainerState) stateList.getConstantState(); Drawable[] children = containerState.getChildren(); try { // This method is public but hidden ("pending API council") Method method = stateList.getClass().getMethod("getStateSet", int.class); for (int i = 0; i < containerState.getChildCount(); i++) { Object state = method.invoke(stateList, i); if (state instanceof int[]) { map.put((int[]) state, children[i]); } } } catch (Exception e) { PXLog.e(PXViewStyleAdapter.class.getSimpleName(), e, "Error getting the state set"); } } return map; }