Example usage for android.content.pm PackageManager resolveService

List of usage examples for android.content.pm PackageManager resolveService

Introduction

In this page you can find the example usage for android.content.pm PackageManager resolveService.

Prototype

public abstract ResolveInfo resolveService(Intent intent, @ResolveInfoFlags int flags);

Source Link

Document

Determine the best service to handle for a given Intent.

Usage

From source file:de.ub0r.android.lib.DonationHelper.java

/**
 * Check if ads should be hidden./* w  w w .  j  a va2s  . co  m*/
 * 
 * @param context
 *            {@link Context}
 * @return true if ads should be hidden
 */
public static boolean hideAds(final Context context) {
    PackageManager pm = context.getPackageManager();
    Intent donationCheck = new Intent(DONATOR_BROADCAST_CHECK);
    ResolveInfo ri = pm.resolveService(donationCheck, 0);
    // Log.d(TAG, "ri: " + ri);
    int match = PackageManager.SIGNATURE_UNKNOWN_PACKAGE;
    if (ri != null) {
        Log.d(TAG, "found package: " + ri.serviceInfo.packageName);
        ComponentName cn = new ComponentName(ri.serviceInfo.packageName, ri.serviceInfo.name);
        // Log.d(TAG, "component name: " + cn);
        int i = pm.getComponentEnabledSetting(cn);
        // Log.d(TAG, "component status: " + i);
        // Log.d(TAG, "package status: " + ri.serviceInfo.enabled);
        if (i == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                || i == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT && ri.serviceInfo.enabled) {
            match = pm.checkSignatures(context.getPackageName(), ri.serviceInfo.packageName);
        } else {
            Log.w(TAG, ri.serviceInfo.packageName + ": " + ri.serviceInfo.enabled);
        }
    }

    Log.i(TAG, "signature match: " + match);
    if (match != PackageManager.SIGNATURE_UNKNOWN_PACKAGE) {
        if (Math.random() < CHECK_DONATOR_LIC) {
            // verify donator license
            ComponentName cn = context.startService(donationCheck);
            Log.d(TAG, "Started service: " + cn);
            if (cn == null) {
                return false;
            }
        }
        return match == PackageManager.SIGNATURE_MATCH;
    }
    pm = null;

    // no donator app installed, check donation traditionally
    // do not drop legacy donators
    boolean ret = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(PREFS_HIDEADS, false);
    Log.d(TAG, "legacy donation check: " + ret);
    return ret;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.M)
public static List<String> getCustomTabSupportingPackages(Context context) {
    PackageManager pm = context.getPackageManager();
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, PackageManager.MATCH_ALL);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }// w  ww.  j  a  v a2  s. c  o  m
    }
    return packagesSupportingCustomTabs;
}

From source file:arun.com.chromer.browsing.customtabs.CustomTabs.java

/**
 * Determines if the provided package name is a valid custom tab provider or not.
 *
 * @param context     Context to work with
 * @param packageName Package name of the app
 * @return true if a provider, false otherwise
 */// ww w .ja  va2 s.  c om
public static boolean isPackageSupportCustomTabs(Context context, @Nullable String packageName) {
    if (packageName == null) {
        return false;
    }
    final PackageManager pm = context.getApplicationContext().getPackageManager();
    final Intent serviceIntent = new Intent();
    serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
    serviceIntent.setPackage(packageName);
    return pm.resolveService(serviceIntent, 0) != null;
}

From source file:Main.java

/**
 * Goes through all apps that handle VIEW intents and have a warmup service. Picks
 * the one chosen by the user if there is one, otherwise makes a best effort to return a
 * valid package name.// w  ww .  j a  v a  2s.co m
 *
 * This is <strong>not</strong> threadsafe.
 *
 * @param context {@link Context} to use for accessing {@link PackageManager}.
 * @return The package name recommended to use for connecting to custom tabs related components.
 */
