/*
* Copyright (c) 2012 The Android Open Source Project, Daniel Huckaby
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.handlerexploit.prime.example;
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;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
/**
* This class is responsible for displaying all avaliable api examples to the
* user. This is a cut down version of the AOSP ApiDemos switcher.
*/
public class Examples extends ListActivity {
private static final String ACTION_EXAMPLE = "com.handlerexploit.prime.example.EXAMPLE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new SimpleAdapter(this, getData(),
android.R.layout.simple_list_item_1, new String[] { "title" },
new int[] { android.R.id.text1 }));
getListView().setTextFilterEnabled(true);
}
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(ACTION_EXAMPLE);
PackageManager pm = getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
if (list != null) {
for (int i = 0; i < list.size(); i++) {
ResolveInfo info = list.get(i);
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 == 0) {
Map<String, Object> temp = new HashMap<String, Object>();
temp.put("title", nextLabel);
Intent intent = new Intent();
intent.setClassName(info.activityInfo.applicationInfo.packageName, info.activityInfo.name);
temp.put("intent", intent);
data.add(temp);
}
}
Collections.sort(data, sDisplayNameComparator);
}
return data;
}
private final static Comparator<Map<String, Object>> sDisplayNameComparator = new Comparator<Map<String, Object>>() {
private final Collator collator = Collator.getInstance();
@Override
public int compare(Map<String, Object> map1, Map<String, Object> map2) {
return collator.compare(map1.get("title"), map2.get("title"));
}
};
@Override
@SuppressWarnings("unchecked")
protected void onListItemClick(ListView l, View v, int position, long id) {
Map<String, Object> map = (Map<String, Object>) l.getItemAtPosition(position);
Intent intent = (Intent) map.get("intent");
startActivity(intent);
}
}