1 package com.xtremelabs.robolectric.shadows; 2 3 import java.lang.reflect.Array; 4 import java.util.ArrayList; 5 import java.util.HashMap; 6 7 import android.text.SpannableString; 8 9 import com.xtremelabs.robolectric.internal.Implementation; 10 import com.xtremelabs.robolectric.internal.Implements; 11 12 @Implements(SpannableString.class) 13 public class ShadowSpannableString { 14 15 private CharSequence text = ""; 16 private HashMap<Object, SpanHolder> spans = new HashMap<Object, SpanHolder>(); 17 __constructor__(CharSequence source)18 public void __constructor__(CharSequence source) { 19 text = source; 20 } 21 22 @Implementation toString()23 public String toString() { 24 return text.toString(); 25 } 26 27 @Implementation setSpan(Object what, int start, int end, int flags)28 public void setSpan(Object what, int start, int end, int flags) { 29 spans.put(what, new SpanHolder(start, end, flags) ); 30 } 31 32 @Implementation removeSpan(Object what)33 public void removeSpan(Object what) { 34 spans.remove(what); 35 } 36 37 @Implementation getSpans(int queryStart, int queryEnd, Class<T> kind)38 public <T> T[] getSpans(int queryStart, int queryEnd, Class<T> kind) { 39 ArrayList<T> retVal = new ArrayList<T>(); 40 41 for (Object s : spans.keySet()) { 42 if (kind.isInstance(s)) { 43 SpanHolder h = spans.get(s); 44 if ( (h.start <= queryStart && h.end >= queryStart) || 45 (h.start <= queryEnd && h.end >= queryEnd) || 46 (h.start >= queryStart && h.end <= queryEnd) ) { 47 retVal.add((T) s); 48 } 49 } 50 } 51 T[] array = (T[]) Array.newInstance(kind, retVal.size()); 52 return retVal.toArray(array); 53 } 54 55 @Implementation getSpanStart(Object what)56 public int getSpanStart(Object what) { 57 SpanHolder spanHolder = spans.get(what); 58 if (spanHolder != null) { 59 return spanHolder.start; 60 } else { 61 return -1; 62 } 63 } 64 65 @Implementation getSpanEnd(Object what)66 public int getSpanEnd(Object what) { 67 SpanHolder spanHolder = spans.get(what); 68 if (spanHolder != null) { 69 return spanHolder.end; 70 } else { 71 return -1; 72 } 73 } 74 75 private class SpanHolder { 76 public int start; 77 public int end; 78 public int flags; 79 SpanHolder(int start, int end, int flags)80 public SpanHolder(int start, int end, int flags) { 81 this.start = start; 82 this.end = end; 83 this.flags = flags; 84 } 85 } 86 } 87