- package com.huaqin.smile;
-
-
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.os.Bundle;
-
import android.app.ListActivity;
-
import android.content.Intent;
-
import android.content.pm.PackageManager;
-
import android.content.pm.ResolveInfo;
-
import android.view.View;
-
import android.widget.ListView;
-
import android.widget.SimpleAdapter;
-
-
/* action/category */
-
-
public class WatchDogActivity extends ListActivity {
-
-
String[] categories = new String[] { Intent.CATEGORY_DEFAULT,
-
Intent.CATEGORY_BROWSABLE, Intent.CATEGORY_TAB,
-
Intent.CATEGORY_ALTERNATIVE, Intent.CATEGORY_SELECTED_ALTERNATIVE,
-
Intent.CATEGORY_LAUNCHER, Intent.CATEGORY_INFO,
-
Intent.CATEGORY_HOME, Intent.CATEGORY_PREFERENCE,
-
Intent.CATEGORY_TEST, Intent.CATEGORY_CAR_DOCK,
-
Intent.CATEGORY_DESK_DOCK, Intent.CATEGORY_CAR_MODE };
-
-
String[] actions = new String[] {
-
Intent.ACTION_MAIN,
-
Intent.ACTION_VIEW,
-
Intent.ACTION_ATTACH_DATA,
-
Intent.ACTION_EDIT,
-
Intent.ACTION_PICK,
-
Intent.ACTION_CHOOSER,
-
Intent.ACTION_GET_CONTENT,
-
Intent.ACTION_DIAL,
-
Intent.ACTION_CALL,
-
Intent.ACTION_SEND,
-
Intent.ACTION_SENDTO,
-
Intent.ACTION_ANSWER,
-
Intent.ACTION_INSERT,
-
Intent.ACTION_DELETE,
-
Intent.ACTION_RUN,
-
Intent.ACTION_SYNC,
-
Intent.ACTION_PICK_ACTIVITY,
-
Intent.ACTION_SEARCH,
-
Intent.ACTION_WEB_SEARCH,
-
Intent.ACTION_FACTORY_TEST
-
};
-
-
/** Called when the activity is first created. */
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
-
Intent intent = getIntent();
-
String path = intent.getStringExtra("com.huaqin.CustomerCare.Path");
-
-
if (path == null) {
-
setListAdapter(new SimpleAdapter(this, getActions(),
-
android.R.layout.simple_list_item_1, new String[] { "title" },
-
new int[] { android.R.id.text1 }));
-
getListView().setTextFilterEnabled(true);
-
return;
-
}
-
-
String action = null;
-
String category = null;
-
-
if (path.indexOf('@') == -1) {
-
setListAdapter(new SimpleAdapter(this, getCategory(path),
-
android.R.layout.simple_list_item_1, new String[] { "title" },
-
new int[] { android.R.id.text1 }));
-
getListView().setTextFilterEnabled(true);
-
return;
-
}
-
-
action = path.substring(0, path.indexOf('@'));
-
category = path.substring(path.indexOf('@') + 1);
-
-
setListAdapter(new SimpleAdapter(this, getData(action, category),
-
android.R.layout.simple_list_item_1, new String[] { "title" },
-
new int[] { android.R.id.text1 }));
-
getListView().setTextFilterEnabled(true);
-
}
-
-
private List getCategory(String action) {
-
-
List<Map> myData = new ArrayList<Map>();
-
-
for (String category: categories) {
-
String label = category.substring(category.lastIndexOf('.') + 1);
-
if (!isCategoryEmpty(action, category))
-
addItem(myData, label, browseIntent(action + "@" + category));
-
}
-
-
return myData;
-
}
-
-
private List getActions() {
-
List<Map> myData = new ArrayList<Map>();
-
-
for (String action: actions) {
-
String label = action.substring(action.lastIndexOf('.') + 1);
-
if (!isActiomEmpty(action))
-
addItem(myData, label, browseIntent(action));
-
}
-
-
return myData;
-
}
-
-
protected boolean isActiomEmpty(String action) {
-
for (String category: categories) {
-
if (!isCategoryEmpty(action, category))
-
return false;
-
}
-
return true;
-
}
-
-
protected boolean isCategoryEmpty(String action, String category) {
-
Intent mainIntent = new Intent(action, null);
-
mainIntent.addCategory(category);
-
-
PackageManager pm = getPackageManager();
-
List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
-
-
if (null == list)
-
return true;
-
-
return list.size() == 0;
-
}
-
-
protected List getData(String action, String category) {
-
List<Map> myData = new ArrayList<Map>();
-
-
Intent mainIntent = new Intent(action, null);
-
mainIntent.addCategory(category);
-
-
PackageManager pm = getPackageManager();
-
List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
-
-
if (null == list)
-
return myData;
-
-
int len = list.size();
-
-
Map<String, Boolean> entries = new HashMap<String, Boolean>();
-
-
for (int i = 0; i < len; i++) {
-
ResolveInfo info = list.get(i);
-
CharSequence labelSeq = info.loadLabel(pm);
-
String label = labelSeq != null? labelSeq.toString(): info.activityInfo.name;
-
-
addItem(myData, label, activityIntent(
-
info.activityInfo.applicationInfo.packageName,
-
info.activityInfo.name));
-
}
-
-
Collections.sort(myData, sDisplayNameComparator);
-
-
return myData;
-
}
-
-
protected Intent activityIntent(String pkg, String componentName) {
-
Intent result = new Intent();
-
result.setClassName(pkg, componentName);
-
return result;
-
}
-
-
protected Intent browseIntent(String path) {
-
Intent result = new Intent();
-
result.setClass(this, WatchDogActivity.class);
-
result.putExtra("com.huaqin.CustomerCare.Path", path);
-
return result;
-
}
-
-
private final static Comparator<Map> sDisplayNameComparator = new Comparator<Map>() {
-
private final Collator collator = Collator.getInstance();
-
-
public int compare(Map map1, Map map2) {
-
return collator.compare(map1.get("title"), map2.get("title"));
-
}
-
};
-
-
protected void addItem(List<Map> data, String name, Intent intent) {
-
Map<String, Object> temp = new HashMap<String, Object>();
-
temp.put("title", name);
-
temp.put("intent", intent);
-
data.add(temp);
-
}
-
-
@Override
-
protected void onListItemClick(ListView l, View v, int position, long id) {
-
Map map = (Map) l.getItemAtPosition(position);
-
-
Intent intent = (Intent) map.get("intent");
-
startActivity(intent);
-
}
- }