Java Code Examples for android.content.Context#getPackageManager()

The following examples show how to use android.content.Context#getPackageManager() . 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: CondomContext.java    From MiPushFramework with GNU General Public License v3.0 6 votes vote down vote up
private CondomContext(final CondomCore condom, final @Nullable Context app_context, final @Nullable @Size(max=16) String tag) {
	super(condom.mBase);
	final Context base = condom.mBase;
	mCondom = condom;
	mApplicationContext = app_context != null ? app_context : this;
	mBaseContext = new Lazy<Context>() { @Override protected Context create() {
		return new PseudoContextImpl(CondomContext.this);
	}};
	mPackageManager = new Lazy<PackageManager>() { @Override protected PackageManager create() {
		return new CondomPackageManager(base.getPackageManager());
	}};
	mContentResolver = new Lazy<ContentResolver>() { @Override protected ContentResolver create() {
		return new CondomContentResolver(base, base.getContentResolver());
	}};
	final List<CondomKit> kits = condom.mKits;
	if (kits != null && ! kits.isEmpty()) {
		mKitManager = new KitManager();
		for (final CondomKit kit : kits)
			kit.onRegister(mKitManager);
	} else mKitManager = null;
	TAG = CondomCore.buildLogTag("Condom", "Condom.", tag);
}
 
Example 2
Source File: Utils.java    From KUtils-master with Apache License 2.0 6 votes vote down vote up
/**
 * 获取系统中所有的应用
 *
 * @param context 上下文
 * @return 应用信息List
 */
public static List<PackageInfo> getAllApps(Context context) {

    List<PackageInfo> apps = new ArrayList<PackageInfo>();
    PackageManager pManager = context.getPackageManager();
    List<PackageInfo> paklist = pManager.getInstalledPackages(0);
    for (int i = 0; i < paklist.size(); i++) {
        PackageInfo pak = paklist.get(i);
        if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <=
                0) {
            // customs applications
            apps.add(pak);
        }
    }
    return apps;
}
 
Example 3
Source File: Util.java    From android-perftracking with MIT License 6 votes vote down vote up
/**
 * Check if the application is debuggable.
 *
 * <p>This method check if the application is signed by a debug key.
 * https://stackoverflow.com/questions/7085644/how-to-check-if-apk-is-signed-or-debug-build
 *
 * @param context application context
 * @return true if app is debuggable, false otherwise
 */
@RestrictTo(LIBRARY)
@VisibleForTesting(otherwise = PACKAGE_PRIVATE)
static boolean isAppDebuggable(@NonNull final Context context) {
  try {
    PackageManager packageManager = context.getPackageManager();
    PackageInfo packageInfo =
        packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
    Signature[] signatures = packageInfo.signatures;

    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");

    for (Signature signature : signatures) {
      ByteArrayInputStream stream = new ByteArrayInputStream(signature.toByteArray());
      X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(stream);
      String principal = cert.getSubjectX500Principal().toString().toUpperCase();
      return principal.contains("C=US")
          && principal.contains("O=ANDROID")
          && principal.contains("CN=ANDROID DEBUG");
    }
  } catch (Exception e) {
    // Things went south, anyway the app is not debuggable.
  }
  return false;
}
 
