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