77package com.fox2code.mmm.manager
88
99import android.content.SharedPreferences
10+ import androidx.room.Room
1011import com.fox2code.mmm.BuildConfig
1112import com.fox2code.mmm.MainApplication
1213import com.fox2code.mmm.installer.InstallerInitializer.Companion.peekModulesPath
1314import com.fox2code.mmm.utils.SyncManager
1415import com.fox2code.mmm.utils.io.PropUtils
15- import com.fox2code.mmm.utils.realm.ModuleListCache
16+ import com.fox2code.mmm.utils.room.ModuleListCache
17+ import com.fox2code.mmm.utils.room.ModuleListCacheDao
18+ import com.fox2code.mmm.utils.room.ModuleListCacheDatabase
1619import com.topjohnwu.superuser.Shell
1720import com.topjohnwu.superuser.io.SuFile
1821import com.topjohnwu.superuser.io.SuFileInputStream
19- import io.realm.Realm
20- import io.realm.RealmConfiguration
2122import org.matomo.sdk.extra.TrackHelper
2223import timber.log.Timber
2324import java.io.BufferedReader
24- import java.io.File
2525import java.io.IOException
2626import java.io.InputStreamReader
2727import java.nio.charset.StandardCharsets
28- import java.util.Objects
2928
3029class ModuleManager private constructor() : SyncManager() {
3130 private val moduleInfos: HashMap <String , LocalModuleInfo > = HashMap ()
3231 private val bootPrefs: SharedPreferences = MainApplication .bootSharedPreferences!!
3332 private var updatableModuleCount = 0
3433
3534 override fun scanInternal (updateListener : UpdateListener ) {
36- // if last_shown_setup is not "v2 ", then refuse to continue
35+ // if last_shown_setup is not "v3 ", then refuse to continue
3736 if (MainApplication .getSharedPreferences(" mmm" )!!
38- .getString(" last_shown_setup" , " " ) != " v2 "
37+ .getString(" last_shown_setup" , " " ) != " v3 "
3938 ) {
4039 return
4140 }
@@ -65,60 +64,36 @@ class ModuleManager private constructor() : SyncManager() {
6564 for (module in modules) {
6665 if (! SuFile (" /data/adb/modules/$module " ).isDirectory) continue // Ignore non directory files inside modules folder
6766 var moduleInfo = moduleInfos[module]
68- // next, merge the module info with a record from ModuleListCache if it exists
69- var realmConfiguration: RealmConfiguration ?
70- // get all dirs under the realms/repos/ dir under app's data dir
71- val cacheRoot =
72- File (MainApplication .INSTANCE !! .getDataDirWithPath(" realms/repos/" ).toURI())
73- var moduleListCache: ModuleListCache ?
74- for (dir in Objects .requireNonNull<Array <File >>(cacheRoot.listFiles())) {
75- if (dir.isDirectory) {
76- // if the dir name matches the module name, use it as the cache dir
77- val tempCacheRoot = File (dir.toString())
78- Timber .d(" Looking for cache in %s" , tempCacheRoot)
79- realmConfiguration =
80- RealmConfiguration .Builder ().name(" ModuleListCache.realm" )
81- .encryptionKey(MainApplication .INSTANCE !! .key).schemaVersion(1 )
82- .deleteRealmIfMigrationNeeded().allowWritesOnUiThread(true )
83- .allowQueriesOnUiThread(true ).directory(tempCacheRoot).build()
84- val realm = Realm .getInstance(realmConfiguration!! )
85- Timber .d(
86- " Looking for cache for %s out of %d" , module, realm.where(
87- ModuleListCache ::class .java
88- ).count()
89- )
90- moduleListCache =
91- realm.where(ModuleListCache ::class .java).equalTo(" codename" , module)
92- .findFirst()
93- Timber .d(" Found cache for %s" , module)
94- // get module info from cache
95- if (moduleInfo == null ) {
96- moduleInfo = LocalModuleInfo (module)
97- }
98- if (moduleListCache != null ) {
99- moduleInfo.name =
100- if (moduleListCache.name != " " ) moduleListCache.name else module
101- moduleInfo.description =
102- if (moduleListCache.description != " " ) moduleListCache.description else moduleInfo.description
103- moduleInfo.author =
104- if (moduleListCache.author != " " ) moduleListCache.author else moduleInfo.author
105- moduleInfo.safe = moduleListCache.isSafe == true
106- moduleInfo.support =
107- if (moduleListCache.support != " " ) moduleListCache.support else null
108- moduleInfo.donate =
109- if (moduleListCache.donate != " " ) moduleListCache.donate else null
110- moduleInfo.flags = moduleInfo.flags or FLAG_MM_REMOTE_MODULE
111- moduleInfos[module] = moduleInfo
112- }
113- realm.close()
114- break
115- }
116- }
67+ // next, merge the module info with a record from ModuleListCache room db if it exists
68+ // initialize modulelistcache db
69+ // DO NOT USE REALM ANYMORE
70+ val db = Room .databaseBuilder(
71+ MainApplication .INSTANCE !! ,
72+ ModuleListCacheDatabase ::class .java,
73+ " ModuleListCache"
74+ ).build()
75+ // get module info from cache
76+ val moduleListCacheDao: ModuleListCacheDao = db.moduleListCacheDao()
77+ Timber .d(" Found cache for %s" , module)
78+ // get module info from cache
11779 if (moduleInfo == null ) {
11880 moduleInfo = LocalModuleInfo (module)
81+ }
82+ if (moduleListCacheDao.exists(module)) {
83+ val moduleListCache: ModuleListCache = moduleListCacheDao.getByCodename(module)
84+ moduleInfo.name =
85+ if (moduleListCache.name != " " ) moduleListCache.name else module
86+ moduleInfo.description =
87+ if (moduleListCache.description != " " ) moduleListCache.description else moduleInfo.description
88+ moduleInfo.author =
89+ if (moduleListCache.author != " " ) moduleListCache.author else moduleInfo.author
90+ moduleInfo.safe = moduleListCache.safe == true
91+ moduleInfo.support =
92+ if (moduleListCache.support != " " ) moduleListCache.support else null
93+ moduleInfo.donate =
94+ if (moduleListCache.donate != " " ) moduleListCache.donate else null
95+ moduleInfo.flags = moduleInfo.flags or FLAG_MM_REMOTE_MODULE
11996 moduleInfos[module] = moduleInfo
120- // This should not really happen, but let's handles theses cases anyway
121- moduleInfo.flags = moduleInfo.flags or ModuleInfo .FLAG_MODULE_UPDATING_ONLY
12297 }
12398 moduleInfo.flags = moduleInfo.flags and FLAGS_RESET_UPDATE .inv ()
12499 if (SuFile (" /data/adb/modules/$module /disable" ).exists()) {
@@ -131,8 +106,7 @@ class ModuleManager private constructor() : SyncManager() {
131106 moduleInfo.flags = moduleInfo.flags or ModuleInfo .FLAG_MODULE_UNINSTALLING
132107 }
133108 if (firstScan && ! needFallback && SuFile (
134- modulesPath,
135- module
109+ modulesPath, module
136110 ).exists() || bootPrefs.getBoolean(" module_" + moduleInfo.id + " _active" , false )
137111 ) {
138112 moduleInfo.flags = moduleInfo.flags or ModuleInfo .FLAG_MODULE_ACTIVE
@@ -152,9 +126,7 @@ class ModuleManager private constructor() : SyncManager() {
152126 }
153127 try {
154128 PropUtils .readProperties(
155- moduleInfo,
156- " /data/adb/modules/$module /module.prop" ,
157- true
129+ moduleInfo, " /data/adb/modules/$module /module.prop" , true
158130 )
159131 } catch (e: Exception ) {
160132 if (BuildConfig .DEBUG ) Timber .d(e)
@@ -186,9 +158,7 @@ class ModuleManager private constructor() : SyncManager() {
186158 moduleInfo.flags = moduleInfo.flags or ModuleInfo .FLAG_MODULE_UPDATING
187159 try {
188160 PropUtils .readProperties(
189- moduleInfo,
190- " /data/adb/modules_update/$module /module.prop" ,
191- true
161+ moduleInfo, " /data/adb/modules_update/$module /module.prop" , true
192162 )
193163 } catch (e: Exception ) {
194164 if (BuildConfig .DEBUG ) Timber .d(e)
0 commit comments