/** * Check if two Drawables are equal. A regular check for a Drawable equals just checks for the * instance reference, while this check is doing a deeper equals when dealing with {@link * DrawableContainer} instances. In these cases, the method will run equals on each of the child * drawables in the container (order is importance as well). * * @param d1 * @param d2 * @return <code>true</code> if the drawables are equal, <code>false</code> otherwise. */ public static boolean isEquals(Drawable d1, Drawable d2) { if (d1 == d2) { return true; } if (d1 == null || d2 == null) { return false; } if (d1 instanceof DrawableContainer && d2 instanceof DrawableContainer) { // Try to match the content of those containers DrawableContainerState containerState1 = (DrawableContainerState) ((DrawableContainer) d1).getConstantState(); DrawableContainerState containerState2 = (DrawableContainerState) ((DrawableContainer) d2).getConstantState(); return Arrays.equals(containerState1.getChildren(), containerState2.getChildren()); } return d1.equals(d2); }
/** * 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; }