Example usage for android.content.pm PackageManager getInstallerPackageName

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

Introduction

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

Prototype

@Nullable
public abstract String getInstallerPackageName(String packageName);

Source Link

Document

Retrieve the package name of the application that installed a package.

Usage

From source file:Main.java

public static boolean isFromGooglePlay(PackageManager mPm, String packageName) {
    String installPM = mPm.getInstallerPackageName(packageName);
    if (installPM == null) {
        // Definitely not from Google Play
        return false;
    } else if (installPM.equals("com.google.android.feedback") || installPM.equals("com.android.vending")) {
        // Installed from the Google Play
        return true;
    }//  ww w  . j a v a2 s  .c  om
    return false;
}

From source file:Main.java

/**
 * Returns the package name of the application installer.
 *
 * @param context The instance of {@link android.content.Context}.
 * @return The package name of the application installer.
 *///from  w ww. j a v a2 s  . c o m
@Nullable
public static String getPackageInstaller(@NonNull final Context context) {
    final PackageManager packageManager = context.getPackageManager();
    return packageManager.getInstallerPackageName(context.getPackageName());
}

From source file:Main.java

/**
 * Check whether the application is installed through the Play Store.
 *
 * @param context Context//from  w w w .j  av  a2s.c  o m
 * @return true when the app is installed through the Play store, otherwise false
 */
public static boolean isInstalledThroughPlayStore(final Context context) {
    boolean playStoreInstalled = false;
    PackageManager packageManager = context.getPackageManager();
    final String packageInstallerName = packageManager.getInstallerPackageName(getPackageName(context));
    if ("com.android.vending".equals(packageInstallerName)) {
        // App is installed through the Play Store
        playStoreInstalled = true;
    }
    return playStoreInstalled;
}

From source file:Main.java

/**
 * Check whether the application is installed through the Play Store.
 *
 * @param context Context/*from   w  w  w .  j av  a 2s .c o  m*/
 * @return true when the app is installed through the Play store, otherwise false
 */
public static boolean isInstalledThroughPlayStore(final Context context) {
    final PackageManager packageManager = context.getPackageManager();
    final String packageInstallerName = packageManager.getInstallerPackageName(getPackageName(context));
    if ("com.android.vending".equals(packageInstallerName)) {
        // App is installed through the Play Store
        return true;
    }
    return false;
}

From source file:Main.java

/**
 * Returns the package name of the installer of the app with the specified package name
 *
 * @param context/*ww  w. ja v  a2s  .  c o m*/
 * @param packageName The package name of the app to check
 * @return The package name of the installer
 */
