@@ -6,6 +6,8 @@ package com.fox2code.mmm.module
66
77import android.app.Activity
88import android.os.Build
9+ import android.os.Handler
10+ import android.os.Looper
911import android.view.View
1012import androidx.recyclerview.widget.RecyclerView
1113import com.fox2code.mmm.AppUpdateManager
@@ -32,6 +34,7 @@ class ModuleViewListBuilder(private val activity: Activity) {
3234 private var updating = false
3335 private var headerPx = 0
3436 private var footerPx = 0
37+ private var tries = 0
3538 private var moduleSorter: ModuleSorter = ModuleSorter .UPDATE
3639 private var updateInsets = RUNNABLE
3740 fun addNotification (notificationType : NotificationType ? ) {
@@ -85,55 +88,70 @@ class ModuleViewListBuilder(private val activity: Activity) {
8588 repoManager?.runAfterUpdate {
8689 Timber .i(" A2: %s" , repoManager.modules.size)
8790 val no32bitSupport = Build .SUPPORTED_32_BIT_ABIS .isEmpty()
88- for (repoModule in repoManager.modules.values) {
89- // add the remote module to the list in MainActivity
90- MainActivity .onlineModuleInfoList + = repoModule
91- // if repoData is null, something is wrong
92- @Suppress(" SENSELESS_COMPARISON" )
93- if (repoModule.repoData == null ) {
94- Timber .w(" RepoData is null for module %s" , repoModule.id)
95- continue
96- }
97- if (! repoModule.repoData.isEnabled) {
98- Timber .i(
99- " Repo %s is disabled, skipping module %s" ,
100- repoModule.repoData.preferenceId,
101- repoModule.id
102- )
103- continue
104- }
105- val moduleInfo = repoModule.moduleInfo
106- if (! showIncompatible && (moduleInfo.minApi > Build .VERSION .SDK_INT || moduleInfo.maxApi != 0 && moduleInfo.maxApi < Build .VERSION .SDK_INT || peekMagiskPath() != null ) && repoModule.moduleInfo.minMagisk > peekMagiskVersion() || no32bitSupport && (AppUpdateManager .getFlagsForModule(
107- repoModule.id
108- )
109- and AppUpdateManager .FLAG_COMPAT_NEED_32BIT ) != 0 || repoModule.moduleInfo.needRamdisk && ! peekHasRamdisk()
110- ) continue // Skip adding incompatible modules
111- var moduleHolder = mappedModuleHolders[repoModule.id]
112- if (moduleHolder == null ) {
113- mappedModuleHolders[repoModule.id] = ModuleHolder (repoModule.id).also {
114- moduleHolder = it
91+ try {
92+ for (repoModule in repoManager.modules.values) {
93+ // add the remote module to the list in MainActivity
94+ MainActivity .onlineModuleInfoList + = repoModule
95+ // if repoData is null, something is wrong
96+ @Suppress(" SENSELESS_COMPARISON" ) if (repoModule.repoData == null ) {
97+ Timber .w(" RepoData is null for module %s" , repoModule.id)
98+ continue
11599 }
116- }
117- moduleHolder!! .repoModule = repoModule
118- // check if local module is installed
119- // iterate over MainActivity.localModuleInfoList until we hit the module with the same id
120- for (localModuleInfo in MainActivity .localModuleInfoList) {
121- if (localModuleInfo.id == repoModule.id) {
122- moduleHolder!! .moduleInfo = localModuleInfo
123- break
100+ if (! repoModule.repoData.isEnabled) {
101+ Timber .i(
102+ " Repo %s is disabled, skipping module %s" ,
103+ repoModule.repoData.preferenceId,
104+ repoModule.id
105+ )
106+ continue
107+ }
108+ val moduleInfo = repoModule.moduleInfo
109+ if (! showIncompatible && (moduleInfo.minApi > Build .VERSION .SDK_INT || moduleInfo.maxApi != 0 && moduleInfo.maxApi < Build .VERSION .SDK_INT || peekMagiskPath() != null ) && repoModule.moduleInfo.minMagisk > peekMagiskVersion() || no32bitSupport && (AppUpdateManager .getFlagsForModule(
110+ repoModule.id
111+ ) and AppUpdateManager .FLAG_COMPAT_NEED_32BIT ) != 0 || repoModule.moduleInfo.needRamdisk && ! peekHasRamdisk()
112+ ) continue // Skip adding incompatible modules
113+ var moduleHolder = mappedModuleHolders[repoModule.id]
114+ if (moduleHolder == null ) {
115+ mappedModuleHolders[repoModule.id] = ModuleHolder (repoModule.id).also {
116+ moduleHolder = it
117+ }
124118 }
119+ moduleHolder!! .repoModule = repoModule
120+ // check if local module is installed
121+ // iterate over MainActivity.localModuleInfoList until we hit the module with the same id
122+ for (localModuleInfo in MainActivity .localModuleInfoList) {
123+ if (localModuleInfo.id == repoModule.id) {
124+ moduleHolder!! .moduleInfo = localModuleInfo
125+ break
126+ }
127+ }
128+ }
129+ } catch (e: Exception ) {
130+ Timber .e(e, " appendRemoteModules() failed" )
131+ // retry up to five times, waiting i * 100ms between each try
132+ if (tries < 5 ) {
133+ tries++
134+ Timber .i(" appendRemoteModules() retrying in %dms" , tries * 100 )
135+ Handler (Looper .getMainLooper()).postDelayed({
136+ appendRemoteModules()
137+ }, tries * 100 .toLong())
138+ } else {
139+ Timber .e(e, " appendRemoteModules() failed after %d tries" , tries)
140+ tries = 0
125141 }
126142 }
127- Timber .i(" appendRemoteModules() finished in %dms" , System .currentTimeMillis() - startTime)
143+ Timber .i(
144+ " appendRemoteModules() finished in %dms" ,
145+ System .currentTimeMillis() - startTime
146+ )
128147 }
129148 }
130149 }
131150
132151 fun refreshNotificationsUI (moduleViewAdapter : ModuleViewAdapter ) {
133152 val notificationCount = notifications.size
134153 notifySizeChanged(
135- moduleViewAdapter, 0 ,
136- notificationCount, notificationCount
154+ moduleViewAdapter, 0 , notificationCount, notificationCount
137155 )
138156 }
139157
@@ -195,7 +213,8 @@ class ModuleViewListBuilder(private val activity: Activity) {
195213 newNotificationsLen = notifications.size + 1 - special
196214 val headerTypes = EnumSet .of(
197215 ModuleHolder .Type .SEPARATOR ,
198- ModuleHolder .Type .NOTIFICATION , ModuleHolder .Type .FOOTER
216+ ModuleHolder .Type .NOTIFICATION ,
217+ ModuleHolder .Type .FOOTER
199218 )
200219 val moduleHolderIterator = mappedModuleHolders.values.iterator()
201220 synchronized(queryLock) {
@@ -218,8 +237,7 @@ class ModuleViewListBuilder(private val activity: Activity) {
218237 Thread (
219238 { // Apply async
220239 applyTo(moduleList, moduleViewAdapter)
221- },
222- " Sorter apply Thread"
240+ }, " Sorter apply Thread"
223241 ).start()
224242 }
225243 }
@@ -256,9 +274,7 @@ class ModuleViewListBuilder(private val activity: Activity) {
256274 if (notificationType != null ) {
257275 oldNotifications.add(notificationType)
258276 if (! notificationType.special) oldNotificationsLen++
259- } else if (moduleHolder.footerPx != - 1 &&
260- moduleHolder.filterLevel == 1
261- ) oldNotificationsLen++ // Fix header
277+ } else if (moduleHolder.footerPx != - 1 && moduleHolder.filterLevel == 1 ) oldNotificationsLen++ // Fix header
262278 if (moduleHolder.separator == = ModuleHolder .Type .INSTALLABLE ) break
263279 oldOfflineModulesLen++
264280 }
@@ -274,25 +290,23 @@ class ModuleViewListBuilder(private val activity: Activity) {
274290 val oldLen = moduleViewAdapter.moduleHolders.size
275291 moduleViewAdapter.moduleHolders.clear()
276292 moduleViewAdapter.moduleHolders.addAll(moduleHolders)
277- if (oldNotificationsLen != newNotificationsLen ||
278- oldNotifications != notifications
279- ) {
293+ if (oldNotificationsLen != newNotificationsLen || oldNotifications != notifications) {
280294 notifySizeChanged(
281- moduleViewAdapter, 0 ,
282- oldNotificationsLen, newNotificationsLen
295+ moduleViewAdapter, 0 , oldNotificationsLen, newNotificationsLen
283296 )
284297 } else {
285298 notifySizeChanged(moduleViewAdapter, 0 , 1 , 1 )
286299 }
287300 if (newLen - newNotificationsLen == 0 ) {
288301 notifySizeChanged(
289- moduleViewAdapter, newNotificationsLen,
290- oldLen - oldNotificationsLen, 0
302+ moduleViewAdapter, newNotificationsLen, oldLen - oldNotificationsLen, 0
291303 )
292304 } else {
293305 notifySizeChanged(
294- moduleViewAdapter, newNotificationsLen,
295- oldOfflineModulesLen, newOfflineModulesLen
306+ moduleViewAdapter,
307+ newNotificationsLen,
308+ oldOfflineModulesLen,
309+ newOfflineModulesLen
296310 )
297311 notifySizeChanged(
298312 moduleViewAdapter,
@@ -306,8 +320,7 @@ class ModuleViewListBuilder(private val activity: Activity) {
306320 updateInsets = Runnable {
307321 notifySizeChanged(moduleViewAdapter, 0 , 1 , 1 )
308322 notifySizeChanged(
309- moduleViewAdapter,
310- moduleHolders.size, 1 , 1
323+ moduleViewAdapter, moduleHolders.size, 1 , 1
311324 )
312325 }
313326 }
@@ -350,8 +363,7 @@ class ModuleViewListBuilder(private val activity: Activity) {
350363 companion object {
351364 private val RUNNABLE = Runnable {}
352365 private fun notifySizeChanged (
353- moduleViewAdapter : ModuleViewAdapter ,
354- index : Int , oldLen : Int , newLen : Int
366+ moduleViewAdapter : ModuleViewAdapter , index : Int , oldLen : Int , newLen : Int
355367 ) {
356368 // Timber.i("A: " + index + " " + oldLen + " " + newLen);
357369 if (oldLen == newLen) {
0 commit comments