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:io.teak.sdk.AppConfiguration.java

public AppConfiguration(@NonNull Context context) {
    // Teak App Id
    {/* ww w.  j  a  va2 s. c o m*/
        this.appId = Helpers.getStringResourceByName(TEAK_APP_ID, context);
        if (this.appId == null) {
            throw new RuntimeException("Failed to find R.string." + TEAK_APP_ID);
        }
    }

    // Teak API Key
    {
        this.apiKey = Helpers.getStringResourceByName(TEAK_API_KEY, context);
        if (this.apiKey == null) {
            throw new RuntimeException("Failed to find R.string." + TEAK_API_KEY);
        }
    }

    // Push Sender Id
    {
        // TODO: Check ADM vs GCM
        this.pushSenderId = Helpers.getStringResourceByName(TEAK_GCM_SENDER_ID, context);
        if (this.pushSenderId == null && Teak.isDebug) {
            Log.d(LOG_TAG, "R.string." + TEAK_GCM_SENDER_ID + " not present, push notifications disabled.");
        }
    }

    // Package Id
    {
        this.bundleId = context.getPackageName();
        if (this.bundleId == null) {
            throw new RuntimeException("Failed to get Bundle Id.");
        }
    }

    PackageManager packageManager = context.getPackageManager();
    if (packageManager == null) {
        throw new RuntimeException("Unable to get Package Manager.");
    }

    // App Version
    {
        int tempAppVersion = 0;
        try {
            tempAppVersion = packageManager.getPackageInfo(this.bundleId, 0).versionCode;
        } catch (Exception e) {
            Log.e(LOG_TAG, "Error getting App Version: " + Log.getStackTraceString(e));
        } finally {
            this.appVersion = tempAppVersion;
        }
    }

    // Get the installer package
    {
        this.installerPackage = packageManager.getInstallerPackageName(this.bundleId);
        if (this.installerPackage == null) {
            Log.e(LOG_TAG, "Installer package (Store) is null, purchase tracking disabled.");
        }
    }

    // Tell the Raven service about the app id
    try {
        Intent intent = new Intent(context, RavenService.class);
        intent.putExtra("appId", this.appId);
        ComponentName componentName = context.startService(intent);
        if (componentName == null) {
            Log.e(LOG_TAG,
                    "Unable to communicate with exception reporting service. Please add:\n\t<service android:name=\"io.teak.sdk.service.RavenService\" android:process=\":teak.raven\" android:exported=\"false\"/>\nTo the <application> section of your AndroidManifest.xml");
        } else if (Teak.isDebug) {
            Log.d(LOG_TAG,
                    "Communication with exception reporting service established: " + componentName.toString());
        }
    } catch (Exception e) {
        Log.e(LOG_TAG,
                "Error calling startService for exception reporting service: " + Log.getStackTraceString(e));
    }
}