Example usage for android.content ComponentName getPackageName

List of usage examples for android.content ComponentName getPackageName

Introduction

In this page you can find the example usage for android.content ComponentName getPackageName.

Prototype

public @NonNull String getPackageName() 

Source Link

Document

Return the package name of this component.

Usage

From source file:Main.java

public static String getAppName(Context context, Intent appIntent) {

    if (appIntent.hasExtra(Intent.EXTRA_SHORTCUT_NAME)) {
        return appIntent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
    }/*from  www.  java 2 s.  com*/

    if (appIntent.hasExtra(Intent.EXTRA_SHORTCUT_INTENT)) {
        appIntent = appIntent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
    }
    ComponentName componentName = appIntent.getComponent();

    PackageManager pm = context.getPackageManager();

    ApplicationInfo appInfo = null;
    ActivityInfo activityInfo = null;
    try {
        appInfo = pm.getApplicationInfo(componentName.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        appInfo = null;
    }
    try {
        activityInfo = pm.getActivityInfo(componentName, 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    if (appInfo == null) {
        return null;
    } else {
        CharSequence appName = pm.getApplicationLabel(appInfo);
        CharSequence activityName = null;

        if (activityInfo != null) {
            activityName = activityInfo.loadLabel(pm);
        }

        if (activityName != null) {
            return activityName.toString();
        }

        if (appName != null) {
            appName.toString();
        }

        return null;
    }
}

From source file:com.googlecode.android_scripting.jsonrpc.JsonBuilder.java

private static JSONObject buildJsonIntent(Intent data) throws JSONException {
    JSONObject result = new JSONObject();
    result.put("data", data.getDataString());
    result.put("type", data.getType());
    result.put("extras", build(data.getExtras()));
    result.put("categories", build(data.getCategories()));
    result.put("action", data.getAction());
    ComponentName component = data.getComponent();
    if (component != null) {
        result.put("packagename", component.getPackageName());
        result.put("classname", component.getClassName());
    }// ww w .  j  a v  a  2  s.co m
    result.put("flags", data.getFlags());
    return result;
}

From source file:com.android.launcher3.QsbContainerView.java

/**
 * Returns a widget with category {@link AppWidgetProviderInfo#WIDGET_CATEGORY_SEARCHBOX}
 * provided by the same package which is set to be global search activity.
 * If widgetCategory is not supported, or no such widget is found, returns the first widget
 * provided by the package./*from  ww w.  j av  a  2  s . c om*/
 */
public static AppWidgetProviderInfo getSearchWidgetProvider(Context context) {
    SearchManager searchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
    ComponentName searchComponent = searchManager.getGlobalSearchActivity();
    if (searchComponent == null)
        return null;
    String providerPkg = searchComponent.getPackageName();

    AppWidgetProviderInfo defaultWidgetForSearchPackage = null;

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    for (AppWidgetProviderInfo info : appWidgetManager.getInstalledProviders()) {
        if (info.provider.getPackageName().equals(providerPkg) && info.configure == null) {
            if ((info.widgetCategory & AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX) != 0) {
                return info;
            } else if (defaultWidgetForSearchPackage == null) {
                defaultWidgetForSearchPackage = info;
            }
        }
    }
    return defaultWidgetForSearchPackage;
}

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. c  o  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:com.adguard.android.commons.BrowserUtils.java

public static void openYandexBlockingOptions(Context context) {
    Intent intent = new Intent();
    intent.setAction(YANDEX_CONTENT_BLOCKER_ACTION);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    if (list.size() > 0) {
        context.startActivity(intent);/*ww w .  j  a  v  a 2  s  . c o  m*/
        return;
    }

    // For samsung-type action in Yandex browser
    intent.setAction(SAMSUNG_CONTENT_BLOCKER_ACTION);
    list = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    if (list.size() > 0) {

        ComponentName componentName = getYandexBrowser(context, SAMSUNG_CONTENT_BLOCKER_ACTION);
        if (componentName != null) {
            intent.setClassName(componentName.getPackageName(), componentName.getClassName());
        }

        context.startActivity(intent);
    }
}

From source file:com.android.browser.DownloadHandler.java

/**
 * Notify the host application a download should be done, or that
 * the data should be streamed if a streaming viewer is available.
 * @param activity Activity requesting the download.
 * @param url The full url to the content that should be downloaded
 * @param userAgent User agent of the downloading application.
 * @param contentDisposition Content-disposition http header, if present.
 * @param mimetype The mimetype of the content reported by the server
 * @param referer The referer associated with the downloaded url
 * @param privateBrowsing If the request is coming from a private browsing tab.
 *///from  w  w w .j a v  a2 s .  c  o m
public static void onDownloadStart(Activity activity, String url, String userAgent, String contentDisposition,
        String mimetype, String referer, boolean privateBrowsing) {
    // if we're dealing wih A/V content that's not explicitly marked
    //     for download, check if it's streamable.
    if (contentDisposition == null || !contentDisposition.regionMatches(true, 0, "attachment", 0, 10)) {
        // query the package manager to see if there's a registered handler
        //     that matches.
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(url), mimetype);
        ResolveInfo info = activity.getPackageManager().resolveActivity(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        if (info != null) {
            ComponentName myName = activity.getComponentName();
            // If we resolved to ourselves, we don't want to attempt to
            // load the url only to try and download it again.
            if (!myName.getPackageName().equals(info.activityInfo.packageName)
                    || !myName.getClassName().equals(info.activityInfo.name)) {
                // someone (other than us) knows how to handle this mime
                // type with this scheme, don't download.
                try {
                    activity.startActivity(intent);
                    return;
                } catch (ActivityNotFoundException ex) {
                    if (LOGD_ENABLED) {
                        Log.d(LOGTAG,
                                "activity not found for " + mimetype + " over " + Uri.parse(url).getScheme(),
                                ex);
                    }
                    // Best behavior is to fall back to a download in this
                    // case
                }
            }
        }
    }
    onDownloadStartNoStream(activity, url, userAgent, contentDisposition, mimetype, referer, privateBrowsing);
}

From source file:net.reichholf.dreamdroid.DreamDroid.java

/**
 * @param context//w  w  w.  ja va2 s.c o m
 * @return
 */
public static String getVersionString(Context context) {
    try {
        ComponentName comp = new ComponentName(context, context.getClass());
        PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
        return String.format("dreamDroid %s (%s)\n Stephan Reichholf\nstephan@reichholf.net",
                pinfo.versionName, pinfo.versionCode);
    } catch (android.content.pm.PackageManager.NameNotFoundException e) {
        return "dreamDroid\n 2013 Stephan Reichholf\nstephan@reichholf.net";
    }
}

From source file:me.calebjones.spacelaunchnow.utils.Utils.java

public static String getVersionName(Context context) {
    try {// www .j  av a2 s .  c  om
        ComponentName comp = new ComponentName(context, context.getClass());
        return context.getPackageManager().getPackageInfo(comp.getPackageName(), 0).versionName;
    } catch (android.content.pm.PackageManager.NameNotFoundException e) {
        return "Unknown";
    }
}

From source file:me.calebjones.spacelaunchnow.utils.Utils.java

public static int getVersionCode(Context context) {
    try {/*  w w w  .  j  ava  2 s. c o  m*/
        ComponentName comp = new ComponentName(context, context.getClass());
        PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
        return pinfo.versionCode;
    } catch (android.content.pm.PackageManager.NameNotFoundException e) {
        return 0;
    }
}

From source file:bolts.MeasurementEvent.java

private static Bundle getApplinkLogData(Context context, String eventName, Bundle appLinkData,
        Intent applinkIntent) {//from w w  w.j  a  v a  2  s .com
    Bundle logData = new Bundle();
    ComponentName resolvedActivity = applinkIntent.resolveActivity(context.getPackageManager());

    if (resolvedActivity != null) {
        logData.putString("class", resolvedActivity.getShortClassName());
    }

    if (APP_LINK_NAVIGATE_OUT_EVENT_NAME.equals(eventName)) {
        if (resolvedActivity != null) {
            logData.putString("package", resolvedActivity.getPackageName());
        }
        if (applinkIntent.getData() != null) {
            logData.putString("outputURL", applinkIntent.getData().toString());
        }
        if (applinkIntent.getScheme() != null) {
            logData.putString("outputURLScheme", applinkIntent.getScheme());
        }
    } else if (APP_LINK_NAVIGATE_IN_EVENT_NAME.equals(eventName)) {
        if (applinkIntent.getData() != null) {
            logData.putString("inputURL", applinkIntent.getData().toString());
        }
        if (applinkIntent.getScheme() != null) {
            logData.putString("inputURLScheme", applinkIntent.getScheme());
        }
    }

    for (String key : appLinkData.keySet()) {
        Object o = appLinkData.get(key);
        if (o instanceof Bundle) {
            for (String subKey : ((Bundle) o).keySet()) {
                String logValue = objectToJSONString(((Bundle) o).get(subKey));
                if (key.equals("referer_app_link")) {
                    if (subKey.equalsIgnoreCase("url")) {
                        logData.putString("refererURL", logValue);
                        continue;
                    } else if (subKey.equalsIgnoreCase("app_name")) {
                        logData.putString("refererAppName", logValue);
                        continue;
                    } else if (subKey.equalsIgnoreCase("package")) {
                        logData.putString("sourceApplication", logValue);
                        continue;
                    }
                }
                logData.putString(key + "/" + subKey, logValue);
            }
        } else {
            String logValue = objectToJSONString(o);
            if (key.equals("target_url")) {
                Uri targetURI = Uri.parse(logValue);
                logData.putString("targetURL", targetURI.toString());
                logData.putString("targetURLHost", targetURI.getHost());
                continue;
            }
            logData.putString(key, logValue);
        }
    }
    return logData;
}