From cdaff5b39c41e3b76b7e0d65ef16de9df2fbc0f1 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 26 Jul 2019 02:26:02 -0700 Subject: [PATCH] Update module download pipeline --- .../data/network/GithubRawApiServices.kt | 4 +++ .../magisk/data/repository/FileRepository.kt | 2 ++ .../topjohnwu/magisk/di/NetworkingModule.kt | 26 +++++-------------- .../model/download/RemoteFileService.kt | 13 ++++------ 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/data/network/GithubRawApiServices.kt b/app/src/main/java/com/topjohnwu/magisk/data/network/GithubRawApiServices.kt index 7663fb839..f90468678 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/network/GithubRawApiServices.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/network/GithubRawApiServices.kt @@ -37,6 +37,10 @@ interface GithubRawApiServices { @Streaming fun fetchBootctl(@Path(REVISION) revision: String = Const.BOOTCTL_REVISION): Single + @GET("$MAGISK_MASTER/scripts/module_installer.sh") + @Streaming + fun fetchInstaller(): Single + //endregion /** diff --git a/app/src/main/java/com/topjohnwu/magisk/data/repository/FileRepository.kt b/app/src/main/java/com/topjohnwu/magisk/data/repository/FileRepository.kt index e69a497b2..e1d9c7f1f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/repository/FileRepository.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/repository/FileRepository.kt @@ -8,4 +8,6 @@ class FileRepository( fun downloadFile(url: String) = api.fetchFile(url) + fun downloadInstaller() = api.fetchInstaller() + } \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/di/NetworkingModule.kt b/app/src/main/java/com/topjohnwu/magisk/di/NetworkingModule.kt index ae69f23b7..9601778a1 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/NetworkingModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/NetworkingModule.kt @@ -8,8 +8,6 @@ import com.topjohnwu.magisk.data.network.GithubRawApiServices import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import org.koin.dsl.module -import retrofit2.CallAdapter -import retrofit2.Converter import retrofit2.Retrofit import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory import retrofit2.converter.moshi.MoshiConverterFactory @@ -17,9 +15,8 @@ import se.ansman.kotshi.KotshiJsonAdapterFactory val networkingModule = module { single { createOkHttpClient() } - single { createConverterFactory() } - single { createCallAdapterFactory() } - single { createRetrofit(get(), get(), get()) } + single { createMoshiConverterFactory() } + single { createRetrofit(get(), get()) } single { createApiService(get(), Const.Url.GITHUB_RAW_API_URL) } } @@ -36,34 +33,25 @@ fun createOkHttpClient(): OkHttpClient { return builder.build() } -fun createConverterFactory(): Converter.Factory { +fun createMoshiConverterFactory(): MoshiConverterFactory { val moshi = Moshi.Builder() - .add(JsonAdapterFactory.INSTANCE) + .add(KotshiJsonAdapterFactory) .build() return MoshiConverterFactory.create(moshi) } -fun createCallAdapterFactory(): CallAdapter.Factory { - return RxJava2CallAdapterFactory.create() -} - fun createRetrofit( okHttpClient: OkHttpClient, - converterFactory: Converter.Factory, - callAdapterFactory: CallAdapter.Factory + converterFactory: MoshiConverterFactory ): Retrofit.Builder { return Retrofit.Builder() .addConverterFactory(converterFactory) - .addCallAdapterFactory(callAdapterFactory) + .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .client(okHttpClient) } @KotshiJsonAdapterFactory -abstract class JsonAdapterFactory : JsonAdapter.Factory { - companion object { - val INSTANCE: JsonAdapterFactory = KotshiJsonAdapterFactory - } -} +abstract class JsonAdapterFactory : JsonAdapter.Factory inline fun createApiService(retrofitBuilder: Retrofit.Builder, baseUrl: String): T { return retrofitBuilder diff --git a/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt b/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt index 5e6e0e7a5..5d66f84b3 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt @@ -4,7 +4,6 @@ import android.content.Intent import androidx.core.app.NotificationCompat import com.skoumal.teanity.extensions.subscribeK import com.topjohnwu.magisk.Config -import com.topjohnwu.magisk.Const import com.topjohnwu.magisk.R import com.topjohnwu.magisk.data.repository.FileRepository import com.topjohnwu.magisk.model.entity.internal.DownloadSubject @@ -71,13 +70,11 @@ abstract class RemoteFileService : NotificationService() { private fun download(subject: DownloadSubject) = repo.downloadFile(subject.url) .map { it.toStream(subject.hashCode()) } - .map { - subject.file.apply { - when (subject) { - is Module -> it.toModule(this, - repo.downloadFile(Const.Url.MODULE_INSTALLER).blockingGet().byteStream()) - else -> it.writeTo(this) - } + .flatMap { stream -> + when (subject) { + is Module -> repo.downloadInstaller() + .map { stream.toModule(subject.file, it.byteStream()); subject.file } + else -> Single.fromCallable { stream.writeTo(subject.file); subject.file } } }