Java Code Examples for android.content.pm.PackageManager#getApplicationInfo()

The following examples show how to use android.content.pm.PackageManager#getApplicationInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UpdateService.java    From sealrtc-android with MIT License 6 votes vote down vote up
public String getApplicationName() {
    PackageManager packageManager = null;
    ApplicationInfo applicationInfo = null;
    try {
        packageManager = getApplicationContext().getPackageManager();
        applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        applicationInfo = null;
    }
    String applicationName = "";
    if (packageManager != null) {
        applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
    }

    return applicationName;
}
 
Example 2
Source File: ChildConnectionAllocator.java    From 365browser with Apache License 2.0 6 votes vote down vote up
static int getNumberOfServices(
        Context context, String packageName, String numChildServicesManifestKey) {
    int numServices = -1;
    try {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo appInfo =
                packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        if (appInfo.metaData != null) {
            numServices = appInfo.metaData.getInt(numChildServicesManifestKey, -1);
        }
    } catch (PackageManager.NameNotFoundException e) {
        throw new RuntimeException("Could not get application info", e);
    }
    if (numServices < 0) {
        throw new RuntimeException("Illegal meta data value for number of child services");
    }
    return numServices;
}
 
Example 3
Source File: RuntimeUtil.java    From quickhybrid-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 获取manifest中配置的metaData信息
 * <meta-data
 * android:name="key"
 * android:value="value" />
 *
 * @param ctx
 * @param key
 * @return
 */
