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 
17 package com.example.androidx.preference;
18 
19 import android.content.Intent;
20 import android.content.pm.PackageManager;
21 import android.content.pm.ResolveInfo;
22 import android.os.Bundle;
23 import android.view.View;
24 import android.widget.ListView;
25 import android.widget.SimpleAdapter;
26 
27 import org.jspecify.annotations.NonNull;
28 
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 
34 /**
35  * Activity that displays and handles launching the demo preference activities with a ListView.
36  */
37 @SuppressWarnings("deprecation")
38 public class MainActivity extends android.app.ListActivity {
39 
40     private static final String INTENT = "intent";
41     private static final String NAME = "name";
42 
43     @Override
onCreate(@onNull Bundle savedInstanceState)44     public void onCreate(@NonNull Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46         SimpleAdapter adapter = new SimpleAdapter(this, getActivityList(),
47                 android.R.layout.simple_list_item_1, new String[]{NAME},
48                 new int[]{android.R.id.text1});
49         setListAdapter(adapter);
50     }
51 
52     @Override
53     @SuppressWarnings("unchecked")
onListItemClick(@onNull ListView l, @NonNull View v, int position, long id)54     protected void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
55         Map<String, Object> map = (Map<String, Object>) l.getItemAtPosition(position);
56         Intent intent = (Intent) map.get(INTENT);
57         startActivity(intent);
58     }
59 
60     @SuppressWarnings("deprecation")
getActivityList()61     protected @NonNull List<Map<String, Object>> getActivityList() {
62         List<Map<String, Object>> activityList = new ArrayList<>();
63 
64         Intent mainIntent = new Intent(Intent.ACTION_MAIN);
65         mainIntent.addCategory("com.example.androidx.preference.SAMPLE_CODE");
66 
67         PackageManager pm = getPackageManager();
68         List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
69         for (int i = 0; i < list.size(); i++) {
70             ResolveInfo info = list.get(i);
71             String label = info.loadLabel(pm).toString();
72             addItem(activityList, label, getIntent(info));
73         }
74         return activityList;
75     }
76 
getIntent(ResolveInfo info)77     private Intent getIntent(ResolveInfo info) {
78         Intent result = new Intent();
79         result.setClassName(info.activityInfo.applicationInfo.packageName, info.activityInfo.name);
80         return result;
81     }
82 
addItem(List<Map<String, Object>> list, String name, Intent intent)83     private void addItem(List<Map<String, Object>> list, String name, Intent intent) {
84         Map<String, Object> temp = new HashMap<>();
85         temp.put(NAME, name);
86         temp.put(INTENT, intent);
87         list.add(temp);
88     }
89 }
90