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