public static String getMetaData(Context ctx, String key) {
    String packageName = ctx.getPackageName();
    PackageManager packageManager = ctx.getPackageManager();
    Bundle bd;
    String value = "";
    try {
        ApplicationInfo info = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        bd = info.metaData;//获取metaData标签内容
        if (bd != null) {
            Object keyO = bd.get(key);
            if (keyO != null) {
                value = keyO.toString();//这里获取的就是value值
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return value;
}
 
Example 4
Source File: ZenModeConfig.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the name of the app associated with owner
 */
public static String getOwnerCaption(Context context, String owner) {
    final PackageManager pm = context.getPackageManager();
    try {
        final ApplicationInfo info = pm.getApplicationInfo(owner, 0);
        if (info != null) {
            final CharSequence seq = info.loadLabel(pm);
            if (seq != null) {
                final String str = seq.toString().trim();
                if (str.length() > 0) {
                    return str;
                }
            }
        }
    } catch (Throwable e) {
        Slog.w(TAG, "Error loading owner caption", e);
    }
    return "";
}
 
Example 5
Source File: TrafficUtils.java    From YCWebView with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前应用uid
 *
 * @return
 */
public int getUid(Context context) {
    try {
        PackageManager pm = context.getPackageManager();
        //修改
        ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
        return ai.uid;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return -1;
}
 
Example 6
Source File: AppLovinUtils.java    From googleads-mobile-android-mediation with Apache License 2.0 5 votes vote down vote up
private static Bundle retrieveMetadata(Context context) {
  try {
    final PackageManager pm = context.getPackageManager();
    final ApplicationInfo ai =
        pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);

    return ai.metaData;
  } catch (PackageManager.NameNotFoundException ignored) {
  }

  return null;
}
 
Example 7
Source File: ManifestHelper.java    From QuantumFlux with Apache License 2.0 5 votes vote down vote up
private static Boolean getMetaDataBoolean(Context context, String name) {
    Boolean value = false;

    PackageManager pm = context.getPackageManager();
    try {
        ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
        value = ai.metaData.getBoolean(name);
    } catch (Exception e) {
        QuantumFluxLog.d("Couldn't find config value: " + name);
    }

    return value;
}
 
Example 8
Source File: Helper.java    From Learning-Resources with MIT License 5 votes vote down vote up
/**
 * @param ctx The Android application context.
 * @return Application name
 */
public static String getAppName(Context ctx) {
    PackageManager packageManager = ctx.getPackageManager();
    ApplicationInfo applicationInfo = null;
    try {
        applicationInfo = packageManager
                .getApplicationInfo(ctx.getApplicationInfo().packageName, 0);
    } catch (final NameNotFoundException ignored) {
    }
    return (String) (applicationInfo != null
            ? packageManager.getApplicationLabel(applicationInfo) : "Unknown");
}
 
Example 9
Source File: PoiKeywordSearchActivity.java    From TraceByAmap with MIT License 5 votes vote down vote up
/**
 * 获取当前app的应用名字
 */
public String getApplicationName() {
	PackageManager packageManager = null;
	ApplicationInfo applicationInfo = null;
	try {
		packageManager = getApplicationContext().getPackageManager();
		applicationInfo = packageManager.getApplicationInfo(
				getPackageName(), 0);
	} catch (PackageManager.NameNotFoundException e) {
		applicationInfo = null;
	}
	String applicationName = (String) packageManager
			.getApplicationLabel(applicationInfo);
	return applicationName;
}
 
Example 10
Source File: Utils.java    From AndroidModulePattern with Apache License 2.0 5 votes vote down vote up
/**
 * 判断App是否是Debug版本
 *
 * @return {@code true}: 是<br>{@code false}: 否
 */
public static boolean isAppDebug() {
    if (StringUtils.isSpace(context.getPackageName())) return false;
    try {
        PackageManager pm = context.getPackageManager();
        ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), 0);
        return ai != null && (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 11
Source File: CleanerService.java    From android-cache-cleaner with MIT License 5 votes vote down vote up
private long addPackage(List<AppsListItem> apps, PackageStats pStats, boolean succeeded) {
    long cacheSize = 0;

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        cacheSize += pStats.cacheSize;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        cacheSize += pStats.externalCacheSize;
    }

    if (!succeeded || cacheSize <= 0) {
        return 0;
    }

    try {
        PackageManager packageManager = getPackageManager();
        ApplicationInfo info = packageManager.getApplicationInfo(pStats.packageName,
                PackageManager.GET_META_DATA);

        apps.add(new AppsListItem(pStats.packageName,
                packageManager.getApplicationLabel(info).toString(),
                packageManager.getApplicationIcon(pStats.packageName),
                cacheSize));
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    return cacheSize;
}
 
Example 12
Source File: NDKHelper.java    From connectivity-samples with Apache License 2.0 5 votes vote down vote up
public String getApplicationName() {
    final PackageManager pm = activity.getPackageManager();
    ApplicationInfo ai;
    try {
        ai = pm.getApplicationInfo(activity.getPackageName(), 0);
    } catch (final NameNotFoundException e) {
        ai = null;
    }
    String applicationName = (String) (ai != null ? pm
            .getApplicationLabel(ai) : "(unknown)");        
    return applicationName;
}
 
Example 13
Source File: AlertWindowNotification.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private ApplicationInfo getApplicationInfo(PackageManager pm, String packageName) {
    try {
        return pm.getApplicationInfo(packageName, 0);
    } catch (PackageManager.NameNotFoundException e) {
        return null;
    }
}
 
Example 14
Source File: SoftwareInformation.java    From mobile-messaging-sdk-android with Apache License 2.0 5 votes vote down vote up
public static String getAppName(Context context) {
    if (appName != null) {
        return appName;
    }

    try {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        appName = packageManager.getApplicationLabel(applicationInfo).toString();
    } catch (Exception e) {
        MobileMessagingLogger.d(Log.getStackTraceString(e));
    }
    return appName;
}
 
Example 15
Source File: ManifestHelper.java    From ApkTrack with GNU General Public License v3.0 5 votes vote down vote up
private static String getMetaDataString(Context context, String name) {
    String value = null;

    PackageManager pm = context.getPackageManager();
    try {
        ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(),
                PackageManager.GET_META_DATA);
        value = ai.metaData.getString(name);
    } catch (Exception e) {
        Log.d("sugar", "Couldn't find config value: " + name);
    }

    return value;
}
 
Example 16
Source File: AppSecurityPermissions.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onClick(View v) {
    if (group != null && perm != null) {
        if (dialog != null) {
            dialog.dismiss();
        }
        PackageManager pm = getContext().getPackageManager();
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle(group.label);
        if (perm.descriptionRes != 0) {
            builder.setMessage(perm.loadDescription(pm));
        } else {
            CharSequence appName;
            try {
                ApplicationInfo app = pm.getApplicationInfo(perm.packageName, 0);
                appName = app.loadLabel(pm);
            } catch (NameNotFoundException e) {
                appName = perm.packageName;
            }
            builder.setMessage(getContext().getString(
                    R.string.perms_description_app, appName) + "\n\n" + perm.name);
        }
        builder.setCancelable(true);
        builder.setIcon(group.loadGroupIcon(getContext(), pm));
        dialog = builder.show();
        dialog.setCanceledOnTouchOutside(true);
    }
}
 
Example 17
Source File: IconPackResourcesProvider.java    From GeometricWeather with GNU Lesser General Public License v3.0 4 votes vote down vote up
IconPackResourcesProvider(@NonNull Context c, @NonNull String pkgName,
                          @NonNull ResourceProvider defaultProvider) {
    this.defaultProvider = defaultProvider;

    try {
        context = c.createPackageContext(
                pkgName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);

        PackageManager manager = context.getPackageManager();
        ApplicationInfo info = manager.getApplicationInfo(pkgName, PackageManager.GET_META_DATA);
        providerName = manager.getApplicationLabel(info).toString();

        iconDrawable = context.getApplicationInfo().loadIcon(context.getPackageManager());

        Resources res = context.getResources();

        int resId = getMetaDataResource(Constants.META_DATA_PROVIDER_CONFIG);
        if (resId != 0) {
            config = XmlHelper.getConfig(res.getXml(resId));
        } else {
            config = new Config();
        }

        resId = getMetaDataResource(Constants.META_DATA_DRAWABLE_FILTER);
        if (resId != 0) {
            drawableFilter = XmlHelper.getFilterMap(res.getXml(resId));
        } else {
            drawableFilter = new HashMap<>();
        }

        resId = getMetaDataResource(Constants.META_DATA_ANIMATOR_FILTER);
        if (resId != 0) {
            animatorFilter = XmlHelper.getFilterMap(res.getXml(resId));
        } else {
            animatorFilter = new HashMap<>();
        }

        resId = getMetaDataResource(Constants.META_DATA_SHORTCUT_FILTER);
        if (resId != 0) {
            shortcutFilter = XmlHelper.getFilterMap(res.getXml(resId));
        } else {
            shortcutFilter = new HashMap<>();
        }

        resId = getMetaDataResource(Constants.META_DATA_SUN_MOON_FILTER);
        if (resId != 0) {
            sunMoonFilter = XmlHelper.getFilterMap(res.getXml(resId));
        } else {
            sunMoonFilter = new HashMap<>();
        }
    } catch (Exception e) {
        buildDefaultInstance(c);
    }
}
 
Example 18
Source File: CustomTabsHelper.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Goes through all apps that handle VIEW intents and have a warmup service. Picks
 * the one chosen by the user if there is one, otherwise makes a best effort to return a
 * valid package name.
 *
 * This is <strong>not</strong> threadsafe.
 *
 * @param context {@link Context} to use for accessing {@link PackageManager}.
 * @return The package name recommended to use for connecting to custom tabs related components.
 */
public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null) return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
        defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
        sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!TextUtils.isEmpty(defaultViewHandlerPackageName)
            && !hasSpecializedHandlerIntents(context, activityIntent)
            && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
        sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        sPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
        sPackageNameToUse = LOCAL_PACKAGE;
    }
    try {
        if ("com.sec.android.app.sbrowser".equalsIgnoreCase(sPackageNameToUse)) {
            pm = ApplicationLoader.applicationContext.getPackageManager();
            ApplicationInfo applicationInfo = pm.getApplicationInfo("com.android.chrome", 0);
            if (applicationInfo != null && applicationInfo.enabled) {
                PackageInfo packageInfo = pm.getPackageInfo("com.android.chrome", PackageManager.GET_ACTIVITIES);
                sPackageNameToUse = "com.android.chrome";
            }
        }
    } catch (Throwable ignore) {

    }
    return sPackageNameToUse;
}
 
