11package com.catchpig.mvvm.manager
22
33import android.app.Activity
4+ import android.view.Display
5+ import com.catchpig.utils.ext.getDisplayId
46import com.catchpig.utils.ext.logi
5- import java.util.LinkedList
67
78object ActivityManager {
89 private const val TAG = " ActivityManager"
9- private var activities = LinkedList <Activity >()
10- private var visibleActivityCount = 0
11- private var isAppForeground: Boolean? = null
12-
13- private val appStatusListeners = mutableListOf<OnAppStatusListener >()
10+ private val displayActivities = mutableMapOf<Int , MutableList <Activity >>()
11+ private val displayVisibleActivityCount = mutableMapOf<Int , Int >()
12+ private val displayAppForeground = mutableMapOf<Int , Boolean >()
13+ private val displayAppStatusListener = mutableMapOf<Int , MutableList <OnAppStatusListener >>()
1414
1515 fun addAppStatusListener (listener : OnAppStatusListener ) {
16+ addAppStatusListener(Display .DEFAULT_DISPLAY , listener)
17+ }
18+
19+ fun addAppStatusListener (displayId : Int , listener : OnAppStatusListener ) {
20+ val appStatusListeners = getAppStatusListeners(displayId)
1621 if (appStatusListeners.contains(listener)) {
17- " addAppStatusListener->listener:$listener has already been added"
22+ " addAppStatusListener->listener:$listener has already been added" .logi( TAG )
1823 return
1924 }
2025 appStatusListeners.add(listener)
2126 }
2227
2328 fun removeAppStatusListener (listener : OnAppStatusListener ) {
29+ removeAppStatusListener(Display .DEFAULT_DISPLAY , listener)
30+ }
31+
32+ fun removeAppStatusListener (displayId : Int , listener : OnAppStatusListener ) {
33+ val appStatusListeners = getAppStatusListeners(displayId)
2434 appStatusListeners.remove(listener)
2535 }
2636
37+ private fun getAppStatusListeners (displayId : Int ): MutableList <OnAppStatusListener > {
38+ if (displayAppStatusListener.containsKey(displayId)) {
39+ return displayAppStatusListener[displayId]!!
40+ }
41+ return mutableListOf ()
42+ }
43+
2744 /* *
28- * t
45+ *
2946 */
30- fun addVisibleActivity () {
47+ fun addVisibleActivity (activity : Activity ) {
48+ val displayId = activity.getDisplayId()
49+ var visibleActivityCount = getVisibleActivityCount(displayId)
3150 visibleActivityCount++
32- notifyAppStatus()
51+ displayVisibleActivityCount[displayId] = visibleActivityCount
52+ notifyAppStatus(displayId)
3353 }
3454
35- fun removeVisibleActivity () {
55+ fun removeVisibleActivity (activity : Activity ) {
56+ val displayId = activity.getDisplayId()
57+ var visibleActivityCount = getVisibleActivityCount(displayId)
3658 visibleActivityCount--
37- notifyAppStatus()
59+ displayVisibleActivityCount[displayId] = visibleActivityCount
60+ notifyAppStatus(displayId)
61+ }
62+
63+ private fun getVisibleActivityCount (displayId : Int ): Int {
64+ return displayVisibleActivityCount[displayId] ? : 0
3865 }
3966
4067 /* *
4168 * 应用是否在前台
4269 */
43- fun isForeground (): Boolean {
70+ fun isForeground (displayId : Int ): Boolean {
71+ val visibleActivityCount = getVisibleActivityCount(displayId)
4472 return visibleActivityCount > 0
4573 }
4674
4775 /* *
4876 * 通知应用前后台状态
4977 */
50- private fun notifyAppStatus () {
51- val isForeground = isForeground()
52- if (isForeground == isAppForeground) {
78+ private fun notifyAppStatus (displayId : Int ) {
79+ val currentAppForeground = isForeground(displayId)
80+ val lastAppForeground = displayAppForeground[displayId] ? : false
81+ if (currentAppForeground == lastAppForeground) {
5382 return
5483 }
55- " notifyAppStatus->isForeground:$isForeground ,isAppForeground:$isAppForeground " .logi(TAG )
56- isAppForeground = isForeground
84+ " notifyAppStatus->displayId:${displayId} ,currentAppForeground:$currentAppForeground ,lastAppForeground:$lastAppForeground " .logi(
85+ TAG
86+ )
87+ displayAppForeground[displayId] = currentAppForeground
88+ val appStatusListeners = getAppStatusListeners(displayId)
5789 appStatusListeners.forEach {
58- if (isForeground ) {
59- it.onForeground()
90+ if (currentAppForeground ) {
91+ it.onForeground(displayId )
6092 } else {
61- it.onBackground()
93+ it.onBackground(displayId )
6294 }
6395 }
6496 }
6597
98+ private fun getActivities (displayId : Int ): MutableList <Activity > {
99+ if (displayActivities.containsKey(displayId)) {
100+ return displayActivities[displayId]!!
101+ }
102+ return mutableListOf ()
103+ }
104+
66105 /* *
67106 * 添加activity
68107 */
69108 fun addActivity (activity : Activity ) {
109+ val activities = getActivities(activity.getDisplayId())
70110 activities.add(activity)
71111 }
72112
73113 /* *
74114 * 删除activity
75115 */
76- fun removeActivity (activity : Activity ? ) {
116+ fun removeActivity (activity : Activity ) {
117+ val activities = getActivities(activity.getDisplayId())
77118 activities.remove(activity)
78119 }
79120
80121 /* *
81122 * 获取最顶层的activity
82123 */
83124 fun getTopActivity (): Activity {
125+ return getTopActivity(Display .DEFAULT_DISPLAY )
126+ }
127+
128+ /* *
129+ * 获取最顶层的activity
130+ * @param displayId
131+ */
132+ fun getTopActivity (displayId : Int ): Activity {
133+ val activities = getActivities(displayId)
84134 return activities.last()
85135 }
86136
87137 /* *
88138 * 获取第一个Activity
89139 */
90140 fun getFirstActivity (): Activity {
91- return activities.first
141+ return getFirstActivity(Display .DEFAULT_DISPLAY )
142+ }
143+
144+ /* *
145+ * 获取第一个Activity
146+ * @param displayId
147+ */
148+ fun getFirstActivity (displayId : Int ): Activity {
149+ val activities = getActivities(displayId)
150+ return activities.first()
92151 }
93152
94153 /* *
95154 * 删除除最上层之外的所有activity
96155 */
97156 fun finishAllActivitiesExceptTop () {
98- val topActivity = getTopActivity()
157+ finishAllActivitiesExceptTop(Display .DEFAULT_DISPLAY )
158+ }
159+
160+ /* *
161+ * 删除除最上层之外的所有activity
162+ * @param displayId
163+ */
164+ fun finishAllActivitiesExceptTop (displayId : Int ) {
165+ val topActivity = getTopActivity(displayId)
166+ val activities = getActivities(displayId)
99167 val iterator = activities.iterator()
100168 while (iterator.hasNext()) {
101169 val activity = iterator.next()
@@ -109,6 +177,17 @@ object ActivityManager {
109177 * 删除所有的activity
110178 */
111179 fun finishAllActivities () {
180+ for (displayId in displayActivities.keys) {
181+ finishAllActivities(displayId)
182+ }
183+ }
184+
185+ /* *
186+ * 删除所有的activity
187+ * @param displayId
188+ */
189+ fun finishAllActivities (displayId : Int ) {
190+ val activities = getActivities(displayId)
112191 val iterator = activities.iterator()
113192 while (iterator.hasNext()) {
114193 val activity = iterator.next()
@@ -121,11 +200,11 @@ object ActivityManager {
121200 /* *
122201 * 前台
123202 */
124- fun onForeground ()
203+ fun onForeground (displayId : Int )
125204
126205 /* *
127206 * 后台
128207 */
129- fun onBackground ()
208+ fun onBackground (displayId : Int )
130209 }
131210}
0 commit comments