pull/1114/head
gedoor 3 years ago
parent ec102a5cd2
commit 3b30b6b496
  1. 4
      app/src/main/java/io/legado/app/App.kt
  2. 56
      app/src/main/java/io/legado/app/help/LifecycleHelp.kt
  3. 8
      app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt

@ -10,9 +10,9 @@ import com.jeremyliao.liveeventbus.LiveEventBus
import io.legado.app.constant.AppConst.channelIdDownload import io.legado.app.constant.AppConst.channelIdDownload
import io.legado.app.constant.AppConst.channelIdReadAloud import io.legado.app.constant.AppConst.channelIdReadAloud
import io.legado.app.constant.AppConst.channelIdWeb import io.legado.app.constant.AppConst.channelIdWeb
import io.legado.app.help.ActivityHelp
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.CrashHandler import io.legado.app.help.CrashHandler
import io.legado.app.help.LifecycleHelp
import io.legado.app.help.ThemeConfig.applyDayNight import io.legado.app.help.ThemeConfig.applyDayNight
import io.legado.app.utils.LanguageUtils import io.legado.app.utils.LanguageUtils
import io.legado.app.utils.defaultSharedPreferences import io.legado.app.utils.defaultSharedPreferences
@ -28,7 +28,7 @@ class App : MultiDexApplication() {
LiveEventBus.config() LiveEventBus.config()
.lifecycleObserverAlwaysActive(true) .lifecycleObserverAlwaysActive(true)
.autoClear(false) .autoClear(false)
registerActivityLifecycleCallbacks(ActivityHelp) registerActivityLifecycleCallbacks(LifecycleHelp)
defaultSharedPreferences.registerOnSharedPreferenceChangeListener(AppConfig) defaultSharedPreferences.registerOnSharedPreferenceChangeListener(AppConfig)
} }

@ -11,18 +11,18 @@ import java.util.*
* Activity管理器,管理项目中Activity的状态 * Activity管理器,管理项目中Activity的状态
*/ */
@Suppress("unused") @Suppress("unused")
object ActivityHelp : Application.ActivityLifecycleCallbacks { object LifecycleHelp : Application.ActivityLifecycleCallbacks {
private val activities: MutableList<WeakReference<Activity>> = arrayListOf() private val activities: MutableList<WeakReference<Activity>> = arrayListOf()
fun size(): Int { fun activitySize(): Int {
return activities.size return activities.size
} }
/** /**
* 判断指定Activity是否存在 * 判断指定Activity是否存在
*/ */
fun isExist(activityClass: Class<*>): Boolean { fun isExistActivity(activityClass: Class<*>): Boolean {
activities.forEach { item -> activities.forEach { item ->
if (item.get()?.javaClass == activityClass) { if (item.get()?.javaClass == activityClass) {
return true return true
@ -31,47 +31,6 @@ object ActivityHelp : Application.ActivityLifecycleCallbacks {
return false return false
} }
/**
* 添加Activity
*/
fun add(activity: Activity) {
activities.add(WeakReference(activity))
}
/**
* 移除Activity
*/
fun remove(activity: Activity) {
for (temp in activities) {
if (null != temp.get() && temp.get() === activity) {
activities.remove(temp)
break
}
}
}
/**
* 移除Activity
*/
fun remove(activityClass: Class<*>) {
val iterator = activities.iterator()
while (iterator.hasNext()) {
val item = iterator.next()
if (item.get()?.javaClass == activityClass) {
iterator.remove()
}
}
}
/**
* 关闭指定 activity
*/
fun finishActivity(vararg activities: Activity) {
activities.forEach { activity ->
activity.finish()
}
}
/** /**
* 关闭指定 activity(class) * 关闭指定 activity(class)
*/ */
@ -101,7 +60,12 @@ object ActivityHelp : Application.ActivityLifecycleCallbacks {
} }
override fun onActivityDestroyed(activity: Activity) { override fun onActivityDestroyed(activity: Activity) {
remove(activity) for (temp in activities) {
if (temp.get() === activity) {
activities.remove(temp)
break
}
}
} }
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) { override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
@ -111,7 +75,7 @@ object ActivityHelp : Application.ActivityLifecycleCallbacks {
} }
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
add(activity) activities.add(WeakReference(activity))
if (!LanguageUtils.isSameWithSetting(activity)){ if (!LanguageUtils.isSameWithSetting(activity)){
LanguageUtils.setConfiguration(activity) LanguageUtils.setConfiguration(activity)
} }

@ -6,8 +6,8 @@ import android.content.Intent
import android.view.KeyEvent import android.view.KeyEvent
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.help.ActivityHelp
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.LifecycleHelp
import io.legado.app.service.AudioPlayService import io.legado.app.service.AudioPlayService
import io.legado.app.service.BaseReadAloudService import io.legado.app.service.BaseReadAloudService
import io.legado.app.service.help.AudioPlay import io.legado.app.service.help.AudioPlay
@ -69,13 +69,13 @@ class MediaButtonReceiver : BroadcastReceiver() {
} else { } else {
AudioPlay.pause(context) AudioPlay.pause(context)
} }
ActivityHelp.isExist(ReadBookActivity::class.java) -> LifecycleHelp.isExistActivity(ReadBookActivity::class.java) ->
postEvent(EventBus.MEDIA_BUTTON, true) postEvent(EventBus.MEDIA_BUTTON, true)
ActivityHelp.isExist(AudioPlayActivity::class.java) -> LifecycleHelp.isExistActivity(AudioPlayActivity::class.java) ->
postEvent(EventBus.MEDIA_BUTTON, true) postEvent(EventBus.MEDIA_BUTTON, true)
else -> if (AppConfig.mediaButtonOnExit || !isMediaKey) { else -> if (AppConfig.mediaButtonOnExit || !isMediaKey) {
appDb.bookDao.lastReadBook?.let { appDb.bookDao.lastReadBook?.let {
if (!ActivityHelp.isExist(MainActivity::class.java)) { if (!LifecycleHelp.isExistActivity(MainActivity::class.java)) {
Intent(context, MainActivity::class.java).let { Intent(context, MainActivity::class.java).let {
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(it) context.startActivity(it)

Loading…
Cancel
Save