Chinaunix首页 | 论坛 | 博客
  • 博客访问: 923743
  • 博文数量: 201
  • 博客积分: 8078
  • 博客等级: 中将
  • 技术积分: 2162
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-20 17:22
文章分类

全部博文(201)

文章存档

2013年(3)

2012年(11)

2011年(34)

2010年(25)

2009年(51)

2008年(77)

分类: LINUX

2011-10-27 18:09:11

  1. package com.huaqin.smile;

  2. import java.text.Collator;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;

  9. import android.os.Bundle;
  10. import android.app.ListActivity;
  11. import android.content.Intent;
  12. import android.content.pm.PackageManager;
  13. import android.content.pm.ResolveInfo;
  14. import android.view.View;
  15. import android.widget.ListView;
  16. import android.widget.SimpleAdapter;

  17. /* action/category */

  18. public class WatchDogActivity extends ListActivity {

  19.     String[] categories = new String[] { Intent.CATEGORY_DEFAULT,
  20.             Intent.CATEGORY_BROWSABLE, Intent.CATEGORY_TAB,
  21.             Intent.CATEGORY_ALTERNATIVE, Intent.CATEGORY_SELECTED_ALTERNATIVE,
  22.             Intent.CATEGORY_LAUNCHER, Intent.CATEGORY_INFO,
  23.             Intent.CATEGORY_HOME, Intent.CATEGORY_PREFERENCE,
  24.             Intent.CATEGORY_TEST, Intent.CATEGORY_CAR_DOCK,
  25.             Intent.CATEGORY_DESK_DOCK, Intent.CATEGORY_CAR_MODE };
  26.     
  27.     String[] actions = new String[] {
  28.             Intent.ACTION_MAIN,
  29.             Intent.ACTION_VIEW,
  30.             Intent.ACTION_ATTACH_DATA,
  31.             Intent.ACTION_EDIT,
  32.             Intent.ACTION_PICK,
  33.             Intent.ACTION_CHOOSER,
  34.             Intent.ACTION_GET_CONTENT,
  35.             Intent.ACTION_DIAL,
  36.             Intent.ACTION_CALL,
  37.             Intent.ACTION_SEND,
  38.             Intent.ACTION_SENDTO,
  39.             Intent.ACTION_ANSWER,
  40.             Intent.ACTION_INSERT,
  41.             Intent.ACTION_DELETE,
  42.             Intent.ACTION_RUN,
  43.             Intent.ACTION_SYNC,
  44.             Intent.ACTION_PICK_ACTIVITY,
  45.             Intent.ACTION_SEARCH,
  46.             Intent.ACTION_WEB_SEARCH,
  47.             Intent.ACTION_FACTORY_TEST
  48.     };
  49.     
  50.     /** Called when the activity is first created. */
  51.     @Override
  52.     public void onCreate(Bundle savedInstanceState) {
  53.         super.onCreate(savedInstanceState);

  54.         Intent intent = getIntent();
  55.         String path = intent.getStringExtra("com.huaqin.CustomerCare.Path");

  56.         if (path == null) {
  57.            setListAdapter(new SimpleAdapter(this, getActions(),
  58.                     android.R.layout.simple_list_item_1, new String[] { "title" },
  59.                     new int[] { android.R.id.text1 }));
  60.            getListView().setTextFilterEnabled(true);
  61.             return;
  62.         }
  63.         
  64.         String action = null;
  65.         String category = null;
  66.         
  67.         if (path.indexOf('@') == -1) {
  68.             setListAdapter(new SimpleAdapter(this, getCategory(path),
  69.                     android.R.layout.simple_list_item_1, new String[] { "title" },
  70.                     new int[] { android.R.id.text1 }));
  71.            getListView().setTextFilterEnabled(true);
  72.             return;     
  73.         }
  74.         
  75.         action = path.substring(0, path.indexOf('@'));
  76.         category = path.substring(path.indexOf('@') + 1);

  77.         setListAdapter(new SimpleAdapter(this, getData(action, category),
  78.                 android.R.layout.simple_list_item_1, new String[] { "title" },
  79.                 new int[] { android.R.id.text1 }));
  80.         getListView().setTextFilterEnabled(true);
  81.     }
  82.     
  83.     private List getCategory(String action) {

  84.         List<Map> myData = new ArrayList<Map>();
  85.         
  86.         for (String category: categories) {
  87.             String label = category.substring(category.lastIndexOf('.') + 1);
  88.             if (!isCategoryEmpty(action, category))
  89.                 addItem(myData, label, browseIntent(action + "@" + category));
  90.         }
  91.         
  92.         return myData;
  93.     }

  94.     private List getActions() {
  95.         List<Map> myData = new ArrayList<Map>();
  96.         
  97.         for (String action: actions) {
  98.             String label = action.substring(action.lastIndexOf('.') + 1);
  99.             if (!isActiomEmpty(action))
  100.                 addItem(myData, label, browseIntent(action));
  101.         }
  102.         
  103.         return myData;
  104.     }
  105.     
  106.     protected boolean isActiomEmpty(String action) {
  107.         for (String category: categories) {
  108.             if (!isCategoryEmpty(action, category))
  109.                 return false;
  110.         }
  111.         return true;
  112.     }
  113.     
  114.     protected boolean isCategoryEmpty(String action, String category) {
  115.         Intent mainIntent = new Intent(action, null);
  116.         mainIntent.addCategory(category);

  117.         PackageManager pm = getPackageManager();
  118.         List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);

  119.         if (null == list)
  120.             return true;
  121.         
  122.         return list.size() == 0;
  123.     }

  124.     protected List getData(String action, String category) {
  125.         List<Map> myData = new ArrayList<Map>();

  126.         Intent mainIntent = new Intent(action, null);
  127.         mainIntent.addCategory(category);

  128.         PackageManager pm = getPackageManager();
  129.         List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);

  130.         if (null == list)
  131.             return myData;

  132.         int len = list.size();

  133.         Map<String, Boolean> entries = new HashMap<String, Boolean>();

  134.         for (int i = 0; i < len; i++) {
  135.             ResolveInfo info = list.get(i);
  136.             CharSequence labelSeq = info.loadLabel(pm);
  137.             String label = labelSeq != null? labelSeq.toString(): info.activityInfo.name;

  138.             addItem(myData, label, activityIntent(
  139.                     info.activityInfo.applicationInfo.packageName,
  140.                     info.activityInfo.name));
  141.         }

  142.         Collections.sort(myData, sDisplayNameComparator);

  143.         return myData;
  144.     }
  145.     
  146.     protected Intent activityIntent(String pkg, String componentName) {
  147.         Intent result = new Intent();
  148.         result.setClassName(pkg, componentName);
  149.         return result;
  150.     }
  151.     
  152.     protected Intent browseIntent(String path) {
  153.         Intent result = new Intent();
  154.         result.setClass(this, WatchDogActivity.class);
  155.         result.putExtra("com.huaqin.CustomerCare.Path", path);
  156.         return result;
  157.     }
  158.     
  159.     private final static Comparator<Map> sDisplayNameComparator = new Comparator<Map>() {
  160.         private final Collator collator = Collator.getInstance();

  161.         public int compare(Map map1, Map map2) {
  162.             return collator.compare(map1.get("title"), map2.get("title"));
  163.         }
  164.     };
  165.     
  166.     protected void addItem(List<Map> data, String name, Intent intent) {
  167.         Map<String, Object> temp = new HashMap<String, Object>();
  168.         temp.put("title", name);
  169.         temp.put("intent", intent);
  170.         data.add(temp);
  171.     }
  172.     
  173.     @Override
  174.     protected void onListItemClick(ListView l, View v, int position, long id) {
  175.         Map map = (Map) l.getItemAtPosition(position);

  176.         Intent intent = (Intent) map.get("intent");
  177.         startActivity(intent);
  178.     }
  179. }
阅读(1908) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~