1 /*
2  * Copyright (C) 2012 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.android.test.hwui;
18 
19 import android.app.*;
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 @SuppressWarnings("UnusedDeclaration")
37 public class HwTests extends android.app.ListActivity {
38     private static final String EXTRA_PATH = "com.android.test.hwui.Path";
39     private static final String CATEGORY_HWUI_TEST = "com.android.test.hwui.TEST";
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.android.test.hwui.Path");
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 
getData(String prefix)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(CATEGORY_HWUI_TEST);
63 
64         PackageManager pm = getPackageManager();
65         List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
66 
67         if (null == list)
68             return myData;
69 
70         String[] prefixPath;
71         String prefixWithSlash = prefix;
72 
73         if (prefix.equals("")) {
74             prefixPath = null;
75         } else {
76             prefixPath = prefix.split("/");
77             prefixWithSlash = prefix + "/";
78         }
79 
80         int len = list.size();
81 
82         Map<String, Boolean> entries = new HashMap<String, Boolean>();
83 
84         for (int i = 0; i < len; i++) {
85             ResolveInfo info = list.get(i);
86             CharSequence labelSeq = info.loadLabel(pm);
87             String label = labelSeq != null
88                     ? labelSeq.toString()
89                     : info.activityInfo.name;
90 
91             if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {
92 
93                 String[] labelPath = label.split("/");
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 
113         return myData;
114     }
115 
116     private final static Comparator<Map<String, Object>> sDisplayNameComparator =
117             new Comparator<Map<String, Object>>() {
118                 private final Collator collator = Collator.getInstance();
119 
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, HwTests.class);
134         result.putExtra(EXTRA_PATH, 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", "UnusedParameters" })
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 = (Intent) map.get("intent");
151         startActivity(intent);
152     }
153 }
154