1 package org.robolectric.res; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 /** 7 * Represents the list of styles applied to a Theme. 8 */ 9 public class ThemeStyleSet implements Style { 10 11 private List<OverlayedStyle> styles = new ArrayList<>(); 12 getAttrValue(ResName attrName)13 @Override public AttributeResource getAttrValue(ResName attrName) { 14 AttributeResource attribute = null; 15 16 for (OverlayedStyle overlayedStyle : styles) { 17 AttributeResource overlayedAttribute = overlayedStyle.style.getAttrValue(attrName); 18 if (overlayedAttribute != null && (attribute == null || overlayedStyle.force)) { 19 attribute = overlayedAttribute; 20 } 21 } 22 23 return attribute; 24 } 25 apply(Style style, boolean force)26 public void apply(Style style, boolean force) { 27 OverlayedStyle styleToAdd = new OverlayedStyle(style, force); 28 for (int i = 0; i < styles.size(); ++i) { 29 if (styleToAdd.equals(styles.get(i))) { 30 styles.remove(i); 31 break; 32 } 33 } 34 styles.add(styleToAdd); 35 } 36 copy()37 public ThemeStyleSet copy() { 38 ThemeStyleSet themeStyleSet = new ThemeStyleSet(); 39 themeStyleSet.styles.addAll(this.styles); 40 return themeStyleSet; 41 } 42 43 @Override toString()44 public String toString() { 45 if (styles.isEmpty()) { 46 return "theme with no applied styles"; 47 } else { 48 return "theme with applied styles: " + styles + ""; 49 } 50 } 51 52 private static class OverlayedStyle { 53 Style style; 54 boolean force; 55 OverlayedStyle(Style style, boolean force)56 OverlayedStyle(Style style, boolean force) { 57 this.style = style; 58 this.force = force; 59 } 60 61 @Override equals(Object obj)62 public boolean equals(Object obj) { 63 if (!(obj instanceof OverlayedStyle)) { 64 return false; 65 } 66 OverlayedStyle overlayedStyle = (OverlayedStyle) obj; 67 return style.equals(overlayedStyle.style); 68 } 69 70 @Override hashCode()71 public int hashCode() { 72 return style.hashCode(); 73 } 74 75 @Override toString()76 public String toString() { 77 return style.toString() + (force ? " (forced)" : ""); 78 } 79 } 80 81 } 82