Skip to content

Commit 50e53e5

Browse files
committed
Replace new database.
1 parent 1eb5b4e commit 50e53e5

File tree

7 files changed

+116
-8
lines changed

7 files changed

+116
-8
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
}
77
dependencies {
88
classpath "com.android.tools.build:gradle:7.3.1"
9-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0"
9+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0"
1010

1111
// NOTE: Do not place your application dependencies here; they belong
1212
// in the individual module build.gradle files

library/build.gradle

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ android {
3636
}
3737

3838
dependencies {
39-
api 'androidx.core:core-ktx:1.9.0'
40-
api 'androidx.appcompat:appcompat:1.6.0'
41-
api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
39+
implementation 'androidx.core:core-ktx:1.9.0'
40+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
4241

43-
api 'androidx.room:room-runtime:2.5.0'
44-
api 'androidx.room:room-ktx:2.5.0'
45-
api 'javax.annotation:javax.annotation-api:1.3.2'
42+
implementation 'androidx.room:room-runtime:2.5.0'
43+
implementation 'androidx.room:room-ktx:2.5.0'
44+
compileOnly 'javax.annotation:javax.annotation-api:1.3.2'
4645
}

library/src/main/java/com/absinthe/rulesbundle/LCRules.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ object LCRules : IAPI {
1414
private var ruleRepo: RuleRepository? = null
1515

1616
fun init(context: Context) {
17+
Repositories.checkRulesDatabase(context)
1718
contextRef = WeakReference(context)
1819
ruleRepo = RuleRepository(RuleDatabase.getDatabase(context).ruleDao())
1920
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.absinthe.rulesbundle
2+
3+
import android.content.Context
4+
import com.absinthe.rulesbundle.utils.FileUtils
5+
import com.absinthe.rulesbundle.utils.HashUtils
6+
import java.io.File
7+
8+
object Repositories {
9+
10+
const val RULES_DATABASE_NAME = "rules_database"
11+
12+
fun checkRulesDatabase(context: Context) {
13+
if (!checkMd5(context) && !checkVersion(context)) {
14+
deleteRulesDatabase(context)
15+
}
16+
}
17+
18+
private fun deleteRulesDatabase(context: Context) {
19+
val databaseDir = context.getDatabasePath(RULES_DATABASE_NAME).parent
20+
FileUtils.delete(File(databaseDir, RULES_DATABASE_NAME))
21+
FileUtils.delete(File(databaseDir, "${RULES_DATABASE_NAME}-shm"))
22+
FileUtils.delete(File(databaseDir, "${RULES_DATABASE_NAME}-wal"))
23+
}
24+
25+
private fun checkMd5(context: Context): Boolean {
26+
val databaseDir = context.getDatabasePath(RULES_DATABASE_NAME).parent
27+
val localDB = File(databaseDir, RULES_DATABASE_NAME)
28+
if (localDB.exists().not()) return false
29+
val localDBMd5 = HashUtils.md5(localDB.readBytes())
30+
val assetDBMd5 = context.assets.open(LCRules.getRulesAssetPath()).use {
31+
HashUtils.md5(it.readBytes())
32+
}
33+
return localDBMd5 == assetDBMd5
34+
}
35+
36+
private fun checkVersion(context: Context): Boolean {
37+
val versionFile = File(context.filesDir, "lcrules/version")
38+
if (versionFile.exists().not()) return false
39+
if (versionFile.isDirectory.not()) return false
40+
versionFile.listFiles()?.takeIf { it.isNotEmpty() }?.let {
41+
val version = runCatching { it[0].name.toInt() }.getOrDefault(0)
42+
return version >= LCRules.getVersion()
43+
} ?: return false
44+
}
45+
}

library/src/main/java/com/absinthe/rulesbundle/RuleDatabase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ abstract class RuleDatabase : RoomDatabase() {
2626
return Room.databaseBuilder(
2727
context.applicationContext,
2828
RuleDatabase::class.java,
29-
"rules_database"
29+
Repositories.RULES_DATABASE_NAME
3030
).fallbackToDestructiveMigration()
3131
.createFromAsset(LCRules.getRulesAssetPath())
3232
.build()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.absinthe.rulesbundle.utils
2+
3+
import java.io.File
4+
5+
object FileUtils {
6+
7+
fun delete(file: File?): Boolean {
8+
if (file == null) return false
9+
return if (file.isDirectory) {
10+
deleteDir(file)
11+
} else {
12+
deleteFile(file)
13+
}
14+
}
15+
16+
private fun deleteDir(dir: File?): Boolean {
17+
if (dir == null) return false
18+
// dir doesn't exist then return true
19+
if (!dir.exists()) return true
20+
// dir isn't a directory then return false
21+
if (!dir.isDirectory) return false
22+
val files = dir.listFiles()
23+
if (files != null && files.isNotEmpty()) {
24+
for (file in files) {
25+
if (file.isFile) {
26+
if (!file.delete()) return false
27+
} else if (file.isDirectory) {
28+
if (!deleteDir(file)) return false
29+
}
30+
}
31+
}
32+
return dir.delete()
33+
}
34+
35+
private fun deleteFile(file: File?): Boolean {
36+
return file != null && (!file.exists() || file.isFile && file.delete())
37+
}
38+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.absinthe.rulesbundle.utils
2+
3+
import java.security.MessageDigest
4+
5+
object HashUtils {
6+
fun md5(byteArray: ByteArray): String = digest(byteArray, "MD5")
7+
8+
fun sha1(byteArray: ByteArray): String = digest(byteArray, "SHA-1")
9+
10+
fun sha256(byteArray: ByteArray): String = digest(byteArray, "SHA-256")
11+
12+
fun sha512(byteArray: ByteArray): String = digest(byteArray, "SHA-512")
13+
14+
private fun digest(byteArray: ByteArray, algorithm: String): String {
15+
val messageDigest = MessageDigest.getInstance(algorithm).apply {
16+
reset()
17+
}
18+
messageDigest.update(byteArray)
19+
return messageDigest.digest().toHexString()
20+
}
21+
22+
private fun ByteArray.toHexString() : String {
23+
return this.joinToString("") { it.toString(16) }
24+
}
25+
}

0 commit comments

Comments
 (0)