1package com.xtremelabs.robolectric.shadows;
2
3import android.view.LayoutInflater;
4import android.view.View;
5import android.view.ViewGroup;
6import android.view.ViewParent;
7import android.view.ViewStub;
8import com.xtremelabs.robolectric.Robolectric;
9import com.xtremelabs.robolectric.internal.Implementation;
10import com.xtremelabs.robolectric.internal.Implements;
11import com.xtremelabs.robolectric.internal.RealObject;
12import com.xtremelabs.robolectric.res.ResourceExtractor;
13
14import java.lang.ref.WeakReference;
15
16@SuppressWarnings({"UnusedDeclaration"})
17@Implements(ViewStub.class)
18public class ShadowViewStub extends ShadowView {
19    @RealObject ViewStub viewStub;
20
21    private int mLayoutResource = 0;
22    private int mInflatedId;
23    private WeakReference<View> mInflatedViewRef;
24    private ViewStub.OnInflateListener mInflateListener;
25
26    @Override public void applyAttributes() {
27        super.applyAttributes();
28
29        String inflatedId = attributeSet.getAttributeValue("android", "inflatedId");
30        if (inflatedId != null) {
31            mInflatedId = getResourceId(inflatedId);
32        }
33
34        String layoutResId = attributeSet.getAttributeValue("android", "layout");
35        if (layoutResId != null) {
36            mLayoutResource = getResourceId(layoutResId);
37        }
38    }
39
40    private Integer getResourceId(String inflatedId) {
41        ResourceExtractor resourceExtractor = Robolectric.getShadowApplication().getResourceLoader().getResourceExtractor();
42        return resourceExtractor.getResourceId(inflatedId, false);
43    }
44
45    @Implementation
46    public int getInflatedId() {
47        return mInflatedId;
48    }
49
50    @Implementation
51    public void setInflatedId(int inflatedId) {
52        mInflatedId = inflatedId;
53    }
54
55    @Implementation
56    public int getLayoutResource() {
57        return mLayoutResource;
58    }
59
60    @Implementation
61    public void setLayoutResource(int layoutResource) {
62        mLayoutResource = layoutResource;
63    }
64
65    @Implementation
66    public View inflate() {
67        ViewParent viewParent = viewStub.getParent();
68
69        if (viewParent != null && viewParent instanceof ViewGroup) {
70            if (mLayoutResource != 0) {
71                ViewGroup parent = (ViewGroup) viewParent;
72                LayoutInflater factory = LayoutInflater.from(viewStub.getContext());
73                View view = factory.inflate(mLayoutResource, parent, false);
74
75                if (mInflatedId != View.NO_ID) {
76                    view.setId(mInflatedId);
77                }
78
79                int index = parent.indexOfChild(viewStub);
80                parent.removeViewAt(index);
81//        parent.removeViewInLayout(viewStub);
82
83                ViewGroup.LayoutParams layoutParams = getLayoutParams();
84                if (layoutParams != null) {
85                    parent.addView(view, index, layoutParams);
86                } else {
87                    parent.addView(view, index);
88                }
89
90                mInflatedViewRef = new WeakReference<View>(view);
91
92                if (mInflateListener != null) {
93                    mInflateListener.onInflate(viewStub, view);
94                }
95
96                return view;
97            } else {
98                throw new IllegalArgumentException("ViewStub must have a valid layoutResource");
99            }
100        } else {
101            throw new IllegalStateException("ViewStub must have a non-null ViewGroup viewParent");
102        }
103    }
104}
105