18739b58f7860d8d03916b9a529d1fd5950937626Adam Powell/* 28739b58f7860d8d03916b9a529d1fd5950937626Adam Powell * Copyright (C) 2012 The Android Open Source Project 38739b58f7860d8d03916b9a529d1fd5950937626Adam Powell * 48739b58f7860d8d03916b9a529d1fd5950937626Adam Powell * Licensed under the Apache License, Version 2.0 (the "License"); 58739b58f7860d8d03916b9a529d1fd5950937626Adam Powell * you may not use this file except in compliance with the License. 68739b58f7860d8d03916b9a529d1fd5950937626Adam Powell * You may obtain a copy of the License at 78739b58f7860d8d03916b9a529d1fd5950937626Adam Powell * 88739b58f7860d8d03916b9a529d1fd5950937626Adam Powell * http://www.apache.org/licenses/LICENSE-2.0 98739b58f7860d8d03916b9a529d1fd5950937626Adam Powell * 108739b58f7860d8d03916b9a529d1fd5950937626Adam Powell * Unless required by applicable law or agreed to in writing, software 118739b58f7860d8d03916b9a529d1fd5950937626Adam Powell * distributed under the License is distributed on an "AS IS" BASIS, 128739b58f7860d8d03916b9a529d1fd5950937626Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138739b58f7860d8d03916b9a529d1fd5950937626Adam Powell * See the License for the specific language governing permissions and 148739b58f7860d8d03916b9a529d1fd5950937626Adam Powell * limitations under the License. 158739b58f7860d8d03916b9a529d1fd5950937626Adam Powell */ 168739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 178739b58f7860d8d03916b9a529d1fd5950937626Adam Powellpackage com.example.android.support.appnavigation.app; 188739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 198739b58f7860d8d03916b9a529d1fd5950937626Adam Powellimport android.app.ListActivity; 208739b58f7860d8d03916b9a529d1fd5950937626Adam Powellimport android.content.Intent; 218739b58f7860d8d03916b9a529d1fd5950937626Adam Powellimport android.content.pm.PackageManager; 228739b58f7860d8d03916b9a529d1fd5950937626Adam Powellimport android.content.pm.ResolveInfo; 238739b58f7860d8d03916b9a529d1fd5950937626Adam Powellimport android.os.Bundle; 248739b58f7860d8d03916b9a529d1fd5950937626Adam Powellimport android.view.View; 258739b58f7860d8d03916b9a529d1fd5950937626Adam Powellimport android.view.ViewGroup; 268739b58f7860d8d03916b9a529d1fd5950937626Adam Powellimport android.widget.BaseAdapter; 278739b58f7860d8d03916b9a529d1fd5950937626Adam Powellimport android.widget.ListView; 288739b58f7860d8d03916b9a529d1fd5950937626Adam Powellimport android.widget.TextView; 298739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 308739b58f7860d8d03916b9a529d1fd5950937626Adam Powellimport java.util.ArrayList; 318739b58f7860d8d03916b9a529d1fd5950937626Adam Powellimport java.util.List; 328739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 338739b58f7860d8d03916b9a529d1fd5950937626Adam Powell/** 348739b58f7860d8d03916b9a529d1fd5950937626Adam Powell * Home activity for app navigation code samples. 358739b58f7860d8d03916b9a529d1fd5950937626Adam Powell */ 368739b58f7860d8d03916b9a529d1fd5950937626Adam Powellpublic class AppNavHomeActivity extends ListActivity { 378739b58f7860d8d03916b9a529d1fd5950937626Adam Powell @Override 388739b58f7860d8d03916b9a529d1fd5950937626Adam Powell protected void onCreate(Bundle savedInstanceState) { 398739b58f7860d8d03916b9a529d1fd5950937626Adam Powell super.onCreate(savedInstanceState); 408739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 418739b58f7860d8d03916b9a529d1fd5950937626Adam Powell setListAdapter(new SampleAdapter(querySampleActivities())); 428739b58f7860d8d03916b9a529d1fd5950937626Adam Powell } 438739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 448739b58f7860d8d03916b9a529d1fd5950937626Adam Powell @Override 458739b58f7860d8d03916b9a529d1fd5950937626Adam Powell protected void onListItemClick(ListView lv, View v, int pos, long id) { 468739b58f7860d8d03916b9a529d1fd5950937626Adam Powell SampleInfo info = (SampleInfo) getListAdapter().getItem(pos); 478739b58f7860d8d03916b9a529d1fd5950937626Adam Powell startActivity(info.intent); 488739b58f7860d8d03916b9a529d1fd5950937626Adam Powell } 498739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 508739b58f7860d8d03916b9a529d1fd5950937626Adam Powell protected List<SampleInfo> querySampleActivities() { 518739b58f7860d8d03916b9a529d1fd5950937626Adam Powell Intent intent = new Intent(Intent.ACTION_MAIN, null); 528739b58f7860d8d03916b9a529d1fd5950937626Adam Powell intent.setPackage(getPackageName()); 538739b58f7860d8d03916b9a529d1fd5950937626Adam Powell intent.addCategory(Intent.CATEGORY_SAMPLE_CODE); 548739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 558739b58f7860d8d03916b9a529d1fd5950937626Adam Powell PackageManager pm = getPackageManager(); 568739b58f7860d8d03916b9a529d1fd5950937626Adam Powell List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0); 578739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 588739b58f7860d8d03916b9a529d1fd5950937626Adam Powell ArrayList<SampleInfo> samples = new ArrayList<SampleInfo>(); 598739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 608739b58f7860d8d03916b9a529d1fd5950937626Adam Powell final int count = infos.size(); 618739b58f7860d8d03916b9a529d1fd5950937626Adam Powell for (int i = 0; i < count; i++) { 628739b58f7860d8d03916b9a529d1fd5950937626Adam Powell final ResolveInfo info = infos.get(i); 638739b58f7860d8d03916b9a529d1fd5950937626Adam Powell final CharSequence labelSeq = info.loadLabel(pm); 648739b58f7860d8d03916b9a529d1fd5950937626Adam Powell String label = labelSeq != null ? labelSeq.toString() : info.activityInfo.name; 658739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 668739b58f7860d8d03916b9a529d1fd5950937626Adam Powell Intent target = new Intent(); 678739b58f7860d8d03916b9a529d1fd5950937626Adam Powell target.setClassName(info.activityInfo.applicationInfo.packageName, 688739b58f7860d8d03916b9a529d1fd5950937626Adam Powell info.activityInfo.name); 698739b58f7860d8d03916b9a529d1fd5950937626Adam Powell SampleInfo sample = new SampleInfo(label, target); 708739b58f7860d8d03916b9a529d1fd5950937626Adam Powell samples.add(sample); 718739b58f7860d8d03916b9a529d1fd5950937626Adam Powell } 728739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 738739b58f7860d8d03916b9a529d1fd5950937626Adam Powell return samples; 748739b58f7860d8d03916b9a529d1fd5950937626Adam Powell } 758739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 768739b58f7860d8d03916b9a529d1fd5950937626Adam Powell static class SampleInfo { 778739b58f7860d8d03916b9a529d1fd5950937626Adam Powell String name; 788739b58f7860d8d03916b9a529d1fd5950937626Adam Powell Intent intent; 798739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 808739b58f7860d8d03916b9a529d1fd5950937626Adam Powell SampleInfo(String name, Intent intent) { 818739b58f7860d8d03916b9a529d1fd5950937626Adam Powell this.name = name; 828739b58f7860d8d03916b9a529d1fd5950937626Adam Powell this.intent = intent; 838739b58f7860d8d03916b9a529d1fd5950937626Adam Powell } 848739b58f7860d8d03916b9a529d1fd5950937626Adam Powell } 858739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 868739b58f7860d8d03916b9a529d1fd5950937626Adam Powell class SampleAdapter extends BaseAdapter { 878739b58f7860d8d03916b9a529d1fd5950937626Adam Powell private List<SampleInfo> mItems; 888739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 898739b58f7860d8d03916b9a529d1fd5950937626Adam Powell public SampleAdapter(List<SampleInfo> items) { 908739b58f7860d8d03916b9a529d1fd5950937626Adam Powell mItems = items; 918739b58f7860d8d03916b9a529d1fd5950937626Adam Powell } 928739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 938739b58f7860d8d03916b9a529d1fd5950937626Adam Powell @Override 948739b58f7860d8d03916b9a529d1fd5950937626Adam Powell public int getCount() { 958739b58f7860d8d03916b9a529d1fd5950937626Adam Powell return mItems.size(); 968739b58f7860d8d03916b9a529d1fd5950937626Adam Powell } 978739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 988739b58f7860d8d03916b9a529d1fd5950937626Adam Powell @Override 998739b58f7860d8d03916b9a529d1fd5950937626Adam Powell public Object getItem(int position) { 1008739b58f7860d8d03916b9a529d1fd5950937626Adam Powell return mItems.get(position); 1018739b58f7860d8d03916b9a529d1fd5950937626Adam Powell } 1028739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 1038739b58f7860d8d03916b9a529d1fd5950937626Adam Powell @Override 1048739b58f7860d8d03916b9a529d1fd5950937626Adam Powell public long getItemId(int position) { 1058739b58f7860d8d03916b9a529d1fd5950937626Adam Powell return position; 1068739b58f7860d8d03916b9a529d1fd5950937626Adam Powell } 1078739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 1088739b58f7860d8d03916b9a529d1fd5950937626Adam Powell @Override 1098739b58f7860d8d03916b9a529d1fd5950937626Adam Powell public View getView(int position, View convertView, ViewGroup parent) { 1108739b58f7860d8d03916b9a529d1fd5950937626Adam Powell if (convertView == null) { 1118739b58f7860d8d03916b9a529d1fd5950937626Adam Powell convertView = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, 1128739b58f7860d8d03916b9a529d1fd5950937626Adam Powell parent, false); 1138739b58f7860d8d03916b9a529d1fd5950937626Adam Powell convertView.setTag(convertView.findViewById(android.R.id.text1)); 1148739b58f7860d8d03916b9a529d1fd5950937626Adam Powell } 1158739b58f7860d8d03916b9a529d1fd5950937626Adam Powell TextView tv = (TextView) convertView.getTag(); 1168739b58f7860d8d03916b9a529d1fd5950937626Adam Powell tv.setText(mItems.get(position).name); 1178739b58f7860d8d03916b9a529d1fd5950937626Adam Powell return convertView; 1188739b58f7860d8d03916b9a529d1fd5950937626Adam Powell } 1198739b58f7860d8d03916b9a529d1fd5950937626Adam Powell 1208739b58f7860d8d03916b9a529d1fd5950937626Adam Powell } 1218739b58f7860d8d03916b9a529d1fd5950937626Adam Powell} 122