1 package org.robolectric.shadows; 2 3 import android.R; 4 import android.app.Activity; 5 import android.content.Context; 6 import android.content.Intent; 7 import android.graphics.drawable.Drawable; 8 import android.view.View; 9 import android.widget.TabHost; 10 import android.widget.TabHost.TabSpec; 11 import android.widget.TabWidget; 12 import java.util.ArrayList; 13 import java.util.List; 14 import org.robolectric.annotation.Implementation; 15 import org.robolectric.annotation.Implements; 16 import org.robolectric.annotation.RealObject; 17 import org.robolectric.shadow.api.Shadow; 18 19 @SuppressWarnings({"UnusedDeclaration"}) 20 @Implements(TabHost.class) 21 public class ShadowTabHost extends ShadowViewGroup { 22 private List<TabHost.TabSpec> tabSpecs = new ArrayList<>(); 23 private TabHost.OnTabChangeListener listener; 24 private int currentTab = -1; 25 26 @RealObject 27 private TabHost realObject; 28 29 @Implementation newTabSpec(java.lang.String tag)30 protected android.widget.TabHost.TabSpec newTabSpec(java.lang.String tag) { 31 TabSpec realTabSpec = Shadow.newInstanceOf(TabHost.TabSpec.class); 32 ShadowTabSpec shadowTabSpec = Shadow.extract(realTabSpec); 33 shadowTabSpec.setTag(tag); 34 return realTabSpec; 35 } 36 37 @Implementation addTab(android.widget.TabHost.TabSpec tabSpec)38 protected void addTab(android.widget.TabHost.TabSpec tabSpec) { 39 tabSpecs.add(tabSpec); 40 ShadowTabSpec shadowTabSpec = Shadow.extract(tabSpec); 41 View indicatorAsView = shadowTabSpec.getIndicatorAsView(); 42 if (indicatorAsView != null) { 43 realObject.addView(indicatorAsView); 44 } 45 } 46 47 @Implementation setCurrentTab(int index)48 protected void setCurrentTab(int index) { 49 currentTab = index; 50 if (listener != null) { 51 listener.onTabChanged(getCurrentTabTag()); 52 } 53 } 54 55 @Implementation setCurrentTabByTag(String tag)56 protected void setCurrentTabByTag(String tag) { 57 for (int x = 0; x < tabSpecs.size(); x++) { 58 TabSpec tabSpec = tabSpecs.get(x); 59 if (tabSpec.getTag().equals(tag)) { 60 currentTab = x; 61 } 62 } 63 if (listener != null) { 64 listener.onTabChanged(getCurrentTabTag()); 65 } 66 } 67 68 @Implementation getCurrentTab()69 protected int getCurrentTab() { 70 if (currentTab == -1 && tabSpecs.size() > 0) currentTab = 0; 71 return currentTab; 72 } 73 getCurrentTabSpec()74 public TabSpec getCurrentTabSpec() { 75 return tabSpecs.get(getCurrentTab()); 76 } 77 78 @Implementation getCurrentTabTag()79 protected String getCurrentTabTag() { 80 int i = getCurrentTab(); 81 if (i >= 0 && i < tabSpecs.size()) { 82 return tabSpecs.get(i).getTag(); 83 } 84 return null; 85 } 86 87 @Implementation setOnTabChangedListener(android.widget.TabHost.OnTabChangeListener listener)88 protected void setOnTabChangedListener(android.widget.TabHost.OnTabChangeListener listener) { 89 this.listener = listener; 90 } 91 92 @Implementation getCurrentView()93 protected View getCurrentView() { 94 ShadowTabSpec ts = Shadow.extract(getCurrentTabSpec()); 95 View v = ts.getContentView(); 96 if (v == null) { 97 int viewId = ts.getContentViewId(); 98 if (realView.getContext() instanceof Activity) { 99 v = ((Activity) realView.getContext()).findViewById(viewId); 100 } else { 101 return null; 102 } 103 } 104 return v; 105 } 106 107 @Implementation getTabWidget()108 protected TabWidget getTabWidget() { 109 Context context = realView.getContext(); 110 if (context instanceof Activity) { 111 return (TabWidget) ((Activity)context).findViewById(R.id.tabs); 112 } else { 113 return null; 114 } 115 } 116 getSpecByTag(String tag)117 public TabHost.TabSpec getSpecByTag(String tag) { 118 for (TabHost.TabSpec tabSpec : tabSpecs) { 119 if (tag.equals(tabSpec.getTag())) { 120 return tabSpec; 121 } 122 } 123 return null; 124 } 125 126 @SuppressWarnings({"UnusedDeclaration"}) 127 @Implements(TabSpec.class) 128 public static class ShadowTabSpec { 129 130 @RealObject 131 TabSpec realObject; 132 private String tag; 133 private View indicatorView; 134 private Intent intent; 135 private int viewId; 136 private View contentView; 137 private CharSequence label; 138 private Drawable icon; 139 140 /** 141 * Sets the tag on the TabSpec. 142 * 143 * @param tag The tag. 144 */ setTag(String tag)145 public void setTag(String tag) { 146 this.tag = tag; 147 } 148 149 @Implementation getTag()150 protected String getTag() { 151 return tag; 152 } 153 154 /** 155 * @return the view object set in a call to {@code TabSpec#setIndicator(View)} 156 */ getIndicatorAsView()157 public View getIndicatorAsView() { 158 return this.indicatorView; 159 } 160 getIndicatorLabel()161 public String getIndicatorLabel() { 162 return this.label.toString(); 163 } 164 getIndicatorIcon()165 public Drawable getIndicatorIcon() { 166 return this.icon; 167 } 168 169 /** 170 * Same as GetIndicatorLabel() 171 * 172 * @return Tab text. 173 */ getText()174 public String getText() { 175 return label.toString(); 176 } 177 178 @Implementation setIndicator(View view)179 protected TabSpec setIndicator(View view) { 180 this.indicatorView = view; 181 return realObject; 182 } 183 184 @Implementation setIndicator(CharSequence label)185 protected TabSpec setIndicator(CharSequence label) { 186 this.label = label; 187 return realObject; 188 } 189 190 @Implementation setIndicator(CharSequence label, Drawable icon)191 protected TabSpec setIndicator(CharSequence label, Drawable icon) { 192 this.label = label; 193 this.icon = icon; 194 return realObject; 195 } 196 197 /** 198 * @return the intent object set in a call to {@code TabSpec#setContent(Intent)} 199 */ getContentAsIntent()200 public Intent getContentAsIntent() { 201 return intent; 202 } 203 204 @Implementation setContent(Intent intent)205 protected TabSpec setContent(Intent intent) { 206 this.intent = intent; 207 return realObject; 208 } 209 210 @Implementation setContent(TabHost.TabContentFactory factory)211 protected TabSpec setContent(TabHost.TabContentFactory factory) { 212 contentView = factory.createTabContent(this.tag); 213 return realObject; 214 } 215 216 @Implementation setContent(int viewId)217 protected TabSpec setContent(int viewId) { 218 this.viewId = viewId; 219 return realObject; 220 } 221 getContentViewId()222 public int getContentViewId() { 223 return viewId; 224 } 225 getContentView()226 public View getContentView() { 227 return contentView; 228 } 229 } 230 } 231