Rev 17 | 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; |
||
| 35 | |||
| 14 | PointedEar | 36 | /** |
| 37 | * Generates the main menu as a list of activities from the manifest |
||
| 38 | * |
||
| 39 | * @author |
||
| 40 | * Thomas 'PointedEars' Lahn; |
||
| 41 | * Class courtesy of The Android Open Source Project |
||
| 42 | * (Android 2.2 SDK Examples), slightly adapted |
||
| 43 | */ |
||
| 6 | PointedEar | 44 | public class MenuActivity extends ListActivity |
| 45 | { |
||
| 46 | @Override |
||
| 47 | public void onCreate(Bundle savedInstanceState) |
||
| 48 | { |
||
| 49 | super.onCreate(savedInstanceState); |
||
| 5 | PointedEar | 50 | |
| 6 | PointedEar | 51 | Intent intent = this.getIntent(); |
| 14 | PointedEar | 52 | String path = intent.getStringExtra("de.pointedears.converter.Path"); //$NON-NLS-1$ |
| 5 | PointedEar | 53 | |
| 6 | PointedEar | 54 | if (path == null) |
| 55 | { |
||
| 14 | PointedEar | 56 | path = ""; //$NON-NLS-1$ |
| 6 | PointedEar | 57 | } |
| 58 | |||
| 59 | this.setListAdapter(new SimpleAdapter(this, this.getData(path), |
||
| 14 | PointedEar | 60 | android.R.layout.simple_list_item_1, new String[] { "title" }, //$NON-NLS-1$ |
| 61 | new int[] { android.R.id.text1 })); |
||
| 6 | PointedEar | 62 | this.getListView().setTextFilterEnabled(true); |
| 63 | } |
||
| 64 | |||
| 14 | PointedEar | 65 | /** |
| 66 | * @param prefix |
||
| 67 | * @return |
||
| 68 | */ |
||
| 6 | PointedEar | 69 | protected List getData(String prefix) |
| 70 | { |
||
| 71 | List<Map> myData = new ArrayList<Map>(); |
||
| 72 | |||
| 73 | Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); |
||
| 74 | mainIntent.addCategory(ConverterApplication.CATEGORY_CONVERTER); |
||
| 75 | |||
| 76 | PackageManager pm = this.getPackageManager(); |
||
| 77 | List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0); |
||
| 78 | |||
| 79 | if (null == list) |
||
| 80 | { |
||
| 81 | return myData; |
||
| 5 | PointedEar | 82 | } |
| 83 | |||
| 6 | PointedEar | 84 | String[] prefixPath; |
| 5 | PointedEar | 85 | |
| 14 | PointedEar | 86 | if (prefix.equals("")) //$NON-NLS-1$ |
| 6 | PointedEar | 87 | { |
| 88 | prefixPath = null; |
||
| 89 | } |
||
| 90 | else |
||
| 91 | { |
||
| 14 | PointedEar | 92 | prefixPath = prefix.split("/"); //$NON-NLS-1$ |
| 6 | PointedEar | 93 | } |
| 5 | PointedEar | 94 | |
| 6 | PointedEar | 95 | int len = list.size(); |
| 5 | PointedEar | 96 | |
| 6 | PointedEar | 97 | Map<String, Boolean> entries = new HashMap<String, Boolean>(); |
| 5 | PointedEar | 98 | |
| 6 | PointedEar | 99 | for (int i = 0; i < len; i++) |
| 100 | { |
||
| 101 | ResolveInfo info = list.get(i); |
||
| 102 | CharSequence labelSeq = info.loadLabel(pm); |
||
| 103 | String label = labelSeq != null |
||
| 5 | PointedEar | 104 | ? labelSeq.toString() |
| 105 | : info.activityInfo.name; |
||
| 106 | |||
| 6 | PointedEar | 107 | if (prefix.length() == 0 || label.startsWith(prefix)) |
| 108 | { |
||
| 5 | PointedEar | 109 | |
| 14 | PointedEar | 110 | String[] labelPath = label.split("/"); //$NON-NLS-1$ |
| 6 | PointedEar | 111 | |
| 112 | String nextLabel = |
||
| 113 | prefixPath == null ? labelPath[0] : labelPath[prefixPath.length]; |
||
| 114 | |||
| 115 | if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) |
||
| 116 | { |
||
| 117 | this.addItem(myData, nextLabel, this.activityIntent( |
||
| 5 | PointedEar | 118 | info.activityInfo.applicationInfo.packageName, |
| 119 | info.activityInfo.name)); |
||
| 120 | } |
||
| 6 | PointedEar | 121 | else |
| 122 | { |
||
| 123 | if (entries.get(nextLabel) == null) |
||
| 124 | { |
||
| 125 | this.addItem( |
||
| 126 | myData, |
||
| 127 | nextLabel, |
||
| 14 | PointedEar | 128 | this.browseIntent(prefix.equals("") ? nextLabel : prefix + "/" //$NON-NLS-1$//$NON-NLS-2$ |
| 6 | PointedEar | 129 | + nextLabel)); |
| 130 | entries.put(nextLabel, true); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 5 | PointedEar | 134 | } |
| 135 | |||
| 6 | PointedEar | 136 | Collections.sort(myData, MenuActivity.sDisplayNameComparator); |
| 5 | PointedEar | 137 | |
| 6 | PointedEar | 138 | return myData; |
| 139 | } |
||
| 140 | |||
| 141 | private final static Comparator<Map> sDisplayNameComparator = |
||
| 142 | new Comparator<Map>() { |
||
| 143 | private final Collator collator = Collator.getInstance(); |
||
| 144 | |||
| 145 | public int compare(Map map1, Map map2) |
||
| 146 | { |
||
| 14 | PointedEar | 147 | return this.collator.compare(map1.get("title"), map2.get("title")); //$NON-NLS-1$ //$NON-NLS-2$ |
| 6 | PointedEar | 148 | } |
| 5 | PointedEar | 149 | }; |
| 150 | |||
| 14 | PointedEar | 151 | /** |
| 152 | * @param pkg |
||
| 153 | * @param componentName |
||
| 154 | * @return |
||
| 155 | */ |
||
| 6 | PointedEar | 156 | protected Intent activityIntent(String pkg, String componentName) |
| 157 | { |
||
| 158 | Intent result = new Intent(); |
||
| 159 | result.setClassName(pkg, componentName); |
||
| 160 | return result; |
||
| 161 | } |
||
| 5 | PointedEar | 162 | |
| 14 | PointedEar | 163 | /** |
| 164 | * @param path |
||
| 165 | * @return |
||
| 166 | */ |
||
| 6 | PointedEar | 167 | protected Intent browseIntent(String path) |
| 168 | { |
||
| 169 | Intent result = new Intent(); |
||
| 170 | result.setClass(this, MenuActivity.class); |
||
| 14 | PointedEar | 171 | result.putExtra("de.pointedears.converter.Path", path); //$NON-NLS-1$ |
| 6 | PointedEar | 172 | return result; |
| 173 | } |
||
| 5 | PointedEar | 174 | |
| 14 | PointedEar | 175 | /** |
| 176 | * @param data |
||
| 177 | * @param name |
||
| 178 | * @param intent |
||
| 179 | */ |
||
| 6 | PointedEar | 180 | protected void addItem(List<Map> data, String name, Intent intent) |
| 181 | { |
||
| 182 | Map<String, Object> temp = new HashMap<String, Object>(); |
||
| 14 | PointedEar | 183 | temp.put("title", name); //$NON-NLS-1$ |
| 184 | temp.put("intent", intent); //$NON-NLS-1$ |
||
| 6 | PointedEar | 185 | data.add(temp); |
| 186 | } |
||
| 5 | PointedEar | 187 | |
| 14 | PointedEar | 188 | /* |
| 189 | * (non-Javadoc) |
||
| 190 | * |
||
| 191 | * @see android.app.ListActivity#onListItemClick(android.widget.ListView, |
||
| 192 | * android.view.View, int, long) |
||
| 193 | */ |
||
| 6 | PointedEar | 194 | @Override |
| 195 | protected void onListItemClick(ListView l, View v, int position, long id) |
||
| 196 | { |
||
| 197 | Map map = (Map) l.getItemAtPosition(position); |
||
| 5 | PointedEar | 198 | |
| 14 | PointedEar | 199 | Intent intent = (Intent) map.get("intent"); //$NON-NLS-1$ |
| 6 | PointedEar | 200 | this.startActivity(intent); |
| 201 | } |
||
| 202 | |||
| 5 | PointedEar | 203 | } |