1 /* 2 * Copyright (C) 2017 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 package com.example.android.support.vectordrawable.app; 18 19 import android.content.Intent; 20 import android.content.pm.PackageManager; 21 import android.content.pm.ResolveInfo; 22 import android.os.Bundle; 23 import android.view.View; 24 import android.widget.ListView; 25 import android.widget.SimpleAdapter; 26 27 import java.text.Collator; 28 import java.util.ArrayList; 29 import java.util.Collections; 30 import java.util.Comparator; 31 import java.util.HashMap; 32 import java.util.List; 33 import java.util.Map; 34 35 /** 36 * The root level activity for this demo. Showing a list of sub demos. 37 */ 38 @SuppressWarnings("deprecation") 39 public class SupportVectorDrawableDemos extends android.app.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.apis.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 58 @SuppressWarnings("deprecation") getData(String prefix)59 protected List<Map<String, Object>> getData(String prefix) { 60 List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>(); 61 62 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 63 mainIntent.addCategory("android.intent.category.SAMPLE_CODE"); 64 65 PackageManager pm = getPackageManager(); 66 List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0); 67 68 if (null == list) { 69 return myData; 70 } 71 72 String[] prefixPath; 73 String prefixWithSlash = prefix; 74 75 if (prefix.equals("")) { 76 prefixPath = null; 77 } else { 78 prefixPath = prefix.split("/", -1); 79 prefixWithSlash = prefix + "/"; 80 } 81 82 int len = list.size(); 83 84 Map<String, Boolean> entries = new HashMap<String, Boolean>(); 85 86 for (int i = 0; i < len; i++) { 87 ResolveInfo info = list.get(i); 88 CharSequence labelSeq = info.loadLabel(pm); 89 String label = labelSeq != null 90 ? labelSeq.toString() 91 : info.activityInfo.name; 92 93 if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) { 94 95 String[] labelPath = label.split("/", -1); 96 97 String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length]; 98 99 if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) { 100 addItem(myData, nextLabel, activityIntent( 101 info.activityInfo.applicationInfo.packageName, 102 info.activityInfo.name)); 103 } else { 104 if (entries.get(nextLabel) == null) { 105 addItem(myData, nextLabel, browseIntent( 106 prefix.equals("") ? nextLabel : prefix + "/" + nextLabel)); 107 entries.put(nextLabel, true); 108 } 109 } 110 } 111 } 112 113 Collections.sort(myData, sDisplayNameComparator); 114 115 return myData; 116 } 117 118 private static final Comparator<Map<String, Object>> sDisplayNameComparator = 119 new Comparator<Map<String, Object>>() { 120 private final Collator mCollator = Collator.getInstance(); 121 122 @Override 123 public int compare(Map<String, Object> map1, Map<String, Object> map2) { 124 return mCollator.compare(map1.get("title"), map2.get("title")); 125 } 126 }; 127 activityIntent(String pkg, String componentName)128 protected Intent activityIntent(String pkg, String componentName) { 129 Intent result = new Intent(); 130 result.setClassName(pkg, componentName); 131 return result; 132 } 133 browseIntent(String path)134 protected Intent browseIntent(String path) { 135 Intent result = new Intent(); 136 result.setClass(this, SupportVectorDrawableDemos.class); 137 result.putExtra("com.example.android.apis.Path", path); 138 return result; 139 } 140 addItem(List<Map<String, Object>> data, String name, Intent intent)141 protected void addItem(List<Map<String, Object>> data, String name, Intent intent) { 142 Map<String, Object> temp = new HashMap<String, Object>(); 143 temp.put("title", name); 144 temp.put("intent", intent); 145 data.add(temp); 146 } 147 148 @Override 149 @SuppressWarnings("unchecked") onListItemClick(ListView l, View v, int position, long id)150 protected void onListItemClick(ListView l, View v, int position, long id) { 151 Map<String, Object> map = (Map<String, Object>) l.getItemAtPosition(position); 152 153 Intent intent = (Intent) map.get("intent"); 154 startActivity(intent); 155 } 156 } 157