1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.layoutlib.bridge;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.Gravity;
22import android.view.View;
23import android.view.ViewGroup;
24import android.widget.FrameLayout;
25import android.widget.TextView;
26
27/**
28 * Base class for mocked views.
29 * <p/>
30 * FrameLayout with a single TextView. Doesn't allow adding any other views to itself.
31 */
32public class MockView extends FrameLayout {
33
34    private final TextView mView;
35
36    public MockView(Context context) {
37        this(context, null);
38    }
39
40    public MockView(Context context, AttributeSet attrs) {
41        this(context, attrs, 0);
42    }
43
44    public MockView(Context context, AttributeSet attrs, int defStyleAttr) {
45        this(context, attrs, defStyleAttr, 0);
46    }
47
48    public MockView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
49        super(context, attrs, defStyleAttr, defStyleRes);
50        mView = new TextView(context, attrs);
51        mView.setTextColor(0xFF000000);
52        setGravity(Gravity.CENTER);
53        setText(getClass().getSimpleName());
54        addView(mView);
55        setBackgroundColor(0xFF7F7F7F);
56    }
57
58    // Only allow adding one TextView.
59    @Override
60    public void addView(View child) {
61        if (child == mView) {
62            super.addView(child);
63        }
64    }
65
66    @Override
67    public void addView(View child, int index) {
68        if (child == mView) {
69            super.addView(child, index);
70        }
71    }
72
73    @Override
74    public void addView(View child, int width, int height) {
75        if (child == mView) {
76            super.addView(child, width, height);
77        }
78    }
79
80    @Override
81    public void addView(View child, ViewGroup.LayoutParams params) {
82        if (child == mView) {
83            super.addView(child, params);
84        }
85    }
86
87    @Override
88    public void addView(View child, int index, ViewGroup.LayoutParams params) {
89        if (child == mView) {
90            super.addView(child, index, params);
91        }
92    }
93
94    // The following methods are called by the IDE via reflection, and should be considered part
95    // of the API.
96    // Historically, MockView used to be a textView and had these methods. Now, we simply delegate
97    // them to the contained textView.
98
99    public void setText(CharSequence text) {
100        mView.setText(text);
101    }
102
103    @SuppressWarnings("WeakerAccess") // This method is used from Studio
104    public void setGravity(int gravity) {
105        mView.setGravity(gravity);
106    }
107}
108