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