Java Code Examples for android.content.pm.PackageInfo

The following examples show how to use android.content.pm.PackageInfo. These examples are extracted from open source projects. 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 Project: EasyProtector   Source File: VirtualApkCheckUtil.java    License: 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 2
Source Project: Android-Skin   Source File: PackageUtil.java    License: 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 3
Source Project: whiteboard   Source File: Utils.java    License: 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 4
Source Project: timecat   Source File: CropImage.java    License: 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 5
Source Project: AndroidBleManager   Source File: AppUpgradeService.java    License: 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 6
Source Project: AndroidChangeSkin   Source File: SkinManager.java    License: 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 7
Source Project: android-sholi   Source File: MainActivity.java    License: 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 8
Source Project: DeviceConnect-Android   Source File: DevicePluginManager.java    License: 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 Project: Android-Plugin-Framework   Source File: PluginDescriptor.java    License: 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 10
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 11
Source Project: palmsuda   Source File: Splash.java    License: 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 12
Source Project: SAI   Source File: SaiExportedAppMeta2.java    License: 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 13
Source Project: SeeWeather   Source File: VersionUtil.java    License: 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 14
Source Project: ApkToolPlus   Source File: SignatureUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static String getCertMD5(Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
        Signature[] signs = packageInfo.signatures;
        if (signs.length > 0) {
            return getCertMD5(signs[0].toByteArray());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}
 
Example 15
Source Project: android   Source File: VpnProfile.java    License: GNU General Public License v3.0 5 votes vote down vote up
public 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 16
Source Project: Awesome-WanAndroid   Source File: NetUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static int getAppUid(@NonNull Context context) {
    if (sUId == -1) {
        PackageManager packageManager = context.getApplicationContext().getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getApplicationContext().getPackageName(), PackageManager.GET_META_DATA);
            if (packageInfo != null) {
                sUId = packageInfo.applicationInfo.uid;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
    return sUId;
}
 
Example 17
Source Project: firebase-android-sdk   Source File: GrpcClientModule.java    License: Apache License 2.0 5 votes vote down vote up
public static String getSignature(@NonNull PackageManager pm, @NonNull String packageName) {
  try {
    PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
    if (packageInfo == null
        || packageInfo.signatures == null
        || packageInfo.signatures.length == 0
        || packageInfo.signatures[0] == null) {
      return null;
    }
    return signatureDigest(packageInfo.signatures[0]);
  } catch (PackageManager.NameNotFoundException e) {
    return null;
  }
}
 
Example 18
Source Project: android_9.0.0_r45   Source File: ShortcutPackageInfo.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Set {@link #mVersionCode}, {@link #mLastUpdateTime} and {@link #mBackupAllowed}
 * from a {@link PackageInfo}.
 */
public void updateFromPackageInfo(@NonNull PackageInfo pi) {
    if (pi != null) {
        mVersionCode = pi.getLongVersionCode();
        mLastUpdateTime = pi.lastUpdateTime;
        mBackupAllowed = ShortcutService.shouldBackupApp(pi);
        mBackupAllowedInitialized = true;
    }
}
 
Example 19
Source Project: Plumble   Source File: Preferences.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static void configureAboutPreferences(Context context, PreferenceScreen screen) {
    String version = "Unknown";
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        version = info.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    Preference versionPreference = screen.findPreference(VERSION_KEY);
    versionPreference.setSummary(version);
}
 
Example 20
@DexWrap
//<4.1// PackageInfo generatePackageInfo(PackageParser.Package p, int flags) {
/*>4.1*/ //<7.0// PackageInfo generatePackageInfo(PackageParser.Package p, int flags, int userId) {
/*>7.0*/ private PackageInfo generatePackageInfo(PackageSetting p, int flags, int userId) {
    //<4.1// PackageInfo pi = generatePackageInfo(p, flags);
    /*>4.1*/ PackageInfo pi = generatePackageInfo(p, flags, userId);
    if (p != null && pi != null) {
        //<4.0// pi = GeneratePackageInfoHookAccessor.hook(pi, mContext, p, flags, -1);
        /*>4.0*/ //<4.1// pi = GeneratePackageInfoHook.hook(pi, mContext, p, flags, -1);
        /*>4.1*/ //<7.0// pi = GeneratePackageInfoHook.hook(pi, mContext, p, flags, userId);
        /*>7.0*/ PackageParser.Package pp = p.pkg;
        /*>7.0*/ if (pp != null) pi = GeneratePackageInfoHook.hook(pi, mContext, pp, flags, userId);
    }
    return pi;
}
 
Example 21
Source Project: volley   Source File: Volley.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
 *
 * @param context A {@link Context} to use for creating the cache dir.
 * @param stack An {@link HttpStack} to use for the network, or null for default.
 * @return A started {@link RequestQueue} instance.
 */
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
    File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

    String userAgent = null;
    try {
        String packageName = context.getPackageName();
        PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
        userAgent = packageName + "/" + info.versionCode;
    } catch (NameNotFoundException e) {
    }

    if (stack == null) {
        if (Build.VERSION.SDK_INT >= 9) {
            stack = new HurlStack(userAgent);
        } else {
            // Prior to Gingerbread, HttpUrlConnection was unreliable.
            // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
            stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
        }
    }

    Network network = new BasicNetwork(stack);

    RequestQueue queue = new RequestQueue(new DiskLruBasedCache(cacheDir), network);
    queue.start();

    return queue;
}
 
Example 22
Source Project: BubbleCloudView   Source File: Utils.java    License: MIT License 5 votes vote down vote up
public static Drawable getApkIcon(Context context, String apkPath) {
    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
    if (info != null) {
        ApplicationInfo appInfo = info.applicationInfo;
        appInfo.sourceDir = apkPath;
        appInfo.publicSourceDir = apkPath;
        try {
            return appInfo.loadIcon(pm);
        } catch (OutOfMemoryError e) {
        }
    }
    return null;
}
 
Example 23
Source Project: NetGuard   Source File: Util.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static String getSelfVersionName(Context context) {
    try {
        PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return pInfo.versionName;
    } catch (PackageManager.NameNotFoundException ex) {
        return ex.toString();
    }
}
 
Example 24
@Override
protected PackageInfo doInBackground(final String... packageName) {
    final PackageManager pm = mContext.getPackageManager();
    mContext = null; // Bazooka-powered anti-leak device
    try {
        final PackageInfo packageInfo = pm.getPackageInfo(packageName[0], 0 /* flags */);
        sCache.put(packageName[0], packageInfo);
        return packageInfo;
    } catch (android.content.pm.PackageManager.NameNotFoundException e) {
        return null;
    }
}
 
Example 25
Source Project: DeviceConnect-Android   Source File: DevicePluginXmlUtil.java    License: MIT License 5 votes vote down vote up
/**
 * コンポーネントのActivityInfoを取得する.
 *
 * @param context コンテキスト
 * @param packageName package name
 * @return コンポーネントのActivityInfo
 */
private static ActivityInfo getReceiverInfo(final Context context, final String packageName) {
    try {
        PackageManager pkgMgr = context.getPackageManager();
        PackageInfo pkg = pkgMgr.getPackageInfo(packageName, PackageManager.GET_RECEIVERS);
        if (pkg != null) {
            ActivityInfo[] receivers = pkg.receivers;
            if (receivers != null) {
                for (int i = 0; i < receivers.length; i++) {
                    String pkgName = receivers[i].packageName;
                    String className = receivers[i].name;
                    ComponentName component = new ComponentName(pkgName, className);
                    ActivityInfo receiverInfo = pkgMgr.getReceiverInfo(component, PackageManager.GET_META_DATA);
                    if (receiverInfo.metaData != null) {
                        Object xmlData = receiverInfo.metaData.get(PLUGIN_META_DATA);
                        if (xmlData instanceof Integer) {
                            XmlResourceParser xrp = receiverInfo.loadXmlMetaData(pkgMgr, PLUGIN_META_DATA);
                            if (xrp != null) {
                                return receiverInfo;
                            }
                        }
                    }
                }
            }
        }
        return null;
    } catch (NameNotFoundException e) {
        return null;
    }
}
 
Example 26
Source Project: BeMusic   Source File: CrashHandler.java    License: Apache License 2.0 5 votes vote down vote up
public static String getAppVersion (Context context) {
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return info.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return "";
}
 
Example 27
Source Project: UtilsLib   Source File: ApplicationUtil.java    License: MIT License 5 votes vote down vote up
/**
 * 获取应用程序的内部版本号
 *
 * @param context 上下文对象
 * @return 程序的内部版本号,用于比对升级用
 */
public static int getVersionCode(Context context) {
    try {
        PackageManager packageManager = context.getPackageManager();// 获取包的管理者
        PackageInfo packageInfo = packageManager.getPackageInfo(
                context.getPackageName(), 0);
        return packageInfo.versionCode;// 获取应用程序的版本名称
    } catch (PackageManager.NameNotFoundException e) {
        LogUtil.e(TAG, e);
    }
    return 0;
}
 
Example 28
Source Project: 365browser   Source File: WebApkUpdateManager.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Reads the WebAPK's version code. Returns 0 on failure.
 */
private int readVersionCodeFromAndroidManifest(String webApkPackage) {
    try {
        PackageManager packageManager =
                ContextUtils.getApplicationContext().getPackageManager();
        PackageInfo packageInfo = packageManager.getPackageInfo(webApkPackage, 0);
        return packageInfo.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return 0;
}
 
Example 29
private void checkInstallation() {
            mButtonInstall.setEnabled(true);
            if (pay != null && pay.price.doubleValue() > 0) {
                isPaidToschedule = true;
//                mButtonInstall.setVisibility(View.GONE);
                InstallListener installListener = new InstallListener(iconUrl, appName, versionName, packageName, md5sum, isPaidApp());
                mButtonInstall.setOnClickListener(installListener);
            } else {
                PackageInfo info = getPackageInfo(getActivity(), packageName);
                if (info == null) {
                    mButtonInstall.setText(getString(R.string.install));
                    mButtonInstall.setOnClickListener(new InstallListener(iconUrl, appName, versionName, packageName, md5sum, isPaidApp()));
                    isInstalled = false;
                } else {
                    isInstalled = true;
                    try {
                        installedSignature = AptoideUtils.Algorithms.computeSHA1sumFromBytes(info.signatures[0].toByteArray()).toUpperCase(Locale.ENGLISH);
                    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
                        Logger.printException(e);
                    }
                    if (versionCode > info.versionCode) {
                        isUpdate = true;
                        mButtonInstall.setText(getString(R.string.update));
                        mButtonInstall.setEnabled(true);
                        mButtonInstall.setOnClickListener(new InstallListener(iconUrl, appName, versionName, packageName, md5sum, isPaidApp()));
//                    UpdateAppVersionInstalled(info.versionName);
                    } else if (versionCode < info.versionCode) {
                        isUpdate = false;
                        mButtonInstall.setText(getString(R.string.downgrade));
                        mButtonInstall.setOnClickListener(new DowngradeListener(iconUrl, appName, info.versionName, versionName, info.packageName));
//                    UpdateAppVersionInstalled(info.versionName);
                    } else {
                        changebtInstalltoOpen();
                    }
                    getActivity().supportInvalidateOptionsMenu();
                }
            }

            setupInstallButton();
        }
 
Example 30
Source Project: Mobilyzer   Source File: GCMManager.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return Application's version code from the {@code PackageManager}.
 */
private static int getAppVersion(Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager()
                .getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionCode;
    } catch (NameNotFoundException e) {
        // should never happen
        throw new RuntimeException("Could not get package name: " + e);
    }
}