Example usage for android.app Application getPackageName

List of usage examples for android.app Application getPackageName

Introduction

In this page you can find the example usage for android.app Application getPackageName.

Prototype

@Override
    public String getPackageName() 

Source Link

Usage

From source file:Main.java

public static final boolean isInstalledOnSDCard(Application app) {
    try {/*ww w.  j  a  v a  2  s . c  o  m*/
        String packageName = app.getPackageName();
        PackageInfo info = app.getPackageManager().getPackageInfo(packageName, 0);
        String dir = info.applicationInfo.sourceDir;

        for (String path : INTERNAL_PATH) {
            if (path.equals(dir.substring(0, path.length())))
                return false;
        }
    } catch (PackageManager.NameNotFoundException exp) {
        throw new IllegalArgumentException(exp);
    }
    return true;
}

From source file:Main.java

public static boolean isApkDebuggable(Application application) {
    PackageManager pm = application.getPackageManager();
    try {/*from   www . j  a  v  a  2  s  .c o  m*/
        return ((pm.getApplicationInfo(application.getPackageName(), 0).flags
                & ApplicationInfo.FLAG_DEBUGGABLE) > 0);
    } catch (NameNotFoundException e) {
        return false;
    }
}

From source file:Main.java

public static boolean isLauncherDefault(Application application) {
    final Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    final ResolveInfo res = application.getPackageManager().resolveActivity(intent, 0);
    if (res.activityInfo == null) {
        return false;
    } else if ("android".equals(res.activityInfo.packageName)) {
        return false;
    } else {/*from w w w .j  a  va 2  s .  c o  m*/
        if (res.activityInfo.packageName.equals(application.getPackageName()))
            return true;
        else
            return false;
    }
}

From source file:com.jeffthefate.stacktrace.ExceptionHandler.java

/**
 * Register handler for unhandled exceptions.
 * @param context/*from   w  w w.ja  va  2s.c o m*/
 */
public static boolean register(Application app) {
    mCallback = (OnStacktraceListener) app;
    Log.i(TAG, "Registering default exceptions handler");
    // Get information about the Package
    PackageManager pm = app.getPackageManager();
    try {
        PackageInfo pi;
        // Version
        pi = pm.getPackageInfo(app.getPackageName(), 0);
        G.APP_VERSION = pi.versionName;
        // Package name
        G.APP_PACKAGE = pi.packageName;
        // Files dir for storing the stack traces
        G.FILES_PATH = app.getFilesDir().getAbsolutePath();
        // Device model
        G.PHONE_MODEL = android.os.Build.MODEL;
        // Android version
        G.ANDROID_VERSION = android.os.Build.VERSION.RELEASE;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    Log.d(TAG, "APP_VERSION: " + G.APP_VERSION);
    Log.d(TAG, "APP_PACKAGE: " + G.APP_PACKAGE);
    Log.d(TAG, "FILES_PATH: " + G.FILES_PATH);

    boolean stackTracesFound = false;
    // We'll return true if any stack traces were found
    if (searchForStackTraces().length > 0)
        stackTracesFound = true;

    new Thread() {
        @Override
        public void run() {
            // First of all transmit any stack traces that may be lying around
            submitStackTraces();
            UncaughtExceptionHandler currentHandler = Thread.getDefaultUncaughtExceptionHandler();
            if (currentHandler != null) {
                Log.d(TAG, "current handler class=" + currentHandler.getClass().getName());
            }
            // don't register again if already registered
            if (!(currentHandler instanceof DefaultExceptionHandler)) {
                // Register default exceptions handler
                Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler(currentHandler));
            }
        }
    }.start();

    return stackTracesFound;
}

From source file:net.wequick.small.Small.java

