Support4Demos.java revision c644c91b91b83a6b400a57b02671f4ef7b7a810b
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.example.android.supportv4;
18
19import android.app.ListActivity;
20import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
23import android.os.Bundle;
24import android.view.View;
25import android.widget.ListView;
26import android.widget.SimpleAdapter;
27
28import java.text.Collator;
29import java.util.ArrayList;
30import java.util.Collections;
31import java.util.Comparator;
32import java.util.HashMap;
33import java.util.List;
34import java.util.Map;
35
36public class Support4Demos extends ListActivity {
37
38    @Override
39    public void onCreate(Bundle savedInstanceState) {
40        super.onCreate(savedInstanceState);
41
42        Intent intent = getIntent();
43        String path = intent.getStringExtra("com.example.android.apis.Path");
44
45        if (path == null) {
46            path = "";
47        }
48
49        setListAdapter(new SimpleAdapter(this, getData(path),
50                android.R.layout.simple_list_item_1, new String[] { "title" },
51                new int[] { android.R.id.text1 }));
52        getListView().setTextFilterEnabled(true);
53    }
54
55    protected List<Map<String, Object>> getData(String prefix) {
56        List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();
57
58        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
59        mainIntent.addCategory("com.example.android.supportv4.SUPPORT4_SAMPLE_CODE");
60
61        PackageManager pm = getPackageManager();
62        List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
63
64        if (null == list)
65            return myData;
66
67        String[] prefixPath;
68        String prefixWithSlash = prefix;
69
70        if (prefix.equals("")) {
71            prefixPath = null;
72        } else {
73            prefixPath = prefix.split("/");
74            prefixWithSlash = prefix + "/";
75        }
76
77        int len = list.size();
78
79        Map<String, Boolean> entries = new HashMap<String, Boolean>();
80
81        for (int i = 0; i < len; i++) {
82            ResolveInfo info = list.get(i);
83            CharSequence labelSeq = info.loadLabel(pm);
84            String label = labelSeq != null
85                    ? labelSeq.toString()
86                    : info.activityInfo.name;
87
88            if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {
89
90                String[] labelPath = label.split("/");
91
92                String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
93
94                if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
95                    addItem(myData, nextLabel, activityIntent(
96                            info.activityInfo.applicationInfo.packageName,
97                            info.activityInfo.name));
98                } else {
99                    if (entries.get(nextLabel) == null) {
100                        addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
101                        entries.put(nextLabel, true);
102                    }
103                }
104            }
105        }
106
107        Collections.sort(myData, sDisplayNameComparator);
108
109        return myData;
110    }
111
112    private final static Comparator<Map<String, Object>> sDisplayNameComparator =
113        new Comparator<Map<String, Object>>() {
114        private final Collator   collator = Collator.getInstance();
115
116        public int compare(Map<String, Object> map1, Map<String, Object> map2) {
117            return collator.compare(map1.get("title"), map2.get("title"));
118        }
119    };
120
121    protected Intent activityIntent(String pkg, String componentName) {
122        Intent result = new Intent();
123        result.setClassName(pkg, componentName);
124        return result;
125    }
126
127    protected Intent browseIntent(String path) {
128        Intent result = new Intent();
129        result.setClass(this, Support4Demos.class);
130        result.putExtra("com.example.android.apis.Path", path);
131        return result;
132    }
133
134    protected void addItem(List<Map<String, Object>> data, String name, Intent intent) {
135        Map<String, Object> temp = new HashMap<String, Object>();
136        temp.put("title", name);
137        temp.put("intent", intent);
138        data.add(temp);
139    }
140
141    @Override
142    @SuppressWarnings("unchecked")
143    protected void onListItemClick(ListView l, View v, int position, long id) {
144        Map<String, Object> map = (Map<String, Object>)l.getItemAtPosition(position);
145
146        Intent intent = (Intent) map.get("intent");
147        startActivity(intent);
148    }
149}
150