Back to project page ui-showcase-for-android.
The source code is released under:
Apache License
If you think the Android project ui-showcase-for-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.github.ksoichiro.uishowcase; /*from w w w .j a va2 s . c o m*/ import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import java.text.Collator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends ActionBarActivity implements ListView.OnItemClickListener { private static final String CATEGORY_SHOWCASE = "com.github.ksoichiro.uishowcase"; private static final Comparator<Map<String, Object>> DISPLAY_NAME_COMPARATOR = new Comparator<Map<String, Object>>() { private final Collator collator = Collator.getInstance(); @Override public int compare(Map<String, Object> lhs, Map<String, Object> rhs) { return collator.compare(lhs.get("className"), rhs.get("className")); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle(R.string.app_name); ListView list = (ListView) findViewById(R.id.list); list.setOnItemClickListener(this); list.setAdapter(new SimpleAdapter(this, getData(), R.layout.list_item_main, new String[]{ "className", "description", }, new int[]{ R.id.className, R.id.description, })); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(final MenuItem menu) { int id = menu.getItemId(); if (id == R.id.menu_about) { startActivity(new Intent(getApplicationContext(), AboutActivity.class)); return true; } return false; } private List<Map<String, Object>> getData() { List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(CATEGORY_SHOWCASE); PackageManager pm = getPackageManager(); List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0); if (list == null) { return data; } for (ResolveInfo info : list) { CharSequence labelSeq = info.loadLabel(pm); String label = labelSeq != null ? labelSeq.toString() : info.activityInfo.name; String[] labelPath = label.split("/"); String nextLabel = labelPath[0]; if (labelPath.length == 1) { addItem(data, info.activityInfo.name.replace(info.activityInfo.packageName + "", ""), nextLabel, activityIntent( info.activityInfo.applicationInfo.packageName, info.activityInfo.name)); } } Collections.sort(data, DISPLAY_NAME_COMPARATOR); return data; } protected Intent activityIntent(String pkg, String componentName) { Intent result = new Intent(); result.setClassName(pkg, componentName); return result; } protected void addItem(List<Map<String, Object>> data, String className, String description, Intent intent) { Map<String, Object> temp = new HashMap<String, Object>(); temp.put("className", className); temp.put("description", description); temp.put("intent", intent); data.add(temp); } @Override @SuppressWarnings("unchecked") public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { Map<String, Object> map = (Map<String, Object>) adapterView.getItemAtPosition(position); Intent intent = (Intent) map.get("intent"); startActivity(intent); } }