-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
1,216 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...common/serialization/ByteStringAdapter.kt → ...erialization/adapter/ByteStringAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app-common/src/main/java/eu/darken/octi/common/serialization/adapter/DurationAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package eu.darken.octi.common.serialization.adapter | ||
|
||
import com.squareup.moshi.FromJson | ||
import com.squareup.moshi.ToJson | ||
import java.time.Duration | ||
|
||
class DurationAdapter { | ||
@ToJson | ||
fun toJson(value: Duration): String = value.toString() | ||
|
||
@FromJson | ||
fun fromJson(raw: String): Duration = Duration.parse(raw) | ||
} |
2 changes: 1 addition & 1 deletion
2
...ti/common/serialization/InstantAdapter.kt → ...n/serialization/adapter/InstantAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app-common/src/main/java/eu/darken/octi/common/serialization/adapter/LocaleAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package eu.darken.octi.common.serialization.adapter | ||
|
||
import com.squareup.moshi.FromJson | ||
import com.squareup.moshi.ToJson | ||
import java.util.Locale | ||
|
||
class LocaleAdapter { | ||
@ToJson | ||
fun toJson(value: Locale): String = value.toLanguageTag() | ||
|
||
@FromJson | ||
fun fromJson(raw: String) = Locale.forLanguageTag(raw) | ||
} |
14 changes: 14 additions & 0 deletions
14
...common/src/main/java/eu/darken/octi/common/serialization/adapter/OffsetDateTimeAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package eu.darken.octi.common.serialization.adapter | ||
|
||
import com.squareup.moshi.FromJson | ||
import com.squareup.moshi.ToJson | ||
import java.time.OffsetDateTime | ||
import java.time.format.DateTimeFormatter | ||
|
||
class OffsetDateTimeAdapter { | ||
@ToJson | ||
fun toJson(value: OffsetDateTime): String = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(value) | ||
|
||
@FromJson | ||
fun fromJson(value: String): OffsetDateTime = OffsetDateTime.parse(value, DateTimeFormatter.ISO_OFFSET_DATE_TIME) | ||
} |
59 changes: 59 additions & 0 deletions
59
app-common/src/main/java/eu/darken/octi/common/serialization/adapter/RegexAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package eu.darken.octi.common.serialization.adapter | ||
|
||
import com.squareup.moshi.FromJson | ||
import com.squareup.moshi.Json | ||
import com.squareup.moshi.JsonClass | ||
import com.squareup.moshi.ToJson | ||
|
||
class RegexAdapter { | ||
|
||
@ToJson | ||
fun toJson(value: Regex): Wrapper = Wrapper( | ||
pattern = value.pattern, | ||
options = value.options.map { it.toWrapperOption() }.toSet(), | ||
) | ||
|
||
@FromJson | ||
fun fromJson(raw: Wrapper): Regex = Regex( | ||
pattern = raw.pattern, | ||
options = raw.options.map { it.toRegexOption() }.toSet() | ||
) | ||
|
||
private fun Wrapper.Option.toRegexOption() = when (this) { | ||
Wrapper.Option.IGNORE_CASE -> RegexOption.IGNORE_CASE | ||
Wrapper.Option.MULTILINE -> RegexOption.MULTILINE | ||
Wrapper.Option.LITERAL -> RegexOption.LITERAL | ||
Wrapper.Option.UNIX_LINES -> RegexOption.UNIX_LINES | ||
Wrapper.Option.COMMENTS -> RegexOption.COMMENTS | ||
Wrapper.Option.DOT_MATCHES_ALL -> RegexOption.DOT_MATCHES_ALL | ||
Wrapper.Option.CANON_EQ -> RegexOption.CANON_EQ | ||
} | ||
|
||
private fun RegexOption.toWrapperOption() = when (this) { | ||
RegexOption.IGNORE_CASE -> Wrapper.Option.IGNORE_CASE | ||
RegexOption.MULTILINE -> Wrapper.Option.MULTILINE | ||
RegexOption.LITERAL -> Wrapper.Option.LITERAL | ||
RegexOption.UNIX_LINES -> Wrapper.Option.UNIX_LINES | ||
RegexOption.COMMENTS -> Wrapper.Option.COMMENTS | ||
RegexOption.DOT_MATCHES_ALL -> Wrapper.Option.DOT_MATCHES_ALL | ||
RegexOption.CANON_EQ -> Wrapper.Option.CANON_EQ | ||
} | ||
|
||
@JsonClass(generateAdapter = true) | ||
data class Wrapper( | ||
@Json(name = "pattern") val pattern: String, | ||
@Json(name = "options") val options: Set<Option> | ||
) { | ||
@JsonClass(generateAdapter = false) | ||
enum class Option { | ||
@Json(name = "IGNORE_CASE") IGNORE_CASE, | ||
@Json(name = "MULTILINE") MULTILINE, | ||
@Json(name = "LITERAL") LITERAL, | ||
@Json(name = "UNIX_LINES") UNIX_LINES, | ||
@Json(name = "COMMENTS") COMMENTS, | ||
@Json(name = "DOT_MATCHES_ALL") DOT_MATCHES_ALL, | ||
@Json(name = "CANON_EQ") CANON_EQ, | ||
; | ||
} | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
.../octi/common/serialization/UUIDAdapter.kt → ...mmon/serialization/adapter/UUIDAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app-common/src/main/java/eu/darken/octi/common/serialization/adapter/UriAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package eu.darken.octi.common.serialization.adapter | ||
|
||
import android.net.Uri | ||
import com.squareup.moshi.FromJson | ||
import com.squareup.moshi.ToJson | ||
|
||
class UriAdapter { | ||
@ToJson | ||
fun toJson(uri: Uri): String = uri.toString() | ||
|
||
@FromJson | ||
fun fromJson(uriString: String): Uri = Uri.parse(uriString) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
app/src/foss/java/eu/darken/octi/main/core/FossUpdateChecker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package eu.darken.octi.main.core | ||
|
||
import dagger.Reusable | ||
import eu.darken.octi.common.WebpageTool | ||
import eu.darken.octi.common.datastore.value | ||
import eu.darken.octi.common.debug.logging.Logging.Priority.ERROR | ||
import eu.darken.octi.common.debug.logging.Logging.Priority.INFO | ||
import eu.darken.octi.common.debug.logging.Logging.Priority.WARN | ||
import eu.darken.octi.common.debug.logging.asLog | ||
import eu.darken.octi.common.debug.logging.log | ||
import eu.darken.octi.common.debug.logging.logTag | ||
import eu.darken.octi.main.core.updater.UpdateChecker | ||
import java.time.Duration | ||
import java.time.Instant | ||
import javax.inject.Inject | ||
|
||
@Reusable | ||
class FossUpdateChecker @Inject constructor( | ||
private val checker: GithubReleaseCheck, | ||
private val webpageTool: WebpageTool, | ||
private val settings: FossUpdateSettings, | ||
) : UpdateChecker { | ||
|
||
override suspend fun getLatest(channel: UpdateChecker.Channel): UpdateChecker.Update? { | ||
log(TAG) { "getLatest($channel) checking..." } | ||
|
||
val release: GithubApi.ReleaseInfo? = try { | ||
if (Duration.between(settings.lastReleaseCheck.value(), Instant.now()) < UPDATE_CHECK_INTERVAL) { | ||
log(TAG) { "Using cached release data" } | ||
when (channel) { | ||
UpdateChecker.Channel.BETA -> settings.lastReleaseBeta.value() | ||
UpdateChecker.Channel.PROD -> settings.lastReleaseProd.value() | ||
} | ||
} else { | ||
log(TAG) { "Fetching new release data" } | ||
when (channel) { | ||
UpdateChecker.Channel.BETA -> checker.allReleases(OWNER, REPO).first() | ||
UpdateChecker.Channel.PROD -> checker.latestRelease(OWNER, REPO) | ||
}.also { | ||
log(TAG, INFO) { "getLatest($channel) new data is $it" } | ||
settings.lastReleaseCheck.value(Instant.now()) | ||
when (channel) { | ||
UpdateChecker.Channel.BETA -> settings.lastReleaseBeta.value(it) | ||
UpdateChecker.Channel.PROD -> settings.lastReleaseProd.value(it) | ||
} | ||
} | ||
} | ||
} catch (e: Exception) { | ||
log(TAG, ERROR) { "getLatest($channel) failed: ${e.asLog()}" } | ||
null | ||
} | ||
|
||
log(TAG, INFO) { "getLatest($channel) is ${release?.tagName}" } | ||
|
||
val update = release?.let { rel -> | ||
Update( | ||
channel = channel, | ||
versionName = rel.tagName, | ||
changelogLink = rel.htmlUrl, | ||
downloadLink = rel.assets.singleOrNull { it.name.endsWith(".apk") }?.downloadUrl, | ||
) | ||
} | ||
|
||
return update | ||
} | ||
|
||
override suspend fun startUpdate(update: UpdateChecker.Update) { | ||
log(TAG, INFO) { "startUpdate($update)" } | ||
update as Update | ||
if (update.downloadLink != null) { | ||
webpageTool.open(update.downloadLink) | ||
} else { | ||
log(TAG, WARN) { "No download link available for $update" } | ||
} | ||
} | ||
|
||
override suspend fun viewUpdate(update: UpdateChecker.Update) { | ||
log(TAG, INFO) { "viewUpdate($update)" } | ||
update as Update | ||
webpageTool.open(update.changelogLink) | ||
} | ||
|
||
override suspend fun dismissUpdate(update: UpdateChecker.Update) { | ||
log(TAG, INFO) { "dismissUpdate($update)" } | ||
update as Update | ||
settings.dismiss(update) | ||
} | ||
|
||
override suspend fun isDismissed(update: UpdateChecker.Update): Boolean { | ||
update as Update | ||
return settings.isDismissed(update) | ||
} | ||
|
||
override fun isEnabledByDefault(): Boolean { | ||
val isEnabled = false | ||
log(TAG, INFO) { "Update check default isEnabled=$isEnabled" } | ||
return isEnabled | ||
} | ||
|
||
override suspend fun isCheckSupported(): Boolean { | ||
return true | ||
} | ||
|
||
data class Update( | ||
override val channel: UpdateChecker.Channel, | ||
override val versionName: String, | ||
val changelogLink: String, | ||
val downloadLink: String?, | ||
) : UpdateChecker.Update | ||
|
||
companion object { | ||
private val UPDATE_CHECK_INTERVAL = Duration.ofHours(6) | ||
private const val OWNER = "d4rken-org" | ||
private const val REPO = "octi" | ||
private val TAG = logTag("Updater", "Checker", "FOSS") | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
app/src/foss/java/eu/darken/octi/main/core/FossUpdateSettings.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package eu.darken.octi.main.core | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import com.squareup.moshi.Moshi | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import eu.darken.octi.common.datastore.DataStoreValue | ||
import eu.darken.octi.common.datastore.createValue | ||
import eu.darken.octi.common.datastore.value | ||
import eu.darken.octi.common.debug.logging.logTag | ||
import java.time.Instant | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class FossUpdateSettings @Inject constructor( | ||
@ApplicationContext private val context: Context, | ||
moshi: Moshi, | ||
) { | ||
|
||
private val Context.dataStore by preferencesDataStore(name = "settings_updater_foss") | ||
|
||
private val dataStore: DataStore<Preferences> | ||
get() = context.dataStore | ||
|
||
private fun FossUpdateChecker.Update.getSetting(): DataStoreValue<Boolean> { | ||
return dataStore.createValue("update.${this.versionName}.dismissed", false) | ||
} | ||
|
||
suspend fun dismiss(update: FossUpdateChecker.Update) { | ||
update.getSetting().value(true) | ||
} | ||
|
||
suspend fun isDismissed(update: FossUpdateChecker.Update): Boolean { | ||
return update.getSetting().value() | ||
} | ||
|
||
val lastReleaseCheck = dataStore.createValue("check.last", Instant.EPOCH, moshi) | ||
val lastReleaseProd = dataStore.createValue<GithubApi.ReleaseInfo?>("check.last.prod", null, moshi) | ||
val lastReleaseBeta = dataStore.createValue<GithubApi.ReleaseInfo?>("check.last.beta", null, moshi) | ||
|
||
companion object { | ||
private val TAG = logTag("Updater", "Checker", "FOSS", "Settings") | ||
} | ||
} |
Oops, something went wrong.