diff --git a/Aria/src/main/java/com/arialyy/aria/core/provider/IdbProvider.kt b/Aria/src/main/java/com/arialyy/aria/core/provider/IdbProvider.kt deleted file mode 100644 index 18d2c80d..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/provider/IdbProvider.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.arialyy.aria.core.provider - -import androidx.room.RoomDatabase - -/** - * @Author laoyuyu - * @Description - * @Date 19:32 PM 2023/1/13 - **/ -interface IdbProvider { - fun initDb() -} \ No newline at end of file diff --git a/PublicComponent/build.gradle b/PublicComponent/build.gradle index 6da507c3..ce6d8d73 100644 --- a/PublicComponent/build.gradle +++ b/PublicComponent/build.gradle @@ -35,6 +35,8 @@ dependencies { implementation(libs.appcompat) implementation(libs.bundles.room) implementation(libs.startup) + implementation(libs.timber) + implementation(libs.gson) kapt libs.room.compiler } diff --git a/PublicComponent/src/main/AndroidManifest.xml b/PublicComponent/src/main/AndroidManifest.xml index 9d8f9d82..ddef2bc3 100644 --- a/PublicComponent/src/main/AndroidManifest.xml +++ b/PublicComponent/src/main/AndroidManifest.xml @@ -12,7 +12,7 @@ android:exported="false" tools:node="merge"> - diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/DuaContext.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/DuaContext.kt new file mode 100644 index 00000000..f00a923f --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/DuaContext.kt @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core + +import android.annotation.SuppressLint +import com.arialyy.aria.core.service.ServiceManager + +/** + * @Author laoyuyu + * @Description + * @Date 10:40 AM 2023/1/16 + **/ +@SuppressLint("StaticFieldLeak") +internal object DuaContext { + const val DB_SERVICE = "DB_SERVICE" + + private val serviceArray = arrayOf(DB_SERVICE) + + fun isService(serviceName: String) = serviceName in serviceArray + + fun getServiceManager() = ServiceManager +} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/provider/DuaStartupProvider.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/provider/DuaStartupProvider.kt new file mode 100644 index 00000000..685f1e70 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/provider/DuaStartupProvider.kt @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.provider + +import android.content.Context +import androidx.startup.Initializer +import com.arialyy.aria.core.DuaContext +import com.arialyy.aria.core.service.DbService +import timber.log.Timber +import timber.log.Timber.DebugTree + +class DuaStartupProvider : Initializer { + + override fun create(context: Context) { + DuaContext.getServiceManager().let { + it.registerService(DuaContext.DB_SERVICE, context, DbService::class.java) + } + initLog() + } + + private fun initLog() { + if (Timber.treeCount == 0) { + Timber.plant(DebugTree()) + } + } + + override fun dependencies(): MutableList>> { + return mutableListOf() + } +} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/provider/IDbProvider.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/provider/IDbProvider.kt index 41df67a8..5d8f5e1a 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/provider/IDbProvider.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/provider/IDbProvider.kt @@ -17,10 +17,11 @@ package com.arialyy.aria.core.provider import android.content.Context import androidx.room.RoomDatabase +import com.arialyy.aria.orm.DuaDb interface IDbProvider { fun getDbName() = "duaDb" - fun generateDb(context: Context): RoomDatabase.Builder + fun generateDb(context: Context): RoomDatabase.Builder } \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/DuaStartupProvider.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/service/DbService.kt similarity index 71% rename from PublicComponent/src/main/java/com/arialyy/aria/core/DuaStartupProvider.kt rename to PublicComponent/src/main/java/com/arialyy/aria/core/service/DbService.kt index 9ded26d8..f28b053a 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/DuaStartupProvider.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/service/DbService.kt @@ -13,22 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core +package com.arialyy.aria.core.service import android.content.Context -import androidx.room.Room -import androidx.room.RoomDatabase import androidx.room.RoomDatabase.Builder -import androidx.startup.Initializer import com.arialyy.aria.core.config.AutoGenerateConstance import com.arialyy.aria.orm.DefaultDbProvider +import com.arialyy.aria.orm.DuaDb import com.arialyy.aria.util.ReflectionUtil -class DuaStartupProvider : Initializer { +/** + * @Author laoyuyu + * @Description + * @Date 19:36 AM 2023/1/16 + **/ +open class DbService : IService { + private var duaDb: DuaDb? = null + /** * Find a user-defined database */ - private fun findCustomDatabase(context: Context): Builder? { + private fun findCustomDatabase(context: Context): Builder? { try { val clazz = javaClass.classLoader.loadClass(AutoGenerateConstance.GenerateClassName) ?: return null @@ -37,22 +42,24 @@ class DuaStartupProvider : Initializer { val method = ReflectionUtil.getMethod(clazz, "generateDb", Context::class.java) ?: return null - return method.invoke(obj, context) as Builder? + return method.invoke(obj, context) as Builder? } catch (e: java.lang.Exception) { return null } } - override fun create(context: Context) { + // fun findDEntity(dId: Int): DEntity? { + // if (duaDb == null) { + // return null + // } + // } + + override fun init(context: Context) { var customDb = findCustomDatabase(context) if (customDb == null) { customDb = DefaultDbProvider().generateDb(context) } - customDb.build() - // .addMigrations(MIGRATION_2_3(), MIGRATION_3_4()) - } - - override fun dependencies(): MutableList>> { - return mutableListOf() + duaDb = customDb + .build() } } \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/service/IService.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/service/IService.kt new file mode 100644 index 00000000..b9ce0ea4 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/service/IService.kt @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.service + +import android.content.Context + +/** + * @Author laoyuyu + * @Description + * @Date 19:34 AM 2023/1/16 + **/ +internal interface IService { + + fun init(context: Context) +} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/service/ServiceManager.kt b/PublicComponent/src/main/java/com/arialyy/aria/core/service/ServiceManager.kt new file mode 100644 index 00000000..243545a0 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/service/ServiceManager.kt @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.service + +import android.content.Context +import com.arialyy.aria.core.DuaContext +import com.arialyy.aria.exception.AriaException +import timber.log.Timber + +object ServiceManager { + private val serviceCache = hashMapOf() + + private fun getServiceName(clazz: Class<*>) = clazz.name + + /** + * register a service + * @param serviceName [DuaContext.DB_SERVICE] + */ + fun registerService(serviceName: String, context: Context, clazz: Class) { + if (!DuaContext.isService(serviceName)) { + throw AriaException("$serviceName Not a service.") + } + val sn = getServiceName(clazz) + val service = serviceCache[sn] + if (service == null) { + Timber.d("start register service: $sn") + val s = clazz.newInstance() + s.init(context) + serviceCache[serviceName] = s + } + } + + /** + * get datebase service, if already [registerService] custom service, return custom service + */ + fun getDbService(serviceName: String): DbService { + if (!DuaContext.isService(serviceName)) { + throw AriaException("$serviceName Not a service.") + } + return (serviceCache[serviceName] + ?: throw AriaException("service not found: $serviceName")) as DbService + } +} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/DGUrlConverter.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/DGUrlConverter.kt new file mode 100644 index 00000000..9c0296d9 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/DGUrlConverter.kt @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.orm + +import androidx.room.ProvidedTypeConverter +import androidx.room.TypeConverter +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken + +/** + * @Author laoyuyu + * @Description + * @Date 7:24 PM 2023/1/16 + **/ +@ProvidedTypeConverter +class DGUrlConverter { + private val gson by lazy { + Gson() + } + + @TypeConverter + fun stringToList(string: String?): List { + if (string.isNullOrEmpty()) return emptyList() + return gson.fromJson(string, object : TypeToken>() {}.type) + } + + @TypeConverter + fun listToString(strList: List): String { + return gson.toJson(strList) + } +} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/DefaultDbProvider.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/DefaultDbProvider.kt index 82eb6c68..eafa68c8 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/DefaultDbProvider.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/DefaultDbProvider.kt @@ -23,7 +23,7 @@ import com.arialyy.aria.core.provider.IDbProvider class DefaultDbProvider : IDbProvider { - override fun generateDb(context: Context): Builder { + override fun generateDb(context: Context): Builder { return Room.databaseBuilder( context, DuaDb::class.java, getDbName() diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/DuaDb.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/DuaDb.kt index c4df9a8e..7e0a75a4 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/DuaDb.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/DuaDb.kt @@ -17,6 +17,8 @@ package com.arialyy.aria.orm import androidx.room.Database import androidx.room.RoomDatabase +import com.arialyy.aria.orm.dao.DEntityDao +import com.arialyy.aria.orm.dao.UEntityDao import com.arialyy.aria.orm.entiry.UEntity @Database( @@ -24,7 +26,7 @@ import com.arialyy.aria.orm.entiry.UEntity version = 1 ) abstract class DuaDb : RoomDatabase() { - companion object { + abstract fun getDEntityDao(): DEntityDao - } + abstract fun getUEntityDao(): UEntityDao } \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/dao/DEntityDao.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/dao/DEntityDao.kt new file mode 100644 index 00000000..11d8908a --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/dao/DEntityDao.kt @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.orm.dao + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.Query +import androidx.room.Update +import com.arialyy.aria.orm.entiry.DEntity + +/** + * @Author laoyuyu + * @Description + * @Date 19:23 AM 2023/1/16 + **/ +@Dao +interface DEntityDao { + @Query("SELECT * FROM DEntity WHERE :dId=dId") + suspend fun queryDEntityById(dId: String): DEntity + + @Query("SELECT * FROM DEntity WHERE :sourceUrl=sourceUrl") + suspend fun queryDEntityBySource(sourceUrl: String): DEntity + + @Insert + suspend fun insert(dEntity: DEntity) + + @Update + suspend fun update(dEntity: DEntity) + + @Delete + suspend fun delete(dEntity: DEntity) +} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/dao/UEntityDao.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/dao/UEntityDao.kt new file mode 100644 index 00000000..4955f5e7 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/dao/UEntityDao.kt @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.orm.dao + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.Query +import androidx.room.Update +import com.arialyy.aria.orm.entiry.UEntity + +/** + * @Author laoyuyu + * @Description + * @Date 19:23 AM 2023/1/16 + **/ +@Dao +interface UEntityDao { + @Query("SELECT * FROM DEntity WHERE :uId=uId") + suspend fun queryUEntityById(uId: String): UEntity + + @Query("SELECT * FROM UEntity WHERE :filePath=filePath") + suspend fun queryUEntityBySource(filePath: String): UEntity + + @Insert + suspend fun insert(uEntity: UEntity) + + @Update + suspend fun update(uEntity: UEntity) + + @Delete + suspend fun delete(uEntity: UEntity) +} \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/entiry/DEntity.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/entiry/DEntity.kt index 6a3ab9e9..eea29349 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/entiry/DEntity.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/entiry/DEntity.kt @@ -22,7 +22,7 @@ import androidx.room.PrimaryKey /** * Download Entity */ -@Entity(tableName = "d_entity", indices = [Index(value = ["sourceUrl", "savePath"])]) +@Entity(indices = [Index(value = ["sourceUrl", "savePath"])]) data class DEntity( @PrimaryKey(autoGenerate = true) val dId: Int = 0, @@ -37,5 +37,9 @@ data class DEntity( /** * extended Information */ - var ext: String? = null + var ext: String? = null, + + val createTime: Long, + + val updateTime: Long ) \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/entiry/DGEntity.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/entiry/DGEntity.kt new file mode 100644 index 00000000..d8760e70 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/entiry/DGEntity.kt @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.orm.entiry + +import androidx.room.Entity +import androidx.room.PrimaryKey +import androidx.room.TypeConverters +import com.arialyy.aria.orm.DGUrlConverter + +/** + * @Author laoyuyu + * @Description + * @Date 4:32 PM 2023/1/16 + **/ +@Entity +@TypeConverters(DGUrlConverter::class) +data class DGEntity( + @PrimaryKey(autoGenerate = true) val dgId: Int = 0, + + /** + * 组合任务等hash为: 为子任务地址相加的url的Md5 + * ftpdir为:ftpdir下载地址 + */ + val groupHash: String, + + /** + * 任务组别名 + */ + val alias: String? = null, + + /** + * 保存路径 + */ + val savePath: String, + + /** + * 子任务url地址 + */ + val urls: List, + + /** + * extended Information + */ + var ext: String? = null, + + val createTime: Long, + + val updateTime: Long +) \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/entiry/DGSubList.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/entiry/DGSubList.kt new file mode 100644 index 00000000..b4405a20 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/entiry/DGSubList.kt @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.orm.entiry + +import androidx.room.Embedded +import androidx.room.Relation + +data class DGSubList( + @Embedded val dgEntity: DGEntity, + @Relation( + parentColumn = "dgId", + entityColumn = "dId" + ) + val subEntity: List +) diff --git a/PublicComponent/src/main/java/com/arialyy/aria/orm/entiry/UEntity.kt b/PublicComponent/src/main/java/com/arialyy/aria/orm/entiry/UEntity.kt index 3df430ea..f212f403 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/orm/entiry/UEntity.kt +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/entiry/UEntity.kt @@ -19,7 +19,7 @@ import androidx.room.Entity import androidx.room.Index import androidx.room.PrimaryKey -@Entity(tableName = "u_entity", indices = [Index(value = ["serverUrl", "filePath"])]) +@Entity(indices = [Index(value = ["serverUrl", "filePath"])]) data class UEntity( @PrimaryKey(autoGenerate = true) val uId: Int = 0, /** @@ -35,5 +35,9 @@ data class UEntity( /** * extended Information */ - var ext: String? = null + var ext: String? = null, + + val createTime: Long, + + val updateTime: Long ) \ No newline at end of file diff --git a/libs.versions.toml b/libs.versions.toml index 17eb4055..7a4b3a9e 100644 --- a/libs.versions.toml +++ b/libs.versions.toml @@ -12,6 +12,8 @@ android-espresso = "3.5.0" android-ktx = "1.7.0" lifecycle = "2.5.1" startup = "1.1.1" +timber = "5.0.1" +gson = "2.10.1" [libraries] @@ -28,6 +30,8 @@ lifecycle-viewmodel-ktx = {module = "androidx.lifecycle:lifecycle-viewmodel-ktx" lifecycle-viewmodel-compose = {module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycle"} lifecycle-runtime-ktx = {module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycle"} startup = {module = "androidx.startup:startup-runtime", version.ref = "startup"} +timber = {module = "com.jakewharton.timber:timber", version.ref = "timber"} +gson = {module = "com.google.code.gson:gson", version.ref = "gson"} [bundles] room = ["room-runtime", "room-common"]