Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.bidi; |
| 18 | |
Fabrice Di Meglio | 7af0522 | 2011-06-10 15:42:05 -0700 | [diff] [blame] | 19 | import java.util.ArrayList; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 23 | |
Fabrice Di Meglio | 7af0522 | 2011-06-10 15:42:05 -0700 | [diff] [blame] | 24 | import android.app.Activity; |
| 25 | import android.app.Fragment; |
| 26 | import android.app.FragmentTransaction; |
| 27 | import android.os.Bundle; |
| 28 | import android.view.View; |
| 29 | import android.widget.AdapterView; |
| 30 | import android.widget.ListView; |
| 31 | import android.widget.SimpleAdapter; |
| 32 | |
| 33 | public class BiDiTestActivity extends Activity { |
| 34 | |
| 35 | private static final String KEY_CLASS = "class"; |
| 36 | private static final String KEY_TITLE = "title"; |
| 37 | private static final String KEY_FRAGMENT_ID = "id"; |
| 38 | |
| 39 | private ListView mList; |
| 40 | |
| 41 | private AdapterView.OnItemClickListener mOnClickListener = |
| 42 | new AdapterView.OnItemClickListener() { |
| 43 | public void onItemClick(AdapterView<?> parent, View v, int position, long id) { |
| 44 | onListItemClick((ListView)parent, v, position, id); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | private void onListItemClick(ListView lv, View v, int position, long id) { |
| 49 | // Show the test |
| 50 | Map<String, Object> map = (Map<String, Object>)lv.getItemAtPosition(position); |
| 51 | int fragmentId = (Integer) map.get(KEY_FRAGMENT_ID); |
| 52 | Fragment fragment = getFragmentManager().findFragmentById(fragmentId); |
| 53 | if (fragment == null) { |
| 54 | try { |
| 55 | // Create an instance of the test |
| 56 | Class<? extends Fragment> clazz = (Class<? extends Fragment>) map.get(KEY_CLASS); |
| 57 | fragment = clazz.newInstance(); |
| 58 | |
| 59 | // Replace the old test fragment with the new one |
| 60 | FragmentTransaction ft = getFragmentManager().beginTransaction(); |
| 61 | ft.replace(R.id.testframe, fragment); |
| 62 | ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); |
| 63 | ft.commit(); |
| 64 | } catch (InstantiationException e) { |
| 65 | } catch (IllegalAccessException e) { |
| 66 | } |
| 67 | } |
| 68 | } |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 69 | |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 70 | @Override |
| 71 | protected void onCreate(Bundle savedInstanceState) { |
| 72 | super.onCreate(savedInstanceState); |
| 73 | |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 74 | setContentView(R.layout.main); |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 75 | |
Fabrice Di Meglio | 7af0522 | 2011-06-10 15:42:05 -0700 | [diff] [blame] | 76 | mList = (ListView) findViewById(R.id.testlist); |
| 77 | mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE); |
| 78 | mList.setFocusableInTouchMode(true); |
| 79 | |
| 80 | final SimpleAdapter adapter = new SimpleAdapter(this, getTests(), |
| 81 | R.layout.custom_list_item, new String[]{"title"}, |
| 82 | new int[]{android.R.id.text1}); |
| 83 | mList.setAdapter(adapter); |
| 84 | |
| 85 | mList.setOnItemClickListener(mOnClickListener); |
| 86 | } |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 87 | |
Fabrice Di Meglio | 7af0522 | 2011-06-10 15:42:05 -0700 | [diff] [blame] | 88 | private void addItem(List<Map<String, Object>> data, String name, |
| 89 | Class<? extends Fragment> clazz, int fragmentId) { |
| 90 | Map<String, Object> temp = new HashMap<String, Object>(); |
| 91 | temp.put(KEY_TITLE, name); |
| 92 | temp.put(KEY_CLASS, clazz); |
| 93 | temp.put(KEY_FRAGMENT_ID, fragmentId); |
| 94 | data.add(temp); |
| 95 | } |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 96 | |
Fabrice Di Meglio | 7af0522 | 2011-06-10 15:42:05 -0700 | [diff] [blame] | 97 | private List<Map<String, Object>> getTests() { |
| 98 | List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 99 | |
Fabrice Di Meglio | 7af0522 | 2011-06-10 15:42:05 -0700 | [diff] [blame] | 100 | addItem(result, "Basic", BiDiTestBasic.class, R.id.basic); |
| 101 | addItem(result, "Canvas", BiDiTestCanvas.class, R.id.canvas); |
| 102 | |
| 103 | addItem(result, "Linear LTR", BiDiTestLinearLayoutLtr.class, R.id.linear_layout_ltr); |
| 104 | addItem(result, "Linear RTL", BiDiTestLinearLayoutRtl.class, R.id.linear_layout_rtl); |
| 105 | addItem(result, "Linear LOC", BiDiTestLinearLayoutLocale.class, R.id.linear_layout_locale); |
| 106 | |
| 107 | addItem(result, "Frame LTR", BiDiTestFrameLayoutLtr.class, R.id.frame_layout_ltr); |
| 108 | addItem(result, "Frame RTL", BiDiTestFrameLayoutRtl.class, R.id.frame_layout_rtl); |
| 109 | addItem(result, "Frame LOC", BiDiTestFrameLayoutLocale.class, R.id.frame_layout_locale); |
| 110 | |
| 111 | addItem(result, "Relative LTR", BiDiTestRelativeLayoutLtr.class, R.id.relative_layout_ltr); |
| 112 | addItem(result, "Relative RTL", BiDiTestRelativeLayoutRtl.class, R.id.relative_layout_rtl); |
| 113 | |
| 114 | addItem(result, "Relative2 LTR", BiDiTestRelativeLayout2Ltr.class, R.id.relative_layout_2_ltr); |
| 115 | addItem(result, "Relative2 RTL", BiDiTestRelativeLayout2Rtl.class, R.id.relative_layout_2_rtl); |
| 116 | addItem(result, "Relative2 LOC", BiDiTestRelativeLayout2Locale.class, R.id.relative_layout_2_locale); |
| 117 | |
| 118 | addItem(result, "Table LTR", BiDiTestTableLayoutLtr.class, R.id.table_layout_ltr); |
| 119 | addItem(result, "Table RTL", BiDiTestTableLayoutRtl.class, R.id.table_layout_rtl); |
| 120 | addItem(result, "Table LOC", BiDiTestTableLayoutLocale.class, R.id.table_layout_locale); |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 121 | |
Fabrice Di Meglio | b071240 | 2011-06-16 16:18:11 -0700 | [diff] [blame^] | 122 | addItem(result, "ViewPadding", BiDiTestViewPadding.class, R.id.view_padding); |
| 123 | |
Fabrice Di Meglio | 7af0522 | 2011-06-10 15:42:05 -0700 | [diff] [blame] | 124 | return result; |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 125 | } |
| 126 | } |