From b57c2a2fdd0655acf1bfc62d95d32405b20245ec Mon Sep 17 00:00:00 2001 From: androidacy-user Date: Wed, 19 Jul 2023 21:50:07 -0400 Subject: [PATCH] [fix] do not accept invalid cache and do not assume we have cache Signed-off-by: androidacy-user --- .../mmm/androidacy/AndroidacyRepoData.kt | 7 +- .../com/fox2code/mmm/repo/RepoUpdater.kt | 79 ++++++++++--------- 2 files changed, 48 insertions(+), 38 deletions(-) diff --git a/app/src/main/kotlin/com/fox2code/mmm/androidacy/AndroidacyRepoData.kt b/app/src/main/kotlin/com/fox2code/mmm/androidacy/AndroidacyRepoData.kt index 8a40624f..4e0f560e 100644 --- a/app/src/main/kotlin/com/fox2code/mmm/androidacy/AndroidacyRepoData.kt +++ b/app/src/main/kotlin/com/fox2code/mmm/androidacy/AndroidacyRepoData.kt @@ -293,7 +293,12 @@ class AndroidacyRepoData(cacheRoot: File?, testMode: Boolean) : RepoData( ) var lastLastUpdate: Long = 0 for (i in 0 until len) { - jsonObject = jsonArray.getJSONObject(i) + try { + jsonObject = jsonArray.getJSONObject(i) + } catch (e: JSONException) { + Timber.e(e, "Failed to parse module") + continue + } val moduleId: String = try { jsonObject.getString("codename") } catch (e: JSONException) { diff --git a/app/src/main/kotlin/com/fox2code/mmm/repo/RepoUpdater.kt b/app/src/main/kotlin/com/fox2code/mmm/repo/RepoUpdater.kt index dfcbc55b..8854cc5e 100644 --- a/app/src/main/kotlin/com/fox2code/mmm/repo/RepoUpdater.kt +++ b/app/src/main/kotlin/com/fox2code/mmm/repo/RepoUpdater.kt @@ -61,46 +61,51 @@ class RepoUpdater(repoData2: RepoData) { val moduleListCacheDao = db.moduleListCacheDao() // now we have the cache, we need to check if it's up to date val results = moduleListCacheDao.getByRepoId(repoData.preferenceId!!) - toUpdate = emptyList() - toApply = HashSet() - for (moduleListCache in results) { - (toApply as HashSet).add( - RepoModule( - repoData, - moduleListCache.codename, - moduleListCache.name, - moduleListCache.description, - moduleListCache.author, - moduleListCache.donate, - moduleListCache.config, - moduleListCache.support, - moduleListCache.version, - moduleListCache.versionCode + if (results.isNotEmpty()) { + toUpdate = emptyList() + toApply = HashSet() + for (moduleListCache in results) { + (toApply as HashSet).add( + RepoModule( + repoData, + moduleListCache.codename, + moduleListCache.name, + moduleListCache.description, + moduleListCache.author, + moduleListCache.donate, + moduleListCache.config, + moduleListCache.support, + moduleListCache.version, + moduleListCache.versionCode + ) ) + } + Timber.d( + "Fetched %d modules from cache for %s, from %s records", + (toApply as HashSet).size, + repoData.preferenceId, + results.size ) + val jsonObject = JSONObject() + // apply the toApply list to the toUpdate list + try { + jsonObject.put("modules", JSONArray(results)) + toUpdate = repoData.populate(jsonObject) + } catch (e: Exception) { + Timber.e(e) + } + // log first 100 chars of indexRaw + indexRaw = jsonObject.toString().toByteArray() + Timber.d( + "Index raw: %s", + String(indexRaw!!, StandardCharsets.UTF_8).subSequence(0, 100) + ) + // Since we reuse instances this should work + toApply = HashSet(repoData.moduleHashMap.values) + (toApply as HashSet).removeAll(toUpdate!!.toSet()) + // Return repo to update + return toUpdate!!.size } - Timber.d( - "Fetched %d modules from cache for %s, from %s records", - (toApply as HashSet).size, - repoData.preferenceId, - results.size - ) - val jsonObject = JSONObject() - // apply the toApply list to the toUpdate list - try { - jsonObject.put("modules", JSONArray(results)) - toUpdate = repoData.populate(jsonObject) - } catch (e: Exception) { - Timber.e(e) - } - // log first 100 chars of indexRaw - indexRaw = jsonObject.toString().toByteArray() - Timber.d("Index raw: %s", String(indexRaw!!, StandardCharsets.UTF_8).subSequence(0, 100)) - // Since we reuse instances this should work - toApply = HashSet(repoData.moduleHashMap.values) - (toApply as HashSet).removeAll(toUpdate!!.toSet()) - // Return repo to update - return toUpdate!!.size } return try { if (!repoData.prepare()) {