1/* 2 * Copyright (C) 2014 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 17package com.android.test.dynamic; 18 19import android.content.Intent; 20import android.content.pm.PackageManager; 21import android.content.pm.ResolveInfo; 22import android.os.Bundle; 23import android.view.View; 24import android.widget.ListView; 25import android.widget.SimpleAdapter; 26 27import java.text.Collator; 28import java.util.ArrayList; 29import java.util.Collections; 30import java.util.Comparator; 31import java.util.HashMap; 32import java.util.List; 33import java.util.Map; 34 35@SuppressWarnings("UnusedDeclaration") 36public class VectorDrawableTest extends android.app.ListActivity { 37 private static final String EXTRA_PATH = "com.android.test.dynamic.Path"; 38 private static final String CATEGORY_HWUI_TEST = "com.android.test.dynamic.TEST"; 39 40 @Override 41 public void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 44 Intent intent = getIntent(); 45 String path = intent.getStringExtra("com.android.test.hwui.Path"); 46 47 if (path == null) { 48 path = ""; 49 } 50 51 setListAdapter(new SimpleAdapter(this, getData(path), 52 android.R.layout.simple_list_item_1, new String[] { "title" }, 53 new int[] { android.R.id.text1 })); 54 getListView().setTextFilterEnabled(true); 55 } 56 57 protected List<Map<String, Object>> getData(String prefix) { 58 List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>(); 59 60 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 61 mainIntent.addCategory(CATEGORY_HWUI_TEST); 62 63 PackageManager pm = getPackageManager(); 64 List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0); 65 66 if (null == list) 67 return myData; 68 69 String[] prefixPath; 70 String prefixWithSlash = prefix; 71 72 if (prefix.equals("")) { 73 prefixPath = null; 74 } else { 75 prefixPath = prefix.split("/"); 76 prefixWithSlash = prefix + "/"; 77 } 78 79 int len = list.size(); 80 81 Map<String, Boolean> entries = new HashMap<String, Boolean>(); 82 83 for (int i = 0; i < len; i++) { 84 ResolveInfo info = list.get(i); 85 CharSequence labelSeq = info.loadLabel(pm); 86 String label = labelSeq != null 87 ? labelSeq.toString() 88 : info.activityInfo.name; 89 90 if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) { 91 92 String[] labelPath = label.split("/"); 93 94 String nextLabel = prefixPath == 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 124 protected Intent activityIntent(String pkg, String componentName) { 125 Intent result = new Intent(); 126 result.setClassName(pkg, componentName); 127 return result; 128 } 129 130 protected Intent browseIntent(String path) { 131 Intent result = new Intent(); 132 result.setClass(this, VectorDrawableTest.class); 133 result.putExtra(EXTRA_PATH, path); 134 return result; 135 } 136 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", "UnusedParameters" }) 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