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