android.content.pm.PackageInfo Java Examples

The following examples show how to use android.content.pm.PackageInfo. 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: PackageUtil.java    From Android-Skin with MIT License 7 votes vote down vote up
public static String getApkPackageName(Context context,String apkPath) {
    try {
        if(context==null)
            return null;
        PackageManager pm = context.getPackageManager();
        if(SkinStringUtils.isNull(apkPath)){
            pm = context.getPackageManager();
        }
        PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
        ApplicationInfo appInfo = null;
        if (info != null) {
            appInfo = info.applicationInfo;
            String packageName = appInfo.packageName;
            return packageName;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}
 
Example #2
Source File: VirtualApkCheckUtil.java    From EasyProtector with Apache License 2.0 7 votes vote down vote up
/**
 * 检测原始的包名,多开应用会hook处理getPackageName方法
 * 顺着这个思路,如果在应用列表里出现了同样的包,那么认为该应用被多开了
 *
 * @param context
 * @param callback
 * @return
 */
public boolean checkByOriginApkPackageName(Context context, VirtualCheckCallback callback) {
    try {
        if (context == null)
            throw new IllegalArgumentException("you have to set context first");
        int count = 0;
        String packageName = context.getPackageName();
        PackageManager pm = context.getPackageManager();
        List<PackageInfo> pkgs = pm.getInstalledPackages(0);
        for (PackageInfo info : pkgs) {
            if (packageName.equals(info.packageName)) {
                count++;
            }
        }
        if (count > 1 && callback != null) callback.findSuspect();
        return count > 1;
    } catch (Exception ignore) {
    }
    return false;
}
 
Example #3
Source File: CropImage.java    From timecat with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the app requests a specific permission in the manifest.
 *
 * @param permissionName the permission to check
 *
 * @return true - the permission in requested in manifest, false - not.
 */
public static boolean hasPermissionInManifest(@NonNull Context context, @NonNull String permissionName) {
    String packageName = context.getPackageName();
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
        final String[] declaredPermisisons = packageInfo.requestedPermissions;
        if (declaredPermisisons != null && declaredPermisisons.length > 0) {
            for (String p : declaredPermisisons) {
                if (p.equalsIgnoreCase(permissionName)) {
                    return true;
                }
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
    }
    return false;
}
 
Example #4
Source File: SaiExportedAppMeta2.java    From SAI with GNU General Public License v3.0 6 votes vote down vote up
public static SaiExportedAppMeta2 createForPackage(Context context, String pkg, long exportTimestamp) throws PackageManager.NameNotFoundException {
    PackageManager pm = context.getPackageManager();
    PackageInfo packageInfo = context.getPackageManager().getPackageInfo(pkg, 0);

    SaiExportedAppMeta2 appMeta = new SaiExportedAppMeta2();
    appMeta.mPackage = packageInfo.packageName;
    appMeta.mLabel = packageInfo.applicationInfo.loadLabel(pm).toString();
    appMeta.mVersionName = packageInfo.versionName;

    if (Utils.apiIsAtLeast(Build.VERSION_CODES.P)) {
        appMeta.mVersionCode = packageInfo.getLongVersionCode();
    } else {
        appMeta.mVersionCode = (long) packageInfo.versionCode;
    }

    appMeta.mExportTimestamp = exportTimestamp;

    if (Utils.apiIsAtLeast(Build.VERSION_CODES.N)) {
        appMeta.mMinSdk = (long) packageInfo.applicationInfo.minSdkVersion;
        appMeta.mTargetSdk = (long) packageInfo.applicationInfo.targetSdkVersion;
    }

    appMeta.mIsSplitApk = packageInfo.applicationInfo.splitPublicSourceDirs != null && packageInfo.applicationInfo.splitPublicSourceDirs.length > 0;

    return appMeta;
}
 
Example #5
Source File: Utils.java    From whiteboard with Apache License 2.0 6 votes vote down vote up
/**
 * @param context
 * @param settings
 *            �������ò���ʵ��
 */
public void saveSettings(Context context, PainterSettings settings)
{
	SharedPreferences sp = context.getSharedPreferences(Commons.SETTINGS_STORAGE, Context.MODE_PRIVATE);
	SharedPreferences.Editor editor = sp.edit();

	try
	{
		PackageInfo pack = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
		editor.putInt(context.getString(R.string.settings_version), pack.versionCode);
	} catch (NameNotFoundException e)
	{
	}

	editor.putInt(context.getString(R.string.settings_orientation), settings.getOrientation());
	editor.putString(context.getString(R.string.settings_last_picture), settings.getLastPicture());
	editor.putFloat(context.getString(R.string.settings_brush_size), settings.getPreset().currentSize);
	editor.putInt(context.getString(R.string.settings_brush_color), settings.getPreset().currentColor);
	editor.putInt(context.getString(R.string.settings_brush_blur_style), (settings.getPreset().currentBlurType != null) ? settings.getPreset().currentBlurType.ordinal() + 1 : 0);
	editor.putInt(context.getString(R.string.settings_brush_blur_radius), settings.getPreset().currentBlurRadius);
	editor.putBoolean(context.getString(R.string.settings_force_open_file), settings.isForceOpenFile());
	editor.commit();
}
 
Example #6
Source File: Splash.java    From palmsuda with Apache License 2.0 6 votes vote down vote up
public void getCurrentVersion() {
	try {
		PackageInfo info = this.getPackageManager().getPackageInfo(
				this.getPackageName(), 0);
		this.versionName = info.versionName;
		PalmSudaApp.versionName = this.versionName;

		Log.d(TAG, "Current version_name is " + versionName);

		TextView textVersion = (TextView) findViewById(R.id.textVersion);
		textVersion.setText(versionName);

	} catch (NameNotFoundException e) {
		e.printStackTrace();
	}
}
 
Example #7
Source File: RequestUserLogin.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
private void infoApp() {

        PackageInfo pInfo = null;
        try {
            pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        if (pInfo != null) {
            AppVersion = pInfo.versionName;
            AppBuildVersion = pInfo.versionCode;
        }
        Device = Build.BRAND;
        Language = Locale.getDefault().getDisplayLanguage();
    }
 
Example #8
Source File: DevicePluginManager.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
 * 指定されたパッケージの中にデバイスプラグインが存在するかチェックし追加する.
 * パッケージの中にデバイスプラグインがない場合には何もしない。
 *
 * @param packageName パッケージ名
 */
private void checkAndAddDevicePlugin(final String packageName) {
    if (packageName == null) {
        throw new IllegalArgumentException("packageName is null.");
    }
    PackageManager pkgMgr = mContext.getPackageManager();
    try {
        int flag = PackageManager.GET_SERVICES | PackageManager.GET_RECEIVERS | PackageManager.GET_PROVIDERS;
        PackageInfo pkg = pkgMgr.getPackageInfo(packageName, flag);
        mLogger.info("PluginManager: get package info: " + pkg);
        if (pkg != null) {
            List<DevicePlugin> plugins = getInstalledPluginsForPackage(pkgMgr, pkg);
            mLogger.info("PluginManager: installed plugins: size=" + plugins.size());
            for (DevicePlugin plugin : filterPlugin(plugins)) {
                addDevicePlugin(plugin);
            }
        }
    } catch (NameNotFoundException e) {
        // NOP.
    }
}
 
Example #9
Source File: AppUpgradeService.java    From AndroidBleManager with Apache License 2.0 6 votes vote down vote up
public boolean checkApkFile(String apkFilePath) {
    boolean result = false;
    try{
        PackageManager pManager = getPackageManager();
        PackageInfo pInfo = pManager.getPackageArchiveInfo(apkFilePath, PackageManager.GET_ACTIVITIES);
        if (pInfo == null) {
            result =  false;
        } else {
            result =  true;
        }
    } catch(Exception e) {
        result =  false;
        e.printStackTrace();
    }
    return result;
}
 
Example #10
Source File: PluginDescriptor.java    From Android-Plugin-Framework with MIT License 6 votes vote down vote up
public PackageInfo getPackageInfo(Integer flags) {
	if (packageInfoHashMap == null) {
		packageInfoHashMap = new HashMap<>();
	}
	PackageInfo packageInfo = packageInfoHashMap.get(flags);
	if (packageInfo == null) {
		packageInfo = FairyGlobal.getHostApplication().getPackageManager().getPackageArchiveInfo(getInstalledPath(), flags);
		if (packageInfo != null && packageInfo.applicationInfo != null) {
			packageInfo.applicationInfo.sourceDir = getInstalledPath();
			packageInfo.applicationInfo.publicSourceDir = getInstalledPath();
		}
		packageInfoHashMap.put(flags, packageInfo);
	}

	return packageInfo;
}
 
Example #11
Source File: MainActivity.java    From android-sholi with GNU General Public License v3.0 6 votes vote down vote up
private void displayAboutDialog() {
    View view = getLayoutInflater().inflate(R.layout.about_dlg, null, false);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    try {
        PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
        TextView text = (TextView) view.findViewById(R.id.version_text);
        text.setText(getString(R.string.dialog_about_version,
                info.versionName, info.versionCode));
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    builder.setIcon(android.R.drawable.ic_dialog_info)
            .setTitle(R.string.dialog_about_title)
            .setView(view)
            .setNeutralButton(android.R.string.ok, null)
            .create()
            .show();
}
 
Example #12
Source File: SkinManager.java    From AndroidChangeSkin with Apache License 2.0 6 votes vote down vote up
private boolean validPluginParams(String skinPath, String skinPkgName)
{
    if (TextUtils.isEmpty(skinPath) || TextUtils.isEmpty(skinPkgName))
    {
        return false;
    }

    File file = new File(skinPath);
    if (!file.exists())
        return false;

    PackageInfo info = getPackageInfo(skinPath);
    if (!info.packageName.equals(skinPkgName))
        return false;
    return true;
}
 
Example #13
Source File: AboutActivity.java    From MensaGuthaben with GNU General Public License v3.0 5 votes vote down vote up
private void showVersion() {
	PackageInfo pInfo = null;
	try {
		pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
		TextView tv = (TextView) findViewById(R.id.tvVersion);
		tv.setText(getString(R.string.version)+" "+pInfo.versionName+" ("+pInfo.versionCode+")");
	} catch (PackageManager.NameNotFoundException e) {
		e.printStackTrace();
	}
}
 
Example #14
Source File: SpecialSupport.java    From rebootmenu with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 检测是否是WearOS(Android Wear)系统
 * Note:com.google.android.wearable.app,希望有更好的方法
 *
 * @param context {@link android.content.Context}
 * @return bool
 */
public static boolean isAndroidWearOS(Context context) {
    try {
        PackageManager mgr = context.getPackageManager();
        PackageInfo info = mgr.getPackageInfo("com.google.android.wearable.app", 0);
        Log.i(TAG, "isAndroidWearOS: App Version:" + info.versionName);
        return (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0;
    } catch (PackageManager.NameNotFoundException ignored) {
        return false;
    }
}
 
Example #15
Source File: DeviceUtils.java    From NoVIP with Apache License 2.0 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 #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: AboutActivity.java    From NetEasyNews with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取版本号
 *
 * @return 当前应用的版本号
 */
public String getVersion() {
    try {
        PackageManager manager = this.getPackageManager();
        PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
        String version = info.versionName;
        return this.getString(R.string.version_name) + version;
    } catch (Exception e) {
        e.printStackTrace();
        return this.getString(R.string.version_name) + "1.0";
    }
}
 
Example #18
Source File: IPackageManagerHookHandler.java    From understand-plugin-framework with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (method.getName().equals("getPackageInfo")) {
        return new PackageInfo();
    }
    return method.invoke(mBase, args);
}
 
Example #19
Source File: Utils.java    From Noyze with Apache License 2.0 5 votes vote down vote up
public static boolean isPackageInstalled(Context context, String pack) {
    if (null == context || TextUtils.isEmpty(pack)) return false;
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(pack, 0);
        return (info.packageName.equals(pack));
    } catch (PackageManager.NameNotFoundException nfe) {
        LOGE("Utils", "Could not find package: " + pack);
        return false;
    }
}
 
Example #20
Source File: PackageUtil.java    From PicKing with Apache License 2.0 5 votes vote down vote up
public static String getAppVersionName(Context context) {
    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
        return pi.versionName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return "";
}
 
Example #21
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/** @hide */
@Override
@SuppressWarnings("unchecked")
public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
    try {
        ParceledListSlice<PackageInfo> parceledList =
                mPM.getInstalledPackages(flags, userId);
        if (parceledList == null) {
            return Collections.emptyList();
        }
        return parceledList.getList();
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
 
Example #22
Source File: VersionUtil.java    From SeeWeather with Apache License 2.0 5 votes vote down vote up
public static String getVersion(Context context) {
    try {
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        return info.versionName;
    } catch (Exception e) {
        e.printStackTrace();
        return context.getString(R.string.can_not_find_version_name);
    }
}
 
Example #23
Source File: ReactNativeAPKModule.java    From react-native-apk with MIT License 5 votes vote down vote up
@ReactMethod
public void isAppInstalled(String packageName, Callback cb) {
  try {
    PackageInfo pInfo = this.reactContext.getPackageManager().getPackageInfo(packageName,
        PackageManager.GET_ACTIVITIES);

    cb.invoke(true);
  } catch (PackageManager.NameNotFoundException e) {
    cb.invoke(false);
  }
}
 
Example #24
Source File: SyncCryptoLegacy.java    From realm-android-user-store with Apache License 2.0 5 votes vote down vote up
public SyncCryptoLegacy (Context context) throws KeyStoreException {
    PRNGFixes.apply();
    this.context = context;

    try {
        PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        alias += "_" + pi.packageName; // make the alias unique per package
    } catch (Exception ignore) {
    }
}
 
Example #25
Source File: RxAppTool.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
/**
 * 得到AppInfo的Bean
 *
 * @param pm 包的管理
 * @param pi 包的信息
 * @return AppInfo类
 */
private static AppInfo getBean(PackageManager pm, PackageInfo pi) {
    ApplicationInfo ai = pi.applicationInfo;
    String name = ai.loadLabel(pm).toString();
    Drawable icon = ai.loadIcon(pm);
    String packageName = pi.packageName;
    String packagePath = ai.sourceDir;
    String versionName = pi.versionName;
    int versionCode = pi.versionCode;
    boolean isSD = (ApplicationInfo.FLAG_SYSTEM & ai.flags) != ApplicationInfo.FLAG_SYSTEM;
    boolean isUser = (ApplicationInfo.FLAG_SYSTEM & ai.flags) != ApplicationInfo.FLAG_SYSTEM;
    return new AppInfo(name, icon, packageName, packagePath, versionName, versionCode, isSD, isUser);
}
 
Example #26
Source File: PackageInfoHelper.java    From HybridCache with MIT License 5 votes vote down vote up
public static int getAppVersionCode(@NonNull Context context) {
    String packageName = context.getPackageName();
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
        return packageInfo.versionCode;
    } catch (Exception e) {
        return 0;
    }
}
 
Example #27
Source File: VpnProfile.java    From Cake-VPN with GNU General Public License v2.0 5 votes vote down vote up
private String getVersionEnvString(Context c) {
    String version = "unknown";
    try {
        PackageInfo packageinfo = c.getPackageManager().getPackageInfo(c.getPackageName(), 0);
        version = packageinfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        VpnStatus.logException(e);
    }
    return String.format(Locale.US, "%s %s", c.getPackageName(), version);
}
 
Example #28
Source File: PackageUtils.java    From SmsCode with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 指定的包名对应的App是否已安装
 *
 */
public static boolean isPackageInstalled(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    try {
        PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
        return packageInfo != null;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example #29
Source File: SystemUtils.java    From PhotoFactory with Apache License 2.0 5 votes vote down vote up
/**
 * 获取应用程序名称
 */
public static String getAppName(Context context)
{
    try {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packageInfo = packageManager.getPackageInfo(
                context.getPackageName(), 0);
        int labelRes = packageInfo.applicationInfo.labelRes;
        return context.getResources().getString(labelRes);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #30
Source File: Util.java    From InviZible with GNU General Public License v3.0 5 votes vote down vote up
static boolean isSystem(String packageName, Context context) {
    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo info = pm.getPackageInfo(packageName, 0);
        return ((info.applicationInfo.flags & (ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0);
    } catch (PackageManager.NameNotFoundException ignore) {
        return false;
    }
}