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