Example usage for android.content.pm PackageManager getPackageInfo

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

Introduction

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

Prototype

public abstract PackageInfo getPackageInfo(VersionedPackage versionedPackage, @PackageInfoFlags int flags)
        throws NameNotFoundException;

Source Link

Document

Retrieve overall information about an application package that is installed on the system.

Usage

From source file:com.atwal.wakeup.battery.util.Utilities.java

public static PackageInfo getPackageInfo(Context context, String packageName) {
    PackageInfo packageInfo = null;//from w  w w. j  a va  2  s  .c  o m
    PackageManager pm = context.getPackageManager();
    try {
        packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
    } catch (NameNotFoundException e) {
    }
    return packageInfo;
}

From source file:com.google.android.gms.common.GooglePlayServicesUtil.java

public static int isGooglePlayServicesAvailable(Context context) {
    PackageManager packageManager = context.getPackageManager();
    try {//from  w w w.j  a  v  a2s .co  m
        context.getResources().getString(C0192R.string.common_google_play_services_unknown_issue);
    } catch (Throwable th) {
        Log.e("GooglePlayServicesUtil",
                "The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.");
    }
    if (System.currentTimeMillis() < 1227312000288L) {
        return 12;
    }
    m118t(context);
    try {
        PackageInfo packageInfo = packageManager.getPackageInfo(GOOGLE_PLAY_SERVICES_PACKAGE, 64);
        if (!gk.m1034y(context)) {
            try {
                if (m107a(packageManager.getPackageInfo(GOOGLE_PLAY_STORE_PACKAGE, 64), Aj) == null) {
                    Log.w("GooglePlayServicesUtil", "Google Play Store signature invalid.");
                    return 9;
                }
                if (m107a(packageInfo,
                        m107a(packageManager.getPackageInfo(GOOGLE_PLAY_STORE_PACKAGE, 64), Aj)) == null) {
                    Log.w("GooglePlayServicesUtil", "Google Play services signature invalid.");
                    return 9;
                }
            } catch (NameNotFoundException e) {
                Log.w("GooglePlayServicesUtil", "Google Play Store is missing.");
                return 9;
            }
        } else if (m107a(packageInfo, Aj) == null) {
            Log.w("GooglePlayServicesUtil", "Google Play services signature invalid.");
            return 9;
        }
        if (packageInfo.versionCode < GOOGLE_PLAY_SERVICES_VERSION_CODE) {
            Log.w("GooglePlayServicesUtil",
                    "Google Play services out of date.  Requires 4452000 but found " + packageInfo.versionCode);
            return 2;
        }
        try {
            return !packageManager.getApplicationInfo(GOOGLE_PLAY_SERVICES_PACKAGE, 0).enabled ? 3 : 0;
        } catch (NameNotFoundException e2) {
            Log.wtf("GooglePlayServicesUtil", "Google Play services missing when getting application info.");
            e2.printStackTrace();
            return 1;
        }
    } catch (NameNotFoundException e3) {
        Log.w("GooglePlayServicesUtil", "Google Play services is missing.");
        return 1;
    }
}

From source file:com.afreire.plugins.video.VideoPlayer.java

private boolean isYouTubeInstalled() {
    PackageManager pm = this.cordova.getActivity().getPackageManager();
    try {/*from  w w w .ja va2 s  . co  m*/
        pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

From source file:ch.liquidconcept.cordova.appversion.AppVersion.java

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext)
        throws JSONException {
    PluginResult result = new PluginResult(PluginResult.Status.INVALID_ACTION);
    PackageManager packageManager = cordova.getActivity().getPackageManager();

    if (action.equals("name")) {
        try {// ww  w  .ja v a  2  s  .  c  om
            PackageInfo packageInfo = packageManager
                    .getPackageInfo(cordova.getActivity().getApplicationContext().getPackageName(), 0);
            result = new PluginResult(PluginResult.Status.OK, packageInfo.versionName);
        } catch (PackageManager.NameNotFoundException exception) {
            result = new PluginResult(PluginResult.Status.ERROR, exception.getMessage());
        }
    }

    callbackContext.sendPluginResult(result);
    return true;
}

From source file:com.cleanwiz.applock.service.AppUpdateService.java

public String getApplicationVersion() {
    PackageManager packageManager = context.getPackageManager();
    PackageInfo packageInfo;/*from   w ww  .  j av a 2  s  . c o m*/
    try {
        packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionName;
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return "";
}

From source file:bg.tudle.mtbtimer.ui.MTBMainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SENDTO);
    intent.setType("text/plain");
    switch (item.getItemId()) {
    case R.id.menu_feedback:
        intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.send_from_my_android));
        intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name));
        intent.setData(Uri.parse(getString(R.string.mailto_dimitarniknikolov_gmail_com)));
        break;/*www. ja  va2s  . c o  m*/
    case R.id.menu_share:
        intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.download_apk_link_) + DOWNLOAD_APP_LINK
                + getString(R.string.send_from_my_android));
        intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.checkout_mtbtimer_for_android));
        intent.setData(Uri.parse("mailto:"));
        break;
    case R.id.menu_about:
        try {
            PackageManager manager = getPackageManager();
            PackageInfo info = manager.getPackageInfo(getPackageName(), 0);
            String aboutMsg = getString(R.string.versioncode_) + info.versionCode
                    + getString(R.string._versionname_) + info.versionName;
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(R.string.app_name).setMessage(aboutMsg).setNeutralButton(R.string.ok, null).show();
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return true;
    default:
        break;
    }
    startActivity(intent);
    return super.onOptionsItemSelected(item);
}

