重构loader

v4
lyy 2 years ago
parent 126b88d67f
commit 0d814d6494
  1. 1
      Aria/build.gradle
  2. 34
      Aria/src/main/java/com/arialyy/aria/core/Dua.kt
  3. 58
      Aria/src/main/java/com/arialyy/aria/core/common/receiver/LifLifecycleReceiver.kt
  4. 24
      Aria/src/main/java/com/arialyy/aria/core/inf/IDuaReceiver.kt
  5. 91
      Aria/src/main/java/com/arialyy/aria/core/manager/LifecycleManager.kt
  6. 45
      HttpComponent/src/main/java/com/arialyy/aria/http/HttpComponentLoader.kt
  7. 27
      HttpComponent/src/main/java/com/arialyy/aria/http/HttpOption.kt
  8. 53
      HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDownloader.kt
  9. 4
      PublicComponent/build.gradle
  10. 1
      PublicComponent/src/main/java/com/arialyy/aria/core/DuaContext.kt
  11. 27
      PublicComponent/src/main/java/com/arialyy/aria/core/inf/IBaseLoader.kt
  12. 36
      PublicComponent/src/main/java/com/arialyy/aria/core/inf/IComponentLoader.kt
  13. 25
      PublicComponent/src/main/java/com/arialyy/aria/core/inf/IDownloader.kt

@ -25,6 +25,7 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation(libs.bundles.android.test)
implementation(libs.appcompat)
implementation(libs.timber)
api project(path: ':AriaAnnotations')
api project(path: ':PublicComponent')
api project(path: ':HttpComponent')

@ -0,0 +1,34 @@
/*
* 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 androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle.Event
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.arialyy.aria.core.common.receiver.LifLifecycleReceiver
/**
* @Author laoyuyu
* @Description
* @Date 10:48 AM 2023/1/20
**/
object Dua {
fun with(lifecycle: LifecycleOwner): LifLifecycleReceiver {
return LifLifecycleReceiver(lifecycle)
}
}

@ -0,0 +1,58 @@
/*
* 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.common.receiver
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.arialyy.aria.core.inf.IComponentLoader
import com.arialyy.aria.core.inf.IDuaReceiver
import com.arialyy.aria.core.scheduler.TaskSchedulers
import java.lang.reflect.Proxy
/**
* @Author laoyuyu
* @Description
* @Date 11:42 AM 2023/1/20
**/
class LifLifecycleReceiver(val lifecycle: LifecycleOwner) : IDuaReceiver {
/**
* You need to associate the appropriate component dependencies
* @param clazz eg: HttpLoader, FtpLoader
*/
fun <T : IComponentLoader> setLoader(clazz: Class<T>): T {
return Proxy.newProxyInstance(
javaClass.classLoader, arrayOf(IComponentLoader::class.java)
) { proxy, method, args ->
val result = method.invoke(proxy, args)
if (method.name in IComponentLoader.proxyMethods) {
lifecycle.lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
TaskSchedulers.getInstance()
.register(lifecycle, (proxy as IComponentLoader).getTaskEnum())
}
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
TaskSchedulers.getInstance().unRegister(lifecycle)
}
})
}
return@newProxyInstance result
} as T
}
}

@ -0,0 +1,24 @@
/*
* 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.inf
/**
* @Author laoyuyu
* @Description
* @Date 11:34 AM 2023/1/20
**/
interface IDuaReceiver {
}

@ -0,0 +1,91 @@
/*
* 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.manager
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import com.arialyy.annotations.TaskEnum
import com.arialyy.aria.core.common.ProxyHelper
import com.arialyy.aria.core.download.DownloadGroupTaskListener
import com.arialyy.aria.core.download.DownloadTaskListener
import com.arialyy.aria.core.scheduler.M3U8PeerTaskListener
import com.arialyy.aria.core.scheduler.SubTaskListener
import com.arialyy.aria.core.scheduler.TaskInternalListenerInterface
import com.arialyy.aria.core.scheduler.TaskSchedulers
import com.arialyy.aria.util.ALog
import timber.log.Timber
import java.util.Timer
/**
* @Author laoyuyu
* @Description
* @Date 10:55 AM 2023/1/20
**/
object LifecycleManager {
private fun register(obj: Any) {
if (obj is TaskInternalListenerInterface) {
ProxyHelper.getInstance().checkProxyType(obj.javaClass)
if (obj is DownloadTaskListener) {
TaskSchedulers.getInstance().register(obj, TaskEnum.DOWNLOAD)
}
if (obj is DownloadGroupTaskListener) {
TaskSchedulers.getInstance().register(obj, TaskEnum.DOWNLOAD_GROUP)
}
if (obj is M3U8PeerTaskListener) {
TaskSchedulers.getInstance().register(obj, TaskEnum.M3U8_PEER)
}
if (obj is SubTaskListener<*, *>) {
TaskSchedulers.getInstance().register(obj, TaskEnum.DOWNLOAD_GROUP_SUB)
}
return
}
val set: Set<Int> = ProxyHelper.getInstance().checkProxyType(obj.javaClass)
if (set.isNotEmpty()) {
for (type in set) {
when (type) {
ProxyHelper.PROXY_TYPE_DOWNLOAD -> {
TaskSchedulers.getInstance().register(obj, TaskEnum.DOWNLOAD)
}
ProxyHelper.PROXY_TYPE_DOWNLOAD_GROUP -> {
TaskSchedulers.getInstance().register(obj, TaskEnum.DOWNLOAD_GROUP)
}
ProxyHelper.PROXY_TYPE_M3U8_PEER -> {
TaskSchedulers.getInstance().register(obj, TaskEnum.M3U8_PEER)
}
ProxyHelper.PROXY_TYPE_DOWNLOAD_GROUP_SUB -> {
TaskSchedulers.getInstance().register(obj, TaskEnum.DOWNLOAD_GROUP_SUB)
}
}
}
return
}
Timber.e("没有Aria的注解方法,详情见:https://aria.laoyuyu.me/aria_doc/other/annotaion_invalid.html")
}
fun addObserver(lifecycle: Lifecycle) {
lifecycle.addObserver(DuaObserver(lifecycle))
}
private class DuaObserver(val obj: Lifecycle) : DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
register(obj)
}
}
}

