Example usage for android.content.pm PackageManager resolveActivity

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

Introduction

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

Prototype

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

Source Link

Document

Determine the best action to perform for a given Intent.

Usage

From source file:Main.java

public static boolean hasPreferredApplication(final Context context, final Intent intent) {
    PackageManager pm = context.getPackageManager();
    ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return !"android".equals(info.activityInfo.packageName);
}

From source file:Main.java

public static boolean canBeStarted(Context context, Intent intent, boolean checkSignature) {
    final PackageManager manager = context.getApplicationContext().getPackageManager();
    final ResolveInfo info = manager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
    if (info == null) {
        return false;
    }/*from www  .  ja  v a2  s .  c o m*/
    final ActivityInfo activityInfo = info.activityInfo;
    if (activityInfo == null) {
        return false;
    }
    if (!checkSignature) {
        return true;
    }
    return PackageManager.SIGNATURE_MATCH == manager.checkSignatures(context.getPackageName(),
            activityInfo.packageName);
}

From source file:Main.java

public static ResolveInfo getDefaultBrowser(Context context) {
    PackageManager pm = context.getPackageManager();

    Intent query = new Intent();
    query.setAction(Intent.ACTION_VIEW);
    query.setData(Uri.parse("http://localhost"));

    ResolveInfo info = pm.resolveActivity(query, 0);

    if (info == null) {
        return info;
    }/*from ww  w.  j av a 2  s . c  om*/

    // Could be a Chooser
    if (info.activityInfo.packageName.equals("android")) {
        return null;
    }

    return info;
}

From source file:Main.java

static boolean isSystemApp(Context context, Intent intent) {
    PackageManager pm = context.getPackageManager();
    ComponentName cn = intent.getComponent();
    String packageName = null;//from   ww  w.j  av a 2  s  .co  m
    if (cn == null) {
        ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
        if ((info != null) && (info.activityInfo != null)) {
            packageName = info.activityInfo.packageName;
        }
    } else {
        packageName = cn.getPackageName();
    }
    if (packageName != null) {
        try {
            PackageInfo info = pm.getPackageInfo(packageName, 0);
            return (info != null) && (info.applicationInfo != null)
                    && ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
        } catch (NameNotFoundException e) {
            return false;
        }
    } else {
        return false;
    }
}

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./*from   www  .  j  av a  2  s .c om*/
 *
 * 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  ww .  j  a  va  2 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:Main.java

/**
 * Given an AppTask retrieves the task class name.
 * @param task The app task to use./*from ww  w .  j  av a 2  s  .c o m*/
 * @param pm The package manager to use for resolving intent.
 * @return Fully qualified class name or null if we were not able to
 * determine it.
 */
public static String getTaskClassName(AppTask task, PackageManager pm) {
    RecentTaskInfo info = getTaskInfoFromTask(task);
    if (info == null)
        return null;

    Intent baseIntent = info.baseIntent;
    if (baseIntent == null) {
        return null;
    } else if (baseIntent.getComponent() != null) {
        return baseIntent.getComponent().getClassName();
    } else {
        ResolveInfo resolveInfo = pm.resolveActivity(baseIntent, 0);
        if (resolveInfo == null)
            return null;
        return resolveInfo.activityInfo.name;
    }
}

From source file:com.sanfriend.launcher4.InstallShortcutReceiver.java

/**
 * Tries to create a new PendingInstallShortcutInfo which represents the same target,
 * but is an app target and not a shortcut.
 * @return the newly created info or the original one.
 *//* w  w w.  j  a v  a2 s .  c  o  m*/
private static PendingInstallShortcutInfo convertToLauncherActivityIfPossible(
        PendingInstallShortcutInfo original) {
    if (original.isLuncherActivity()) {
        // Already an activity target
        return original;
    }
    if (!Utilities.isLauncherAppTarget(original.launchIntent)
            || !original.user.equals(UserHandleCompat.myUserHandle())) {
        // We can only convert shortcuts which point to a main activity in the current user.
        return original;
    }

    PackageManager pm = original.mContext.getPackageManager();
    ResolveInfo info = pm.resolveActivity(original.launchIntent, 0);

    if (info == null) {
        return original;
    }

    // Ignore any conflicts in the label name, as that can change based on locale.
    LauncherActivityInfoCompat launcherInfo = LauncherActivityInfoCompat.fromResolveInfo(info,
            original.mContext);
    return new PendingInstallShortcutInfo(launcherInfo, original.mContext);
}

From source file:Main.java

/**
 * Retrieve launcher activity name of the application from the context
 *
 * @param context The context of the application package.
 * @return launcher activity name of this application. From the
 *         "android:name" attribute./*from  w w  w .  j  a  va2s. com*/
 */
private static String getLauncherClassName(Context context) {
    PackageManager packageManager = context.getPackageManager();

    Intent intent = new Intent(Intent.ACTION_MAIN);
    // To limit the components this Intent will resolve to, by setting an
    // explicit package name.
    intent.setPackage(context.getPackageName());
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    // All Application must have 1 Activity at least.
    // Launcher activity must be found!
    ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);

    // get a ResolveInfo containing ACTION_MAIN, CATEGORY_LAUNCHER
    // if there is no Activity which has filtered by CATEGORY_DEFAULT
    if (info == null) {
        info = packageManager.resolveActivity(intent, 0);
    }

    return info.activityInfo.name;
}

From source file:com.marlonjones.voidlauncher.InstallShortcutReceiver.java

/**
 * Tries to create a new PendingInstallShortcutInfo which represents the same target,
 * but is an app target and not a shortcut.
 * @return the newly created info or the original one.
 *///from  ww w.j  av a2  s. c o  m
private static PendingInstallShortcutInfo convertToLauncherActivityIfPossible(
        PendingInstallShortcutInfo original) {
    if (original.isLauncherActivity()) {
        // Already an activity target
        return original;
    }
    if (!Utilities.isLauncherAppTarget(original.launchIntent)
            || !original.user.equals(UserHandleCompat.myUserHandle())) {
        // We can only convert shortcuts which point to a main activity in the current user.
        return original;
    }

    PackageManager pm = original.mContext.getPackageManager();
    ResolveInfo info = pm.resolveActivity(original.launchIntent, 0);

    if (info == null) {
        return original;
    }

    // Ignore any conflicts in the label name, as that can change based on locale.
    LauncherActivityInfoCompat launcherInfo = LauncherActivityInfoCompat.fromResolveInfo(info,
            original.mContext);
    return new PendingInstallShortcutInfo(launcherInfo, original.mContext);
}