ShadowTextView.java revision afe0a89d904a7fe2f5980b9deb26cc3240192459
1a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultzpackage com.xtremelabs.robolectric.fakes; 26eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 3924f851cb0ec279b6974744ce623f25bdc657551Ryan Richardimport android.graphics.drawable.Drawable; 41712b8163f6212ee27c973d4cdffc8c9bf0ad2ecChristian Williams & Jay Phillipsimport android.text.style.URLSpan; 5542bd08b00db32a43d0a5ffe137017e04bcd7022Christian Williamsimport android.text.util.Linkify; 65411bf7cde998b5c2e70b69fe3d7227b6783ab7cChristian Williamsimport android.view.KeyEvent; 76eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulzimport android.widget.TextView; 8a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultzimport com.xtremelabs.robolectric.util.Implementation; 9a2043a8fff01f3e2d9a5e30dd1cf5fb6a5342f4dTyler Schultzimport com.xtremelabs.robolectric.util.Implements; 106eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 118bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisherimport java.util.ArrayList; 128bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisherimport java.util.List; 13daeb2fe3c6ab9c98fbbfd3f5c25bc92a15798a3eIan Fisher & Ryan Richard 14a89236397e869467b3fc6f7208f551da61235795Christian Williams & Tyler Schultzimport static android.view.View.VISIBLE; 15a89236397e869467b3fc6f7208f551da61235795Christian Williams & Tyler Schultz 160b378afcde0200166e546c46a7642015accfce49Christian Williams & Ian Fisher@SuppressWarnings({"UnusedDeclaration"}) 170b378afcde0200166e546c46a7642015accfce49Christian Williams & Ian Fisher@Implements(TextView.class) 18afe0a89d904a7fe2f5980b9deb26cc3240192459Christian Williamspublic class ShadowTextView extends ShadowView { 1907002220b5af63a89e960902a080af69fc26e5f8Christian Williams private CharSequence text = ""; 20a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard public CompoundDrawables compoundDrawables; 2118061bf213790ec5c347bf7fbb22f50fa858b1c2Ian Fisher & Tyler Schultz public int textColorHexValue = UNINITIALIZED_ATTRIBUTE; 22f33e228a6f3eed1c97488df2ca30da315324aa1eHarry Ugol public int textSize = UNINITIALIZED_ATTRIBUTE; 23542bd08b00db32a43d0a5ffe137017e04bcd7022Christian Williams public boolean autoLinkPhoneNumbers; 24542bd08b00db32a43d0a5ffe137017e04bcd7022Christian Williams private int autoLinkMask; 25602a09d30446366755f442899b0563cc47af2b2bRyan Richard & Tyler Schultz private CharSequence hintText; 26a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard private int compoundDrawablePadding; 276eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 28afe0a89d904a7fe2f5980b9deb26cc3240192459Christian Williams public ShadowTextView(TextView view) { 296eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz super(view); 306eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 316eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 32de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams @Implementation 33775ccf7ae53d9b4c77d3adf2514f26747cebd89cIan Fisher & Ryan Richard public void setText(CharSequence text) { 3435a84059bfd69e953541a69ba353a941c9579a99Phil Goodwin & Ryan Richard if (text == null) { 3535a84059bfd69e953541a69ba353a941c9579a99Phil Goodwin & Ryan Richard text = ""; 3635a84059bfd69e953541a69ba353a941c9579a99Phil Goodwin & Ryan Richard } 376eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz this.text = text; 386eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 396eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz 40de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams @Implementation 41775ccf7ae53d9b4c77d3adf2514f26747cebd89cIan Fisher & Ryan Richard public void setText(int textResourceId) { 42684ccc2fc5a1c79aa45af22b95c37f7c509a00c1Christian Williams this.text = getResources().getText(textResourceId); 43810ca4e6f0dc6d69504b3e03ef61155f10083fbeChristian Williams & Joe Moore } 44810ca4e6f0dc6d69504b3e03ef61155f10083fbeChristian Williams & Joe Moore 45de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams @Implementation 466eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz public CharSequence getText() { 476eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz return text; 486eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz } 4921da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol 50de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams @Implementation 510f455051f747e5c472b939665ef66487819c3c6eChristian Williams & Ian Fisher public int length() { 520f455051f747e5c472b939665ef66487819c3c6eChristian Williams & Ian Fisher return text.length(); 530f455051f747e5c472b939665ef66487819c3c6eChristian Williams & Ian Fisher } 540f455051f747e5c472b939665ef66487819c3c6eChristian Williams & Ian Fisher 55de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams @Implementation 56f33e228a6f3eed1c97488df2ca30da315324aa1eHarry Ugol public void setTextColor(int color) { 5718061bf213790ec5c347bf7fbb22f50fa858b1c2Ian Fisher & Tyler Schultz textColorHexValue = color; 58f33e228a6f3eed1c97488df2ca30da315324aa1eHarry Ugol } 59f33e228a6f3eed1c97488df2ca30da315324aa1eHarry Ugol 60de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams @Implementation 61f33e228a6f3eed1c97488df2ca30da315324aa1eHarry Ugol public void setTextSize(float size) { 62f33e228a6f3eed1c97488df2ca30da315324aa1eHarry Ugol textSize = (int) size; 63f33e228a6f3eed1c97488df2ca30da315324aa1eHarry Ugol } 64f33e228a6f3eed1c97488df2ca30da315324aa1eHarry Ugol 65de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams @Implementation 66602a09d30446366755f442899b0563cc47af2b2bRyan Richard & Tyler Schultz public final void setHint(int resId) { 67602a09d30446366755f442899b0563cc47af2b2bRyan Richard & Tyler Schultz this.hintText = getResources().getText(resId); 68602a09d30446366755f442899b0563cc47af2b2bRyan Richard & Tyler Schultz } 69602a09d30446366755f442899b0563cc47af2b2bRyan Richard & Tyler Schultz 70de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams @Implementation 71602a09d30446366755f442899b0563cc47af2b2bRyan Richard & Tyler Schultz public CharSequence getHint() { 72602a09d30446366755f442899b0563cc47af2b2bRyan Richard & Tyler Schultz return hintText; 73602a09d30446366755f442899b0563cc47af2b2bRyan Richard & Tyler Schultz } 74602a09d30446366755f442899b0563cc47af2b2bRyan Richard & Tyler Schultz 75de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams @Implementation 761712b8163f6212ee27c973d4cdffc8c9bf0ad2ecChristian Williams & Jay Phillips public URLSpan[] getUrls() { 771712b8163f6212ee27c973d4cdffc8c9bf0ad2ecChristian Williams & Jay Phillips String[] words = text.toString().split("\\s+"); 781712b8163f6212ee27c973d4cdffc8c9bf0ad2ecChristian Williams & Jay Phillips List<URLSpan> urlSpans = new ArrayList<URLSpan>(); 791712b8163f6212ee27c973d4cdffc8c9bf0ad2ecChristian Williams & Jay Phillips for (String word : words) { 801712b8163f6212ee27c973d4cdffc8c9bf0ad2ecChristian Williams & Jay Phillips if (word.startsWith("http://")) { 811712b8163f6212ee27c973d4cdffc8c9bf0ad2ecChristian Williams & Jay Phillips urlSpans.add(new URLSpan(word)); 821712b8163f6212ee27c973d4cdffc8c9bf0ad2ecChristian Williams & Jay Phillips } 831712b8163f6212ee27c973d4cdffc8c9bf0ad2ecChristian Williams & Jay Phillips } 841712b8163f6212ee27c973d4cdffc8c9bf0ad2ecChristian Williams & Jay Phillips return urlSpans.toArray(new URLSpan[urlSpans.size()]); 851712b8163f6212ee27c973d4cdffc8c9bf0ad2ecChristian Williams & Jay Phillips } 861712b8163f6212ee27c973d4cdffc8c9bf0ad2ecChristian Williams & Jay Phillips 87de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams @Implementation 88542bd08b00db32a43d0a5ffe137017e04bcd7022Christian Williams public final void setAutoLinkMask(int mask) { 89542bd08b00db32a43d0a5ffe137017e04bcd7022Christian Williams autoLinkMask = mask; 90542bd08b00db32a43d0a5ffe137017e04bcd7022Christian Williams 91542bd08b00db32a43d0a5ffe137017e04bcd7022Christian Williams autoLinkPhoneNumbers = (mask & Linkify.PHONE_NUMBERS) != 0; 92542bd08b00db32a43d0a5ffe137017e04bcd7022Christian Williams } 93542bd08b00db32a43d0a5ffe137017e04bcd7022Christian Williams 94de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams @Implementation 9521da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) { 96a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard compoundDrawables = new CompoundDrawables(left, top , right, bottom); 9721da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol } 9821da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol 99de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams @Implementation 100924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, 101924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard Drawable right, Drawable bottom) { 102a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard compoundDrawables = new CompoundDrawables(left, top , right, bottom); 103a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard } 104a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard 105a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard @Implementation 106a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) { 107a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard compoundDrawables = new CompoundDrawables(left, top , right, bottom); 108924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard } 109924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard 110de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams @Implementation 111924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard public Drawable[] getCompoundDrawables() { 112a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard if (compoundDrawables == null) { 113924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard return new Drawable[]{null, null, null, null}; 114924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard } 115924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard return new Drawable[]{ 116a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard compoundDrawables.leftDrawable, 117a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard compoundDrawables.topDrawable, 118a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard compoundDrawables.rightDrawable, 119a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard compoundDrawables.bottomDrawable 120924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard }; 121924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard } 122924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard 123de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams @Implementation 124a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard public void setCompoundDrawablePadding(int compoundDrawablePadding) { 125a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard this.compoundDrawablePadding = compoundDrawablePadding; 126a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard } 127a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard 128a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard @Implementation 129a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard public int getCompoundDrawablePadding() { 130a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard return compoundDrawablePadding; 131a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard } 132a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard 133a329318a8b2661107af6f9b25ffecf892b3c2c72Christian Williams & Ryan Richard @Implementation 1345411bf7cde998b5c2e70b69fe3d7227b6783ab7cChristian Williams public boolean onKeyDown(int keyCode, KeyEvent event) { 1355411bf7cde998b5c2e70b69fe3d7227b6783ab7cChristian Williams if (onKeyListener != null) { 1365411bf7cde998b5c2e70b69fe3d7227b6783ab7cChristian Williams return onKeyListener.onKey(realView, keyCode, event); 1375411bf7cde998b5c2e70b69fe3d7227b6783ab7cChristian Williams } else { 1385411bf7cde998b5c2e70b69fe3d7227b6783ab7cChristian Williams return false; 1395411bf7cde998b5c2e70b69fe3d7227b6783ab7cChristian Williams } 1405411bf7cde998b5c2e70b69fe3d7227b6783ab7cChristian Williams } 141924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard 1422ff94a9e4df5b0d3eb21bda70793b27d9f1f353cChristian Williams & Phil Goodwin @Override 1432ff94a9e4df5b0d3eb21bda70793b27d9f1f353cChristian Williams & Phil Goodwin public String innerText() { 144a89236397e869467b3fc6f7208f551da61235795Christian Williams & Tyler Schultz return (text == null || visibility != VISIBLE) ? "" : text.toString(); 1452ff94a9e4df5b0d3eb21bda70793b27d9f1f353cChristian Williams & Phil Goodwin } 1462ff94a9e4df5b0d3eb21bda70793b27d9f1f353cChristian Williams & Phil Goodwin 1478bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher public static class CompoundDrawables { 14821da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol public int left; 14921da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol public int top; 15021da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol public int right; 15121da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol public int bottom; 152924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard 153924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard public Drawable leftDrawable; 154924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard public Drawable topDrawable; 155924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard public Drawable rightDrawable; 156924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard public Drawable bottomDrawable; 157924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard 158924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard public CompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) { 159924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard leftDrawable = left; 160924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard topDrawable = top; 161924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard rightDrawable = right; 162924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard bottomDrawable = bottom; 163924f851cb0ec279b6974744ce623f25bdc657551Ryan Richard } 16421da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol 1658bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher public CompoundDrawables(int left, int top, int right, int bottom) { 16621da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol this.left = left; 16721da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol this.top = top; 16821da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol this.right = right; 16921da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol this.bottom = bottom; 17021da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol } 1718bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher 1728bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher @Override 1738bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher public boolean equals(Object o) { 1748bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher if (this == o) return true; 1758bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher if (o == null || getClass() != o.getClass()) return false; 1768bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher 1778bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher CompoundDrawables that = (CompoundDrawables) o; 1788bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher 1798bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher if (bottom != that.bottom) return false; 1808bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher if (left != that.left) return false; 1818bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher if (right != that.right) return false; 1828bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher if (top != that.top) return false; 1838bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher 1848bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher return true; 1858bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher } 1868bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher 1878bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher @Override 1888bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher public int hashCode() { 1898bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher int result = left; 1908bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher result = 31 * result + top; 1918bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher result = 31 * result + right; 1928bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher result = 31 * result + bottom; 1938bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher return result; 1948bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher } 1958bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher 1968bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher @Override 1978bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher public String toString() { 1988bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher return "CompoundDrawables{" + 1998bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher "left=" + left + 2008bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher ", top=" + top + 2018bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher ", right=" + right + 2028bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher ", bottom=" + bottom + 2038bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher '}'; 2048bb1af1810bfbfd724d88898d1c4e7aef9945fb9Christian Williams & Ian Fisher } 20521da5e43a4a57a9c76b9f7a9e335268e93feb262Christian Williams & Harry Ugol } 2066eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz} 207