Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// Use of this source code is governed by a BSD-style license that can be

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.text.TextUtils;
import android.util.Log;

import java.util.List;

public class Main {
    private static final String TAG = "UrlUtilities";

    public static boolean isSpecializedHandlerAvailable(Context context, Intent intent) {
        return isPackageSpecializedHandler(context, null, intent);
    }

    /**
     * Check whether the given package is a specialized handler for the given intent
     *
     * @param context {@link Context} to use for getting the {@link PackageManager}.
     * @param packageName Package name to check against. Can be null or empty.
     * @param intent The intent to resolve for.
     * @return Whether the given package is a specialized handler for the given intent. If there is
     *         no package name given checks whether there is any specialized handler.
     */
    public static boolean isPackageSpecializedHandler(Context context, String packageName, Intent intent) {
        try {
            List<ResolveInfo> handlers = context.getPackageManager().queryIntentActivities(intent,
                    PackageManager.GET_RESOLVED_FILTER);
            if (handlers == null || handlers.size() == 0)
                return false;
            for (ResolveInfo resolveInfo : handlers) {
                IntentFilter filter = resolveInfo.filter;
                if (filter == null) {
                    // No intent filter matches this intent?
                    // Error on the side of staying in the browser, ignore
                    continue;
                }
                if (filter.countDataAuthorities() == 0 || filter.countDataPaths() == 0) {
                    // Generic handler, skip
                    continue;
                }
                if (TextUtils.isEmpty(packageName))
                    return true;
                ActivityInfo activityInfo = resolveInfo.activityInfo;
                if (activityInfo == null)
                    continue;
                if (!activityInfo.packageName.equals(packageName))
                    continue;
                return true;
            }
        } catch (RuntimeException e) {
            Log.e(TAG, "isPackageSpecializedHandler e=" + e);
        }
        return false;
    }
}