Example 19
Source File: EventPreferencesNotification.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
String getPreferencesDescription(boolean addBullet, boolean addPassStatus, Context context)
{
    String descr = "";

    if (!this._enabled) {
        if (!addBullet)
            descr = context.getString(R.string.event_preference_sensor_notification_summary);
    } else {
        if (Event.isEventPreferenceAllowed(PREF_EVENT_NOTIFICATION_ENABLED, context).allowed == PreferenceAllowed.PREFERENCE_ALLOWED) {
            if (addBullet) {
                descr = descr + "<b>";
                descr = descr + getPassStatusString(context.getString(R.string.event_type_notifications), addPassStatus, DatabaseHandler.ETYPE_NOTIFICATION, context);
                descr = descr + "</b> ";
            }

            if (!PPNotificationListenerService.isNotificationListenerServiceEnabled(context)) {
                descr = descr + "* " + context.getString(R.string.event_preferences_notificationsAccessSettings_disabled_summary) + "! *";
            } else {
                //descr = descr + context.getString(R.string.event_preferences_notificationsAccessSettings_enabled_summary) + "<br>";

                if (this._inCall) {
                    descr = descr + "<b>" + context.getString(R.string.event_preferences_notifications_inCall) + "</b>";
                }
                if (this._missedCall) {
                    if (this._inCall)
                        descr = descr + " • ";
                    descr = descr + "<b>" +context.getString(R.string.event_preferences_notifications_missedCall) + "</b>";
                }
                String selectedApplications = context.getString(R.string.applications_multiselect_summary_text_not_selected);
                if (!this._applications.isEmpty() && !this._applications.equals("-")) {
                    String[] splits = this._applications.split("\\|");
                    if (splits.length == 1) {
                        String packageName = Application.getPackageName(splits[0]);
                        String activityName = Application.getActivityName(splits[0]);
                        PackageManager packageManager = context.getPackageManager();
                        if (activityName.isEmpty()) {
                            ApplicationInfo app;
                            try {
                                app = packageManager.getApplicationInfo(packageName, 0);
                                if (app != null)
                                    selectedApplications = packageManager.getApplicationLabel(app).toString();
                            } catch (Exception e) {
                                selectedApplications = context.getString(R.string.applications_multiselect_summary_text_selected) + ": " + splits.length;
                            }
                        } else {
                            Intent intent = new Intent();
                            intent.setClassName(packageName, activityName);
                            ActivityInfo info = intent.resolveActivityInfo(packageManager, 0);
                            if (info != null)
                                selectedApplications = info.loadLabel(packageManager).toString();
                        }
                    } else
                        selectedApplications = context.getString(R.string.applications_multiselect_summary_text_selected) + ": " + splits.length;
                }
                if (this._inCall || this._missedCall)
                    descr = descr + " • ";
                descr = descr + /*"(S) "+*/context.getString(R.string.event_preferences_notifications_applications) + ": <b>" + selectedApplications + "</b>";

                if (this._checkContacts) {
                    descr = descr + " • ";
                    descr = descr + "<b>" + context.getString(R.string.event_preferences_notifications_checkContacts) + "</b>: ";

                    descr = descr + context.getString(R.string.event_preferences_notifications_contact_groups) + ": ";
                    descr = descr + "<b>" + ContactGroupsMultiSelectDialogPreferenceX.getSummary(_contactGroups, context) + "</b> • ";

                    descr = descr + context.getString(R.string.event_preferences_notifications_contacts) + ": ";
                    descr = descr + "<b>" + ContactsMultiSelectDialogPreferenceX.getSummary(_contacts, true, context) + "</b> • ";

                    descr = descr + context.getString(R.string.event_preferences_contactListType) + ": ";
                    String[] contactListTypes = context.getResources().getStringArray(R.array.eventNotificationContactListTypeArray);
                    descr = descr + "<b>" + contactListTypes[this._contactListType] + "</b>";
                }
                if (this._checkText) {
                    descr = descr + " • ";
                    descr = descr + "<b>" + context.getString(R.string.event_preferences_notifications_checkText) + "</b>: ";

                    descr = descr + context.getString(R.string.event_preferences_notifications_text) + ": ";
                    descr = descr + "<b>" + _text + "</b>";
                }
                descr = descr + " • ";
                descr = descr + context.getString(R.string.pref_event_duration) + ": <b>" + GlobalGUIRoutines.getDurationString(this._duration) + "</b>";
            }
        }
    }

    return descr;
}
 
