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.android.support.animation;
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 */
39public class BrowseActivity extends ListActivity {
40
41    @Override
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    protected List<Map<String, Object>> getData(String prefix) {
59        List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();
60
61        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
62        mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
63
64        PackageManager pm = getPackageManager();
65        List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
66
67        if (null == list) {
68            return myData;
69        }
70
71        String[] prefixPath;
72        String prefixWithSlash = prefix;
73
74        if (prefix.equals("")) {
75            prefixPath = null;
76        } else {
77            prefixPath = prefix.split("/");
78            prefixWithSlash = prefix + "/";
79        }
80
81        int len = list.size();
82
83        Map<String, Boolean> entries = new HashMap<String, Boolean>();
84
85        for (int i = 0; i < len; i++) {
86            ResolveInfo info = list.get(i);
87            CharSequence labelSeq = info.loadLabel(pm);
88            String label = labelSeq != null ? labelSeq.toString() : info.activityInfo.name;
89
90            if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {
91
92                String[] labelPath = label.split("/");
93
94                String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
95
96                if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
97                    addItem(myData, nextLabel, activityIntent(
98                            info.activityInfo.applicationInfo.packageName,
99                            info.activityInfo.name));
100                } else {
101                    if (entries.get(nextLabel) == null) {
102                        addItem(myData, nextLabel, browseIntent(prefix.equals("")
103                                ? nextLabel : prefix + "/" + nextLabel));
104                        entries.put(nextLabel, true);
105                    }
106                }
107            }
108        }
109
110        Collections.sort(myData, sDisplayNameComparator);
111        return myData;
112    }
113
114    private static final Comparator<Map<String, Object>> sDisplayNameComparator =
115            new Comparator<Map<String, Object>>() {
116                public final Collator collator = Collator.getInstance();
117
118                public int compare(Map<String, Object> map1, Map<String, Object> map2) {
119                    return collator.compare(map1.get("title"), map2.get("title"));
120                }
121            };
122
123    protected Intent activityIntent(String pkg, String componentName) {
124        Intent result = new Intent();
125        result.setClassName(pkg, componentName);
126        return result;
127    }
128
129    protected Intent browseIntent(String path) {
130        Intent result = new Intent();
131        result.setClass(this, BrowseActivity.class);
132        result.putExtra("com.example.androidx.dynamicanimation.animation", path);
133        return result;
134    }
135
136    protected void addItem(List<Map<String, Object>> data, String name, Intent intent) {
137        Map<String, Object> temp = new HashMap<String, Object>();
138        temp.put("title", name);
139        temp.put("intent", intent);
140        data.add(temp);
141    }
142
143    @Override
144    @SuppressWarnings("unchecked")
145    protected void onListItemClick(ListView l, View v, int position, long id) {
146        Map<String, Object> map = (Map<String, Object>) l.getItemAtPosition(position);
147
148        Intent intent = new Intent((Intent) map.get("intent"));
149        intent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
150        startActivity(intent);
151    }
152}
153
154
155