Skip to content
This repository has been archived by the owner on Oct 3, 2022. It is now read-only.

Commit

Permalink
Add F-Droid updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Doomsdayrs committed Aug 29, 2022
1 parent 6afc322 commit fd2d0c2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ val remoteDataSouceModule: DI.Module = DI.Module("remote_data_source") {
bind<IRemoteAppUpdateDataSource>() with singleton {
when (flavor()) {
ProductFlavors.PLAY_STORE -> PlayAppUpdateDataSource()
ProductFlavors.F_DROID -> FDroidAppUpdateDataSource()
ProductFlavors.F_DROID -> FDroidAppUpdateDataSource(instance())
ProductFlavors.UP_TO_DOWN -> UpToDownAppUpdateDataSource()
ProductFlavors.STANDARD -> GithubAppUpdateDataSource(instance())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,89 @@
package app.shosetsu.android.datasource.remote.impl.update

import app.shosetsu.android.common.EmptyResponseBodyException
import app.shosetsu.android.common.ext.quickie
import app.shosetsu.android.datasource.remote.base.IRemoteAppUpdateDataSource
import app.shosetsu.android.domain.model.local.AppUpdateEntity
import app.shosetsu.lib.exceptions.HTTPException
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import okhttp3.OkHttpClient
import java.io.IOException
import java.io.InputStream

class FDroidAppUpdateDataSource : IRemoteAppUpdateDataSource,
/**
* Load app updates from F-Droid
*/
class FDroidAppUpdateDataSource(
private val okHttpClient: OkHttpClient
) : IRemoteAppUpdateDataSource,
IRemoteAppUpdateDataSource.Downloadable {
companion object {
private const val FDROID_UPDATE_URL =
"https://f-droid.org/api/v1/packages/app.shosetsu.android"

private const val FDROID_DOWNLOAD_URL = "https://f-droid.org/repo/app.shosetsu.android_"

private val json = Json {
encodeDefaults = true
}
}

@Serializable
data class PackagesInfo(
val packageName: String = "",
val suggestedVersionCode: Int = -1,
val packages: List<PackageData> = emptyList(),
val error: String? = null
)

@Serializable
data class PackageData(
val versionName: String,
val versionCode: Int
)

@Throws(HTTPException::class, EmptyResponseBodyException::class, IOException::class)
@OptIn(ExperimentalSerializationApi::class)
override suspend fun loadAppUpdate(): AppUpdateEntity {
TODO("Add F-DROID update source")
okHttpClient.quickie(FDROID_UPDATE_URL).use { response ->
if (response.isSuccessful) {
return response.body?.use { responseBody ->
responseBody.byteStream().use { responseStream ->
val info = json.decodeFromStream<PackagesInfo>(responseStream)
if (info.error != null)
throw EmptyResponseBodyException(info.error)

var packageData = info.packages.firstOrNull {
it.versionCode == info.suggestedVersionCode
}

if (packageData == null)
packageData = info.packages.first()

AppUpdateEntity(
packageData.versionName,
packageData.versionCode,
url = FDROID_DOWNLOAD_URL + "${packageData.versionCode}.apk",
notes = emptyList()
)
}
} ?: throw EmptyResponseBodyException(FDROID_UPDATE_URL)
}
throw HTTPException(response.code)
}
}

@Throws(EmptyResponseBodyException::class, HTTPException::class, IOException::class)
override suspend fun downloadAppUpdate(update: AppUpdateEntity): InputStream {
TODO("Add F-DROID update source")
okHttpClient.quickie(update.url).let { response ->
if (response.isSuccessful) {
return response.body?.byteStream()
?: throw EmptyResponseBodyException(update.url)
} else throw HTTPException(response.code)
}
}

}

0 comments on commit fd2d0c2

Please sign in to comment.