1 package com.xtremelabs.robolectric.shadows; 2 3 import android.graphics.drawable.Drawable; 4 import android.graphics.drawable.StateListDrawable; 5 import android.util.StateSet; 6 import com.xtremelabs.robolectric.internal.Implementation; 7 import com.xtremelabs.robolectric.internal.Implements; 8 9 import java.util.ArrayList; 10 import java.util.HashMap; 11 import java.util.List; 12 import java.util.Map; 13 14 @Implements(StateListDrawable.class) 15 public class ShadowStateListDrawable extends ShadowDrawable { 16 17 private Map<Integer, Integer> stateToResource; 18 private Map<List<Integer>, Drawable> stateToDrawable; 19 __constructor__()20 public void __constructor__() { 21 stateToResource = new HashMap<Integer, Integer>(); 22 stateToDrawable = new HashMap<List<Integer>, Drawable>(); 23 } 24 addState(int stateId, int resId)25 public void addState(int stateId, int resId) { 26 stateToResource.put(stateId, resId); 27 } 28 getResourceIdForState(int stateId)29 public int getResourceIdForState(int stateId) { 30 return stateToResource.get(stateId); 31 } 32 33 @Implementation addState(int[] stateSet, Drawable drawable)34 public void addState(int[] stateSet, Drawable drawable) { 35 stateToDrawable.put(createStateList(stateSet), drawable); 36 } 37 38 /** 39 * Non Android accessor to retrieve drawable added for a specific state. 40 * 41 * @param stateSet Int array describing the state 42 * @return Drawable added via {@link #addState(int[], android.graphics.drawable.Drawable)} 43 */ getDrawableForState(int[] stateSet)44 public Drawable getDrawableForState(int[] stateSet) { 45 return stateToDrawable.get(createStateList(stateSet)); 46 } 47 createStateList(int[] stateSet)48 private List<Integer> createStateList(int[] stateSet) { 49 List<Integer> stateList = new ArrayList<Integer>(); 50 if (stateSet == StateSet.WILD_CARD) { 51 stateList.add(-1); 52 } else { 53 for (int state : stateSet) { 54 stateList.add(state); 55 } 56 } 57 58 return stateList; 59 } 60 } 61