Example 20
Source File: FinalizeActivity.java    From android-testdpc with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState == null) {
        if (Util.isManagedProfileOwner(this)) {
            ProvisioningUtil.enableProfile(this);
        }
    }
    setContentView(R.layout.finalize_activity);
    mSetupWizardLayout = findViewById(R.id.setup_wizard_layout);
    mFinishButton = mSetupWizardLayout.findViewById(R.id.suw_navbar_next);
    mFinishButton.setText(R.string.finish_button);
    mFinishButton.setOnClickListener(this::onNavigateNext);


    // This is just a user friendly shortcut to the policy management screen of this app.
    ImageView appIcon = findViewById(R.id.app_icon);
    TextView appLabel = findViewById(R.id.app_label);
    try {
        PackageManager packageManager = getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(
                getPackageName(), 0 /* Default flags */);
        appIcon.setImageDrawable(packageManager.getApplicationIcon(applicationInfo));
        appLabel.setText(packageManager.getApplicationLabel(applicationInfo));
    } catch (PackageManager.NameNotFoundException e) {
        Log.w("TestDPC", "Couldn't look up our own package?!?!", e);
    }

    // Show the user which account now has management, if specified.
    final String addedAccount = getAddedAccountName();
    if (addedAccount != null) {
        View accountMigrationStatusLayout;
        if (isAccountMigrated(addedAccount)) {
            accountMigrationStatusLayout = findViewById(R.id.account_migration_success);
        } else {
            accountMigrationStatusLayout = findViewById(R.id.account_migration_fail);
        }
        accountMigrationStatusLayout.setVisibility(View.VISIBLE);
        TextView managedAccountName = (TextView) accountMigrationStatusLayout.findViewById(
                R.id.managed_account_name);
        managedAccountName.setText(addedAccount);
    }

    ((TextView) findViewById(R.id.explanation)).setText(Util.isDeviceOwner(this)
            ? R.string.all_done_explanation_device_owner
            : R.string.all_done_explanation_profile_owner);
}