fix: 移除为了能正常收取 BroadcastReceiver 而创建的 InstallService (后续已改为回到应用刷新列表) https://sentry.shanqu.cc/organizations/lightgame/issues/389377/

This commit is contained in:
chenjuntao
2024-12-02 14:31:29 +08:00
parent 028974ec0d
commit d939aae901
4 changed files with 0 additions and 115 deletions

View File

@ -18,7 +18,6 @@ import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.MD5Utils
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.install.InstallService
import com.gh.vspace.VHelper
import com.halo.assistant.HaloApp
import com.lightgame.download.DownloadEntity
@ -194,12 +193,6 @@ object PackageInstaller {
private fun install(context: Context, pkgPath: String, pkgName: String?) {
HaloApp.put(Constants.LAST_INSTALL_GAME, pkgPath)
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU && Build.MANUFACTURER.lowercase().contains("xiaomi")) {
val foregroundServiceIntent = Intent(context, InstallService::class.java)
foregroundServiceIntent.putExtra(InstallService.KEY_SERVICE_ACTION, InstallService.START_FOREGROUND)
context.startForegroundService(foregroundServiceIntent)
}
val installIntent = getInstallIntent(context, pkgPath)
context.startActivity(installIntent)

View File

@ -1,93 +0,0 @@
package com.gh.gamecenter.install
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
import com.lightgame.download.ForegroundNotificationManager
import com.lightgame.utils.Utils
import java.util.*
class InstallService : Service() {
private var mForegroundTimer: Timer? = null
private var mForegroundNotificationManager: ForegroundNotificationManager? = null
override fun onCreate() {
super.onCreate()
mForegroundNotificationManager = ForegroundNotificationManager(this, this.application)
Utils.log(InstallService::class.java.simpleName, "onCreate")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Utils.log(InstallService::class.java.simpleName, "onStartCommand")
val notificationId = 9999
if (intent != null && intent.extras != null) {
val serviceAction = intent.getStringExtra(KEY_SERVICE_ACTION)
if (START_FOREGROUND == serviceAction) {
startForegroundIfNeeded(notificationId)
} else if (STOP_FOREGROUND == serviceAction) {
startForegroundIfNeeded(notificationId)
stopForegroundIfNeeded(notificationId)
}
} else {
startForegroundIfNeeded(notificationId)
stopForegroundIfNeeded(notificationId)
}
// 不需要 intent 为空的重建
return START_NOT_STICKY
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
private fun startForegroundIfNeeded(notificationId: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel =
NotificationChannel(SERVICE_CHANNEL_ID, SERVICE_CHANNEL_ID, NotificationManager.IMPORTANCE_LOW)
val manager = applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(channel)
val notification: Notification = NotificationCompat.Builder(this, SERVICE_CHANNEL_ID)
.setSmallIcon(com.lightgame.R.drawable.ic_download_notification)
.setContentTitle("光环助手安装服务")
.build()
mForegroundNotificationManager?.notify(notificationId, notification)
mForegroundTimer?.cancel()
mForegroundTimer = Timer()
val task: TimerTask = object : TimerTask() {
override fun run() {
stopForegroundIfNeeded(notificationId)
}
}
mForegroundTimer?.schedule(task, FOREGROUND_COUNT_DOWN_TIME)
}
}
private fun stopForegroundIfNeeded(notificationId: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mForegroundNotificationManager?.cancel(notificationId)
mForegroundTimer?.cancel()
mForegroundTimer = null
}
}
companion object {
private const val SERVICE_CHANNEL_ID = "安装服务"
private const val FOREGROUND_COUNT_DOWN_TIME = 20 * 1000L
const val START_FOREGROUND = "start_foreground"
const val STOP_FOREGROUND = "stop_foreground"
const val KEY_SERVICE_ACTION = "service_action"
}
}

View File

@ -19,7 +19,6 @@ import com.gh.gamecenter.core.utils.SPUtils;
import com.gh.gamecenter.entity.NewApiSettingsEntity;
import com.gh.gamecenter.eventbus.EBPackage;
import com.gh.gamecenter.feature.entity.GameInstall;
import com.gh.gamecenter.install.InstallService;
import com.gh.gamecenter.manager.PackagesManager;
import com.halo.assistant.HaloApp;
import com.lightgame.download.DownloadEntity;
@ -27,8 +26,6 @@ import com.lightgame.utils.Utils;
import org.greenrobot.eventbus.EventBus;
import java.util.Locale;
/**
* 监听安装包变更
*/
@ -49,14 +46,6 @@ public class PackageChangeBroadcastReceiver extends BroadcastReceiver {
PackageHelper.INSTANCE.dumpInstalledListCache();
ExtensionsKt.doOnMainProcessOnly(() -> {
Utils.log(TAG, "onReceive->" + intent.getAction() + "==" + intent.getDataString());
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU
&& Build.MANUFACTURER.toLowerCase(Locale.CHINA).contains("xiaomi")) {
Intent foregroundServiceIntent = new Intent(context, InstallService.class);
foregroundServiceIntent.putExtra(InstallService.KEY_SERVICE_ACTION, InstallService.STOP_FOREGROUND);
context.startForegroundService(foregroundServiceIntent);
}
// 接收安装广播
if (intent.getAction().equals(mActions.getAdd())) {
String packageName = intent.getDataString();