Example usage for android.content.pm PackageManager getApplicationInfo

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

Introduction

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

Prototype

public abstract ApplicationInfo getApplicationInfo(String packageName, @ApplicationInfoFlags int flags)
        throws NameNotFoundException;

Source Link

Document

Retrieve all of the information we know about a particular package/application.

Usage

From source file:de.arcus.framework.activities.CrashActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crash);

    // Reads the crash information
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        if (bundle.containsKey(EXTRA_FLAG_CRASH_MESSAGE))
            mCrashMessage = bundle.getString(EXTRA_FLAG_CRASH_MESSAGE);
        if (bundle.containsKey(EXTRA_FLAG_CRASH_LOG))
            mCrashLog = bundle.getString(EXTRA_FLAG_CRASH_LOG);
    } else {/*from  www.j  av a  2  s  .  c  om*/
        // No information; close activity
        finish();
        return;
    }

    try {
        // Get the PackageManager to load information about the app
        PackageManager packageManager = getPackageManager();
        // Loads the ApplicationInfo with meta data
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(),
                PackageManager.GET_META_DATA);

        if (applicationInfo.metaData != null) {
            // Reads the crash handler settings from meta data
            if (applicationInfo.metaData.containsKey("crashhandler.email"))
                mMetaDataEmail = applicationInfo.metaData.getString("crashhandler.email");
            if (applicationInfo.metaData.containsKey("crashhandler.supporturl"))
                mMetaDataSupportURL = applicationInfo.metaData.getString("crashhandler.supporturl");
        }

        // Gets the app name
        mAppName = packageManager.getApplicationLabel(applicationInfo).toString();
        // Gets the launch intent for the restart
        mLaunchIntent = packageManager.getLaunchIntentForPackage(getPackageName());

    } catch (PackageManager.NameNotFoundException ex) {
        // If this occurs then god must be already dead
        Logger.getInstance().logError("CrashHandler", ex.toString());
    }

    // Set the action bar title
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setTitle(getString(R.string.crashhandler_app_has_stopped_working, mAppName));
    }

    // Set the message
    TextView textViewMessage = (TextView) findViewById(R.id.text_view_crash_message);
    if (textViewMessage != null) {
        textViewMessage.setText(mCrashLog);
    }
}

From source file:me.spadival.podmode.PodNotifyService.java

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {

    String notifyPackage = (String) event.getPackageName();

    if (!event.getClassName().equals(NOTIFICATION_CLASS))
        return;//from   w  w w.  ja v  a  2s  . c o m

    if (notifyPackage.equals(SYSTEMUI_PACKAGE) || notifyPackage.equals(THIS_PACKAGE)
            || notifyPackage.equals(ANDROID_PACKAGE))
        return;

    PackageManager pm = getPackageManager();

    String notifyAppName = null;

    try {
        notifyAppName = (String) pm.getApplicationLabel(pm.getApplicationInfo(notifyPackage, 0));
    } catch (NameNotFoundException e1) {
        e1.printStackTrace();
    }

    if (notifyAppName == null)
        return;

    if (notifyPackage.equals(GMAPS_PACKAGE))
        notifyAppName = getString(R.string.nav_appname);

    if (notifyPackage.equals(GNOW_PACKAGE))
        notifyAppName = "Google Now";

    List<CharSequence> textList = event.getText();

    String notifyText = "";

    if (textList.size() > 0)
        notifyText = textList.get(0).toString();

    if (notifyText.equals("") || notifyPackage.equals(GMAIL_PACKAGE)) {
        Notification eventNotification = (Notification) event.getParcelableData();

        RemoteViews notifyView = eventNotification.contentView;
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        ViewGroup localView = null;
        try {
            localView = (ViewGroup) inflater.inflate(notifyView.getLayoutId(), null);
        } catch (Exception e) {
            //      e.printStackTrace();
            return;
        }

        try {

            notifyView.reapply(getApplicationContext(), localView);
        } catch (NotFoundException e) {
            //         e.printStackTrace();
        }

        View tv = localView.findViewById(android.R.id.title);
        if (tv != null && tv instanceof TextView) {
            if (notifyPackage.equals(GNOW_PACKAGE) || notifyPackage.equals(PANDORA_PACKAGE))
                notifyText = ((TextView) tv).getText().toString();
            else
                notifyAppName += ": " + ((TextView) tv).getText().toString();
        }

        if (!notifyPackage.equals(GNOW_PACKAGE)) {

            tv = localView.findViewById(16908358);
            if (tv != null && tv instanceof TextView)
                if (notifyPackage.equals(PANDORA_PACKAGE))
                    notifyAppName += ": " + ((TextView) tv).getText().toString();
                else
                    notifyText = (String) ((TextView) tv).getText().toString();
        }

        if (notifyPackage.equals(GMAIL_PACKAGE)) {
            tv = localView.findViewById(android.R.id.text2);
            if (tv != null && tv instanceof TextView)
                notifyText = (String) ((TextView) tv).getText().toString();
        }
    }

    Intent localIntent = new Intent(PodModeService.NOTIFYACTION);
    localIntent.putExtra("package", notifyPackage);
    localIntent.putExtra("appname", notifyAppName);
    localIntent.putExtra("text", notifyText);

    LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);

}

