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