Example 4
Source File: AutoErrorReporter.java    From AutoCrashReporter with Apache License 2.0 6 votes vote down vote up
private void recordInformations(Context context) {
	try {
		PackageManager pm = context.getPackageManager();
		PackageInfo pi;
		// Version
		pi = pm.getPackageInfo(context.getPackageName(), 0);
		versionName = pi.versionName;
		//buildNumber = currentVersionNumber(context);
		// Package name
		packageName = pi.packageName;

		// Device model
		phoneModel = Build.MODEL;
		// Android version
		androidVersion = Build.VERSION.RELEASE;

		board = Build.BOARD;
		brand = Build.BRAND;
		device = Build.DEVICE;
		display = Build.DISPLAY;
		fingerPrint = Build.FINGERPRINT;
		host = Build.HOST;
		id = Build.ID;
		model = Build.MODEL;
		product = Build.PRODUCT;
		manufacturer = Build.MANUFACTURER;
		tags = Build.TAGS;
		time = Build.TIME;
		type = Build.TYPE;
		user = Build.USER;

	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 5
Source File: Tools.java    From android-tv-launcher with MIT License 5 votes vote down vote up
public static PackageInfo getAPKVersionInfo(Context context,
		String packageName) {
	PackageManager packageManager = context.getPackageManager();
	PackageInfo packInfo = null;
	try {
		packInfo = packageManager.getPackageInfo(packageName, 0);
	} catch (PackageManager.NameNotFoundException e) {
		e.printStackTrace();
	}
	return packInfo;
}
 
Example 6
Source File: SuntimesCalculatorDescriptor.java    From SuntimesWidget with GNU General Public License v3.0 5 votes vote down vote up
public static void initCalculators(Context context)
{
    SuntimesCalculatorDescriptor.addValue(com.forrestguice.suntimeswidget.calculator.sunrisesunset_java.SunriseSunsetSuntimesCalculator.getDescriptor());
    SuntimesCalculatorDescriptor.addValue(com.forrestguice.suntimeswidget.calculator.ca.rmen.sunrisesunset.SunriseSunsetSuntimesCalculator.getDescriptor());
    SuntimesCalculatorDescriptor.addValue(com.forrestguice.suntimeswidget.calculator.time4a.Time4ASimpleSuntimesCalculator.getDescriptor());
    SuntimesCalculatorDescriptor.addValue(com.forrestguice.suntimeswidget.calculator.time4a.Time4ANOAASuntimesCalculator.getDescriptor());
    SuntimesCalculatorDescriptor.addValue(com.forrestguice.suntimeswidget.calculator.time4a.Time4ACCSuntimesCalculator.getDescriptor());
    SuntimesCalculatorDescriptor.addValue(com.forrestguice.suntimeswidget.calculator.time4a.Time4A4JSuntimesCalculator.getDescriptor());

    boolean scanForPlugins = AppSettings.loadScanForPluginsPref(context);
    if (scanForPlugins)
    {
        PackageManager packageManager = context.getPackageManager();
        Intent packageQuery = new Intent(Intent.ACTION_RUN);    // get a list of installed plugins
        packageQuery.addCategory(CATEGORY_SUNTIMES_CALCULATOR);
        List<ResolveInfo> packages = packageManager.queryIntentActivities(packageQuery, PackageManager.GET_META_DATA);
        Log.i(LOGTAG, "Scanning for calculator plugins... found " + packages.size());

        for (ResolveInfo packageInfo : packages)
        {
            if (packageInfo.activityInfo != null
                    && packageInfo.activityInfo.metaData != null)
            {
                String calculatorName = packageInfo.activityInfo.metaData.getString(KEY_NAME);
                String calculatorDisplayString = packageInfo.activityInfo.metaData.getString(KEY_DISPLAYSTRING);
                String calculatorDisplayReference = packageInfo.activityInfo.metaData.getString(KEY_REFERENCE);
                int[] calculatorFeatures = parseFlags(packageInfo.activityInfo.metaData.getString(KEY_FEATURES));

                SuntimesCalculatorDescriptor descriptor = new SuntimesCalculatorDescriptor(calculatorName, calculatorDisplayString, calculatorDisplayReference, -1, calculatorFeatures);
                descriptor.setIsPlugin(true);
                SuntimesCalculatorDescriptor.addValue(descriptor);
                Log.i(LOGTAG, "..initialized calculator plugin: " + descriptor.toString());
            }
        }
    }

    initialized = true;
    //Log.d("CalculatorFactory", "Initialized suntimes calculator list.");
}
 
Example 7
Source File: Constants.java    From RememBirthday with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the current package version.
 *
 * @return The current version.
 */
public static String getVersion(Context context) {
    String result;
    try {
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);

        result = String.format("%s (%s)", info.versionName, info.versionCode);
    } catch (PackageManager.NameNotFoundException e) {
        Log.w(Utility.class.getSimpleName(), "Unable to getAutoSmsById application version", e);
        result = "Unable to getAutoSmsById application version.";
    }

    return result;
}
 
Example 8
Source File: AppUtil.java    From GithubApp with Apache License 2.0 5 votes vote down vote up
public static String getVersionName(Context context) {
    String versionName = "1.0";

    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionName;
    }
    catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    return versionName;
}
 