From source file:org.peercast.core.PeerCastServiceController.java

/**
 * PeerCast for Android??????//from www.  j a v a 2 s. c  om
 * 
 * @return "org.peercast.core" ?????true
 */
public boolean isInstalled() {
    PackageManager pm = context.getPackageManager();
    try {
        pm.getApplicationInfo(PKG_PEERCAST, 0);
        return true;
    } catch (NameNotFoundException e) {
        return false;
    }
}

From source file:org.openmrs.mobile.activities.settings.SettingsFragment.java

@Override
public void addBuildVersionInfo() {
    String versionName = "";
    int buildVersion = 0;

    PackageManager packageManager = this.getActivity().getPackageManager();
    String packageName = this.getActivity().getPackageName();

    try {//from   w  ww.j a  va  2s .com
        versionName = packageManager.getPackageInfo(packageName, 0).versionName;
        ApplicationInfo ai = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        buildVersion = ai.metaData.getInt("buildVersion");
    } catch (PackageManager.NameNotFoundException e) {
        mPresenter.logException("Failed to load meta-data, NameNotFound: " + e.getMessage());
    } catch (NullPointerException e) {
        mPresenter.logException("Failed to load meta-data, NullPointer: " + e.getMessage());
    }

    mListItem.add(new SettingsListItemDTO(getResources().getString(R.string.settings_about),
            getResources().getString(R.string.app_name), versionName + " Build: " + buildVersion));
}

From source file:org.microg.gms.ui.GcmFragment.java

private void updateContent() {
    PreferenceScreen root = getPreferenceScreen();

    if (McsService.isConnected()) {
        root.findPreference(PREF_GCM_STATUS).setSummary(getString(R.string.gcm_state_connected, DateUtils
                .getRelativeTimeSpanString(McsService.getStartTimestamp(), System.currentTimeMillis(), 0)));
    } else {// w w  w.ja  v  a  2  s.  c  o  m
        root.findPreference(PREF_GCM_STATUS).setSummary(getString(R.string.gcm_state_disconnected));
    }

    PreferenceCategory appList = (PreferenceCategory) root.findPreference(PREF_GCM_APPS);
    appList.removeAll();
    List<GcmDatabase.App> list = database.getAppList();
    if (!list.isEmpty()) {
        List<Preference> appListPrefs = new ArrayList<>();
        PackageManager pm = getContext().getPackageManager();
        for (GcmDatabase.App app : list) {
            try {
                pm.getApplicationInfo(app.packageName, 0);
                appListPrefs.add(new GcmAppPreference(getPreferenceManager().getContext(), app));
            } catch (PackageManager.NameNotFoundException e) {
                final List<GcmDatabase.Registration> registrations = database
                        .getRegistrationsByApp(app.packageName);
                if (registrations.isEmpty()) {
                    database.removeApp(app.packageName);
                } else {
                    appListPrefs.add(new GcmAppPreference(getPreferenceManager().getContext(), app));
                }
            }
        }
        addPreferencesSorted(appListPrefs, appList);
    } else {
        // If there's no item to display, add a "None" item.
        Preference banner = new Preference(getPreferenceManager().getContext());
        banner.setLayoutResource(R.layout.list_no_item);
        banner.setTitle(R.string.list_no_item_none);
        banner.setSelectable(false);
        appList.addPreference(banner);
    }
}

From source file:com.jtechme.apphub.FDroidApp.java

public void sendViaBluetooth(Activity activity, int resultCode, String packageName) {
    if (resultCode == Activity.RESULT_CANCELED)
        return;/*from  w  w w. ja v  a2  s.c o m*/
    String bluetoothPackageName = null;
    String className = null;
    boolean found = false;
    Intent sendBt = null;
    try {
        PackageManager pm = getPackageManager();
        ApplicationInfo appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        sendBt = new Intent(Intent.ACTION_SEND);
        // The APK type is blocked by stock Android, so use zip
        // sendBt.setType("application/vnd.android.package-archive");
        sendBt.setType("application/zip");
        sendBt.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + appInfo.publicSourceDir));
        // not all devices have the same Bluetooth Activities, so
        // let's find it
        for (ResolveInfo info : pm.queryIntentActivities(sendBt, 0)) {
            bluetoothPackageName = info.activityInfo.packageName;
            if ("com.android.bluetooth".equals(bluetoothPackageName)
                    || "com.mediatek.bluetooth".equals(bluetoothPackageName)) {
                className = info.activityInfo.name;
                found = true;
                break;
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "Could not get application info to send via bluetooth", e);
        found = false;
    }
    if (sendBt != null) {
        if (found) {
            sendBt.setClassName(bluetoothPackageName, className);
            activity.startActivity(sendBt);
        } else {
            Toast.makeText(this, R.string.bluetooth_activity_not_found, Toast.LENGTH_SHORT).show();
            activity.startActivity(Intent.createChooser(sendBt, getString(R.string.choose_bt_send)));
        }
    }
}

