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