public static void preSetUp(Application context) {
    sContext = context;//from  ww w  .ja v  a 2  s .c  o m

    // Register default bundle launchers
    registerLauncher(new ActivityLauncher());
    registerLauncher(new ApkBundleLauncher());
    registerLauncher(new WebBundleLauncher());

    PackageManager pm = context.getPackageManager();
    String packageName = context.getPackageName();

    // Check if host app is first-installed or upgraded
    int backupHostVersion = getHostVersionCode();
    int currHostVersion = 0;
    try {
        PackageInfo pi = pm.getPackageInfo(packageName, 0);
        currHostVersion = pi.versionCode;
    } catch (PackageManager.NameNotFoundException ignored) {
        // Never reach
    }

    if (backupHostVersion != currHostVersion) {
        sIsNewHostApp = true;
        setHostVersionCode(currHostVersion);
    } else {
        sIsNewHostApp = false;
    }

    // Collect host certificates
    try {
        Signature[] ss = pm.getPackageInfo(Small.getContext().getPackageName(),
                PackageManager.GET_SIGNATURES).signatures;
        if (ss != null) {
            int N = ss.length;
            sHostCertificates = new byte[N][];
            for (int i = 0; i < N; i++) {
                sHostCertificates[i] = ss[i].toByteArray();
            }
        }
    } catch (PackageManager.NameNotFoundException ignored) {

    }

    // Check if application is started after unexpected exit (killed in background etc.)
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ComponentName launchingComponent = am.getRunningTasks(1).get(0).topActivity;
    ComponentName launcherComponent = pm.getLaunchIntentForPackage(packageName).getComponent();
    if (!launchingComponent.equals(launcherComponent)) {
        // In this case, system launching the last restored activity instead of our launcher
        // activity. Call `setUp' synchronously to ensure `Small' available.
        setUp(context, null);
    }
}

From source file:de.fmaul.android.cmis.utils.StorageUtils.java

public static File getStorageFile(Application app, String repoId, String storageType, String itemId,
        String filename) throws StorageException {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        StringBuilder builder = new StringBuilder();
        builder.append(Environment.getExternalStorageDirectory());
        builder.append("/");
        builder.append("Android");
        builder.append("/");
        builder.append("data");
        builder.append("/");
        builder.append(app.getPackageName());
        builder.append("/");
        if (storageType != null) {
            builder.append("/");
            builder.append(storageType);
        }//from  ww  w  .ja  va 2s . c o m
        if (repoId != null) {
            builder.append("/");
            builder.append(repoId);
        }
        if (itemId != null) {
            builder.append("/");
            builder.append(itemId.replaceAll(":", "_"));
        }
        if (filename != null) {
            builder.append("/");
            builder.append(filename);
        }
        return new File(builder.toString());
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        throw new StorageException("Storage in Read Only Mode");
    } else {
        throw new StorageException("Storage is unavailable");
    }
}

From source file:com.outsystemscloud.andrevieira.secureDevice.java

private void checkDevice() {
    boolean _isDeviceRooted = isDeviceRooted();
    boolean _isPasscodeSet = doesDeviceHaveSecuritySetup(this.cordova.getActivity());

    if (_isDeviceRooted || !_isPasscodeSet) {
        // Remove View
        View v = this.view.getView();
        ViewGroup viewParent = (ViewGroup) v.getParent();
        viewParent.removeView(v);//from  www  .  j av  a  2 s.  co  m

        // Show message and quit
        Application app = cordova.getActivity().getApplication();
        String package_name = app.getPackageName();
        Resources resources = app.getResources();
        String message = resources.getString(resources.getIdentifier("message", "string", package_name));
        String label = resources.getString(resources.getIdentifier("label", "string", package_name));
        this.alert(message, label);
    }
}

From source file:org.acra.ErrorReporter.java

/**
 * Sets relevant ReportSenders to the ErrorReporter, replacing any
 * previously set ReportSender./*from  w  w w .  j  av  a2  s  . c  om*/
 */
public void setDefaultReportSenders() {
    ReportsCrashes conf = ACRA.getConfig();
    Application mApplication = ACRA.getApplication();
    removeAllReportSenders();

    // Try to send by mail. If a mailTo address is provided, do not add
    // other senders.
    if (!"".equals(conf.mailTo())) {
        ACRA.log.w(LOG_TAG,
                mApplication.getPackageName() + " reports will be sent by email (if accepted by user).");
        setReportSender(new EmailIntentSender(mApplication));
        return;
    }

    final PackageManagerWrapper pm = new PackageManagerWrapper(mApplication);
    if (!pm.hasPermission(permission.INTERNET)) {
        // NB If the PackageManager has died then this will erroneously log
        // the error that the App doesn't have Internet (even though it
        // does).
        // I think that is a small price to pay to ensure that ACRA doesn't
        // crash if the PackageManager has died.
        ACRA.log.e(LOG_TAG, mApplication.getPackageName() + " should be granted permission "
                + permission.INTERNET
                + " if you want your crash reports to be sent. If you don't want to add this permission to your application you can also enable sending reports by email. If this is your will then provide your email address in @ReportsCrashes(mailTo=\"your.account@domain.com\"");
        return;
    }

    // If formUri is set, instantiate a sender for a generic HTTP POST form
    // with default mapping.
    if (conf.formUri() != null && !"".equals(conf.formUri())) {
        setReportSender(new HttpSender(ACRA.getConfig().httpMethod(), ACRA.getConfig().reportType(), null));
    }
}