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