From 0ff5d733e6b482eb11b7ac64b3f10bcc6b7a8eb8 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 16 Aug 2019 17:16:42 +0800 Subject: [PATCH] up --- .../java/io/legado/app/help/ActivityHelp.kt | 108 ++++++++++++++++++ .../app/receiver/MediaButtonReceiver.kt | 13 +-- 2 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/io/legado/app/help/ActivityHelp.kt diff --git a/app/src/main/java/io/legado/app/help/ActivityHelp.kt b/app/src/main/java/io/legado/app/help/ActivityHelp.kt new file mode 100644 index 000000000..83705f0ed --- /dev/null +++ b/app/src/main/java/io/legado/app/help/ActivityHelp.kt @@ -0,0 +1,108 @@ +package io.legado.app.help + +import android.app.Activity +import java.lang.ref.WeakReference +import java.util.* + +/** + * Activity管理器,管理项目中Activity的状态 + */ +class ActivityHelp private constructor() { + + companion object { + + private var activities: MutableList> = arrayListOf() + + @Volatile + private var instance: ActivityHelp? = null + + fun getInstance(): ActivityHelp? { + if (null == instance) { + synchronized(ActivityHelp::class.java) { + if (null == instance) { + instance = ActivityHelp() + } + } + } + return instance + } + } + + fun getActivities(): List> { + return activities + } + + /** + * 添加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) { + for (activity in activities) { + activity?.finish() + } + } + + /** + * 关闭指定 activity(class) + */ + fun finishActivity(vararg activityClasses: Class<*>) { + val waitFinish = ArrayList>() + for (temp in activities) { + for (activityClass in activityClasses) { + if (temp.get()?.javaClass == activityClass) { + waitFinish.add(temp) + break + } + } + } + for (activityWeakReference in waitFinish) { + activityWeakReference.get()?.finish() + } + } + + /** + * 判断指定Activity是否存在 + */ + fun isExist(activityClass: Class<*>): Boolean? { + var result = false + for (item in activities) { + if (item.get()?.javaClass == activityClass) { + result = true + break + } + } + return result + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt index 9b6d62035..e58743086 100644 --- a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt +++ b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt @@ -24,16 +24,15 @@ class MediaButtonReceiver : BroadcastReceiver() { val keycode = event.keyCode val action = event.action - val command: String? = null + var command = false when (keycode) { KeyEvent.KEYCODE_MEDIA_STOP, KeyEvent.KEYCODE_MEDIA_PAUSE, KeyEvent.KEYCODE_MEDIA_PLAY, KeyEvent.KEYCODE_HEADSETHOOK, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE -> { + command = true } - else -> { - } - }// command = ReadAloudService.ActionMediaButton; - if (command != null) { + } + if (command) { if (action == KeyEvent.ACTION_DOWN) { - readAloud(context, command) + readAloud(context) return true } } @@ -41,7 +40,7 @@ class MediaButtonReceiver : BroadcastReceiver() { return false } - private fun readAloud(context: Context, command: String?) { + private fun readAloud(context: Context) { } }