From source file:com.zokin.common.Application.java

/**
 * //  www .j  a v  a  2s .c om
 * @return 
 */
public String getVersion() {
    try {
        PackageManager manager = this.getPackageManager();
        PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
        String version = info.versionName;
        return version;
    } catch (Exception e) {
        e.printStackTrace();
        return "error";
    }
}

From source file:ly.count.android.api.DeviceInfoTests.java

public void testGetAppVersion_pkgManagerThrows() throws PackageManager.NameNotFoundException {
    final String fakePkgName = "i.like.chicken";
    final PackageManager mockPkgMgr = mock(PackageManager.class);
    when(mockPkgMgr.getPackageInfo(fakePkgName, 0)).thenThrow(new PackageManager.NameNotFoundException());
    final Context mockContext = mock(Context.class);
    when(mockContext.getPackageName()).thenReturn(fakePkgName);
    when(mockContext.getPackageManager()).thenReturn(mockPkgMgr);
    assertEquals("1.0", DeviceInfo.getAppVersion(mockContext));
}

From source file:ly.count.android.api.DeviceInfoTests.java

public void testGetAppVersion() throws PackageManager.NameNotFoundException {
    final PackageInfo pkgInfo = new PackageInfo();
    pkgInfo.versionName = "42.0";
    final String fakePkgName = "i.like.chicken";
    final PackageManager mockPkgMgr = mock(PackageManager.class);
    when(mockPkgMgr.getPackageInfo(fakePkgName, 0)).thenReturn(pkgInfo);
    final Context mockContext = mock(Context.class);
    when(mockContext.getPackageName()).thenReturn(fakePkgName);
    when(mockContext.getPackageManager()).thenReturn(mockPkgMgr);
    assertEquals("42.0", DeviceInfo.getAppVersion(mockContext));
}

From source file:com.apptentive.android.sdk.Apptentive.java

