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 147b2d5..5ba2e41 100644 --- a/app/src/main/kotlin/com/fox2code/mmm/androidacy/AndroidacyRepoData.kt +++ b/app/src/main/kotlin/com/fox2code/mmm/androidacy/AndroidacyRepoData.kt @@ -305,13 +305,16 @@ class AndroidacyRepoData(cacheRoot: File?, testMode: Boolean) : RepoData( } val moduleId: String = try { jsonObject.getString("codename") + // try to get id value } catch (e: JSONException) { - Timber.e( - "Module %s has no codename or json %s is invalid", - jsonObject.optString("codename", "Unknown"), - jsonObject.toString() - ) - continue + // try to get codename value + try { + jsonObject.getString("id") + } catch (e2: JSONException) { + // we should never get here, bail out + Timber.e(e2, "Failed to parse module id") + continue + } } // Normally, we'd validate the module id here, but we don't need to because the server does it for us val lastUpdate: Long = try { diff --git a/app/src/main/kotlin/com/fox2code/mmm/module/ModuleViewAdapter.kt b/app/src/main/kotlin/com/fox2code/mmm/module/ModuleViewAdapter.kt index 7a3ae44..d89d451 100644 --- a/app/src/main/kotlin/com/fox2code/mmm/module/ModuleViewAdapter.kt +++ b/app/src/main/kotlin/com/fox2code/mmm/module/ModuleViewAdapter.kt @@ -56,7 +56,10 @@ class ModuleViewAdapter : RecyclerView.Adapter() { } } } catch (e: Exception) { - Timber.e(e, "Error while updating module holder. This may mean we're trying to update too early.") + Timber.e( + e, + "Error while updating module holder. This may mean we're trying to update too early." + ) } } diff --git a/app/src/main/kotlin/com/fox2code/mmm/repo/RepoData.kt b/app/src/main/kotlin/com/fox2code/mmm/repo/RepoData.kt index de05179..973233f 100644 --- a/app/src/main/kotlin/com/fox2code/mmm/repo/RepoData.kt +++ b/app/src/main/kotlin/com/fox2code/mmm/repo/RepoData.kt @@ -14,7 +14,6 @@ import com.fox2code.mmm.XRepo import com.fox2code.mmm.manager.ModuleInfo import com.fox2code.mmm.utils.io.Files.Companion.write import com.fox2code.mmm.utils.io.PropUtils.Companion.readProperties -import com.fox2code.mmm.utils.room.ModuleListCacheDatabase import com.fox2code.mmm.utils.room.ReposListDatabase import org.json.JSONException import org.json.JSONObject @@ -377,40 +376,7 @@ open class RepoData(url: String, cacheRoot: File) : XRepo() { // should update (lastUpdate > 15 minutes) fun shouldUpdate(): Boolean { - if (BuildConfig.DEBUG) Timber.d("Repo $preferenceId should update check called") - val db = Room.databaseBuilder( - INSTANCE!!.applicationContext, - ReposListDatabase::class.java, - "ReposList.db", - ).allowMainThreadQueries().build() - val repo = db.reposListDao().getById(preferenceId!!) - // get modulelistcache - val db2 = Room.databaseBuilder( - INSTANCE!!.applicationContext, - ModuleListCacheDatabase::class.java, - "ModuleListCache.db", - ).allowMainThreadQueries().build() - val moduleListCache = db2.moduleListCacheDao().getByRepoId(preferenceId!!) - if (repo != null) { - return if (repo.lastUpdate != 0 && moduleListCache.isNotEmpty() && moduleListCache.size > 1) { - val lastUpdate = repo.lastUpdate.toLong() - val currentTime = System.currentTimeMillis() - val diff = currentTime - lastUpdate - val diffMinutes = diff / (60 * 1000) % 60 - if (BuildConfig.DEBUG) Timber.d("Repo $preferenceId updated: $diffMinutes minutes ago") - db.close() - db2.close() - diffMinutes > if (BuildConfig.DEBUG) 15 else 30 - } else { - if (BuildConfig.DEBUG) Timber.d("Repo $preferenceId shouldUpdate true: lastUpdate is " + repo.lastUpdate + " and moduleListCache is " + moduleListCache.size) - db.close() - db2.close() - true - } - } else { - db.close() - db2.close() - } + Timber.w("shouldUpdate() called but cache not implemented for %s", preferenceId) return true } 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 f9978dd..b17ab73 100644 --- a/app/src/main/kotlin/com/fox2code/mmm/repo/RepoUpdater.kt +++ b/app/src/main/kotlin/com/fox2code/mmm/repo/RepoUpdater.kt @@ -8,7 +8,6 @@ import androidx.room.Room import com.fox2code.mmm.BuildConfig import com.fox2code.mmm.MainApplication import com.fox2code.mmm.utils.io.net.Http.Companion.doHttpGet -import com.fox2code.mmm.utils.room.ModuleListCache import com.fox2code.mmm.utils.room.ModuleListCacheDatabase import com.fox2code.mmm.utils.room.ReposListDatabase import org.json.JSONArray @@ -62,6 +61,7 @@ class RepoUpdater(repoData2: RepoData) { // now we have the cache, we need to check if it's up to date var results = moduleListCacheDao.getByRepoId(repoData.preferenceId!!) if (results.isNotEmpty()) { + Timber.d("Got %d modules from cache for %s", results.size, repoData.preferenceId) toUpdate = emptyList() toApply = HashSet() // if results is not empty, check each module to see if it's null. this should never happen, but if it does, remove it from the cache @@ -92,9 +92,26 @@ class RepoUpdater(repoData2: RepoData) { results.size ) val jsonObject = JSONObject() + val jsonArray = JSONArray() + for (module in results) { + val moduleJson = JSONObject() + moduleJson.put("id", module!!.codename) + moduleJson.put("name", module.name) + moduleJson.put("description", module.description) + moduleJson.put("author", module.author) + moduleJson.put("donate", module.donate) + moduleJson.put("config", module.config) + moduleJson.put("support", module.support) + moduleJson.put("version", module.version) + moduleJson.put("versionCode", module.versionCode) + moduleJson.put("minApi", module.minApi) + moduleJson.put("maxApi", module.maxApi) + moduleJson.put("minMagisk", module.minMagisk) + jsonArray.put(moduleJson) + } // apply the toApply list to the toUpdate list try { - jsonObject.put("modules", JSONArray(results)) + jsonObject.put("modules", jsonArray) toUpdate = repoData.populate(jsonObject) } catch (e: Exception) { Timber.e(e) @@ -105,9 +122,7 @@ class RepoUpdater(repoData2: RepoData) { "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()) + if (BuildConfig.DEBUG) Timber.d("Returning %d modules for %s", toUpdate!!.size, repoData.preferenceId) // Return repo to update return toUpdate!!.size } @@ -158,194 +173,6 @@ class RepoUpdater(repoData2: RepoData) { val success = AtomicBoolean(false) if (BuildConfig.DEBUG) Timber.d("Finishing update for %s", repoData.preferenceId) if (indexRaw != null) { - val tmpIndexRaw = indexRaw!! - if (BuildConfig.DEBUG) Timber.d("Updating database for %s", repoData.preferenceId) - // new thread to update the database - val thread = Thread { - val startTime = System.currentTimeMillis() - if (BuildConfig.DEBUG) Timber.d("Updating database thread for %s", repoData.preferenceId) - try { - // iterate over modules, using this.supportedProperties as a template to attempt to get each property from the module. everything that is not null is added to the module - // use room to insert to - // props avail: - val db = Room.databaseBuilder( - MainApplication.INSTANCE!!, - ModuleListCacheDatabase::class.java, - "ModuleListCache.db" - ).build() - // all except first six can be null - // this.indexRaw is the raw index file (json) - val modules = JSONObject(String(tmpIndexRaw, StandardCharsets.UTF_8)) - // androidacy repo uses "data" key, others should use "modules" key. Both are JSONArrays - val modulesArray = try { - modules.getJSONArray("data") - } catch (e: Exception) { - modules.getJSONArray("modules") - } catch (e: Exception) { - Timber.e(e) - Timber.w("No modules were found in the index file for %s", repoData.preferenceId) - if (BuildConfig.DEBUG) Timber.d("Finished updating database for %s in %dms", repoData.preferenceId, System.currentTimeMillis() - startTime) - success.set(false) - return@Thread - } - if (BuildConfig.DEBUG) Timber.d("Got modules for %s", repoData.preferenceId) - val moduleListCacheDao = db.moduleListCacheDao() - moduleListCacheDao.deleteByRepoId(repoData.preferenceId!!) - if (BuildConfig.DEBUG) Timber.d("Deleted old modules for %s", repoData.preferenceId) - if (modulesArray.length() == 0) { - Timber.w("No modules were found in the index file for %s", repoData.preferenceId) - if (BuildConfig.DEBUG) Timber.d("Finished updating database for %s in %dms", repoData.preferenceId, System.currentTimeMillis() - startTime) - success.set(false) - return@Thread - } - if (BuildConfig.DEBUG) Timber.d("Iterating over modules for %s", repoData.preferenceId) - // iterate over modules - for (n in 0 until modulesArray.length()) { - // get module - val module = modulesArray.getJSONObject(n) - try { - // get module id - // if codename is present, prefer that over id - val id: String = - if (module.has("codename") && module.getString("codename") != "") { - module.getString("codename") - } else { - module.getString("id") - } - // get module name - val name = module.getString("name") - // get module version - val version = module.getString("version") - // get module version code - val versionCode = module.getInt("versionCode") - // get module author - val author = module.getString("author") - // get module description - val description = module.getString("description") - // get module min api - val minApi: String = - if (module.has("minApi") && module.getString("minApi") != "") { - module.getString("minApi") - } else { - "0" - } - // coerce min api to int - val minApiInt = minApi.toInt() - // get module max api and set to 0 if it's "" or null - val maxApi: String = - if (module.has("maxApi") && module.getString("maxApi") != "") { - module.getString("maxApi") - } else { - "0" - } - // coerce max api to int - val maxApiInt = maxApi.toInt() - // get module min magisk - val minMagisk: String = - if (module.has("minMagisk") && module.getString("minMagisk") != "") { - module.getString("minMagisk") - } else { - "0" - } - // coerce min magisk to int - val minMagiskInt = minMagisk.toInt() - // get module need ramdisk - val needRamdisk: Boolean = if (module.has("needRamdisk")) { - module.getBoolean("needRamdisk") - } else { - false - } - // get module support - val support: String? = if (module.has("support")) { - module.getString("support") - } else { - "" - } - // get module donate - val donate: String? = if (module.has("donate")) { - module.getString("donate") - } else { - "" - } - // get module config - val config: String? = if (module.has("config")) { - module.getString("config") - } else { - "" - } - // get module change boot - val changeBoot: Boolean = if (module.has("changeBoot")) { - module.getBoolean("changeBoot") - } else { - false - } - // get module mmt reborn - val mmtReborn: Boolean = if (module.has("mmtReborn")) { - module.getBoolean("mmtReborn") - } else { - false - } - // try to get updated_at or lastUpdate value for lastUpdate - val lastUpdate: Int = if (module.has("updated_at")) { - module.getInt("updated_at") - } else if (module.has("lastUpdate")) { - module.getInt("lastUpdate") - } else { - 0 - } - // now downloads or stars - val downloads: Int = if (module.has("downloads")) { - module.getInt("downloads") - } else if (module.has("stars")) { - module.getInt("stars") - } else { - 0 - } - // get module repo id - val repoId = repoData.preferenceId - // get safe property. for now, only supported by androidacy repo and they use "vt_status" key - var safe = false - if (repoData.name == "Androidacy Modules Repo") { - if (module.has("vt_status")) { - if (module.getString("vt_status") == "Clean") { - safe = true - } - } - } - val moduleListCache = ModuleListCache( - name = name, - version = version, - versionCode = versionCode, - author = author, - description = description, - minApi = minApiInt, - maxApi = maxApiInt, - minMagisk = minMagiskInt, - needRamdisk = needRamdisk, - support = support ?: "", - donate = donate ?: "", - config = config ?: "", - changeBoot = changeBoot, - mmtReborn = mmtReborn, - repoId = repoId!!, - safe = safe, - lastUpdate = lastUpdate.toLong(), - stats = downloads, - codename = id - ) - moduleListCacheDao.insert(moduleListCache) - } catch (ignored: Exception) { - } - } - db.close() - val endTime = System.currentTimeMillis() - val timeTaken = endTime - startTime - if (BuildConfig.DEBUG) Timber.d("Time taken to parse modules: $timeTaken ms") - } catch (ignored: Exception) { - } - } - thread.start() - indexRaw = null // set lastUpdate val db = Room.databaseBuilder( MainApplication.INSTANCE!!.applicationContext,