Skip to content

Commit b57c2a2

Browse files
[fix] do not accept invalid cache and do not assume we have cache
Signed-off-by: androidacy-user <[email protected]>
1 parent 7adc736 commit b57c2a2

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

app/src/main/kotlin/com/fox2code/mmm/androidacy/AndroidacyRepoData.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,12 @@ class AndroidacyRepoData(cacheRoot: File?, testMode: Boolean) : RepoData(
293293
)
294294
var lastLastUpdate: Long = 0
295295
for (i in 0 until len) {
296-
jsonObject = jsonArray.getJSONObject(i)
296+
try {
297+
jsonObject = jsonArray.getJSONObject(i)
298+
} catch (e: JSONException) {
299+
Timber.e(e, "Failed to parse module")
300+
continue
301+
}
297302
val moduleId: String = try {
298303
jsonObject.getString("codename")
299304
} catch (e: JSONException) {

app/src/main/kotlin/com/fox2code/mmm/repo/RepoUpdater.kt

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -61,46 +61,51 @@ class RepoUpdater(repoData2: RepoData) {
6161
val moduleListCacheDao = db.moduleListCacheDao()
6262
// now we have the cache, we need to check if it's up to date
6363
val results = moduleListCacheDao.getByRepoId(repoData.preferenceId!!)
64-
toUpdate = emptyList()
65-
toApply = HashSet()
66-
for (moduleListCache in results) {
67-
(toApply as HashSet<RepoModule>).add(
68-
RepoModule(
69-
repoData,
70-
moduleListCache.codename,
71-
moduleListCache.name,
72-
moduleListCache.description,
73-
moduleListCache.author,
74-
moduleListCache.donate,
75-
moduleListCache.config,
76-
moduleListCache.support,
77-
moduleListCache.version,
78-
moduleListCache.versionCode
64+
if (results.isNotEmpty()) {
65+
toUpdate = emptyList()
66+
toApply = HashSet()
67+
for (moduleListCache in results) {
68+
(toApply as HashSet<RepoModule>).add(
69+
RepoModule(
70+
repoData,
71+
moduleListCache.codename,
72+
moduleListCache.name,
73+
moduleListCache.description,
74+
moduleListCache.author,
75+
moduleListCache.donate,
76+
moduleListCache.config,
77+
moduleListCache.support,
78+
moduleListCache.version,
79+
moduleListCache.versionCode
80+
)
7981
)
82+
}
83+
Timber.d(
84+
"Fetched %d modules from cache for %s, from %s records",
85+
(toApply as HashSet<RepoModule>).size,
86+
repoData.preferenceId,
87+
results.size
8088
)
89+
val jsonObject = JSONObject()
90+
// apply the toApply list to the toUpdate list
91+
try {
92+
jsonObject.put("modules", JSONArray(results))
93+
toUpdate = repoData.populate(jsonObject)
94+
} catch (e: Exception) {
95+
Timber.e(e)
96+
}
97+
// log first 100 chars of indexRaw
98+
indexRaw = jsonObject.toString().toByteArray()
99+
Timber.d(
100+
"Index raw: %s",
101+
String(indexRaw!!, StandardCharsets.UTF_8).subSequence(0, 100)
102+
)
103+
// Since we reuse instances this should work
104+
toApply = HashSet(repoData.moduleHashMap.values)
105+
(toApply as HashSet<RepoModule>).removeAll(toUpdate!!.toSet())
106+
// Return repo to update
107+
return toUpdate!!.size
81108
}
82-
Timber.d(
83-
"Fetched %d modules from cache for %s, from %s records",
84-
(toApply as HashSet<RepoModule>).size,
85-
repoData.preferenceId,
86-
results.size
87-
)
88-
val jsonObject = JSONObject()
89-
// apply the toApply list to the toUpdate list
90-
try {
91-
jsonObject.put("modules", JSONArray(results))
92-
toUpdate = repoData.populate(jsonObject)
93-
} catch (e: Exception) {
94-
Timber.e(e)
95-
}
96-
// log first 100 chars of indexRaw
97-
indexRaw = jsonObject.toString().toByteArray()
98-
Timber.d("Index raw: %s", String(indexRaw!!, StandardCharsets.UTF_8).subSequence(0, 100))
99-
// Since we reuse instances this should work
100-
toApply = HashSet(repoData.moduleHashMap.values)
101-
(toApply as HashSet<RepoModule>).removeAll(toUpdate!!.toSet())
102-
// Return repo to update
103-
return toUpdate!!.size
104109
}
105110
return try {
106111
if (!repoData.prepare()) {

0 commit comments

Comments
 (0)