public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null)
        return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
        defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
        sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!TextUtils.isEmpty(defaultViewHandlerPackageName)
            && !hasSpecializedHandlerIntents(context, activityIntent)
            && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
        sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        sPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
        sPackageNameToUse = LOCAL_PACKAGE;
    }
    return sPackageNameToUse;
}

From source file:com.elkriefy.android.apps.chubbytabby.CustomTabsHelper.java

/**
 * Goes through all apps that handle VIEW intents and have a warmup service. Picks the one
 * chosen by the user if there is one, otherwise makes a best effort to return a valid package
 * name./*from   w w  w  . ja  va2 s.  co m*/
 *
 * This is <strong>not</strong> threadsafe.
 *
 * @param context {@link Context} to use for accessing {@link PackageManager}.
 * @return The package name recommended to use for connecting to custom tabs related components.
 */
public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null)
        return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.packtpub.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
        defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
        sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!android.text.TextUtils.isEmpty(defaultViewHandlerPackageName)
            && !hasSpecializedHandlerIntents(context, activityIntent)
            && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
        sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        sPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
        sPackageNameToUse = LOCAL_PACKAGE;
    }
    return sPackageNameToUse;
}

From source file:org.wahtod.wififixer.ui.MainActivity.java

void authCheck() {
    if (!PrefUtil.readBoolean(this, this.getString(R.string.isauthed))) {
        // Handle Donate Auth
        PackageManager pm = getPackageManager();
        String component = getString(R.string.donateservice);
        Intent intent = new Intent(component);
        ResolveInfo info = pm.resolveService(intent, 0);
        if (info != null) {
            intent.setClassName(info.serviceInfo.packageName, info.serviceInfo.name);
            this.startService(intent);
        }//from   ww  w .  ja v a 2  s .  c om
        nagNotification(this);
    }
}

From source file:net.gsantner.opoc.util.ShareUtil.java

/**
 * By default Chrome Custom Tabs only uses Chrome Stable to open links
 * There are also other packages (like Chrome Beta, Chromium, Firefox, ..)
 * which implement the Chrome Custom Tab interface. This method changes
 * the customtab intent to use an available compatible browser, if available.
 *///from   w w w.j  a  v a  2 s . c  om
public void enableChromeCustomTabsForOtherBrowsers(Intent customTabIntent) {
    String[] checkpkgs = new String[] { "com.android.chrome", "com.chrome.beta", "com.chrome.dev",
            "com.google.android.apps.chrome", "org.chromium.chrome", "org.mozilla.fennec_fdroid",
            "org.mozilla.firefox", "org.mozilla.firefox_beta", "org.mozilla.fennec_aurora", "org.mozilla.klar",
            "org.mozilla.focus", };

    // Get all intent handlers for web links
    PackageManager pm = _context.getPackageManager();
    Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com"));
    List<String> browsers = new ArrayList<>();
    for (ResolveInfo ri : pm.queryIntentActivities(urlIntent, 0)) {
        Intent i = new Intent("android.support.customtabs.action.CustomTabsService");
        i.setPackage(ri.activityInfo.packageName);
        if (pm.resolveService(i, 0) != null) {
            browsers.add(ri.activityInfo.packageName);
        }
    }

    // Check if the user has a "default browser" selected
    ResolveInfo ri = pm.resolveActivity(urlIntent, 0);
    String userDefaultBrowser = (ri == null) ? null : ri.activityInfo.packageName;

    // Select which browser to use out of all installed customtab supporting browsers
    String pkg = null;
    if (browsers.isEmpty()) {
        pkg = null;
    } else if (browsers.size() == 1) {
        pkg = browsers.get(0);
    } else if (!TextUtils.isEmpty(userDefaultBrowser) && browsers.contains(userDefaultBrowser)) {
        pkg = userDefaultBrowser;
    } else {
        for (String checkpkg : checkpkgs) {
            if (browsers.contains(checkpkg)) {
                pkg = checkpkg;
                break;
            }
        }
        if (pkg == null && !browsers.isEmpty()) {
            pkg = browsers.get(0);
        }
    }
    if (pkg != null && customTabIntent != null) {
        customTabIntent.setPackage(pkg);
    }
}