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.animation;
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  * This activity lists all the activities in this application.
37  */
38 @SuppressWarnings("deprecation")
39 public class BrowseActivity 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.androidx.dynamicanimation.animation");
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(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 ? labelSeq.toString() : info.activityInfo.name;
90 
91             if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {
92 
93                 String[] labelPath = label.split("/", -1);
94 
95                 String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
96 
97                 if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
98                     addItem(myData, nextLabel, activityIntent(
99                             info.activityInfo.applicationInfo.packageName,
100                             info.activityInfo.name));
101                 } else {
102                     if (entries.get(nextLabel) == null) {
103                         addItem(myData, nextLabel, browseIntent(prefix.equals("")
104                                 ? nextLabel : prefix + "/" + nextLabel));
105                         entries.put(nextLabel, true);
106                     }
107                 }
108             }
109         }
110 
111         Collections.sort(myData, sDisplayNameComparator);
112         return myData;
113     }
114 
115     private static final Comparator<Map<String, Object>> sDisplayNameComparator =
116             new Comparator<Map<String, Object>>() {
117                 public final Collator collator = Collator.getInstance();
118 
119                 @Override
120                 public int compare(Map<String, Object> map1, Map<String, Object> map2) {
121                     return collator.compare(map1.get("title"), map2.get("title"));
122                 }
123             };
124 
activityIntent(String pkg, String componentName)125     protected Intent activityIntent(String pkg, String componentName) {
126         Intent result = new Intent();
127         result.setClassName(pkg, componentName);
128         return result;
129     }
130 
browseIntent(String path)131     protected Intent browseIntent(String path) {
132         Intent result = new Intent();
133         result.setClass(this, BrowseActivity.class);
134         result.putExtra("com.example.androidx.dynamicanimation.animation", path);
135         return result;
136     }
137 
addItem(List<Map<String, Object>> data, String name, Intent intent)138     protected void addItem(List<Map<String, Object>> data, String name, Intent intent) {
139         Map<String, Object> temp = new HashMap<String, Object>();
140         temp.put("title", name);
141         temp.put("intent", intent);
142         data.add(temp);
143     }
144 
145     @Override
146     @SuppressWarnings("unchecked")
onListItemClick(ListView l, View v, int position, long id)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 
156 
157