|
|
|
@ -3,6 +3,8 @@ package io.legado.app.help |
|
|
|
|
import android.app.Activity |
|
|
|
|
import android.app.Application |
|
|
|
|
import android.os.Bundle |
|
|
|
|
import io.legado.app.base.BaseService |
|
|
|
|
import io.legado.app.help.coroutine.Coroutine |
|
|
|
|
import io.legado.app.utils.LanguageUtils |
|
|
|
|
import java.lang.ref.WeakReference |
|
|
|
|
import java.util.* |
|
|
|
@ -11,18 +13,20 @@ import java.util.* |
|
|
|
|
* Activity管理器,管理项目中Activity的状态 |
|
|
|
|
*/ |
|
|
|
|
@Suppress("unused") |
|
|
|
|
object ActivityHelp : Application.ActivityLifecycleCallbacks { |
|
|
|
|
object LifecycleHelp : Application.ActivityLifecycleCallbacks { |
|
|
|
|
|
|
|
|
|
private val activities: MutableList<WeakReference<Activity>> = arrayListOf() |
|
|
|
|
private val services: MutableList<WeakReference<BaseService>> = arrayListOf() |
|
|
|
|
private var finishedListener: (() -> Unit)? = null |
|
|
|
|
|
|
|
|
|
fun size(): Int { |
|
|
|
|
fun activitySize(): Int { |
|
|
|
|
return activities.size |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 判断指定Activity是否存在 |
|
|
|
|
*/ |
|
|
|
|
fun isExist(activityClass: Class<*>): Boolean { |
|
|
|
|
fun isExistActivity(activityClass: Class<*>): Boolean { |
|
|
|
|
activities.forEach { item -> |
|
|
|
|
if (item.get()?.javaClass == activityClass) { |
|
|
|
|
return true |
|
|
|
@ -31,47 +35,6 @@ object ActivityHelp : Application.ActivityLifecycleCallbacks { |
|
|
|
|
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) |
|
|
|
|
*/ |
|
|
|
@ -90,6 +53,10 @@ object ActivityHelp : Application.ActivityLifecycleCallbacks { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun setOnFinishedListener(finishedListener: (() -> Unit)) { |
|
|
|
|
this.finishedListener = finishedListener |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onActivityPaused(activity: Activity) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -101,7 +68,15 @@ object ActivityHelp : Application.ActivityLifecycleCallbacks { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onActivityDestroyed(activity: Activity) { |
|
|
|
|
remove(activity) |
|
|
|
|
for (temp in activities) { |
|
|
|
|
if (temp.get() != null && temp.get() === activity) { |
|
|
|
|
activities.remove(temp) |
|
|
|
|
if (services.size == 0 && activities.size == 0) { |
|
|
|
|
onFinished() |
|
|
|
|
} |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) { |
|
|
|
@ -111,9 +86,32 @@ object ActivityHelp : Application.ActivityLifecycleCallbacks { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { |
|
|
|
|
add(activity) |
|
|
|
|
if (!LanguageUtils.isSameWithSetting(activity)){ |
|
|
|
|
activities.add(WeakReference(activity)) |
|
|
|
|
if (!LanguageUtils.isSameWithSetting(activity)) { |
|
|
|
|
LanguageUtils.setConfiguration(activity) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
fun onServiceCreate(service: BaseService) { |
|
|
|
|
services.add(WeakReference(service)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
fun onServiceDestroy(service: BaseService) { |
|
|
|
|
for (temp in services) { |
|
|
|
|
if (temp.get() != null && temp.get() === service) { |
|
|
|
|
services.remove(temp) |
|
|
|
|
if (services.size == 0 && activities.size == 0) { |
|
|
|
|
onFinished() |
|
|
|
|
} |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun onFinished() { |
|
|
|
|
Coroutine.cancel() |
|
|
|
|
finishedListener?.invoke() |
|
|
|
|
} |
|
|
|
|
} |