Subversion Repositories ES

Rev

Rev 6 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 PointedEar 1
package ch.ffhs.converter;
2
/*
3
 * Copyright (C) 2007 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
 
18
 
19
 
20
import android.app.ListActivity;
21
import android.content.Intent;
22
import android.content.pm.PackageManager;
23
import android.content.pm.ResolveInfo;
24
import android.os.Bundle;
25
import android.view.View;
26
import android.widget.ListView;
27
import android.widget.SimpleAdapter;
28
 
29
import java.text.Collator;
30
import java.util.ArrayList;
31
import java.util.Collections;
32
import java.util.Comparator;
33
import java.util.HashMap;
34
import java.util.List;
35
import java.util.Map;
36
 
37
public class MenuActivity extends ListActivity {
38
 
39
    @Override
40
    public void onCreate(Bundle savedInstanceState) {
41
        super.onCreate(savedInstanceState);
42
 
43
        Intent intent = getIntent();
44
        String path = intent.getStringExtra("ch.ffhs.converter.Path");
45
 
46
        if (path == null) {
47
            path = "";
48
        }
49
 
50
        setListAdapter(new SimpleAdapter(this, getData(path),
51
                android.R.layout.simple_list_item_1, new String[] { "title" },
52
                new int[] { android.R.id.text1 }));
53
        getListView().setTextFilterEnabled(true);
54
    }
55
 
56
    protected List getData(String prefix) {
57
        List<Map> myData = new ArrayList<Map>();
58
 
59
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
60
        mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
61
 
62
        PackageManager pm = getPackageManager();
63
        List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
64
 
65
        if (null == list)
66
            return myData;
67
 
68
        String[] prefixPath;
69
 
70
        if (prefix.equals("")) {
71
            prefixPath = null;
72
        } else {
73
            prefixPath = prefix.split("/");
74
        }
75
 
76
        int len = list.size();
77
 
78
        Map<String, Boolean> entries = new HashMap<String, Boolean>();
79
 
80
        for (int i = 0; i < len; i++) {
81
            ResolveInfo info = list.get(i);
82
            CharSequence labelSeq = info.loadLabel(pm);
83
            String label = labelSeq != null
84
                    ? labelSeq.toString()
85
                    : info.activityInfo.name;
86
 
87
            if (prefix.length() == 0 || label.startsWith(prefix)) {
88
 
89
                String[] labelPath = label.split("/");
90
 
91
                String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
92
 
93
                if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
94
                    addItem(myData, nextLabel, activityIntent(
95
                            info.activityInfo.applicationInfo.packageName,
96
                            info.activityInfo.name));
97
                } else {
98
                    if (entries.get(nextLabel) == null) {
99
                        addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
100
                        entries.put(nextLabel, true);
101
                    }
102
                }
103
            }
104
        }
105
 
106
        Collections.sort(myData, sDisplayNameComparator);
107
 
108
        return myData;
109
    }
110
 
111
    private final static Comparator<Map> sDisplayNameComparator = new Comparator<Map>() {
112
        private final Collator   collator = Collator.getInstance();
113
 
114
        public int compare(Map map1, Map map2) {
115
            return collator.compare(map1.get("title"), map2.get("title"));
116
        }
117
    };
118
 
119
    protected Intent activityIntent(String pkg, String componentName) {
120
        Intent result = new Intent();
121
        result.setClassName(pkg, componentName);
122
        return result;
123
    }
124
 
125
    protected Intent browseIntent(String path) {
126
        Intent result = new Intent();
127
        result.setClass(this, MenuActivity.class);
128
        result.putExtra("ch.ffhs.converter.Path", path);
129
        return result;
130
    }
131
 
132
    protected void addItem(List<Map> data, String name, Intent intent) {
133
        Map<String, Object> temp = new HashMap<String, Object>();
134
        temp.put("title", name);
135
        temp.put("intent", intent);
136
        data.add(temp);
137
    }
138
 
139
    @Override
140
    protected void onListItemClick(ListView l, View v, int position, long id) {
141
        Map map = (Map) l.getItemAtPosition(position);
142
 
143
        Intent intent = (Intent) map.get("intent");
144
        startActivity(intent);
145
    }
146
 
147
}