Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/***
 * Mages: Multiplayer Game Engine for mobile devices
 * Copyright (C) 2008 aksonov
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Contact: aksonov dot gmail dot com
 *
 * Author: Pavlo Aksonov
 */

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import android.content.Context;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

public class Main {
    /** The Constant LABEL. */
    public static final String LABEL = "label";
    /** The Constant INTENT. */
    public static final String INTENT = "intent";
    /** The Constant displayNameComparator. */
    private final static Comparator<Map> displayNameComparator = new Comparator<Map>() {
        private final Collator collator = Collator.getInstance();

        public int compare(Map map1, Map map2) {
            return collator.compare(map1.get(LABEL), map2.get(LABEL));
        }
    };

    /**
     * Gets the activity list.
     * 
     * @param context
     *            the context
     * @param intent
     *            the intent
     * 
     * @return the activity list
     */
    public static List<Hashtable<String, Object>> getActivityList(Context context, Intent intent) {
        List<Hashtable<String, Object>> result = new ArrayList<Hashtable<String, Object>>();
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> list = pm.queryIntentActivities(intent.addCategory("android.intent.category.DEFAULT"), 0);
        for (ResolveInfo info : list) {
            Hashtable<String, Object> h = new Hashtable<String, Object>();

            CharSequence labelSeq = info.activityInfo.loadLabel(pm);
            String label = labelSeq != null ? labelSeq.toString() : info.activityInfo.name;

            h.put(LABEL, label);
            h.put(INTENT, activityIntent(info.activityInfo.applicationInfo.packageName, info.activityInfo.name));
            result.add(h);
        }
        Collections.sort(result, displayNameComparator);
        return result;

    }

    /**
     * Activity intent.
     * 
     * @param pkg
     *            the pkg
     * @param componentName
     *            the component name
     * 
     * @return the intent
     */
    private static Intent activityIntent(String pkg, String componentName) {
        Intent result = new Intent();
        result.setClassName(pkg, componentName);
        return result;
    }
}