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
17package com.example.android.support.appnavigation.app;
18
19import android.app.ListActivity;
20import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
23import android.os.Bundle;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.BaseAdapter;
27import android.widget.ListView;
28import android.widget.TextView;
29
30import java.util.ArrayList;
31import java.util.List;
32
33/**
34 * Home activity for app navigation code samples.
35 */
36public class AppNavHomeActivity extends ListActivity {
37    @Override
38    protected void onCreate(Bundle savedInstanceState) {
39        super.onCreate(savedInstanceState);
40
41        setListAdapter(new SampleAdapter(querySampleActivities()));
42    }
43
44    @Override
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
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
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
89        public SampleAdapter(List<SampleInfo> items) {
90            mItems = items;
91        }
92
93        @Override
94        public int getCount() {
95            return mItems.size();
96        }
97
98        @Override
99        public Object getItem(int position) {
100            return mItems.get(position);
101        }
102
103        @Override
104        public long getItemId(int position) {
105            return position;
106        }
107
108        @Override
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