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