Java Code Examples for android.content.pm.PackageInfo#getLongVersionCode()

The following examples show how to use android.content.pm.PackageInfo#getLongVersionCode() . 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: ShortcutPackageInfo.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public int canRestoreTo(ShortcutService s, PackageInfo currentPackage, boolean anyVersionOkay) {
    PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class);
    if (!BackupUtils.signaturesMatch(mSigHashes, currentPackage, pmi)) {
        Slog.w(TAG, "Can't restore: Package signature mismatch");
        return ShortcutInfo.DISABLED_REASON_SIGNATURE_MISMATCH;
    }
    if (!ShortcutService.shouldBackupApp(currentPackage) || !mBackupSourceBackupAllowed) {
        // "allowBackup" was true when backed up, but now false.
        Slog.w(TAG, "Can't restore: package didn't or doesn't allow backup");
        return ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED;
    }
    if (!anyVersionOkay && (currentPackage.getLongVersionCode() < mBackupSourceVersionCode)) {
        Slog.w(TAG, String.format(
                "Can't restore: package current version %d < backed up version %d",
                currentPackage.getLongVersionCode(), mBackupSourceVersionCode));
        return ShortcutInfo.DISABLED_REASON_VERSION_LOWER;
    }
    return ShortcutInfo.DISABLED_REASON_NOT_DISABLED;
}
 
Example 2
Source File: ShortcutPackageInfo.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public static ShortcutPackageInfo generateForInstalledPackageForTest(
        ShortcutService s, String packageName, @UserIdInt int packageUserId) {
    final PackageInfo pi = s.getPackageInfoWithSignatures(packageName, packageUserId);
    // retrieve the newest sigs
    SigningInfo signingInfo = pi.signingInfo;
    if (signingInfo == null) {
        Slog.e(TAG, "Can't get signatures: package=" + packageName);
        return null;
    }
    // TODO (b/73988180) use entire signing history in case of rollbacks
    Signature[] signatures = signingInfo.getApkContentsSigners();
    final ShortcutPackageInfo ret = new ShortcutPackageInfo(pi.getLongVersionCode(),
            pi.lastUpdateTime, BackupUtils.hashSignatureArray(signatures), /* shadow=*/ false);

    ret.mBackupSourceBackupAllowed = s.shouldBackupApp(pi);
    ret.mBackupSourceVersionCode = pi.getLongVersionCode();
    return ret;
}
 
Example 3
Source File: WebViewFactory.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private static void verifyPackageInfo(PackageInfo chosen, PackageInfo toUse)
        throws MissingWebViewPackageException {
    if (!chosen.packageName.equals(toUse.packageName)) {
        throw new MissingWebViewPackageException("Failed to verify WebView provider, "
                + "packageName mismatch, expected: "
                + chosen.packageName + " actual: " + toUse.packageName);
    }
    if (chosen.getLongVersionCode() > toUse.getLongVersionCode()) {
        throw new MissingWebViewPackageException("Failed to verify WebView provider, "
                + "version code is lower than expected: " + chosen.getLongVersionCode()
                + " actual: " + toUse.getLongVersionCode());
    }
    if (getWebViewLibrary(toUse.applicationInfo) == null) {
        throw new MissingWebViewPackageException("Tried to load an invalid WebView provider: "
                + toUse.packageName);
    }
    if (!signaturesEquals(chosen.signatures, toUse.signatures)) {
        throw new MissingWebViewPackageException("Failed to verify WebView provider, "
                + "signature mismatch");
    }
}
 
Example 4
Source File: ManifestUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 获取 APP 版本信息
 * @param packageName 应用包名
 * @return 0 = versionName, 1 = versionCode
 */
public static String[] getAppVersion(final String packageName) {
    try {
        PackageInfo packageInfo = AppUtils.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
        if (packageInfo != null) {
            String versionName = packageInfo.versionName == null ? "null" : packageInfo.versionName;
            String versionCode;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                versionCode = packageInfo.getLongVersionCode() + "";
            } else {
                versionCode = packageInfo.versionCode + "";
            }
            return new String[]{versionName, versionCode};
        }
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getAppVersion");
    }
    return null;
}
 
Example 5
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 6
Source File: VMSystem.java    From VMLibrary with Apache License 2.0 6 votes vote down vote up
/**
 * 获取应用当前版本号
 *
 * @param context 上下文对象
 */
public static long getVersionCode(Context context) {
    PackageManager manager = context.getPackageManager();
    long code = 0;
    try {
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            code = info.getLongVersionCode();
        } else {
            code = info.versionCode;
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return code;
}
 
Example 7
Source File: KeyAttestationApplicationIdProviderService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public KeyAttestationApplicationId getKeyAttestationApplicationId(int uid)
        throws RemoteException {
    if (Binder.getCallingUid() != android.os.Process.KEYSTORE_UID) {
        throw new SecurityException("This service can only be used by Keystore");
    }
    KeyAttestationPackageInfo[] keyAttestationPackageInfos = null;
    final long token = Binder.clearCallingIdentity();
    try {
        String[] packageNames = mPackageManager.getPackagesForUid(uid);
        if (packageNames == null) {
            throw new RemoteException("No packages for uid");
        }
        int userId = UserHandle.getUserId(uid);
        keyAttestationPackageInfos = new KeyAttestationPackageInfo[packageNames.length];

        for (int i = 0; i < packageNames.length; ++i) {
            PackageInfo packageInfo = mPackageManager.getPackageInfoAsUser(packageNames[i],
                    PackageManager.GET_SIGNATURES, userId);
            keyAttestationPackageInfos[i] = new KeyAttestationPackageInfo(packageNames[i],
                    packageInfo.getLongVersionCode(), packageInfo.signatures);
        }
    } catch (NameNotFoundException nnfe) {
        throw new RemoteException(nnfe.getMessage());
    } finally {
        Binder.restoreCallingIdentity(token);
    }
    return new KeyAttestationApplicationId(keyAttestationPackageInfos);
}
 
Example 8
Source File: PackageTrackerHelperImpl.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public long getInstalledPackageVersion(String packageName)
        throws PackageManager.NameNotFoundException {
    int flags = PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS;
    PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName, flags);
    return packageInfo.getLongVersionCode();
}
 