From source file:com.mobilesolutionworks.android.http.WorksHttpClient.java

/**
 * Constructor of http client//from  w w w.  ja  v a2 s. c  o m
 *
 * @param context application context
 */
protected WorksHttpClient(Context context) {
    mContext = context.getApplicationContext();
    String name = mContext.getPackageName();

    try {
        PackageManager pm = mContext.getPackageManager();
        if (pm != null) {
            ApplicationInfo ai = pm.getApplicationInfo(mContext.getPackageName(), 128);
            if ((ai != null) && (ai.metaData != null)) {
                name = ai.metaData.getString("user_agent");
            }

            PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), 0);
            if (pi != null) {
                name = name + " ver-" + pi.versionName + " build-" + pi.versionCode;
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
    }

    mName = name;
}

From source file:org.sufficientlysecure.keychain.service.remote.AppSettingsFragment.java

private void setPackage(String packageName) {
    PackageManager pm = getActivity().getApplicationContext().getPackageManager();

    // get application name and icon from package manager
    String appName = null;/*from w w w .j av  a2  s.  c  o m*/
    Drawable appIcon = null;
    try {
        ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);

        appName = (String) pm.getApplicationLabel(ai);
        appIcon = pm.getApplicationIcon(ai);
    } catch (final NameNotFoundException e) {
        // fallback
        appName = packageName;
    }
    mAppNameView.setText(appName);
    mAppIconView.setImageDrawable(appIcon);
}

From source file:com.example.android.apprestrictionenforcer.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_real);
    if (null == savedInstanceState) {
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(
                Context.DEVICE_POLICY_SERVICE);
        PackageManager packageManager = getPackageManager();
        if (!devicePolicyManager.isProfileOwnerApp(getApplicationContext().getPackageName())) {
            // If the managed profile is not yet set up, we show the setup screen.
            showSetupProfile();/*w  ww .ja va  2 s .c o  m*/
        } else {
            try {
                ApplicationInfo info = packageManager.getApplicationInfo(
                        Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA, PackageManager.GET_UNINSTALLED_PACKAGES);
                if (0 == (info.flags & ApplicationInfo.FLAG_INSTALLED)) {
                    // Need to reinstall the sample app
                    showStatusProfile();
                } else if (devicePolicyManager.isApplicationHidden(
                        EnforcerDeviceAdminReceiver.getComponentName(this),
                        Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA)) {
                    // The app is installed but hidden in this profile
                    showStatusProfile();
                } else {
                    // Everything is clear; show the main screen
                    showMainFragment();
                }
            } catch (PackageManager.NameNotFoundException e) {
                showStatusProfile();
            }
        }
    }
}

From source file:org.fdroid.fdroid.FDroidApp.java

public void sendViaBluetooth(Activity activity, int resultCode, String packageName) {
    if (resultCode == Activity.RESULT_CANCELED) {
        return;/*from   w w  w .  j  av a 2s.c om*/
    }
    String bluetoothPackageName = null;
    String className = null;
    boolean found = false;
    Intent sendBt = null;
    try {
        PackageManager pm = getPackageManager();
        ApplicationInfo appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        sendBt = new Intent(Intent.ACTION_SEND);
        // The APK type is blocked by stock Android, so use zip
        // sendBt.setType("application/vnd.android.package-archive");
        sendBt.setType("application/zip");
        sendBt.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + appInfo.publicSourceDir));
        // not all devices have the same Bluetooth Activities, so
        // let's find it
        for (ResolveInfo info : pm.queryIntentActivities(sendBt, 0)) {
            bluetoothPackageName = info.activityInfo.packageName;
            if ("com.android.bluetooth".equals(bluetoothPackageName)
                    || "com.mediatek.bluetooth".equals(bluetoothPackageName)) {
                className = info.activityInfo.name;
                found = true;
                break;
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "Could not get application info to send via bluetooth", e);
        found = false;
    }
    if (sendBt != null) {
        if (found) {
            sendBt.setClassName(bluetoothPackageName, className);
            activity.startActivity(sendBt);
        } else {
            Toast.makeText(this, R.string.bluetooth_activity_not_found, Toast.LENGTH_SHORT).show();
            activity.startActivity(Intent.createChooser(sendBt, getString(R.string.choose_bt_send)));
        }
    }
}