diff --git a/app/src/main/java/com/gh/common/util/DownloadObserver.kt b/app/src/main/java/com/gh/common/util/DownloadObserver.kt index 8816acf9e8..11fa9a29ba 100644 --- a/app/src/main/java/com/gh/common/util/DownloadObserver.kt +++ b/app/src/main/java/com/gh/common/util/DownloadObserver.kt @@ -6,8 +6,6 @@ import com.gh.base.BaseActivity import com.gh.common.constant.Constants import com.gh.common.exposure.ExposureUtils import com.gh.common.exposure.meta.MetaUtil -import com.gh.common.runOnIoThread -import com.gh.common.runOnUiThread import com.gh.common.simulator.SimulatorDownloadManager import com.gh.common.simulator.SimulatorGameManager import com.gh.common.util.EnergyTaskHelper.postEnergyTask @@ -168,21 +166,17 @@ object DownloadObserver { Utils.toast(mApplication, R.string.install_failure_hint) downloadManager.cancel(downloadEntity.url) } else { - runOnIoThread { - if (PackageUtils.isInstallable(mApplication, downloadEntity.path)) { - downloadEntity.meta[Constants.MARK_ALREADY_TRIGGERED_INSTALLATION] = "YES" - tryWithDefaultCatch { - runOnUiThread { - PackageInstaller.install(mApplication, downloadEntity, false) - } - } + if (PackageUtils.isCanLaunchSetup(mApplication, downloadEntity.path)) { + downloadEntity.meta[Constants.MARK_ALREADY_TRIGGERED_INSTALLATION] = "YES" + tryWithDefaultCatch { + PackageInstaller.install(mApplication, downloadEntity, false) + } + } else { + // 弹出卸载提示框 + if (downloadEntity.isPlugin) { + EventBus.getDefault().post(EBShowDialog(BaseActivity.PLUGGABLE, downloadEntity.path)) } else { - // 弹出卸载提示框 - if (downloadEntity.isPlugin) { - EventBus.getDefault().post(EBShowDialog(BaseActivity.PLUGGABLE, downloadEntity.path)) - } else { - EventBus.getDefault().post(EBShowDialog(BaseActivity.SIGNATURE_CONFLICT, downloadEntity.path)) - } + EventBus.getDefault().post(EBShowDialog(BaseActivity.SIGNATURE_CONFLICT, downloadEntity.path)) } } } diff --git a/app/src/main/java/com/gh/common/util/PackageInstaller.kt b/app/src/main/java/com/gh/common/util/PackageInstaller.kt index cc79b536cf..a59453478e 100644 --- a/app/src/main/java/com/gh/common/util/PackageInstaller.kt +++ b/app/src/main/java/com/gh/common/util/PackageInstaller.kt @@ -10,8 +10,6 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.content.FileProvider import com.gh.common.constant.Constants import com.gh.common.dialog.InstallPermissionDialogFragment -import com.gh.common.runOnIoThread -import com.gh.common.runOnUiThread import com.gh.common.xapk.XapkInstaller import com.gh.download.server.BrowserInstallHelper import com.gh.gamecenter.BuildConfig @@ -79,26 +77,20 @@ object PackageInstaller { return } - runOnIoThread { - if (PackageUtils.isInstallable(context, pkgPath)) { - runOnUiThread { - HaloApp.put(Constants.LAST_INSTALL_GAME, pkgPath) + if (PackageUtils.isCanLaunchSetup(context, pkgPath)) { + HaloApp.put(Constants.LAST_INSTALL_GAME, pkgPath) - val installIntent = getInstallIntent(context, pkgPath) - context.startActivity(installIntent) + val installIntent = getInstallIntent(context, pkgPath) + context.startActivity(installIntent) + } else { + if (isPluggin) { + DialogHelper.showPluginDialog(context) { + uninstall(context, pkgPath) } } else { - runOnUiThread { - if (isPluggin) { - DialogHelper.showPluginDialog(context) { - uninstall(context, pkgPath) - } - } else { - // 非插件化的同包名不同签名冲突 - DialogHelper.showSignatureConflictDialog(context) { - uninstall(context, pkgPath) - } - } + // 非插件化的同包名不同签名冲突 + DialogHelper.showSignatureConflictDialog(context) { + uninstall(context, pkgPath) } } } 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 a0c627bda1..d5b688132b 100644 --- a/app/src/main/java/com/gh/common/util/PackageUtils.java +++ b/app/src/main/java/com/gh/common/util/PackageUtils.java @@ -16,10 +16,8 @@ import android.os.PowerManager; import android.text.TextUtils; import androidx.annotation.Nullable; -import androidx.annotation.WorkerThread; import com.g00fy2.versioncompare.Version; -import com.gh.common.AppExecutor; import com.gh.common.xapk.XapkInstaller; import com.gh.gamecenter.BuildConfig; import com.gh.gamecenter.entity.ApkEntity; @@ -228,13 +226,9 @@ public class PackageUtils { return null; } - // TODO 找一个更高效的方式来比较 V2 签名 + // TODO 找一个更好的办法来比较签名并且不触发 ANR public static boolean compareSignatureBetweenInstalledAppWithApk(Context context, String packageName, String apkFilePath) { try { - AppExecutor.getUiExecutor().execute(() -> { - ToastUtils.toast("安装包校验中,约需要3~5秒,请稍候"); - }); - // 据 Sentry 统计,刚上架一个周末的包里对这个方法有 700+ 次调用,然后其中一部分会造成 ANR Signature sig = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures[0]; @@ -287,14 +281,11 @@ public class PackageUtils { } /** - * 根据 path 获取 apk 是否可安装 (全新安装或覆盖安装) - * - * 由于部分不存在 V1 签名的大安装包在调用系统 API 获取签名信息时会非常慢,所以请在工作线程里调用此方法 + * 根据 path 获取 apk 信息确定处理方式 * - * @return true 为可直接唤起系统 PackageInstaller, false 为需要插件化 + * @return true 为直接唤起系统 PackageInstaller, false 为需要插件化 */ - @WorkerThread - public static boolean isInstallable(Context context, String path) { + public static boolean isCanLaunchSetup(Context context, String path) { String packageName = getPackageNameByPath(context, path); if (TextUtils.isEmpty(packageName)) { diff --git a/app/src/main/java/com/gh/gamecenter/receiver/InstallReceiver.java b/app/src/main/java/com/gh/gamecenter/receiver/InstallReceiver.java index d18dfa1c1b..69e48dc8bf 100644 --- a/app/src/main/java/com/gh/gamecenter/receiver/InstallReceiver.java +++ b/app/src/main/java/com/gh/gamecenter/receiver/InstallReceiver.java @@ -7,7 +7,6 @@ import android.content.Intent; import android.os.Bundle; import com.gh.base.fragment.BaseFragment_ViewPager; -import com.gh.common.AppExecutor; import com.gh.common.constant.Constants; import com.gh.common.util.DownloadNotificationHelper; import com.gh.common.util.EntranceUtils; @@ -45,49 +44,42 @@ public class InstallReceiver extends BroadcastReceiver { } updateNotification(downloadEntity); - DownloadEntity finalDownloadEntity = downloadEntity; - AppExecutor.getIoExecutor().execute(() -> { - if (PackageUtils.isInstallable(context, path)) { - AppExecutor.getUiExecutor().execute(() -> { - if (finalDownloadEntity != null) { - PackageInstaller.install(context, finalDownloadEntity); - } else { - PackageInstaller.install(context, false, path); - } - }); + if (PackageUtils.isCanLaunchSetup(context, path)) { + if (downloadEntity != null) { + PackageInstaller.install(context, downloadEntity); } else { - AppExecutor.getUiExecutor().execute(() -> { - if (RunningUtils.isRunning(context)) { - if (RunningUtils.isEqualsTop(context, DownloadManagerActivity.class.getName())) { - // 这里是指从后台返回到前台 前两个的是关键 - Intent intent2 = new Intent(); - intent2.setAction(Intent.ACTION_MAIN); - intent2.addCategory(Intent.CATEGORY_LAUNCHER); - intent2.setComponent(new ComponentName(context, DownloadManagerActivity.class)); - intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); - context.startActivity(intent2); - - EventBus.getDefault().post(new EBMiPush("plugin_install", path)); - } else { - Intent intent2 = new Intent(context, DownloadManagerActivity.class); - intent2.putExtra(BaseFragment_ViewPager.ARGS_INDEX, 0); - intent2.putExtra(EntranceUtils.KEY_PATH, path); - intent2.putExtra(EntranceUtils.KEY_ENTRANCE, "(安装跳转)"); - intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - context.startActivity(intent2); - } - } else { - // 应用未在运行 - Bundle bundle = new Bundle(); - bundle.putString(KEY_TO, DownloadManagerActivity.TAG); - bundle.putInt(BaseFragment_ViewPager.ARGS_INDEX, 0); - bundle.putString(EntranceUtils.KEY_PATH, path); - bundle.putString(EntranceUtils.KEY_ENTRANCE, "(安装跳转)"); - context.startActivity(SplashScreenActivity.getSplashScreenIntent(context, bundle)); - } - }); + PackageInstaller.install(context, false, path); } - }); + } else { + if (RunningUtils.isRunning(context)) { + if (RunningUtils.isEqualsTop(context, DownloadManagerActivity.class.getName())) { + // 这里是指从后台返回到前台 前两个的是关键 + Intent intent2 = new Intent(); + intent2.setAction(Intent.ACTION_MAIN); + intent2.addCategory(Intent.CATEGORY_LAUNCHER); + intent2.setComponent(new ComponentName(context, DownloadManagerActivity.class)); + intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); + context.startActivity(intent2); + + EventBus.getDefault().post(new EBMiPush("plugin_install", path)); + } else { + Intent intent2 = new Intent(context, DownloadManagerActivity.class); + intent2.putExtra(BaseFragment_ViewPager.ARGS_INDEX, 0); + intent2.putExtra(EntranceUtils.KEY_PATH, path); + intent2.putExtra(EntranceUtils.KEY_ENTRANCE, "(安装跳转)"); + intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent2); + } + } else { + // 应用未在运行 + Bundle bundle = new Bundle(); + bundle.putString(KEY_TO, DownloadManagerActivity.TAG); + bundle.putInt(BaseFragment_ViewPager.ARGS_INDEX, 0); + bundle.putString(EntranceUtils.KEY_PATH, path); + bundle.putString(EntranceUtils.KEY_ENTRANCE, "(安装跳转)"); + context.startActivity(SplashScreenActivity.getSplashScreenIntent(context, bundle)); + } + } }); }