private static void init(Activity activity) {

    ///*  w w w  .j a v a2 s.com*/
    // First, initialize data relies on synchronous reads from local resources.
    //

    final Context appContext = activity.getApplicationContext();

    if (!GlobalInfo.initialized) {
        SharedPreferences prefs = appContext.getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE);

        // First, Get the api key, and figure out if app is debuggable.
        GlobalInfo.isAppDebuggable = false;
        String apiKey = null;
        boolean apptentiveDebug = false;
        String logLevelOverride = null;
        try {
            ApplicationInfo ai = appContext.getPackageManager().getApplicationInfo(appContext.getPackageName(),
                    PackageManager.GET_META_DATA);
            Bundle metaData = ai.metaData;
            if (metaData != null) {
                apiKey = metaData.getString(Constants.MANIFEST_KEY_APPTENTIVE_API_KEY);
                logLevelOverride = metaData.getString(Constants.MANIFEST_KEY_APPTENTIVE_LOG_LEVEL);
                apptentiveDebug = metaData.getBoolean(Constants.MANIFEST_KEY_APPTENTIVE_DEBUG);
                ApptentiveClient.useStagingServer = metaData
                        .getBoolean(Constants.MANIFEST_KEY_USE_STAGING_SERVER);
            }
            if (apptentiveDebug) {
                Log.i("Apptentive debug logging set to VERBOSE.");
                ApptentiveInternal.setMinimumLogLevel(Log.Level.VERBOSE);
            } else if (logLevelOverride != null) {
                Log.i("Overriding log level: %s", logLevelOverride);
                ApptentiveInternal.setMinimumLogLevel(Log.Level.parse(logLevelOverride));
            } else {
                GlobalInfo.isAppDebuggable = (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
                if (GlobalInfo.isAppDebuggable) {
                    ApptentiveInternal.setMinimumLogLevel(Log.Level.VERBOSE);
                }
            }
        } catch (Exception e) {
            Log.e("Unexpected error while reading application info.", e);
        }

        Log.i("Debug mode enabled? %b", GlobalInfo.isAppDebuggable);

        // If we are in debug mode, but no api key is found, throw an exception. Otherwise, just assert log. We don't want to crash a production app.
        String errorString = "No Apptentive api key specified. Please make sure you have specified your api key in your AndroidManifest.xml";
        if ((Util.isEmpty(apiKey))) {
            if (GlobalInfo.isAppDebuggable) {
                AlertDialog alertDialog = new AlertDialog.Builder(activity).setTitle("Error")
                        .setMessage(errorString).setPositiveButton("OK", null).create();
                alertDialog.setCanceledOnTouchOutside(false);
                alertDialog.show();
            }
            Log.e(errorString);
        }
        GlobalInfo.apiKey = apiKey;

        Log.i("API Key: %s", GlobalInfo.apiKey);

        // Grab app info we need to access later on.
        GlobalInfo.appPackage = appContext.getPackageName();
        GlobalInfo.androidId = Settings.Secure.getString(appContext.getContentResolver(),
                android.provider.Settings.Secure.ANDROID_ID);

        // Check the host app version, and notify modules if it's changed.
        try {
            PackageManager packageManager = appContext.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(appContext.getPackageName(), 0);

            Integer currentVersionCode = packageInfo.versionCode;
            String currentVersionName = packageInfo.versionName;
            VersionHistoryStore.VersionHistoryEntry lastVersionEntrySeen = VersionHistoryStore
                    .getLastVersionSeen(appContext);
            if (lastVersionEntrySeen == null) {
                onVersionChanged(appContext, null, currentVersionCode, null, currentVersionName);
            } else {
                if (!currentVersionCode.equals(lastVersionEntrySeen.versionCode)
                        || !currentVersionName.equals(lastVersionEntrySeen.versionName)) {
                    onVersionChanged(appContext, lastVersionEntrySeen.versionCode, currentVersionCode,
                            lastVersionEntrySeen.versionName, currentVersionName);
                }
            }

            GlobalInfo.appDisplayName = packageManager
                    .getApplicationLabel(packageManager.getApplicationInfo(packageInfo.packageName, 0))
                    .toString();
        } catch (PackageManager.NameNotFoundException e) {
            // Nothing we can do then.
            GlobalInfo.appDisplayName = "this app";
        }

        // Grab the conversation token from shared preferences.
        if (prefs.contains(Constants.PREF_KEY_CONVERSATION_TOKEN)
                && prefs.contains(Constants.PREF_KEY_PERSON_ID)) {
            GlobalInfo.conversationToken = prefs.getString(Constants.PREF_KEY_CONVERSATION_TOKEN, null);
            GlobalInfo.personId = prefs.getString(Constants.PREF_KEY_PERSON_ID, null);
        }

        GlobalInfo.initialized = true;
        Log.v("Done initializing...");
    } else {
        Log.v("Already initialized...");
    }

    // Initialize the Conversation Token, or fetch if needed. Fetch config it the token is available.
    if (GlobalInfo.conversationToken == null || GlobalInfo.personId == null) {
        asyncFetchConversationToken(appContext.getApplicationContext());
    } else {
        asyncFetchAppConfiguration(appContext.getApplicationContext());
        InteractionManager.asyncFetchAndStoreInteractions(appContext.getApplicationContext());
    }

    // TODO: Do this on a dedicated thread if it takes too long. Some devices are slow to read device data.
    syncDevice(appContext);
    syncSdk(appContext);
    syncPerson(appContext);

    Log.d("Default Locale: %s", Locale.getDefault().toString());
    SharedPreferences prefs = appContext.getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE);
    Log.d("Conversation id: %s", prefs.getString(Constants.PREF_KEY_CONVERSATION_ID, "null"));
}