1/*
2 * Copyright (C) 2011 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.bidi;
18
19import java.util.ArrayList;
20import java.util.HashMap;
21import java.util.List;
22import java.util.Map;
23
24import android.app.Activity;
25import android.app.Fragment;
26import android.app.FragmentTransaction;
27import android.os.Bundle;
28import android.view.Menu;
29import android.view.MenuInflater;
30import android.view.View;
31import android.widget.AdapterView;
32import android.widget.ListView;
33import android.widget.SimpleAdapter;
34
35public class BiDiTestActivity extends Activity {
36
37    private static final String KEY_CLASS = "class";
38    private static final String KEY_TITLE = "title";
39    private static final String KEY_FRAGMENT_ID = "id";
40
41    private ListView mList;
42
43    private AdapterView.OnItemClickListener mOnClickListener =
44            new AdapterView.OnItemClickListener() {
45                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
46                    onListItemClick((ListView)parent, v, position, id);
47                }
48    };
49
50    private void onListItemClick(ListView lv, View v, int position, long id) {
51        // Show the test
52        Map<String, Object> map = (Map<String, Object>)lv.getItemAtPosition(position);
53        int fragmentId = (Integer) map.get(KEY_FRAGMENT_ID);
54        Fragment fragment = getFragmentManager().findFragmentById(fragmentId);
55        if (fragment == null) {
56            try {
57                // Create an instance of the test
58                Class<? extends Fragment> clazz = (Class<? extends Fragment>) map.get(KEY_CLASS);
59                fragment = clazz.newInstance();
60
61                // Replace the old test fragment with the new one
62                FragmentTransaction ft = getFragmentManager().beginTransaction();
63                ft.replace(R.id.testframe, fragment);
64                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
65                ft.commit();
66            } catch (InstantiationException e) {
67            } catch (IllegalAccessException e) {
68            }
69        }
70    }
71
72    @Override
73    protected void onCreate(Bundle savedInstanceState) {
74        super.onCreate(savedInstanceState);
75
76        setContentView(R.layout.main);
77
78        mList = (ListView) findViewById(R.id.testlist);
79        mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
80        mList.setFocusableInTouchMode(true);
81
82        final SimpleAdapter adapter = new SimpleAdapter(this, getTests(),
83                R.layout.custom_list_item, new String[]{"title"},
84                new int[]{android.R.id.text1});
85        mList.setAdapter(adapter);
86
87        mList.setOnItemClickListener(mOnClickListener);
88    }
89
90    private void addItem(List<Map<String, Object>> data, String name,
91            Class<? extends Fragment> clazz, int fragmentId) {
92        Map<String, Object> temp = new HashMap<String, Object>();
93        temp.put(KEY_TITLE, name);
94        temp.put(KEY_CLASS, clazz);
95        temp.put(KEY_FRAGMENT_ID, fragmentId);
96        data.add(temp);
97    }
98
99    private List<Map<String, Object>> getTests() {
100        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
101
102        addItem(result, "Basic", BiDiTestBasic.class, R.id.basic);
103
104        addItem(result, "Canvas", BiDiTestCanvas.class, R.id.canvas);
105        addItem(result, "Canvas2", BiDiTestCanvas2.class, R.id.canvas2);
106
107        addItem(result, "TextView LTR", BiDiTestTextViewLtr.class, R.id.textview_ltr);
108        addItem(result, "TextView RTL", BiDiTestTextViewRtl.class, R.id.textview_rtl);
109        addItem(result, "TextView LOC", BiDiTestTextViewLocale.class, R.id.textview_locale);
110
111        addItem(result, "TextDirection LTR", BiDiTestTextViewDirectionLtr.class, R.id.textview_direction_ltr);
112        addItem(result, "TextDirection RTL", BiDiTestTextViewDirectionRtl.class, R.id.textview_direction_rtl);
113
114        addItem(result, "TextAlignment LTR", BiDiTestTextViewAlignmentLtr.class, R.id.textview_alignment_ltr);
115        addItem(result, "TextAlignment RTL", BiDiTestTextViewAlignmentRtl.class, R.id.textview_alignment_rtl);
116
117        addItem(result, "Linear LTR", BiDiTestLinearLayoutLtr.class, R.id.linear_layout_ltr);
118        addItem(result, "Linear RTL", BiDiTestLinearLayoutRtl.class, R.id.linear_layout_rtl);
119        addItem(result, "Linear LOC", BiDiTestLinearLayoutLocale.class, R.id.linear_layout_locale);
120
121        addItem(result, "Grid LTR", BiDiTestGridLayoutLtr.class, R.id.grid_layout_ltr);
122        addItem(result, "Grid RTL", BiDiTestGridLayoutRtl.class, R.id.grid_layout_rtl);
123        addItem(result, "Grid LOC", BiDiTestGridLayoutLocale.class, R.id.grid_layout_locale);
124        addItem(result, "Grid C-LTR", BiDiTestGridLayoutCodeLtr.class, R.id.grid_layout_code);
125        addItem(result, "Grid C-RTL", BiDiTestGridLayoutCodeRtl.class, R.id.grid_layout_code);
126
127        addItem(result, "Frame LTR", BiDiTestFrameLayoutLtr.class, R.id.frame_layout_ltr);
128        addItem(result, "Frame RTL", BiDiTestFrameLayoutRtl.class, R.id.frame_layout_rtl);
129        addItem(result, "Frame LOC", BiDiTestFrameLayoutLocale.class, R.id.frame_layout_locale);
130
131        addItem(result, "Relative LTR", BiDiTestRelativeLayoutLtr.class, R.id.relative_layout_ltr);
132        addItem(result, "Relative RTL", BiDiTestRelativeLayoutRtl.class, R.id.relative_layout_rtl);
133
134        addItem(result, "Relative2 LTR", BiDiTestRelativeLayout2Ltr.class, R.id.relative_layout_2_ltr);
135        addItem(result, "Relative2 RTL", BiDiTestRelativeLayout2Rtl.class, R.id.relative_layout_2_rtl);
136        addItem(result, "Relative2 LOC", BiDiTestRelativeLayout2Locale.class, R.id.relative_layout_2_locale);
137
138        addItem(result, "Table LTR", BiDiTestTableLayoutLtr.class, R.id.table_layout_ltr);
139        addItem(result, "Table RTL", BiDiTestTableLayoutRtl.class, R.id.table_layout_rtl);
140        addItem(result, "Table LOC", BiDiTestTableLayoutLocale.class, R.id.table_layout_locale);
141
142        addItem(result, "Padding", BiDiTestViewPadding.class, R.id.view_padding);
143        addItem(result, "Padding MIXED", BiDiTestViewPaddingMixed.class, R.id.view_padding_mixed);
144
145        addItem(result, "Margin MIXED", BiDiTestViewGroupMarginMixed.class, R.id.view_group_margin_mixed);
146
147        addItem(result, "TextView Drawables LTR", BiDiTestTextViewDrawablesLtr.class, R.id.textview_drawables_ltr);
148        addItem(result, "TextView Drawables RTL", BiDiTestTextViewDrawablesRtl.class, R.id.textview_drawables_rtl);
149
150        addItem(result, "Gallery LTR", BiDiTestGalleryLtr.class, R.id.gallery_ltr);
151        addItem(result, "Gallery RTL", BiDiTestGalleryRtl.class, R.id.gallery_rtl);
152
153        return result;
154    }
155
156    @Override
157    public boolean onCreateOptionsMenu(Menu menu) {
158        MenuInflater inflater = getMenuInflater();
159        inflater.inflate(R.menu.main_menu, menu);
160        return true;
161    }
162}
163