Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<!--
Needed only for the sideloaded build: it installs its own verified update. An app that
came from Google Play never uses this path - it opens the Play listing instead, because
Play policy forbids a Play-distributed app from updating itself any other way.
-->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

<!-- Android 11+ package visibility: resolve web and app links we hand off. -->
<queries>
<intent>
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/com/bansify/browser/ui/BrowserApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ fun BrowserApp(viewModel: BrowserViewModel) {
val sitePermissionDecisions by viewModel.sitePermissionDecisions.collectAsStateWithLifecycle()
val downloadProgress by viewModel.downloadProgress.collectAsStateWithLifecycle()
val adBlockAllowlist by viewModel.adBlockAllowlist.collectAsStateWithLifecycle()
val updateState by viewModel.updateState.collectAsStateWithLifecycle()
val bookmarks by viewModel.bookmarks.collectAsStateWithLifecycle()
val history by viewModel.history.collectAsStateWithLifecycle()
val downloads by viewModel.downloads.collectAsStateWithLifecycle()
Expand Down Expand Up @@ -845,7 +846,18 @@ fun BrowserApp(viewModel: BrowserViewModel) {
onForgetAllSitePermissions = { viewModel.forgetAllSitePermissions() },
adBlockAllowlist = adBlockAllowlist,
onDisallowAds = { host -> viewModel.disallowAds(host) },
onClearAdsAllowlist = { viewModel.clearAdBlockAllowlist() }
onClearAdsAllowlist = { viewModel.clearAdBlockAllowlist() },
updateState = updateState,
appVersionName = com.bansify.browser.BuildConfig.VERSION_NAME,
onCheckForUpdates = { viewModel.checkForUpdates() },
onDownloadUpdate = { viewModel.downloadUpdate() },
onInstallUpdate = {
if (!viewModel.installPendingUpdate()) {
viewModel.openInstallPermissionSettings()
}
},
onOpenPlayListing = { viewModel.openPlayListing() },
onOpenInstallPermissionSettings = { viewModel.openInstallPermissionSettings() }
)
}
}
Expand Down
18 changes: 16 additions & 2 deletions app/src/main/java/com/bansify/browser/ui/screens/SettingsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ fun SettingsScreen(
onForgetAllSitePermissions: () -> Unit = {},
adBlockAllowlist: Set<String> = emptySet(),
onDisallowAds: (String) -> Unit = {},
onClearAdsAllowlist: () -> Unit = {}
onClearAdsAllowlist: () -> Unit = {},
updateState: com.bansify.browser.updater.UpdateState = com.bansify.browser.updater.UpdateState.Idle,
appVersionName: String = "",
onCheckForUpdates: () -> Unit = {},
onDownloadUpdate: () -> Unit = {},
onInstallUpdate: () -> Unit = {},
onOpenPlayListing: () -> Unit = {},
onOpenInstallPermissionSettings: () -> Unit = {}
) {
BackHandler { onBack() }

Expand Down Expand Up @@ -150,7 +157,14 @@ fun SettingsScreen(
onForgetAllSitePermissions = onForgetAllSitePermissions,
adBlockAllowlist = adBlockAllowlist,
onDisallowAds = onDisallowAds,
onClearAdsAllowlist = onClearAdsAllowlist
onClearAdsAllowlist = onClearAdsAllowlist,
updateState = updateState,
appVersionName = appVersionName,
onCheckForUpdates = onCheckForUpdates,
onDownloadUpdate = onDownloadUpdate,
onInstallUpdate = onInstallUpdate,
onOpenPlayListing = onOpenPlayListing,
onOpenInstallPermissionSettings = onOpenInstallPermissionSettings
)
return
}
Expand Down
119 changes: 117 additions & 2 deletions app/src/main/java/com/bansify/browser/ui/screens/SettingsSubScreens.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import com.bansify.browser.data.SafeBrowsingLevel
import com.bansify.browser.data.SearchEngine
import com.bansify.browser.data.SettingsSubScreen
import com.bansify.browser.data.SitePermissionDecision
import com.bansify.browser.updater.UpdateState
import com.bansify.browser.data.ThemeMode
import com.bansify.browser.data.ThirdPartyCookiePolicy
import com.bansify.browser.data.ToolbarShortcut
Expand All @@ -86,7 +87,14 @@ fun SettingsSubScreenContent(
onForgetAllSitePermissions: () -> Unit = {},
adBlockAllowlist: Set<String> = emptySet(),
onDisallowAds: (String) -> Unit = {},
onClearAdsAllowlist: () -> Unit = {}
onClearAdsAllowlist: () -> Unit = {},
updateState: UpdateState = UpdateState.Idle,
appVersionName: String = "",
onCheckForUpdates: () -> Unit = {},
onDownloadUpdate: () -> Unit = {},
onInstallUpdate: () -> Unit = {},
onOpenPlayListing: () -> Unit = {},
onOpenInstallPermissionSettings: () -> Unit = {}
) {
val canvasBg = expressiveCanvasColor()
val context = LocalContext.current
Expand Down Expand Up @@ -933,6 +941,20 @@ fun SettingsSubScreenContent(
}

SettingsSubScreen.ABOUT -> {
item {
UpdateCard(
state = updateState,
currentVersion = appVersionName.ifBlank { "unknown" },
onCheck = onCheckForUpdates,
onDownload = onDownloadUpdate,
onInstall = {
onInstallUpdate()
onOpenInstallPermissionSettings()
},
onOpenPlay = onOpenPlayListing
)
}

item {
val webViewPkg = remember {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Expand All @@ -942,7 +964,7 @@ fun SettingsSubScreenContent(
}
}
val aboutItems = listOf(
"Application version" to "Bansify Browser 1.0.0",
"Application version" to "Bansify Browser ${appVersionName.ifBlank { "unknown" }}",
"Package name" to "com.bansify.browser",
"WebView engine" to (webViewPkg?.let { "${it.packageName} (${it.versionName})" }
?: "Android System WebView"),
Expand Down Expand Up @@ -1106,3 +1128,96 @@ private fun ExpressiveSwitchCard(
}
}
}

/**
* "Check for updates" row.
*
* The button label and the action depend on where this copy came from: a Google Play install
* may only update through Play (policy), while a sideloaded build installs the verified APK
* itself.
*/
@Composable
private fun UpdateCard(
state: UpdateState,
currentVersion: String,
onCheck: () -> Unit,
onDownload: () -> Unit,
onInstall: () -> Unit,
onOpenPlay: () -> Unit
) {
val title: String
val body: String
when (state) {
is UpdateState.Idle -> {
title = "Bansify Browser $currentVersion"
body = "Check whether a newer build is available."
}
is UpdateState.Checking -> {
title = "Checking for updates..."
body = "Contacting the release server."
}
is UpdateState.UpToDate -> {
title = "You are up to date"
body = "Bansify Browser ${state.currentVersion} is the latest build."
}
is UpdateState.Available -> {
title = "Update available: v${state.release.versionName}"
body = if (state.fromPlay) {
"This copy came from Google Play, so the update installs through Play."
} else {
"Your tabs, bookmarks and settings are kept. The file is verified before it installs."
}
}
is UpdateState.Downloading -> {
title = "Downloading ${(state.progress * 100).toInt()}%"
body = "The file is checked against the app signature before anything is installed."
}
is UpdateState.ReadyToInstall -> {
title = "Ready to install"
body = "Tap install and confirm the system prompt."
}
is UpdateState.Failed -> {
title = "Could not check for updates"
body = state.message
}
}

ExpressiveGroupedCard(index = 0, totalCount = 1) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(20.dp),
verticalArrangement = Arrangement.spacedBy(10.dp)
) {
Text(
text = title,
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.SemiBold,
color = MaterialTheme.colorScheme.onSurface
)
Text(
text = body,
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
when (state) {
is UpdateState.Available -> Button(
onClick = if (state.fromPlay) onOpenPlay else onDownload,
shape = RoundedCornerShape(24.dp)
) {
Text(if (state.fromPlay) "Update on Play" else "Download update")
}
is UpdateState.ReadyToInstall -> Button(
onClick = onInstall,
shape = RoundedCornerShape(24.dp)
) {
Text("Install update")
}
is UpdateState.Checking, is UpdateState.Downloading -> Unit
else -> Button(onClick = onCheck, shape = RoundedCornerShape(24.dp)) {
Text("Check for updates")
}
}
}
}
}
88 changes: 88 additions & 0 deletions app/src/main/java/com/bansify/browser/updater/UpdateChecker.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.bansify.browser.updater

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL

/**
* Reads the latest published build from the GitHub releases feed.
*
* Nothing is downloaded here — the caller gets a [RemoteRelease] and decides what to do with
* it. No account, no key, no tracking.
*/
object UpdateChecker {

private const val LATEST_URL =
"https://api.github.com/repos/Bansify/browser/releases/latest"

/** Returns null when the installed build is already current. */
suspend fun latest(currentVersionName: String): Result<RemoteRelease?> =
withContext(Dispatchers.IO) {
runCatching {
val connection = (URL(LATEST_URL).openConnection() as HttpURLConnection).apply {
connectTimeout = 15_000
readTimeout = 15_000
setRequestProperty("Accept", "application/vnd.github+json")
setRequestProperty("User-Agent", "BansifyBrowser")
}
val body = try {
connection.inputStream.bufferedReader().use { it.readText() }
} finally {
connection.disconnect()
}

val json = JSONObject(body)
val tag = json.optString("tag_name").removePrefix("v").removePrefix("V")
if (tag.isBlank()) return@runCatching null

val assets = json.optJSONArray("assets") ?: return@runCatching null
var apkUrl: String? = null
var checksumUrl: String? = null
var apkSize = 0L
for (i in 0 until assets.length()) {
val asset = assets.getJSONObject(i)
val name = asset.optString("name")
when {
name.endsWith("-Release.apk") -> {
apkUrl = asset.optString("browser_download_url")
apkSize = asset.optLong("size")
}
name.endsWith(".apk.sha256") -> {
checksumUrl = asset.optString("browser_download_url")
}
}
}
val downloadUrl = apkUrl ?: return@runCatching null

if (compareVersions(tag, currentVersionName) <= 0) {
null
} else {
RemoteRelease(
versionName = tag,
apkUrl = downloadUrl,
sha256Url = checksumUrl,
sizeBytes = apkSize,
releasePageUrl = json.optString("html_url")
)
}
}
}

/**
* Compares dotted versions numerically: "1.0.152" vs "1.0.99" -> 1 (a is newer).
* Anything that is not a number is treated as 0, so "1.0.152-beta" still compares sanely.
*/
fun compareVersions(a: String, b: String): Int {
val left = a.split('.', '-', '+')
val right = b.split('.', '-', '+')
val size = maxOf(left.size, right.size)
for (i in 0 until size) {
val l = left.getOrNull(i)?.takeWhile { it.isDigit() }?.toIntOrNull() ?: 0
val r = right.getOrNull(i)?.takeWhile { it.isDigit() }?.toIntOrNull() ?: 0
if (l != r) return if (l > r) 1 else -1
}
return 0
}
}
Loading
Loading