public static String getInstallerPackageName(Context context, String packageName) {
    PackageManager packageManager = context.getPackageManager();

    try {
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
        return packageManager.getInstallerPackageName(applicationInfo.packageName);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.oliversride.wordryo.UpdateCheckReceiver.java

public static void checkVersions(Context context, boolean fromUI) {
    JSONObject params = new JSONObject();
    PackageManager pm = context.getPackageManager();
    String packageName = context.getPackageName();
    String installer = pm.getInstallerPackageName(packageName);

    if ("com.google.android.feedback".equals(installer) || "com.android.vending".equals(installer)) {
        // Do nothing; it's a Market app
    } else {/*  ww w.ja va2s. c o m*/
        try {
            int versionCode = pm.getPackageInfo(packageName, 0).versionCode;

            JSONObject appParams = new JSONObject();

            appParams.put(k_NAME, packageName);
            appParams.put(k_AVERS, versionCode);
            appParams.put(k_GVERS, GitVersion.VERS);
            appParams.put(k_INSTALLER, installer);
            if (devOK(context)) {
                appParams.put(k_DEVOK, true);
            }
            params.put(k_APP, appParams);
            params.put(k_DEVID, XWPrefs.getDevID(context));
        } catch (PackageManager.NameNotFoundException nnfe) {
            DbgUtils.loge(nnfe);
        } catch (org.json.JSONException jse) {
            DbgUtils.loge(jse);
        }
    }
    JSONArray dictParams = new JSONArray();
    DictUtils.DictAndLoc[] dals = DictUtils.dictList(context);
    for (int ii = 0; ii < dals.length; ++ii) {
        DictUtils.DictAndLoc dal = dals[ii];
        switch (dal.loc) {
        // case DOWNLOAD:
        case EXTERNAL:
        case INTERNAL:
            dictParams.put(makeDictParams(context, dal, ii));
        }
    }
    if (0 < dictParams.length()) {
        try {
            params.put(k_DICTS, dictParams);
            params.put(k_DEVID, XWPrefs.getDevID(context));
        } catch (org.json.JSONException jse) {
            DbgUtils.loge(jse);
        }
    }

    if (0 < params.length()) {
        new UpdateQueryTask(context, params, fromUI, pm, packageName, dals).execute();
    }
}

From source file:biz.bokhorst.xprivacy.Util.java

public static boolean hasMarketLink(Context context, String packageName) {
    try {/* w  w  w .  ja v a2  s. c  o m*/
        PackageManager pm = context.getPackageManager();
        String installer = pm.getInstallerPackageName(packageName);
        if (installer != null)
            return installer.equals("com.android.vending") || installer.contains("google");
    } catch (Exception ex) {
        log(null, Log.WARN, ex.toString());
    }
    return false;
}

From source file:net.frakbot.FWeather.util.FeedbackService.java

/**
 * Determines if the app is installed from the Google Play Store.
 *
 * @return Returns true if the app is installed from the Google
 * Play Store, or false if it has been installed from other
 * sources (sideload, other app stores, etc)
 *///from  w  w w. j  av a2 s  .  c  om
private boolean isInstalledFromPlayStore() {
    PackageManager pm = getPackageManager();
    String installationSource = pm.getInstallerPackageName(getPackageName());
    return "com.android.vending".equals(installationSource)
            || "com.google.android.feedback".equals(installationSource); // This is for Titanium Backup compatibility
}

From source file:com.javielinux.utils.Utils.java

public static boolean isLite(Context context) {
    String pName = context.getString(R.string.package_verified_premium);
    boolean isLite = true;
    PackageManager packageManager = context.getPackageManager();
    try {//from w  w  w .  ja  v a2  s  .com
        String installPM = packageManager.getInstallerPackageName(pName);
        if (installPM == null) {
            isLite = true;
        } else if (installPM.equals("com.google.android.feedback")) {
            isLite = false;
        } else if (installPM.equals("com.android.vending")) {
            isLite = false;
        }
    } catch (IllegalArgumentException e) {
    }

    return isLite;
}

From source file:eu.faircode.netguard.ServiceJob.java

private static void submit(Rule rule, PersistableBundle bundle, Context context) {
    PackageManager pm = context.getPackageManager();
    JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

    // Get english application label
    String label = null;//from   ww  w. ja  v a2s  . c om
    try {
        Configuration config = new Configuration();
        config.setLocale(new Locale("en"));
        Resources res = pm.getResourcesForApplication(rule.info.packageName);
        res.updateConfiguration(config, res.getDisplayMetrics());
        label = res.getString(rule.info.applicationInfo.labelRes);
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        CharSequence cs = rule.info.applicationInfo.loadLabel(pm);
        if (cs != null)
            label = cs.toString();
    }

    // Add application data
    bundle.putInt("uid", rule.info.applicationInfo.uid);
    bundle.putString("package", rule.info.packageName);
    bundle.putInt("version_code", rule.info.versionCode);
    bundle.putString("version_name", rule.info.versionName);
    bundle.putString("label", label);
    bundle.putInt("system", rule.system ? 1 : 0);
    try {
        bundle.putString("installer", pm.getInstallerPackageName(rule.info.packageName));
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        bundle.putString("installer", null);
    }

    // Cancel overlapping jobs
    for (JobInfo pending : scheduler.getAllPendingJobs()) {
        String type = pending.getExtras().getString("type");
        if (type != null && type.equals(bundle.getString("type"))) {
            if (type.equals("rule")) {
                int uid = pending.getExtras().getInt("uid");
                if (uid == bundle.getInt("uid")) {
                    Log.i(TAG, "Canceling id=" + pending.getId());
                    scheduler.cancel(pending.getId());
                }
            } else if (type.equals("host")) {
                int uid = pending.getExtras().getInt("uid");
                int version = pending.getExtras().getInt("version");
                int protocol = pending.getExtras().getInt("protocol");
                String daddr = pending.getExtras().getString("daddr");
                int dport = pending.getExtras().getInt("dport");
                if (uid == bundle.getInt("uid") && version == bundle.getInt("version")
                        && protocol == bundle.getInt("protocol") && daddr != null
                        && daddr.equals(bundle.getString("daddr")) && dport == bundle.getInt("dport")) {
                    Log.i(TAG, "Canceling id=" + pending.getId());
                    scheduler.cancel(pending.getId());
                }
            }
        }
    }

    // Schedule job
    ComponentName serviceName = new ComponentName(context, ServiceJob.class);
    JobInfo job = new JobInfo.Builder(++id, serviceName).setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
            .setMinimumLatency(Util.isDebuggable(context) ? 10 * 1000 : 60 * 1000).setExtras(bundle)
            .setPersisted(true).build();
    if (scheduler.schedule(job) == JobScheduler.RESULT_SUCCESS)
        Log.i(TAG, "Scheduled job=" + job.getId() + " success");
    else
        Log.e(TAG, "Scheduled job=" + job.getId() + " failed");
}