1/*
2 * Copyright (C) 2006 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 android.widget;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.View;
22import android.view.ViewGroup;
23import android.view.accessibility.AccessibilityEvent;
24import android.view.accessibility.AccessibilityNodeInfo;
25
26/**
27 * {@link ViewAnimator} that switches between two views, and has a factory
28 * from which these views are created.  You can either use the factory to
29 * create the views, or add them yourself.  A ViewSwitcher can only have two
30 * child views, of which only one is shown at a time.
31 */
32public class ViewSwitcher extends ViewAnimator {
33    /**
34     * The factory used to create the two children.
35     */
36    ViewFactory mFactory;
37
38    /**
39     * Creates a new empty ViewSwitcher.
40     *
41     * @param context the application's environment
42     */
43    public ViewSwitcher(Context context) {
44        super(context);
45    }
46
47    /**
48     * Creates a new empty ViewSwitcher for the given context and with the
49     * specified set attributes.
50     *
51     * @param context the application environment
52     * @param attrs a collection of attributes
53     */
54    public ViewSwitcher(Context context, AttributeSet attrs) {
55        super(context, attrs);
56    }
57
58    /**
59     * {@inheritDoc}
60     *
61     * @throws IllegalStateException if this switcher already contains two children
62     */
63    @Override
64    public void addView(View child, int index, ViewGroup.LayoutParams params) {
65        if (getChildCount() >= 2) {
66            throw new IllegalStateException("Can't add more than 2 views to a ViewSwitcher");
67        }
68        super.addView(child, index, params);
69    }
70
71    @Override
72    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
73        super.onInitializeAccessibilityEvent(event);
74        event.setClassName(ViewSwitcher.class.getName());
75    }
76
77    @Override
78    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
79        super.onInitializeAccessibilityNodeInfo(info);
80        info.setClassName(ViewSwitcher.class.getName());
81    }
82
83    /**
84     * Returns the next view to be displayed.
85     *
86     * @return the view that will be displayed after the next views flip.
87     */
88    public View getNextView() {
89        int which = mWhichChild == 0 ? 1 : 0;
90        return getChildAt(which);
91    }
92
93    private View obtainView() {
94        View child = mFactory.makeView();
95        LayoutParams lp = (LayoutParams) child.getLayoutParams();
96        if (lp == null) {
97            lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
98        }
99        addView(child, lp);
100        return child;
101    }
102
103    /**
104     * Sets the factory used to create the two views between which the
105     * ViewSwitcher will flip. Instead of using a factory, you can call
106     * {@link #addView(android.view.View, int, android.view.ViewGroup.LayoutParams)}
107     * twice.
108     *
109     * @param factory the view factory used to generate the switcher's content
110     */
111    public void setFactory(ViewFactory factory) {
112        mFactory = factory;
113        obtainView();
114        obtainView();
115    }
116
117    /**
118     * Reset the ViewSwitcher to hide all of the existing views and to make it
119     * think that the first time animation has not yet played.
120     */
121    public void reset() {
122        mFirstTime = true;
123        View v;
124        v = getChildAt(0);
125        if (v != null) {
126            v.setVisibility(View.GONE);
127        }
128        v = getChildAt(1);
129        if (v != null) {
130            v.setVisibility(View.GONE);
131        }
132    }
133
134    /**
135     * Creates views in a ViewSwitcher.
136     */
137    public interface ViewFactory {
138        /**
139         * Creates a new {@link android.view.View} to be added in a
140         * {@link android.widget.ViewSwitcher}.
141         *
142         * @return a {@link android.view.View}
143         */
144        View makeView();
145    }
146}
147
148