Example 9
Source File: BaseQQShareHandler.java    From BiliShare with Apache License 2.0 5 votes vote down vote up
private boolean isMobileQQSupportShare(Context context) {
    PackageManager packageManager = context.getPackageManager();
    PackageInfo packageInfo = null;
    try {
        packageInfo = packageManager.getPackageInfo("com.tencent.mobileqq", 0);
        return compareVersion(packageInfo.versionName, "4.1") >= 0;
    } catch (PackageManager.NameNotFoundException var4) {
        return false;
    }
}
 
Example 10
Source File: Log.java    From COCOFramework with Apache License 2.0 5 votes vote down vote up
private static boolean isActivityAvailable(Context context, String className) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent();
    final String packageName = context.getApplicationInfo().packageName;
    intent.setClassName(packageName, className);
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}
 
Example 11
Source File: BaseUtil.java    From MyBlogDemo with Apache License 2.0 5 votes vote down vote up
private static PackageInfo getPackageInfo(Context context) {
    PackageInfo pi = null;

    try {
        PackageManager pm = context.getPackageManager();
        pi = pm.getPackageInfo(context.getPackageName(),
                PackageManager.GET_CONFIGURATIONS);

        return pi;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return pi;
}
 
Example 12
Source File: ManifestHelper.java    From QuantumFlux with Apache License 2.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) {
        QuantumFluxLog.d("Couldn't find config value: " + name);
    }

    return value;
}
 
Example 13
Source File: Common.java    From Pixiv-Shaft with MIT License 5 votes vote down vote up
/**
 * 返回当前程序版本名
 */
public static String getAppVersionName(Context context) {
    String versionName=null;
    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
        versionName = pi.versionName;
    } catch (Exception e) {
        Log.e("VersionInfo", "Exception", e);
    }
    return versionName;
}
 
Example 14
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 15
Source File: IconPackHelper.java    From Hangar with GNU General Public License v3.0 5 votes vote down vote up
public static Map<String, IconPackInfo> getPickerPackages(Context context) {
    Intent i = new Intent();
    Map<String, IconPackInfo> packages = new HashMap<String, IconPackInfo>();
    PackageManager packageManager = context.getPackageManager();
    i.setAction(Settings.ACTION_ADW_PICK_ICON);
    for (ResolveInfo r : packageManager.queryIntentActivities(i, 0)) {
        IconPackInfo info = new IconPackInfo(r, packageManager);
        packages.put(r.activityInfo.packageName, info);
    }
    return packages;
}
 
Example 16
Source File: ColorUtils.java    From Status with Apache License 2.0 5 votes vote down vote up
private static List<Integer> getPrimaryColors(Context context, ComponentName componentName) {
    List<Integer> colors = new ArrayList<>();

    PackageManager packageManager = context.getPackageManager();

    ActivityInfo activityInfo = null;
    PackageInfo packageInfo = null;
    Resources resources = null, activityResources = null;
    try {
        packageInfo = packageManager.getPackageInfo(componentName.getPackageName(), PackageManager.GET_META_DATA);
        resources = packageManager.getResourcesForApplication(packageInfo.applicationInfo);
        activityInfo = packageManager.getActivityInfo(componentName, 0);
        activityResources = packageManager.getResourcesForActivity(componentName);
    } catch (PackageManager.NameNotFoundException ignored) {
    }

    if (packageInfo != null && resources != null) {
        if (activityInfo != null && activityResources != null) {
            colors.addAll(getStatusBarColors(activityInfo.packageName, resources, activityInfo.theme));
        }

        colors.addAll(getStatusBarColors(packageInfo.packageName, resources, packageInfo.applicationInfo.theme));

        if (packageInfo.activities != null) {
            for (ActivityInfo otherActivityInfo : packageInfo.activities) {
                colors.addAll(getStatusBarColors(packageInfo.packageName, resources, otherActivityInfo.theme));
            }
        }
    }

    return colors;
}
 