Example 9
Source File: ShortcutPackageItem.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public void attemptToRestoreIfNeededAndSave() {
    if (!mPackageInfo.isShadow()) {
        return; // Already installed, nothing to do.
    }
    final ShortcutService s = mShortcutUser.mService;
    if (!s.isPackageInstalled(mPackageName, mPackageUserId)) {
        if (ShortcutService.DEBUG) {
            Slog.d(TAG, String.format("Package still not installed: %s/u%d",
                    mPackageName, mPackageUserId));
        }
        return; // Not installed, no need to restore yet.
    }
    int restoreBlockReason;
    long currentVersionCode = ShortcutInfo.VERSION_CODE_UNKNOWN;

    if (!mPackageInfo.hasSignatures()) {
        s.wtf("Attempted to restore package " + mPackageName + "/u" + mPackageUserId
                + " but signatures not found in the restore data.");
        restoreBlockReason = ShortcutInfo.DISABLED_REASON_SIGNATURE_MISMATCH;
    } else {
        final PackageInfo pi = s.getPackageInfoWithSignatures(mPackageName, mPackageUserId);
        currentVersionCode = pi.getLongVersionCode();
        restoreBlockReason = mPackageInfo.canRestoreTo(s, pi, canRestoreAnyVersion());
    }

    if (ShortcutService.DEBUG) {
        Slog.d(TAG, String.format("Restoring package: %s/u%d (version=%d) %s for u%d",
                mPackageName, mPackageUserId, currentVersionCode,
                ShortcutInfo.getDisabledReasonDebugString(restoreBlockReason),
                getOwnerUserId()));
    }

    onRestored(restoreBlockReason);

    // Either way, it's no longer a shadow.
    mPackageInfo.setShadow(false);

    s.scheduleSaveUser(mPackageUserId);
}
 
Example 10
Source File: ShortcutPackageInfo.java    From android_9.0.0_r45 with 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 11
Source File: HYHelper.java    From mhzs with MIT License 5 votes vote down vote up
/**
 * 获取packageCode
 *
 * @param context
 * @return
 */
public static long getPackageCode(Context context) {
    PackageManager manager = context.getPackageManager();
    long code = 0;
    try {
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        code = info.getLongVersionCode();
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return code;
}
 
Example 12
Source File: ModuleUtil.java    From EdXposedManager with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private InstalledModule(PackageInfo pkg, boolean isFramework) {
    this.app = pkg.applicationInfo;
    this.packageName = pkg.packageName;
    this.isFramework = isFramework;
    this.versionName = pkg.versionName;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        this.versionCode = pkg.getLongVersionCode();
    } else {
        this.versionCode = pkg.versionCode;
    }
    this.installTime = pkg.firstInstallTime;
    this.updateTime = pkg.lastUpdateTime;

    if (isFramework) {
        this.minVersion = 0;
        this.description = "";
    } else {
        int version = XposedApp.getXposedVersion();
        if (version > 0 && XposedApp.getPreferences().getBoolean("skip_xposedminversion_check", false)) {
            this.minVersion = version;
        } else {
            Object minVersionRaw = app.metaData.get("xposedminversion");
            if (minVersionRaw instanceof Integer) {
                this.minVersion = (Integer) minVersionRaw;
            } else if (minVersionRaw instanceof String) {
                this.minVersion = extractIntPart((String) minVersionRaw);
            } else {
                this.minVersion = 0;
            }
        }
    }
}
 
Example 13
Source File: AppInfoBean.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化 AppInfoBean
 * @param packageInfo    {@link PackageInfo}
 * @param packageManager {@link PackageManager}
 */
protected AppInfoBean(final PackageInfo packageInfo, final PackageManager packageManager) {
    // APP 包名
    appPackName = packageInfo.applicationInfo.packageName;
    // APP 应用名
    appName = packageManager.getApplicationLabel(packageInfo.applicationInfo).toString();
    // APP 图标
    appIcon = packageManager.getApplicationIcon(packageInfo.applicationInfo);
    // APP 类型
    appType = AppInfoBean.getAppType(packageInfo);
    // APP 版本号
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        versionCode = packageInfo.getLongVersionCode();
    } else {
        versionCode = packageInfo.versionCode;
    }
    // APP 版本名
    versionName = packageInfo.versionName;
    // APP 首次安装时间
    firstInstallTime = packageInfo.firstInstallTime;
    // APP 最后一次更新时间
    lastUpdateTime = packageInfo.lastUpdateTime;
    // APP 地址
    sourceDir = packageInfo.applicationInfo.sourceDir;
    // APK 大小
    apkSize = FileUtils.getFileLength(sourceDir);
}
 
Example 14
Source File: AppUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取 APP versionCode
 * @param packageName 应用包名
 * @return APP versionCode
 */
public static long getAppVersionCode(final String packageName) {
    if (StringUtils.isSpace(packageName)) return -1;
    try {
        PackageInfo packageInfo = getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            return packageInfo.getLongVersionCode();
        } else {
            return packageInfo.versionCode;
        }
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getAppVersionCode");
        return -1;
    }
}