diff --git a/app/build.gradle b/app/build.gradle index 6474c50e72..fe53f15565 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -260,6 +260,9 @@ dependencies { implementation "com.scwang.smartrefresh:SmartRefreshLayout:${smartRefreshLayout}" implementation "net.cachapa.expandablelayout:expandablelayout:${expandableLayout}" + // 用于比较 versionName 是大于小于或等于 + implementation "com.g00fy2:versioncompare:${versioncompare}" + implementation "top.zibin:Luban:${luban}" implementation project(':libraries:gid') diff --git a/app/src/main/java/com/gh/common/util/PackageUtils.java b/app/src/main/java/com/gh/common/util/PackageUtils.java index 8bae4a99e0..676d3527bc 100644 --- a/app/src/main/java/com/gh/common/util/PackageUtils.java +++ b/app/src/main/java/com/gh/common/util/PackageUtils.java @@ -11,9 +11,12 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.support.v4.content.FileProvider; import android.text.TextUtils; +import com.g00fy2.versioncompare.Version; import com.gh.common.constant.Config; +import com.gh.gamecenter.BuildConfig; import com.gh.gamecenter.entity.ApkEntity; import com.gh.gamecenter.entity.GameEntity; import com.gh.gamecenter.entity.GameUpdateEntity; @@ -42,10 +45,14 @@ public class PackageUtils { List updateList = new ArrayList<>(); + boolean isAPluginGame = false; for (ApkEntity apkEntity : gameEntity.getApk()) { // 判断是否gh_version是否存在 String gh_version = (String) PackageUtils.getMetaData(context, apkEntity.getPackageName(), "gh_version"); if (gh_version != null && apkEntity.getGhVersion() != null) { + + // 确定这是一个插件游戏 + if (!isAPluginGame) isAPluginGame = true; gh_version = gh_version.substring(2); if (Long.parseLong(gh_version) < Long.parseLong(apkEntity.getGhVersion()) && apkEntity.getForce()) { GameUpdateEntity updateEntity = new GameUpdateEntity(); @@ -66,6 +73,41 @@ public class PackageUtils { } } + // 不是插件游戏 + if (!isAPluginGame) { + for (ApkEntity apkEntity : gameEntity.getApkNormal()) { + + String versionFromRequest = apkEntity.getVersion(); + String versionFromInstalledApp = getVersionByPackage(context, apkEntity.getPackageName()); + + // 是否需要显示更新 + boolean shouldShouldUpdate = apkEntity.getForce(); + + if (shouldShouldUpdate && !TextUtils.isEmpty(versionFromRequest) && !TextUtils.isEmpty(versionFromInstalledApp)) { + + // 根据版本判断是否需要更新 + shouldShouldUpdate = new Version(versionFromRequest).isHigherThan(versionFromInstalledApp); + + if (shouldShouldUpdate) { + GameUpdateEntity updateEntity = new GameUpdateEntity(); + updateEntity.setId(gameEntity.getId()); + updateEntity.setName(gameEntity.getName()); + updateEntity.setIcon(gameEntity.getIcon()); + updateEntity.setPackageName(apkEntity.getPackageName()); + updateEntity.setSize(apkEntity.getSize()); + updateEntity.setVersion(apkEntity.getVersion()); + updateEntity.setGhVersion(apkEntity.getGhVersion()); + updateEntity.setUrl(apkEntity.getUrl()); + updateEntity.setPlatform(apkEntity.getPlatform()); + updateEntity.setEtag(apkEntity.getEtag()); + updateEntity.setBrief(gameEntity.getBrief()); + updateEntity.setTag(gameEntity.getTag()); + updateList.add(updateEntity); + } + } + } + } + return updateList; } @@ -172,7 +214,15 @@ public class PackageUtils { if ("smartisan".equals(Build.MANUFACTURER)) { installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } - installIntent.setDataAndType(uri, "application/vnd.android.package-archive"); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, new File(path)); + installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + installIntent.setDataAndType(uri, "application/vnd.android.package-archive"); + installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + } else { + installIntent.setDataAndType(uri, "application/vnd.android.package-archive"); + } InstallUtils.getInstance(context).addInstall(getPackageNameByPath(context, path)); return installIntent; } diff --git a/dependencies.gradle b/dependencies.gradle index c9188ec61b..7e2ce309b2 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -64,4 +64,6 @@ ext { smartRefreshLayout = "1.1.0-alpha-7" luban = "1.1.7" + + versioncompare = "1.2.8" } \ No newline at end of file