@ -0,0 +1,45 @@
/*
* 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.http
import com.arialyy.annotations.TaskEnum
import com.arialyy.aria.core.inf.IBaseLoader
import com.arialyy.aria.core.inf.IComponentLoader
import com.arialyy.aria.core.inf.IDownloader
import com.arialyy.aria.http.download.HttpDownloader
import kotlin.LazyThreadSafetyMode.SYNCHRONIZED
/**
* @Author laoyuyu
* @Description
* @Date 14:07 AM 2023/1/20
**/
class HttpComponentLoader : IComponentLoader {
private val downloader by lazy(SYNCHRONIZED) {
HttpDownloader()
}
private lateinit var loader: IBaseLoader
override fun <T : IDownloader> download(): T {
loader = downloader
return downloader as T
}
override fun getTaskEnum(): TaskEnum {
return loader.getTaskEnum()
}
}

@ -0,0 +1,27 @@
/*
* 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.http
/**
* @Author laoyuyu
* @Description
* @Date 14:27 AM 2023/1/20
**/
class HttpOption {
fun se
}

@ -0,0 +1,53 @@
/*
* 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.http.download
import com.arialyy.annotations.TaskEnum
import com.arialyy.annotations.TaskEnum.DOWNLOAD
import com.arialyy.aria.core.inf.IDownloader
import com.arialyy.aria.http.HttpOption
/**
* @Author laoyuyu
* @Description
* @Date 14:11 AM 2023/1/20
**/
internal class HttpDownloader : IDownloader {
private lateinit var uri: String
private lateinit var savePath: String
private var httpOption = HttpOption()
override fun getTaskEnum(): TaskEnum {
return DOWNLOAD
}
fun setSourceUri(uri: String): HttpDownloader {
this.uri = uri
return this
}
fun setSavePath(savePath: String): HttpDownloader {
this.savePath = savePath
return this
}
fun setHttpOption(httpOption: HttpOption): HttpDownloader {
this.httpOption = httpOption
return this
}
}

@ -26,16 +26,16 @@ android {
lintOptions {
abortOnError false
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
api project(path: ':AriaAnnotations')
testImplementation(libs.bundles.android.test)
implementation(libs.appcompat)
implementation(libs.bundles.room)
implementation(libs.startup)
implementation(libs.timber)
api(libs.timber)
implementation(libs.gson)
kapt libs.room.compiler
}

@ -34,4 +34,5 @@ internal object DuaContext {
fun isService(serviceName: String) = serviceName in serviceArray
fun getServiceManager() = ServiceManager
}

@ -0,0 +1,27 @@
/*
* 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.inf
import com.arialyy.annotations.TaskEnum
/**
* @Author laoyuyu
* @Description
* @Date 14:16 AM 2023/1/20
**/
interface IBaseLoader {
fun getTaskEnum(): TaskEnum
}

@ -0,0 +1,36 @@
/*
* 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.inf
import com.arialyy.annotations.TaskEnum
/**
* @Author laoyuyu
* @Description
* @Date 10:44 AM 2023/1/20
**/
interface IComponentLoader {
companion object {
val proxyMethods = arrayOf("download", "upload")
}
// fun upload()
fun <T : IDownloader> download(): T
fun getTaskEnum(): TaskEnum
}

@ -0,0 +1,25 @@
/*
* 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.inf
/**
* @Author laoyuyu
* @Description
* @Date 14:04 AM 2023/1/20
**/
interface IDownloader : IBaseLoader {
}
Loading…
Cancel
Save