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.example.android.appnavigation.app;
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.view.ViewGroup;
26 import android.widget.BaseAdapter;
27 import android.widget.ListView;
28 import android.widget.TextView;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /**
34  * Home activity for app navigation code samples.
35  */
36 public class AppNavHomeActivity extends ListActivity {
37     @Override
onCreate(Bundle savedInstanceState)38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40 
41         setListAdapter(new SampleAdapter(querySampleActivities()));
42     }
43 
44     @Override
onListItemClick(ListView lv, View v, int pos, long id)45     protected void onListItemClick(ListView lv, View v, int pos, long id) {
46         SampleInfo info = (SampleInfo) getListAdapter().getItem(pos);
47         startActivity(info.intent);
48     }
49 
querySampleActivities()50     protected List<SampleInfo> querySampleActivities() {
51         Intent intent = new Intent(Intent.ACTION_MAIN, null);
52         intent.setPackage(getPackageName());
53         intent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
54 
55         PackageManager pm = getPackageManager();
56         List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
57 
58         ArrayList<SampleInfo> samples = new ArrayList<SampleInfo>();
59 
60         final int count = infos.size();
61         for (int i = 0; i < count; i++) {
62             final ResolveInfo info = infos.get(i);
63             final CharSequence labelSeq = info.loadLabel(pm);
64             String label = labelSeq != null ? labelSeq.toString() : info.activityInfo.name;
65 
66             Intent target = new Intent();
67             target.setClassName(info.activityInfo.applicationInfo.packageName,
68                     info.activityInfo.name);
69             SampleInfo sample = new SampleInfo(label, target);
70             samples.add(sample);
71         }
72 
73         return samples;
74     }
75 
76     static class SampleInfo {
77         String name;
78         Intent intent;
79 
SampleInfo(String name, Intent intent)80         SampleInfo(String name, Intent intent) {
81             this.name = name;
82             this.intent = intent;
83         }
84     }
85 
86     class SampleAdapter extends BaseAdapter {
87         private List<SampleInfo> mItems;
88 
SampleAdapter(List<SampleInfo> items)89         public SampleAdapter(List<SampleInfo> items) {
90             mItems = items;
91         }
92 
93         @Override
getCount()94         public int getCount() {
95             return mItems.size();
96         }
97 
98         @Override
getItem(int position)99         public Object getItem(int position) {
100             return mItems.get(position);
101         }
102 
103         @Override
getItemId(int position)104         public long getItemId(int position) {
105             return position;
106         }
107 
108         @Override
getView(int position, View convertView, ViewGroup parent)109         public View getView(int position, View convertView, ViewGroup parent) {
110             if (convertView == null) {
111                 convertView = getLayoutInflater().inflate(android.R.layout.simple_list_item_1,
112                         parent, false);
113                 convertView.setTag(convertView.findViewById(android.R.id.text1));
114             }
115             TextView tv = (TextView) convertView.getTag();
116             tv.setText(mItems.get(position).name);
117             return convertView;
118         }
119 
120     }
121 }
122