Example 17
Source File: RecognitionServiceManager.java    From speechutils with Apache License 2.0 5 votes vote down vote up
public static ServiceInfo getServiceInfo(Context context, ComponentName recognizerComponentName) {
    try {
        PackageManager pm = context.getPackageManager();
        return pm.getServiceInfo(recognizerComponentName, PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        // ignored
    }
    return null;
}
 
Example 18
Source File: SkiaImageRegionDecoder.java    From BlogDemo with Apache License 2.0 4 votes vote down vote up
@Override
@NonNull
public Point init(Context context, @NonNull Uri uri) throws Exception {
    String uriString = uri.toString();
    if (uriString.startsWith(RESOURCE_PREFIX)) {
        Resources res;
        String packageName = uri.getAuthority();
        if (context.getPackageName().equals(packageName)) {
            res = context.getResources();
        } else {
            PackageManager pm = context.getPackageManager();
            res = pm.getResourcesForApplication(packageName);
        }

        int id = 0;
        List<String> segments = uri.getPathSegments();
        int size = segments.size();
        if (size == 2 && segments.get(0).equals("drawable")) {
            String resName = segments.get(1);
            id = res.getIdentifier(resName, "drawable", packageName);
        } else if (size == 1 && TextUtils.isDigitsOnly(segments.get(0))) {
            try {
                id = Integer.parseInt(segments.get(0));
            } catch (NumberFormatException ignored) {
            }
        }

        decoder = BitmapRegionDecoder.newInstance(context.getResources().openRawResource(id), false);
    } else if (uriString.startsWith(ASSET_PREFIX)) {
        String assetName = uriString.substring(ASSET_PREFIX.length());
        decoder = BitmapRegionDecoder.newInstance(context.getAssets().open(assetName, AssetManager.ACCESS_RANDOM), false);
    } else if (uriString.startsWith(FILE_PREFIX)) {
        decoder = BitmapRegionDecoder.newInstance(uriString.substring(FILE_PREFIX.length()), false);
    } else {
        InputStream inputStream = null;
        try {
            ContentResolver contentResolver = context.getContentResolver();
            inputStream = contentResolver.openInputStream(uri);
            decoder = BitmapRegionDecoder.newInstance(inputStream, false);
        } finally {
            if (inputStream != null) {
                try { inputStream.close(); } catch (Exception e) { /* Ignore */ }
            }
        }
    }
    return new Point(decoder.getWidth(), decoder.getHeight());
}
 
Example 19
Source File: SettingsLauncherActivity.java    From android_packages_apps_UnifiedNlp with Apache License 2.0 4 votes vote down vote up
public static void setLauncherIconEnabled(Context context, boolean enabled) {
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(new ComponentName(context, SettingsLauncherActivity.class),
            enabled ? COMPONENT_ENABLED_STATE_ENABLED : COMPONENT_ENABLED_STATE_DISABLED,
            DONT_KILL_APP);
}
 
Example 20
Source File: AppListFragment.java    From effective_android_sample with Apache License 2.0 4 votes vote down vote up
public CardListAdapter(Context context) {
    super(context, 0);
    mInflater = LayoutInflater.from(context);
    packageManager = context.getPackageManager();
}