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
11 changes: 11 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@
<data android:scheme="https" />
</intent-filter>
</activity>

<!-- Lets the user open or share a downloaded file without exposing the whole disk. -->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>

</manifest>
18 changes: 14 additions & 4 deletions app/src/main/java/com/bansify/browser/browser/WebViewPool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ class WebViewPool(
fun onTitleReceived(tabId: String, title: String)
fun onFaviconReceived(tabId: String, icon: Bitmap?)
fun onThumbnailCaptured(tabId: String, bitmap: Bitmap)
fun onDownloadStarted(fileName: String, url: String, mimeType: String, sizeBytes: Long, localPath: String)
fun onDownloadStarted(
fileName: String,
url: String,
mimeType: String,
sizeBytes: Long,
localPath: String,
managerId: Long
)
fun onJsDialog(dialog: JsDialogState)
fun onShowCustomView(view: View, callback: WebChromeClient.CustomViewCallback)
fun onHideCustomView()
Expand Down Expand Up @@ -296,7 +303,8 @@ class WebViewPool(
url = url,
mimeType = "multipart/related",
sizeBytes = size,
localPath = actualPath
localPath = actualPath,
managerId = -1L
)
}
}
Expand Down Expand Up @@ -702,6 +710,7 @@ class WebViewPool(
val fileName = URLUtil.guessFileName(url, contentDisposition, mimeType)
val resolvedMime = mimeType.ifBlank { "application/octet-stream" }
var localPath = ""
var managerId = -1L
if (url.startsWith("http://", ignoreCase = true) ||
url.startsWith("https://", ignoreCase = true)
) {
Expand All @@ -719,7 +728,7 @@ class WebViewPool(
setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
}
val dm = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
dm.enqueue(request)
managerId = dm.enqueue(request)
localPath = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
fileName
Expand All @@ -731,7 +740,8 @@ class WebViewPool(
url = url,
mimeType = resolvedMime,
sizeBytes = contentLength.coerceAtLeast(0L),
localPath = localPath
localPath = localPath,
managerId = managerId
)
}

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/bansify/browser/data/BrowserDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ interface BrowserDao {
@Query("UPDATE downloads SET status = :status, sizeBytes = :sizeBytes, localPath = :localPath WHERE id = :id")
suspend fun updateDownloadStatus(id: Long, status: String, sizeBytes: Long, localPath: String)

@Query("SELECT * FROM downloads WHERE id = :id")
suspend fun getDownloadById(id: Long): DownloadEntity?

@Query("SELECT * FROM downloads WHERE status = 'Downloading' AND managerId >= 0")
suspend fun getActiveDownloads(): List<DownloadEntity>

@Query("UPDATE downloads SET managerId = :managerId, status = :status WHERE id = :id")
suspend fun updateDownloadManagerId(id: Long, managerId: Long, status: String)

@Query("DELETE FROM downloads WHERE id = :id")
suspend fun deleteDownloadById(id: Long)

Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/com/bansify/browser/data/BrowserDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

@Database(
entities = [
Expand All @@ -13,7 +15,7 @@ import androidx.room.RoomDatabase
ShortcutEntity::class,
SavedTabEntity::class
],
version = 1,
version = 2,
exportSchema = false
)
abstract class BrowserDatabase : RoomDatabase() {
Expand All @@ -23,13 +25,21 @@ abstract class BrowserDatabase : RoomDatabase() {
@Volatile
private var INSTANCE: BrowserDatabase? = null

/** Adds the DownloadManager id to existing download rows without dropping user data. */
private val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE downloads ADD COLUMN managerId INTEGER NOT NULL DEFAULT -1")
}
}

fun getInstance(context: Context): BrowserDatabase {
return INSTANCE ?: synchronized(this) {
INSTANCE ?: Room.databaseBuilder(
context.applicationContext,
BrowserDatabase::class.java,
"bansify_browser.db"
).fallbackToDestructiveMigration(dropAllTables = true)
).addMigrations(MIGRATION_1_2)
.fallbackToDestructiveMigration(dropAllTables = true)
.build()
.also { INSTANCE = it }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ data class DownloadEntity(
val category: String = "Other", // Pages, Images, Documents, Other
val status: String = "Completed", // Downloading, Completed, Failed
val downloadedAt: Long = System.currentTimeMillis(),
val localPath: String = ""
val localPath: String = "",
/** DownloadManager id, so progress, cancel and retry can act on the real download. */
val managerId: Long = -1L
)

@Entity(tableName = "shortcuts")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ class BrowserRepository(
dao.updateDownloadStatus(id, status, sizeBytes, localPath)
}

suspend fun getDownloadById(id: Long): DownloadEntity? = dao.getDownloadById(id)

suspend fun getActiveDownloads(): List<DownloadEntity> = dao.getActiveDownloads()

suspend fun updateDownloadManagerId(id: Long, managerId: Long, status: String) {
dao.updateDownloadManagerId(id, managerId, status)
}

suspend fun deleteDownload(id: Long) = dao.deleteDownloadById(id)

suspend fun clearAllDownloads() = dao.clearAllDownloads()
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/java/com/bansify/browser/ui/BrowserApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ fun BrowserApp(viewModel: BrowserViewModel) {
val pendingPermission by viewModel.pendingPermission.collectAsStateWithLifecycle()
val safeBrowsingWarning by viewModel.safeBrowsingWarning.collectAsStateWithLifecycle()
val sitePermissionDecisions by viewModel.sitePermissionDecisions.collectAsStateWithLifecycle()
val downloadProgress by viewModel.downloadProgress.collectAsStateWithLifecycle()
val bookmarks by viewModel.bookmarks.collectAsStateWithLifecycle()
val history by viewModel.history.collectAsStateWithLifecycle()
val downloads by viewModel.downloads.collectAsStateWithLifecycle()
Expand Down Expand Up @@ -213,9 +214,10 @@ fun BrowserApp(viewModel: BrowserViewModel) {
url: String,
mimeType: String,
sizeBytes: Long,
localPath: String
localPath: String,
managerId: Long
) {
viewModel.onDownloadRecorded(fileName, url, mimeType, sizeBytes, localPath)
viewModel.onDownloadRecorded(fileName, url, mimeType, sizeBytes, localPath, managerId)
}

override fun onJsDialog(dialog: JsDialogState) {
Expand Down Expand Up @@ -741,7 +743,11 @@ fun BrowserApp(viewModel: BrowserViewModel) {
downloads = downloads,
onClose = { viewModel.closeOverlay() },
onOpenDownloadUrl = { url -> viewModel.openUrlInActiveTab(url) },
onShareDownload = { dl -> shareText(dl.fileName, dl.url) },
onShareDownload = { dl -> viewModel.shareDownloadedFile(dl.id) },
downloadProgress = downloadProgress,
onOpenFile = { id -> viewModel.openDownloadedFile(id) },
onCancelDownload = { id -> viewModel.cancelDownload(id) },
onRetryDownload = { id -> viewModel.retryDownload(id) },
onDeleteDownload = { id -> viewModel.deleteDownload(id) },
onOpenDownloadsSettings = {
viewModel.openOverlay(BrowserOverlay.Settings(SettingsSubScreen.DOWNLOADS_SETTINGS))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.compose.material3.FilterChip
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
Expand Down Expand Up @@ -65,7 +66,11 @@ fun DownloadsScreen(
onOpenDownloadUrl: (String) -> Unit,
onShareDownload: (DownloadEntity) -> Unit,
onDeleteDownload: (Long) -> Unit,
onOpenDownloadsSettings: () -> Unit
onOpenDownloadsSettings: () -> Unit,
downloadProgress: Map<Long, Float> = emptyMap(),
onOpenFile: (Long) -> Unit = {},
onCancelDownload: (Long) -> Unit = {},
onRetryDownload: (Long) -> Unit = {}
) {
BackHandler { onClose() }

Expand Down Expand Up @@ -190,7 +195,13 @@ fun DownloadsScreen(
Row(
modifier = Modifier
.fillMaxWidth()
.clickable { onOpenDownloadUrl(item.url) }
.clickable {
if (item.status == "Completed" && item.localPath.isNotBlank()) {
onOpenFile(item.id)
} else {
onOpenDownloadUrl(item.url)
}
}
.padding(start = 16.dp, end = 6.dp, top = 12.dp, bottom = 12.dp),
verticalAlignment = Alignment.CenterVertically
) {
Expand Down Expand Up @@ -218,12 +229,38 @@ fun DownloadsScreen(
overflow = TextOverflow.Ellipsis
)
Text(
text = "${UrlUtils.formatFileSize(item.sizeBytes)} • ${UrlUtils.extractShortDomain(item.url)} • ${UrlUtils.formatShortDate(item.downloadedAt)}",
text = buildString {
append(UrlUtils.formatFileSize(item.sizeBytes))
append(" • ")
append(UrlUtils.extractShortDomain(item.url))
append(" • ")
append(
when (item.status) {
"Downloading" -> {
val pct = ((downloadProgress[item.id] ?: 0f) * 100).toInt()
"Downloading $pct%"
}
"Failed" -> "Failed"
else -> UrlUtils.formatShortDate(item.downloadedAt)
}
)
},
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
color = if (item.status == "Failed") {
MaterialTheme.colorScheme.error
} else {
MaterialTheme.colorScheme.onSurfaceVariant
},
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
if (item.status == "Downloading") {
Spacer(modifier = Modifier.height(6.dp))
LinearProgressIndicator(
progress = { downloadProgress[item.id] ?: 0f },
modifier = Modifier.fillMaxWidth()
)
}
}
Box {
IconButton(onClick = { itemMenu = true }) {
Expand All @@ -237,6 +274,33 @@ fun DownloadsScreen(
expanded = itemMenu,
onDismissRequest = { itemMenu = false }
) {
if (item.status == "Completed" && item.localPath.isNotBlank()) {
DropdownMenuItem(
text = { Text("Open file") },
onClick = {
itemMenu = false
onOpenFile(item.id)
}
)
}
if (item.status == "Downloading") {
DropdownMenuItem(
text = { Text("Cancel download", color = MaterialTheme.colorScheme.error) },
onClick = {
itemMenu = false
onCancelDownload(item.id)
}
)
}
if (item.status == "Failed") {
DropdownMenuItem(
text = { Text("Retry") },
onClick = {
itemMenu = false
onRetryDownload(item.id)
}
)
}
DropdownMenuItem(
text = { Text("Open source page") },
onClick = {
Expand Down
Loading
Loading