@ -1,13 +1,12 @@
package com.fox2code.mmm.repo ;
import androidx.annotation.NonNull ;
import com.fox2code.mmm.utils.io.Http ;
import com.fox2code.mmm.utils.realm.ModuleListCache ;
import org.json.JSON Exception ;
import org.json.JSON Array ;
import org.json.JSONObject ;
import java.io.File ;
import java.nio.charset.StandardCharsets ;
import java.util.Collection ;
import java.util.Collections ;
@ -84,8 +83,8 @@ public class RepoUpdater {
// 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 realm to insert to
// props avail:
//
RealmConfiguration realmConfiguration = new RealmConfiguration . Builder ( ) . name ( "ModuleListCache.realm" ) . schemaVersion ( 1 ) . deleteRealmIfMigrationNeeded ( ) . build( ) ;
File cacheRoot = this . repoData . cacheRoot ;
RealmConfiguration realmConfiguration = new RealmConfiguration . Builder ( ) . name ( "ModuleListCache.realm" ) . schemaVersion ( 1 ) . deleteRealmIfMigrationNeeded ( ) . allowWritesOnUiThread( true ) . allowQueriesOnUiThread ( true ) . directory ( cacheRoot ) . build( ) ;
// array with module info default values
// supported properties for a module
//id=<string>
@ -109,32 +108,21 @@ public class RepoUpdater {
//installedVersionCode=<int> (only if installed)
//
// all except first six can be null
// this.indexRaw is the raw index file (json) and the modules can be either under the "modules" key or the "data" key
// both are arrays of modules
// try to get modules from "modules" key
// this.indexRaw is the raw index file (json)
JSONObject modules = new JSONObject ( new String ( this . indexRaw , StandardCharsets . UTF_8 ) ) ;
// check if modules has key "modules" or "data"
try {
if ( modules . has ( "modules" ) ) {
// get modules from "modules" key
modules = modules . getJSONObject ( "modules" ) ;
} else if ( modules . has ( "data" ) ) {
// get modules from "data" key
modules = modules . getJSONObject ( "data" ) ;
}
} catch ( JSONException e ) {
// there's a possibility that the modules key is an array, so we need to convert it to a json object
// get modules array
JSONObject [ ] modulesArray = new JSONObject [ ] { modules } ;
// create new json object
modules = new JSONObject ( ) ;
// iterate over modules array
for ( int i = 0 ; i < modulesArray . length ; i + + ) {
// put module in json object
modules . put ( String . valueOf ( i ) , modulesArray [ i ] ) ;
}
JSONArray modulesArray ;
// androidacy repo uses "data" key, others should use "modules" key. Both are JSONArrays
if ( this . repoData . getName ( ) . equals ( "Androidacy Modules Repo" ) ) {
// get modules from "data" key. This is a JSONArray so we need to convert it to a JSONObject
modulesArray = modules . getJSONArray ( "data" ) ;
} else {
// get modules from "modules" key. This is a JSONArray so we need to convert it to a JSONObject
modulesArray = modules . getJSONArray ( "modules" ) ;
}
for ( JSONObject module : new JSONObject [ ] { modules } ) {
// iterate over modules. pls dont hate me for this, its ugly but it works
for ( int n = 0 ; n < modulesArray . length ( ) ; n + + ) {
// get module
JSONObject module = modulesArray . getJSONObject ( n ) ;
try {
// get module id
String id = module . getString ( "id" ) ;
@ -149,23 +137,74 @@ public class RepoUpdater {
// get module description
String description = module . getString ( "description" ) ;
// get module min api
int minApi = module . getInt ( "minApi" ) ;
// get module max api
int maxApi = module . getInt ( "maxApi" ) ;
String minApi ;
if ( module . has ( "minApi" ) & & ! module . getString ( "minApi" ) . equals ( "" ) ) {
minApi = module . getString ( "minApi" ) ;
} else {
minApi = "0" ;
}
// coerce min api to int
int minApiInt = Integer . parseInt ( minApi ) ;
// get module max api and set to 0 if it's "" or null
String maxApi ;
if ( module . has ( "maxApi" ) & & ! module . getString ( "maxApi" ) . equals ( "" ) ) {
maxApi = module . getString ( "maxApi" ) ;
} else {
maxApi = "0" ;
}
// coerce max api to int
int maxApiInt = Integer . parseInt ( maxApi ) ;
// get module min magisk
int minMagisk = module . getInt ( "minMagisk" ) ;
String minMagisk ;
if ( module . has ( "minMagisk" ) & & ! module . getString ( "minMagisk" ) . equals ( "" ) ) {
minMagisk = module . getString ( "minMagisk" ) ;
} else {
minMagisk = "0" ;
}
// coerce min magisk to int
int minMagiskInt = Integer . parseInt ( minMagisk ) ;
// get module need ramdisk
boolean needRamdisk = module . getBoolean ( "needRamdisk" ) ;
boolean needRamdisk ;
if ( module . has ( "needRamdisk" ) ) {
needRamdisk = module . getBoolean ( "needRamdisk" ) ;
} else {
needRamdisk = false ;
}
// get module support
String support = module . getString ( "support" ) ;
String support ;
if ( module . has ( "support" ) ) {
support = module . getString ( "support" ) ;
} else {
support = "" ;
}
// get module donate
String donate = module . getString ( "donate" ) ;
String donate ;
if ( module . has ( "donate" ) ) {
donate = module . getString ( "donate" ) ;
} else {
donate = "" ;
}
// get module config
String config = module . getString ( "config" ) ;
String config ;
if ( module . has ( "config" ) ) {
config = module . getString ( "config" ) ;
} else {
config = "" ;
}
// get module change boot
boolean changeBoot = module . getBoolean ( "changeBoot" ) ;
boolean changeBoot ;
if ( module . has ( "changeBoot" ) ) {
changeBoot = module . getBoolean ( "changeBoot" ) ;
} else {
changeBoot = false ;
}
// get module mmt reborn
boolean mmtReborn = module . getBoolean ( "mmtReborn" ) ;
boolean mmtReborn ;
if ( module . has ( "mmtReborn" ) ) {
mmtReborn = module . getBoolean ( "mmtReborn" ) ;
} else {
mmtReborn = false ;
}
// get module repo id
String repoId = this . repoData . id ;
// get module installed
@ -177,63 +216,53 @@ public class RepoUpdater {
// then insert to realm
// then commit
// then close
Realm . getInstanceAsync ( realmConfiguration , new Realm . Callback ( ) {
@Override
public void onSuccess ( @NonNull Realm realm ) {
realm . executeTransactionAsync ( r - > {
// create a new module
ModuleListCache moduleListCache = r . createObject ( ModuleListCache . class ) ;
// set module id
moduleListCache . setId ( id ) ;
// set module name
moduleListCache . setName ( name ) ;
// set module version
moduleListCache . setVersion ( version ) ;
// set module version code
moduleListCache . setVersionCode ( versionCode ) ;
// set module author
moduleListCache . setAuthor ( author ) ;
// set module description
moduleListCache . setDescription ( description ) ;
// set module min api
moduleListCache . setMinApi ( minApi ) ;
// set module max api
moduleListCache . setMaxApi ( maxApi ) ;
// set module min magisk
moduleListCache . setMinMagisk ( minMagisk ) ;
// set module need ramdisk
moduleListCache . setNeedRamdisk ( needRamdisk ) ;
// set module support
moduleListCache . setSupport ( support ) ;
// set module donate
moduleListCache . setDonate ( donate ) ;
// set module config
moduleListCache . setConfig ( config ) ;
// set module change boot
moduleListCache . setChangeBoot ( changeBoot ) ;
// set module mmt reborn
moduleListCache . setMmtReborn ( mmtReborn ) ;
// set module repo id
moduleListCache . setRepoId ( repoId ) ;
// set module installed
moduleListCache . setInstalled ( installed ) ;
// set module installed version code
moduleListCache . setInstalledVersionCode ( installedVersionCode ) ;
} , ( ) - > {
// Transaction was a success.
Timber . d ( "onSuccess: Transaction was a success." ) ;
// close realm
realm . close ( ) ;
} , error - > {
// Transaction failed and was automatically canceled.
Timber . e ( error ) ;
// close realm
realm . close ( ) ;
} ) ;
}
Realm realm = Realm . getInstance ( realmConfiguration ) ;
if ( realm . isInTransaction ( ) ) {
realm . cancelTransaction ( ) ;
}
realm . executeTransaction ( r - > {
// create the object
// if it already exists, it will be updated
// create a new module
ModuleListCache moduleListCache = r . createObject ( ModuleListCache . class , id ) ;
// set module name
moduleListCache . setName ( name ) ;
// set module version
moduleListCache . setVersion ( version ) ;
// set module version code
moduleListCache . setVersionCode ( versionCode ) ;
// set module author
moduleListCache . setAuthor ( author ) ;
// set module description
moduleListCache . setDescription ( description ) ;
// set module min api
moduleListCache . setMinApi ( minApiInt ) ;
// set module max api
moduleListCache . setMaxApi ( maxApiInt ) ;
// set module min magisk
moduleListCache . setMinMagisk ( minMagiskInt ) ;
// set module need ramdisk
moduleListCache . setNeedRamdisk ( needRamdisk ) ;
// set module support
moduleListCache . setSupport ( support ) ;
// set module donate
moduleListCache . setDonate ( donate ) ;
// set module config
moduleListCache . setConfig ( config ) ;
// set module change boot
moduleListCache . setChangeBoot ( changeBoot ) ;
// set module mmt reborn
moduleListCache . setMmtReborn ( mmtReborn ) ;
// set module repo id
moduleListCache . setRepoId ( repoId ) ;
// set module installed
moduleListCache . setInstalled ( installed ) ;
// set module installed version code
moduleListCache . setInstalledVersionCode ( installedVersionCode ) ;
} ) ;
realm . close ( ) ;
} catch (
JSON Exception e ) {
Exception e ) {
e . printStackTrace ( ) ;
Timber . w ( "Failed to get module info from module " + module + " in repo " + this . repoData . id + " with error " + e . getMessage ( ) ) ;
}