Rev 14 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8 | PointedEar | 1 | package de.pointedears.converter; |
6 | PointedEar | 2 | |
5 | PointedEar | 3 | /* |
4 | * Copyright (C) 2007 The Android Open Source Project |
||
6 | PointedEar | 5 | * |
5 | PointedEar | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
||
8 | * You may obtain a copy of the License at |
||
6 | PointedEar | 9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
11 | * |
||
5 | PointedEar | 12 | * Unless required by applicable law or agreed to in writing, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
15 | * See the License for the specific language governing permissions and |
||
16 | * limitations under the License. |
||
17 | */ |
||
18 | |||
6 | PointedEar | 19 | import java.text.Collator; |
20 | import java.util.ArrayList; |
||
21 | import java.util.Collections; |
||
22 | import java.util.Comparator; |
||
23 | import java.util.HashMap; |
||
24 | import java.util.List; |
||
25 | import java.util.Map; |
||
5 | PointedEar | 26 | |
27 | import android.app.ListActivity; |
||
28 | import android.content.Intent; |
||
29 | import android.content.pm.PackageManager; |
||
30 | import android.content.pm.ResolveInfo; |
||
31 | import android.os.Bundle; |
||
32 | import android.view.View; |
||
33 | import android.widget.ListView; |
||
34 | import android.widget.SimpleAdapter; |
||
17 | PointedEar | 35 | import de.pointedears.converter.helpers.UpdateService; |
5 | PointedEar | 36 | |
14 | PointedEar | 37 | /** |
38 | * Generates the main menu as a list of activities from the manifest |
||
39 | * |
||
40 | * @author |
||
41 | * Thomas 'PointedEars' Lahn; |
||
42 | * Class courtesy of The Android Open Source Project |
||
43 | * (Android 2.2 SDK Examples), slightly adapted |
||
44 | */ |
||
6 | PointedEar | 45 | public class MenuActivity extends ListActivity |
46 | { |
||
47 | @Override |
||
48 | public void onCreate(Bundle savedInstanceState) |
||
49 | { |
||
50 | super.onCreate(savedInstanceState); |
||
5 | PointedEar | 51 | |
17 | PointedEar | 52 | this.startService(new Intent(this, UpdateService.class)); |
53 | |||
6 | PointedEar | 54 | Intent intent = this.getIntent(); |
14 | PointedEar | 55 | String path = intent.getStringExtra("de.pointedears.converter.Path"); //$NON-NLS-1$ |
5 | PointedEar | 56 | |
6 | PointedEar | 57 | if (path == null) |
58 | { |
||
14 | PointedEar | 59 | path = ""; //$NON-NLS-1$ |
6 | PointedEar | 60 | } |
61 | |||
62 | this.setListAdapter(new SimpleAdapter(this, this.getData(path), |
||
14 | PointedEar | 63 | android.R.layout.simple_list_item_1, new String[] { "title" }, //$NON-NLS-1$ |
64 | new int[] { android.R.id.text1 })); |
||
6 | PointedEar | 65 | this.getListView().setTextFilterEnabled(true); |
66 | } |
||
67 | |||
14 | PointedEar | 68 | /** |
69 | * @param prefix |
||
70 | * @return |
||
71 | */ |
||
6 | PointedEar | 72 | protected List getData(String prefix) |
73 | { |
||
74 | List<Map> myData = new ArrayList<Map>(); |
||
75 | |||
76 | Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); |
||
77 | mainIntent.addCategory(ConverterApplication.CATEGORY_CONVERTER); |
||
78 | |||
79 | PackageManager pm = this.getPackageManager(); |
||
80 | List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0); |
||
81 | |||
82 | if (null == list) |
||
83 | { |
||
84 | return myData; |
||
5 | PointedEar | 85 | } |
86 | |||
6 | PointedEar | 87 | String[] prefixPath; |
5 | PointedEar | 88 | |
14 | PointedEar | 89 | if (prefix.equals("")) //$NON-NLS-1$ |
6 | PointedEar | 90 | { |
91 | prefixPath = null; |
||
92 | } |
||
93 | else |
||
94 | { |
||
14 | PointedEar | 95 | prefixPath = prefix.split("/"); //$NON-NLS-1$ |
6 | PointedEar | 96 | } |
5 | PointedEar | 97 | |
6 | PointedEar | 98 | int len = list.size(); |
5 | PointedEar | 99 | |
6 | PointedEar | 100 | Map<String, Boolean> entries = new HashMap<String, Boolean>(); |
5 | PointedEar | 101 | |
6 | PointedEar | 102 | for (int i = 0; i < len; i++) |
103 | { |
||
104 | ResolveInfo info = list.get(i); |
||
105 | CharSequence labelSeq = info.loadLabel(pm); |
||
106 | String label = labelSeq != null |
||
5 | PointedEar | 107 | ? labelSeq.toString() |
108 | : info.activityInfo.name; |
||
109 | |||
6 | PointedEar | 110 | if (prefix.length() == 0 || label.startsWith(prefix)) |
111 | { |
||
5 | PointedEar | 112 | |
14 | PointedEar | 113 | String[] labelPath = label.split("/"); //$NON-NLS-1$ |
6 | PointedEar | 114 | |
115 | String nextLabel = |
||
116 | prefixPath == null ? labelPath[0] : labelPath[prefixPath.length]; |
||
117 | |||
118 | if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) |
||
119 | { |
||
120 | this.addItem(myData, nextLabel, this.activityIntent( |
||
5 | PointedEar | 121 | info.activityInfo.applicationInfo.packageName, |
122 | info.activityInfo.name)); |
||
123 | } |
||
6 | PointedEar | 124 | else |
125 | { |
||
126 | if (entries.get(nextLabel) == null) |
||
127 | { |
||
128 | this.addItem( |
||
129 | myData, |
||
130 | nextLabel, |
||
14 | PointedEar | 131 | this.browseIntent(prefix.equals("") ? nextLabel : prefix + "/" //$NON-NLS-1$//$NON-NLS-2$ |
6 | PointedEar | 132 | + nextLabel)); |
133 | entries.put(nextLabel, true); |
||
134 | } |
||
135 | } |
||
136 | } |
||
5 | PointedEar | 137 | } |
138 | |||
6 | PointedEar | 139 | Collections.sort(myData, MenuActivity.sDisplayNameComparator); |
5 | PointedEar | 140 | |
6 | PointedEar | 141 | return myData; |
142 | } |
||
143 | |||
144 | private final static Comparator<Map> sDisplayNameComparator = |
||
145 | new Comparator<Map>() { |
||
146 | private final Collator collator = Collator.getInstance(); |
||
147 | |||
148 | public int compare(Map map1, Map map2) |
||
149 | { |
||
14 | PointedEar | 150 | return this.collator.compare(map1.get("title"), map2.get("title")); //$NON-NLS-1$ //$NON-NLS-2$ |
6 | PointedEar | 151 | } |
5 | PointedEar | 152 | }; |
153 | |||
14 | PointedEar | 154 | /** |
155 | * @param pkg |
||
156 | * @param componentName |
||
157 | * @return |
||
158 | */ |
||
6 | PointedEar | 159 | protected Intent activityIntent(String pkg, String componentName) |
160 | { |
||
161 | Intent result = new Intent(); |
||
162 | result.setClassName(pkg, componentName); |
||
163 | return result; |
||
164 | } |
||
5 | PointedEar | 165 | |
14 | PointedEar | 166 | /** |
167 | * @param path |
||
168 | * @return |
||
169 | */ |
||
6 | PointedEar | 170 | protected Intent browseIntent(String path) |
171 | { |
||
172 | Intent result = new Intent(); |
||
173 | result.setClass(this, MenuActivity.class); |
||
14 | PointedEar | 174 | result.putExtra("de.pointedears.converter.Path", path); //$NON-NLS-1$ |
6 | PointedEar | 175 | return result; |
176 | } |
||
5 | PointedEar | 177 | |
14 | PointedEar | 178 | /** |
179 | * @param data |
||
180 | * @param name |
||
181 | * @param intent |
||
182 | */ |
||
6 | PointedEar | 183 | protected void addItem(List<Map> data, String name, Intent intent) |
184 | { |
||
185 | Map<String, Object> temp = new HashMap<String, Object>(); |
||
14 | PointedEar | 186 | temp.put("title", name); //$NON-NLS-1$ |
187 | temp.put("intent", intent); //$NON-NLS-1$ |
||
6 | PointedEar | 188 | data.add(temp); |
189 | } |
||
5 | PointedEar | 190 | |
14 | PointedEar | 191 | /* |
192 | * (non-Javadoc) |
||
193 | * |
||
194 | * @see android.app.ListActivity#onListItemClick(android.widget.ListView, |
||
195 | * android.view.View, int, long) |
||
196 | */ |
||
6 | PointedEar | 197 | @Override |
198 | protected void onListItemClick(ListView l, View v, int position, long id) |
||
199 | { |
||
200 | Map map = (Map) l.getItemAtPosition(position); |
||
5 | PointedEar | 201 | |
14 | PointedEar | 202 | Intent intent = (Intent) map.get("intent"); //$NON-NLS-1$ |
6 | PointedEar | 203 | this.startActivity(intent); |
204 | } |
||
205 | |||
5 | PointedEar | 206 | } |