From ec6ad35adef509ce570027a1b4251bfcd2e14707 Mon Sep 17 00:00:00 2001 From: chenjuntao Date: Fri, 12 Apr 2024 14:52:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=87=8D=E6=9E=84=E9=99=90=E5=88=B6?= =?UTF-8?q?=E4=B8=BB=E8=BF=9B=E7=A8=8B=E6=89=A7=E8=A1=8C=E7=9A=84=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/gh/common/FixedRateJobHelper.kt | 102 +++++++-------- .../provider/PackageUtilsProviderImpl.kt | 5 +- .../java/com/gh/common/util/DataUtils.java | 9 +- .../java/com/gh/common/util/PackageUtils.java | 31 ----- .../receiver/ActivitySkipReceiver.java | 4 +- .../gamecenter/receiver/DownloadReceiver.java | 3 +- .../receiver/InstallAndUninstallReceiver.java | 3 +- .../gamecenter/receiver/InstallReceiver.java | 3 +- .../receiver/NetworkStateReceiver.java | 3 +- .../main/java/com/halo/assistant/HaloApp.java | 123 ++++++++---------- .../provider/PackageUtilsProviderImpl.kt | 2 +- .../provider/PackageUtilsProviderImpl.kt | 2 +- .../java/com/gh/gamecenter/qgame/HaloApp.kt | 2 +- .../java/com/gh/gamecenter/Injection.java | 7 - .../gh/gamecenter/common/utils/Extensions.kt | 27 +--- .../java/com/gh/gamecenter/Injection.java | 8 -- .../core/provider/IPackageUtilsProvider.kt | 2 +- .../gh/gamecenter/core/utils/ProcessUtil.java | 67 ++++++++++ .../provider/PackageUtilsProviderImpl.kt | 2 +- .../provider/PackageUtilsProviderImpl.kt | 2 +- .../provider/PackageUtilsProviderImpl.kt | 2 +- 21 files changed, 197 insertions(+), 212 deletions(-) create mode 100644 module_core/src/main/java/com/gh/gamecenter/core/utils/ProcessUtil.java diff --git a/app/src/main/java/com/gh/common/FixedRateJobHelper.kt b/app/src/main/java/com/gh/common/FixedRateJobHelper.kt index c9943845da..01234e1172 100644 --- a/app/src/main/java/com/gh/common/FixedRateJobHelper.kt +++ b/app/src/main/java/com/gh/common/FixedRateJobHelper.kt @@ -2,17 +2,16 @@ package com.gh.common import com.gh.common.exposure.ExposureManager import com.gh.common.filter.RegionSettingHelper -import com.gh.common.util.AdHelper import com.gh.common.videolog.VideoRecordUtils import com.gh.download.DownloadDataHelper import com.gh.gamecenter.common.loghub.LoghubUtils import com.gh.gamecenter.common.retrofit.Response -import com.gh.gamecenter.common.utils.doOnMainProcessOnly import com.gh.gamecenter.common.utils.tryCatchInRelease import com.gh.gamecenter.core.runOnUiThread import com.gh.gamecenter.entity.TimeEntity import com.gh.gamecenter.retrofit.RetrofitManager import com.halo.assistant.HaloApp +import com.lightgame.utils.Utils import io.reactivex.schedulers.Schedulers import kotlin.concurrent.fixedRateTimer @@ -26,7 +25,6 @@ object FixedRateJobHelper { private const val DOWNLOAD_HEARTBEAT_PERIOD: Long = 60 * 1000L private const val DOWNLOAD_HEARTBEAT_SHEET_PERIOD: Long = 15 * 1000L - private const val STARTUP_AD: Long = 30 * 60 * 1000L private var mExecuteCount: Int = 0 @@ -34,58 +32,56 @@ object FixedRateJobHelper { @JvmStatic fun begin() { - doOnMainProcessOnly { - // 时间检查,每15秒检查一次 - fixedRateTimer("Global-Fixed-Rate-Timer", initialDelay = 100, period = CHECKER_PERIOD) { - val elapsedTime = mExecuteCount * CHECKER_PERIOD - // 时间校对,10分钟一次 - if (elapsedTime % TIME_PERIOD == 0L) { - RetrofitManager.getInstance().api.time - .subscribeOn(Schedulers.io()) - .subscribe(object : Response() { - override fun onResponse(response: TimeEntity?) { - val serverTime = response?.time - serverTime?.let { - timeDeltaBetweenServerAndClient = it * 1000 - System.currentTimeMillis() - } + // 时间检查,每15秒检查一次 + fixedRateTimer("Global-Fixed-Rate-Timer", initialDelay = 100, period = CHECKER_PERIOD) { + val elapsedTime = mExecuteCount * CHECKER_PERIOD + // 时间校对,10分钟一次 + if (elapsedTime % TIME_PERIOD == 0L) { + RetrofitManager.getInstance().api.time + .subscribeOn(Schedulers.io()) + .subscribe(object : Response() { + override fun onResponse(response: TimeEntity?) { + val serverTime = response?.time + serverTime?.let { + timeDeltaBetweenServerAndClient = it * 1000 - System.currentTimeMillis() } - }) - } - - // 提交曝光数据 - if (elapsedTime % EXPOSURE_PERIOD == 0L) { - ExposureManager.commitSavedExposureEvents(true) - } - - // 分片检测下载进度 - if (elapsedTime % DOWNLOAD_HEARTBEAT_SHEET_PERIOD == 0L) { - tryCatchInRelease { - val upload = (mExecuteCount * CHECKER_PERIOD) % DOWNLOAD_HEARTBEAT_PERIOD == 0L - DownloadDataHelper.uploadDownloadHeartbeat(upload) - } - } - - // 提交普通 loghub 数据 - if (elapsedTime % LOGHUB_PERIOD == 0L) { - runOnUiThread { - LoghubUtils.commitSavedLoghubEvents(true) - } - } - - // 更新游戏屏蔽信息 - if (elapsedTime % REGION_SETTING_PERIOD == 0L) { - if (HaloApp.getInstance().isRunningForeground) { - RegionSettingHelper.getRegionSetting() - } - } - - // 提交视频浏览记录数据 - if (elapsedTime % VIDEO_RECORD_PERIOD == 0L) { - VideoRecordUtils.commitVideoRecord() - } - - mExecuteCount++ + } + }) } + + // 提交曝光数据 + if (elapsedTime % EXPOSURE_PERIOD == 0L) { + ExposureManager.commitSavedExposureEvents(true) + } + + // 分片检测下载进度 + if (elapsedTime % DOWNLOAD_HEARTBEAT_SHEET_PERIOD == 0L) { + tryCatchInRelease { + val upload = (mExecuteCount * CHECKER_PERIOD) % DOWNLOAD_HEARTBEAT_PERIOD == 0L + DownloadDataHelper.uploadDownloadHeartbeat(upload) + } + } + + // 提交普通 loghub 数据 + if (elapsedTime % LOGHUB_PERIOD == 0L) { + runOnUiThread { + LoghubUtils.commitSavedLoghubEvents(true) + } + } + + // 更新游戏屏蔽信息 + if (elapsedTime % REGION_SETTING_PERIOD == 0L) { + if (HaloApp.getInstance().isRunningForeground) { + RegionSettingHelper.getRegionSetting() + } + } + + // 提交视频浏览记录数据 + if (elapsedTime % VIDEO_RECORD_PERIOD == 0L) { + VideoRecordUtils.commitVideoRecord() + } + + mExecuteCount++ } } } \ No newline at end of file diff --git a/app/src/main/java/com/gh/common/provider/PackageUtilsProviderImpl.kt b/app/src/main/java/com/gh/common/provider/PackageUtilsProviderImpl.kt index f854fabac1..46881f91a2 100644 --- a/app/src/main/java/com/gh/common/provider/PackageUtilsProviderImpl.kt +++ b/app/src/main/java/com/gh/common/provider/PackageUtilsProviderImpl.kt @@ -6,11 +6,12 @@ import com.alibaba.android.arouter.facade.annotation.Route import com.gh.common.util.PackageUtils import com.gh.gamecenter.common.constant.RouteConsts import com.gh.gamecenter.core.provider.IPackageUtilsProvider +import com.gh.gamecenter.core.utils.ProcessUtil @Route(path = RouteConsts.provider.packageUtils, name = "PackageUtils暴露服务") class PackageUtilsProviderImpl : IPackageUtilsProvider { - override fun obtainProcessName(context: Context): String? { - return PackageUtils.obtainProcessName(context) + override fun obtainProcessName(): String? { + return ProcessUtil.getCurrentProcessName() } override fun getGhVersionName(): String { diff --git a/app/src/main/java/com/gh/common/util/DataUtils.java b/app/src/main/java/com/gh/common/util/DataUtils.java index 51380fb117..5511c228ad 100644 --- a/app/src/main/java/com/gh/common/util/DataUtils.java +++ b/app/src/main/java/com/gh/common/util/DataUtils.java @@ -125,9 +125,7 @@ public class DataUtils { HaloApp.getInstance().setGid(gid); // 更新广告配置 - ExtensionsKt.doOnMainProcessOnly(HaloApp.getInstance(), () -> { - AdDelegateHelper.INSTANCE.requestAdConfig(false, "", null); - }); + AdDelegateHelper.INSTANCE.requestAdConfig(false, "", null); getDeviceCertification(gid); @@ -147,11 +145,8 @@ public class DataUtils { @Override public void onFailure(String s) { - MtaHelper.onEventWithBasicDeviceInfo("开发辅助", "GID 获取异常", s); // 更新广告配置 - ExtensionsKt.doOnMainProcessOnly(HaloApp.getInstance(), () -> { - AdDelegateHelper.INSTANCE.requestAdConfig(false, "", null); - }); + AdDelegateHelper.INSTANCE.requestAdConfig(false, "", null); } }); } 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 e511150126..96b8157ef5 100644 --- a/app/src/main/java/com/gh/common/util/PackageUtils.java +++ b/app/src/main/java/com/gh/common/util/PackageUtils.java @@ -888,37 +888,6 @@ public class PackageUtils { && !PackageUtils.isSignedByGh(HaloApp.getInstance().getApplication(), apkEntity.getPackageName()); } - /** - * 获取调用者的进程名 - * - * @param context 调用者的上下文 - * @return 进程名 - */ - public static String obtainProcessName(Context context) { - if (PackageFlavorHelper.IS_TEST_FLAVOR) { - try { - final int pid = android.os.Process.myPid(); - ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); - List listTaskInfo = am.getRunningAppProcesses(); - if (listTaskInfo != null && !listTaskInfo.isEmpty()) { - for (ActivityManager.RunningAppProcessInfo info : listTaskInfo) { - if (info != null && info.pid == pid) { - return info.processName; - } - } - } - } catch (Exception e) { - // 遇到异常了让这次调用正常执行 - e.printStackTrace(); - return BuildConfig.APPLICATION_ID; - } - } else { - return null; - } - return null; - } - - /** * 应用是否在前台运行 */ diff --git a/app/src/main/java/com/gh/gamecenter/receiver/ActivitySkipReceiver.java b/app/src/main/java/com/gh/gamecenter/receiver/ActivitySkipReceiver.java index 1f4e818e76..2dd5a54e07 100644 --- a/app/src/main/java/com/gh/gamecenter/receiver/ActivitySkipReceiver.java +++ b/app/src/main/java/com/gh/gamecenter/receiver/ActivitySkipReceiver.java @@ -21,7 +21,7 @@ public class ActivitySkipReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - ExtensionsKt.doOnMainProcessOnly(context, () -> { + ExtensionsKt.doOnMainProcessOnly(() -> { if (ACTION_ACTIVITY_SKIP.equals(intent.getAction())) { Bundle bundle = intent.getExtras(); if (HaloApp.getInstance().isRunningForeground && bundle != null) { @@ -41,6 +41,8 @@ public class ActivitySkipReceiver extends BroadcastReceiver { context.startActivity(SplashScreenActivity.getSplashScreenIntent(context, bundle)); } } + + return null; }); } diff --git a/app/src/main/java/com/gh/gamecenter/receiver/DownloadReceiver.java b/app/src/main/java/com/gh/gamecenter/receiver/DownloadReceiver.java index f93d4fe959..2844da1203 100644 --- a/app/src/main/java/com/gh/gamecenter/receiver/DownloadReceiver.java +++ b/app/src/main/java/com/gh/gamecenter/receiver/DownloadReceiver.java @@ -19,7 +19,7 @@ public class DownloadReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - ExtensionsKt.doOnMainProcessOnly(context, () -> { + ExtensionsKt.doOnMainProcessOnly(() -> { try { if (DownloadNotificationHelper.ACTION_VDOWNLOAD.equals(intent.getAction())) { DirectUtils.directToVGameDownload(context, "其他", true); @@ -32,6 +32,7 @@ public class DownloadReceiver extends BroadcastReceiver { ToastUtils.toast(exception.getMessage()); } } + return null; }); } diff --git a/app/src/main/java/com/gh/gamecenter/receiver/InstallAndUninstallReceiver.java b/app/src/main/java/com/gh/gamecenter/receiver/InstallAndUninstallReceiver.java index bb520ab188..5857e71cc8 100644 --- a/app/src/main/java/com/gh/gamecenter/receiver/InstallAndUninstallReceiver.java +++ b/app/src/main/java/com/gh/gamecenter/receiver/InstallAndUninstallReceiver.java @@ -42,7 +42,7 @@ public class InstallAndUninstallReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { PackageUtils.dumpInstalledListCache(); - ExtensionsKt.doOnMainProcessOnly(context, () -> { + ExtensionsKt.doOnMainProcessOnly(() -> { Utils.log("InstallAndUninstallReceiver:: onReceive->" + intent.getAction() + "==" + intent.getDataString()); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU @@ -120,6 +120,7 @@ public class InstallAndUninstallReceiver extends BroadcastReceiver { HaloApp.getInstance().getWebviewAbiList(); } } + return null; }); } 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 f10c62940a..60bccb6f64 100644 --- a/app/src/main/java/com/gh/gamecenter/receiver/InstallReceiver.java +++ b/app/src/main/java/com/gh/gamecenter/receiver/InstallReceiver.java @@ -40,7 +40,7 @@ public class InstallReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - ExtensionsKt.doOnMainProcessOnly(context, () -> { + ExtensionsKt.doOnMainProcessOnly(() -> { String path = intent.getStringExtra(EntranceConsts.KEY_PATH); DownloadEntity downloadEntity; String downloadUrl = intent.getStringExtra(EntranceConsts.KEY_URL); @@ -115,6 +115,7 @@ public class InstallReceiver extends BroadcastReceiver { context.startActivity(SplashScreenActivity.getSplashScreenIntent(context, bundle)); } } + return null; }); } diff --git a/app/src/main/java/com/gh/gamecenter/receiver/NetworkStateReceiver.java b/app/src/main/java/com/gh/gamecenter/receiver/NetworkStateReceiver.java index d18a3a54c0..17b43e9b71 100644 --- a/app/src/main/java/com/gh/gamecenter/receiver/NetworkStateReceiver.java +++ b/app/src/main/java/com/gh/gamecenter/receiver/NetworkStateReceiver.java @@ -23,7 +23,7 @@ public class NetworkStateReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { // 网络变更这里会被主进程和推送进程分别调用,这里只对主进程响应, // 奇怪,初次注册监听就会有回调,会导致部分接口短时间内触发两次调用 - ExtensionsKt.doOnMainProcessOnly(context, () -> { + ExtensionsKt.doOnMainProcessOnly(() -> { MetaUtil.updateCachedNetwork(); if (NetworkUtils.isNetworkConnected(context)) { AdDelegateHelper.INSTANCE.requestAdConfig(true, "", null); @@ -35,6 +35,7 @@ public class NetworkStateReceiver extends BroadcastReceiver { HaloApp.put(Constants.SHOULD_SHOW_VIDEO_MOBILE_WARNING, true); } } + return null; }); } } diff --git a/app/src/main/java/com/halo/assistant/HaloApp.java b/app/src/main/java/com/halo/assistant/HaloApp.java index 837d9566e1..6a83ae7450 100644 --- a/app/src/main/java/com/halo/assistant/HaloApp.java +++ b/app/src/main/java/com/halo/assistant/HaloApp.java @@ -210,10 +210,6 @@ public class HaloApp extends MultiDexApplication { super.onCreate(); initArouter(); - if (!Injection.appInit(this)) { - return; - } - mInstance = this; // 每个进程都用自己的进程名作为后缀的文件夹来存 WebView cache @@ -229,57 +225,59 @@ public class HaloApp extends MultiDexApplication { } } - for (IApplication application : mApplicationList) { - application.onCreate(mInstance); - } + ExtensionsKt.doOnMainProcessOnly(() -> { + for (IApplication application : mApplicationList) { + application.onCreate(mInstance); + } - // 似乎只是 load SO 不涉及方法调用,所以可以在隐私政策前调用吧? - OAIDHelper.INSTANCE.doSystemLoad(); + // 似乎只是 load SO 不涉及方法调用,所以可以在隐私政策前调用吧? + OAIDHelper.INSTANCE.doSystemLoad(); - // 70ms - PlayerFactory.setPlayManager(Exo2PlayerManager.class); - CacheFactory.setCacheManager(ExoPlayerCacheManager.class); + PlayerFactory.setPlayManager(Exo2PlayerManager.class); + CacheFactory.setCacheManager(ExoPlayerCacheManager.class); - initFresco(); + initFresco(); - isNewForThisVersion = - PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.SP_NEW_FIRST_LAUNCH_VERSION + PackageUtils.getGhVersionName(), true); + isNewForThisVersion = + PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.SP_NEW_FIRST_LAUNCH_VERSION + PackageUtils.getGhVersionName(), true); - AppExecutor.getIoExecutor().execute(() -> { - initDataHelper(); - ExtensionsKt.doOnMainProcessOnly(this, () -> { + AppExecutor.getIoExecutor().execute(() -> { + initDataHelper(); Tracker.init(this); DownloadCore.init(this); + + deviceRamSize = DeviceUtils.getTotalRamSizeOfDevice(this); + mChannel = mFlavorProvider.getChannelStr(this); + + // 初始化推送 + initPushEngine(); + + // 异步初始化 SP + SPUtils.getString(""); }); - deviceRamSize = DeviceUtils.getTotalRamSizeOfDevice(this); - mChannel = mFlavorProvider.getChannelStr(this); + RxJavaPlugins.setIoSchedulerHandler(scheduler -> AppExecutor.INSTANCE.getCachedScheduler()); - // 初始化阿里云推送 - ExtensionsKt.doOnMainProcessOnly(this, this::initPushEngine); + if (isUserAcceptPrivacyPolicy(this)) { + postInit(false); + } - // 异步初始化 SP - SPUtils.getString(""); + //设置严格模式 + if (BuildConfig.DEBUG) { + StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() + .detectAll() + .penaltyLog() + .build(); + StrictMode.setVmPolicy(policy); + } + + registerActivityLifecycleCallbacks(new GlobalActivityLifecycleObserver()); + + DarkModeUtils.INSTANCE.initDarkMode(); + + return null; }); - RxJavaPlugins.setIoSchedulerHandler(scheduler -> AppExecutor.INSTANCE.getCachedScheduler()); - - if (isUserAcceptPrivacyPolicy(this)) { - postInit(false); - } - - //设置严格模式 - if (BuildConfig.DEBUG) { - StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() - .detectAll() - .penaltyLog() - .build(); - StrictMode.setVmPolicy(policy); - } - - registerActivityLifecycleCallbacks(new GlobalActivityLifecycleObserver()); - - DarkModeUtils.INSTANCE.initDarkMode(); } /** @@ -302,9 +300,7 @@ public class HaloApp extends MultiDexApplication { DataUtils.init(this, mChannel); // 初始化广告 SDK - ExtensionsKt.doOnMainProcessOnly(this, () -> { - AdDelegateHelper.INSTANCE.initAdSdk(this); - }); + AdDelegateHelper.INSTANCE.initAdSdk(this); SignatureRepository.INSTANCE.init(); @@ -314,29 +310,26 @@ public class HaloApp extends MultiDexApplication { // https://jira.shanqu.cc/browse/GHZS-3765 // 港澳APP去掉oaid的获取 if (!EnvHelper.isGATApp()) { - ExtensionsKt.doOnMainProcessOnly(() -> { - OAIDHelper.INSTANCE.getOAID(HaloApp.this, (s, isSuccess) -> { - setOAID(s); - MetaUtil.INSTANCE.refreshMeta(); - SensorsBridge.INSTANCE.setOAID(s); + OAIDHelper.INSTANCE.getOAID(HaloApp.this, (s, isSuccess) -> { + setOAID(s); + MetaUtil.INSTANCE.refreshMeta(); + SensorsBridge.INSTANCE.setOAID(s); - registerPushEngine(); + registerPushEngine(); - // 上报设备安装事件 - if (isNewForThisVersion) { - final LunchType launchType = getLaunchType(); - LogUtils.uploadDevice(launchType); + // 上报设备安装事件 + if (isNewForThisVersion) { + final LunchType launchType = getLaunchType(); + LogUtils.uploadDevice(launchType); - ActivationHelper.sendActivationInfo(); - } - return null; - }); + ActivationHelper.sendActivationInfo(); + } return null; }); } // 获取 settings 配置 - ExtensionsKt.doOnMainProcessOnly(this, com.gh.common.constant.Config::getGhzsSettings); + com.gh.common.constant.Config.getGhzsSettings(); String localTemporaryDeviceId = SPUtils.getString(Constants.SP_TEMPORARY_DEVICE_ID); if (!TextUtils.isEmpty(localTemporaryDeviceId)) { @@ -356,11 +349,9 @@ public class HaloApp extends MultiDexApplication { RegionSettingHelper.getRegionSetting(); - ExtensionsKt.doOnMainProcessOnly(this, () -> { - PackageRepository.initData(); - PackageHelper.refreshLocalPackageList(); - PackageHelper.initList(); - }); + PackageRepository.initData(); + PackageHelper.refreshLocalPackageList(); + PackageHelper.initList(); initReceiver(); initPackageChangesReceiver(); @@ -374,7 +365,7 @@ public class HaloApp extends MultiDexApplication { ProcessLifecycleOwner.get().getLifecycle().addObserver(new ProcessorLifeCycleOwner()); // 初始化畅玩相关数据 - ExtensionsKt.doOnMainProcessOnly(this, this::retrieveVGameInfoIfNeeded); + retrieveVGameInfoIfNeeded(); // 开发环境不要强制捕获相关异常,这些异常通常是需要处理的 if (!BuildConfig.DEBUG) { diff --git a/feature/floating-window/src/demo/java/com/gh/gamecenter/feedback/provider/PackageUtilsProviderImpl.kt b/feature/floating-window/src/demo/java/com/gh/gamecenter/feedback/provider/PackageUtilsProviderImpl.kt index 7197d9a62a..77a69c7d7a 100644 --- a/feature/floating-window/src/demo/java/com/gh/gamecenter/feedback/provider/PackageUtilsProviderImpl.kt +++ b/feature/floating-window/src/demo/java/com/gh/gamecenter/feedback/provider/PackageUtilsProviderImpl.kt @@ -8,7 +8,7 @@ import com.gh.gamecenter.core.provider.IPackageUtilsProvider @Route(path = RouteConsts.provider.packageUtils, name = "PackageUtils暴露服务") class PackageUtilsProviderImpl : IPackageUtilsProvider { - override fun obtainProcessName(context: Context): String? { + override fun obtainProcessName(): String? { return "" } diff --git a/feature/new_feedback/src/feedback/java/com/gh/gamecenter/feedback/provider/PackageUtilsProviderImpl.kt b/feature/new_feedback/src/feedback/java/com/gh/gamecenter/feedback/provider/PackageUtilsProviderImpl.kt index b12adbb256..b762b7076c 100644 --- a/feature/new_feedback/src/feedback/java/com/gh/gamecenter/feedback/provider/PackageUtilsProviderImpl.kt +++ b/feature/new_feedback/src/feedback/java/com/gh/gamecenter/feedback/provider/PackageUtilsProviderImpl.kt @@ -8,7 +8,7 @@ import com.gh.gamecenter.core.provider.IPackageUtilsProvider @Route(path = RouteConsts.provider.packageUtils, name = "PackageUtils暴露服务") class PackageUtilsProviderImpl : IPackageUtilsProvider { - override fun obtainProcessName(context: Context): String? { + override fun obtainProcessName(): String? { return "" } diff --git a/feature/qq_game/src/main/java/com/gh/gamecenter/qgame/HaloApp.kt b/feature/qq_game/src/main/java/com/gh/gamecenter/qgame/HaloApp.kt index 4f9416b6fa..3034a0a449 100644 --- a/feature/qq_game/src/main/java/com/gh/gamecenter/qgame/HaloApp.kt +++ b/feature/qq_game/src/main/java/com/gh/gamecenter/qgame/HaloApp.kt @@ -61,6 +61,6 @@ class HaloApp : IApplication { private fun getProcessName(application: Application): String { val packageUtilsConfig = ARouter.getInstance().build(RouteConsts.provider.packageUtils).navigation() as? IPackageUtilsProvider - return packageUtilsConfig?.obtainProcessName(application) ?: application.packageName + return packageUtilsConfig?.obtainProcessName() ?: application.packageName } } \ No newline at end of file diff --git a/module_common/src/debug/java/com/gh/gamecenter/Injection.java b/module_common/src/debug/java/com/gh/gamecenter/Injection.java index c97cfd88c4..aff1cc6293 100644 --- a/module_common/src/debug/java/com/gh/gamecenter/Injection.java +++ b/module_common/src/debug/java/com/gh/gamecenter/Injection.java @@ -13,12 +13,6 @@ import okhttp3.logging.HttpLoggingInterceptor; public class Injection { - public static boolean appInit(Application application) { - // 监控Bundle大小,预防溢出(需要调试的时候再开启吧!) -// TooLargeTool.startLogging(application); - return true; - } - public static OkHttpClient.Builder provideRetrofitBuilder() { OkHttpClient.Builder builder = new OkHttpClient.Builder(); HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); @@ -27,5 +21,4 @@ public class Injection { return builder; } - } diff --git a/module_common/src/main/java/com/gh/gamecenter/common/utils/Extensions.kt b/module_common/src/main/java/com/gh/gamecenter/common/utils/Extensions.kt index cf8c22b702..63596deeda 100644 --- a/module_common/src/main/java/com/gh/gamecenter/common/utils/Extensions.kt +++ b/module_common/src/main/java/com/gh/gamecenter/common/utils/Extensions.kt @@ -1127,37 +1127,12 @@ fun DownloadEntity.putGameCategory(gameCategory: String) { addMetaExtra(Constants.GAME_CATEGORY, gameCategory) } -/** - * Process related - */ -fun Context.doOnMainProcessOnly(callback: EmptyCallback) { - doOnMainProcessOnly { callback.onCallback() } -} - -/** - * 虽然现在我们没有了友盟以后只是单进程APP,但在 debug 模式下还有 whatTheStack 这个进程如果不限定主进程会出现奇奇怪怪的问题 (BroadcastReceiver相关) - */ -inline fun Context.doOnMainProcessOnly(f: () -> Unit) { - val buildConfig = - ARouter.getInstance().build(RouteConsts.provider.buildConfig).navigation() as? IBuildConfigProvider - val packageUtilsConfig = - ARouter.getInstance().build(RouteConsts.provider.packageUtils).navigation() as? IPackageUtilsProvider - val processName = packageUtilsConfig?.obtainProcessName(this) - if (processName == null || buildConfig?.getApplicationId() == processName) { - f.invoke() - } else { - tryWithDefaultCatch { - Utils.log("Block one useless sub process method call from ${Thread.currentThread().stackTrace[3].methodName} -> ${Thread.currentThread().stackTrace[2].methodName}.") - } - } -} - inline fun doOnMainProcessOnly(f: () -> Unit) { val buildConfig = ARouter.getInstance().build(RouteConsts.provider.buildConfig).navigation() as? IBuildConfigProvider val packageUtilsConfig = ARouter.getInstance().build(RouteConsts.provider.packageUtils).navigation() as? IPackageUtilsProvider - val processName = packageUtilsConfig?.obtainProcessName(HaloApp.getInstance()) + val processName = packageUtilsConfig?.obtainProcessName() if (processName == null || buildConfig?.getApplicationId() == processName) { f.invoke() } else { diff --git a/module_common/src/release/java/com/gh/gamecenter/Injection.java b/module_common/src/release/java/com/gh/gamecenter/Injection.java index 104ed9a42a..a4598fe216 100644 --- a/module_common/src/release/java/com/gh/gamecenter/Injection.java +++ b/module_common/src/release/java/com/gh/gamecenter/Injection.java @@ -2,8 +2,6 @@ package com.gh.gamecenter; import android.app.Application; -//import com.gh.base.AppUncaughtHandler; - import okhttp3.OkHttpClient; /** @@ -14,12 +12,6 @@ import okhttp3.OkHttpClient; public class Injection { - public static boolean appInit(Application application) { -// AppUncaughtHandler appUncaughtHandler = new AppUncaughtHandler(application); -// Thread.setDefaultUncaughtExceptionHandler(appUncaughtHandler); - return true; - } - public static OkHttpClient.Builder provideRetrofitBuilder() { return new OkHttpClient.Builder(); } diff --git a/module_core/src/main/java/com/gh/gamecenter/core/provider/IPackageUtilsProvider.kt b/module_core/src/main/java/com/gh/gamecenter/core/provider/IPackageUtilsProvider.kt index ce51073d04..0380e60272 100644 --- a/module_core/src/main/java/com/gh/gamecenter/core/provider/IPackageUtilsProvider.kt +++ b/module_core/src/main/java/com/gh/gamecenter/core/provider/IPackageUtilsProvider.kt @@ -6,7 +6,7 @@ import com.alibaba.android.arouter.facade.template.IProvider interface IPackageUtilsProvider : IProvider { - fun obtainProcessName(context: Context): String? + fun obtainProcessName(): String? fun getGhVersionName(): String diff --git a/module_core/src/main/java/com/gh/gamecenter/core/utils/ProcessUtil.java b/module_core/src/main/java/com/gh/gamecenter/core/utils/ProcessUtil.java new file mode 100644 index 0000000000..7bff86d457 --- /dev/null +++ b/module_core/src/main/java/com/gh/gamecenter/core/utils/ProcessUtil.java @@ -0,0 +1,67 @@ +package com.gh.gamecenter.core.utils; + +import android.app.Application; +import android.os.Build; +import android.text.TextUtils; + +import androidx.annotation.Nullable; + +import java.lang.reflect.Method; + +public class ProcessUtil { + private static String currentProcessName; + + /** + * @return 当前进程名 + */ + @Nullable + public static String getCurrentProcessName() { + if (!TextUtils.isEmpty(currentProcessName)) { + return currentProcessName; + } + + //1)通过Application的API获取当前进程名 + currentProcessName = getCurrentProcessNameByApplication(); + if (!TextUtils.isEmpty(currentProcessName)) { + return currentProcessName; + } + + //2)通过反射ActivityThread获取当前进程名 + currentProcessName = getCurrentProcessNameByActivityThread(); + if (!TextUtils.isEmpty(currentProcessName)) { + return currentProcessName; + } + + return currentProcessName; + } + + /** + * 通过Application新的API获取进程名,无需反射,无需IPC,效率最高。 + */ + public static String getCurrentProcessNameByApplication() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + return Application.getProcessName(); + } + return null; + } + + /** + * 通过反射ActivityThread获取进程名,避免了ipc + */ + public static String getCurrentProcessNameByActivityThread() { + String processName = null; + try { + final Method declaredMethod = Class.forName("android.app.ActivityThread", false, Application.class.getClassLoader()) + .getDeclaredMethod("currentProcessName", (Class[]) new Class[0]); + declaredMethod.setAccessible(true); + final Object invoke = declaredMethod.invoke(null, new Object[0]); + if (invoke instanceof String) { + processName = (String) invoke; + } + } catch (Throwable e) { + e.printStackTrace(); + } + return processName; + } + +} \ No newline at end of file diff --git a/module_message/src/message/java/com/gh/gamecenter/message/provider/PackageUtilsProviderImpl.kt b/module_message/src/message/java/com/gh/gamecenter/message/provider/PackageUtilsProviderImpl.kt index e31aaf2929..44f15ecb3e 100644 --- a/module_message/src/message/java/com/gh/gamecenter/message/provider/PackageUtilsProviderImpl.kt +++ b/module_message/src/message/java/com/gh/gamecenter/message/provider/PackageUtilsProviderImpl.kt @@ -8,7 +8,7 @@ import com.gh.gamecenter.core.provider.IPackageUtilsProvider @Route(path = RouteConsts.provider.packageUtils, name = "PackageUtils暴露服务") class PackageUtilsProviderImpl : IPackageUtilsProvider { - override fun obtainProcessName(context: Context): String? { + override fun obtainProcessName(): String? { return "" } diff --git a/module_setting/src/setting/java/com/gh/gamecenter/setting/provider/PackageUtilsProviderImpl.kt b/module_setting/src/setting/java/com/gh/gamecenter/setting/provider/PackageUtilsProviderImpl.kt index 441ec48634..4027d2d0df 100644 --- a/module_setting/src/setting/java/com/gh/gamecenter/setting/provider/PackageUtilsProviderImpl.kt +++ b/module_setting/src/setting/java/com/gh/gamecenter/setting/provider/PackageUtilsProviderImpl.kt @@ -8,7 +8,7 @@ import com.gh.gamecenter.core.provider.IPackageUtilsProvider @Route(path = RouteConsts.provider.packageUtils, name = "PackageUtils暴露服务") class PackageUtilsProviderImpl : IPackageUtilsProvider { - override fun obtainProcessName(context: Context): String { + override fun obtainProcessName(): String { return "" } diff --git a/module_setting_compose/src/setting/java/com/gh/gamecenter/setting/compose/provider/PackageUtilsProviderImpl.kt b/module_setting_compose/src/setting/java/com/gh/gamecenter/setting/compose/provider/PackageUtilsProviderImpl.kt index 350b1ce511..b13465300c 100644 --- a/module_setting_compose/src/setting/java/com/gh/gamecenter/setting/compose/provider/PackageUtilsProviderImpl.kt +++ b/module_setting_compose/src/setting/java/com/gh/gamecenter/setting/compose/provider/PackageUtilsProviderImpl.kt @@ -8,7 +8,7 @@ import com.gh.gamecenter.core.provider.IPackageUtilsProvider @Route(path = RouteConsts.provider.packageUtils, name = "PackageUtils暴露服务") class PackageUtilsProviderImpl : IPackageUtilsProvider { - override fun obtainProcessName(context: Context): String { + override fun obtainProcessName(): String { return "" }