Skip to content

Commit

Permalink
Rename and move file/http related classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jayohms committed Jun 13, 2024
1 parent 10ec3fb commit 56ff42f
Show file tree
Hide file tree
Showing 27 changed files with 116 additions and 109 deletions.
4 changes: 2 additions & 2 deletions core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<application>

<provider
android:name=".turbo.util.TurboFileProvider"
android:authorities="${applicationId}.turbo.fileprovider"
android:name=".files.util.HotwireFileProvider"
android:authorities="${applicationId}.hotwire.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/kotlin/dev/hotwire/core/config/HotwireConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import dev.hotwire.core.bridge.BridgeComponent
import dev.hotwire.core.bridge.BridgeComponentFactory
import dev.hotwire.core.bridge.StradaJsonConverter
import dev.hotwire.core.turbo.config.PathConfiguration
import dev.hotwire.core.turbo.http.TurboHttpClient
import dev.hotwire.core.turbo.http.TurboOfflineRequestHandler
import dev.hotwire.core.turbo.http.HotwireHttpClient
import dev.hotwire.core.turbo.offline.OfflineRequestHandler
import dev.hotwire.core.turbo.views.TurboWebView

class HotwireConfig internal constructor() {
Expand All @@ -29,7 +29,7 @@ class HotwireConfig internal constructor() {
/**
* Experimental: API may be removed, not ready for production use.
*/
var offlineRequestHandler: TurboOfflineRequestHandler? = null
var offlineRequestHandler: OfflineRequestHandler? = null

/**
* Enables/disables debug logging. This should be disabled in production environments.
Expand All @@ -40,7 +40,7 @@ class HotwireConfig internal constructor() {
var debugLoggingEnabled = false
set(value) {
field = value
TurboHttpClient.reset()
HotwireHttpClient.reset()
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package dev.hotwire.core.turbo.delegates
package dev.hotwire.core.files.delegates

import android.content.ClipData
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.webkit.WebChromeClient.FileChooserParams
import dev.hotwire.core.turbo.util.TurboFileProvider
import dev.hotwire.core.files.util.HotwireFileProvider
import dev.hotwire.core.turbo.util.dispatcherProvider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext

internal class TurboBrowseFilesDelegate(val context: Context) : CoroutineScope {
internal class BrowseFilesDelegate(val context: Context) : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = dispatcherProvider.io + Job()

Expand Down Expand Up @@ -74,8 +74,8 @@ internal class TurboBrowseFilesDelegate(val context: Context) : CoroutineScope {
}

private suspend fun writeToCachedFile(uri: Uri): Uri? {
return TurboFileProvider.writeUriToFile(context, uri)?.let {
TurboFileProvider.contentUriForFile(context, it)
return HotwireFileProvider.writeUriToFile(context, uri)?.let {
HotwireFileProvider.contentUriForFile(context, it)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package dev.hotwire.core.turbo.delegates
package dev.hotwire.core.files.delegates

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.MediaStore
import android.webkit.WebChromeClient.FileChooserParams
import dev.hotwire.core.files.util.HotwireFileProvider
import dev.hotwire.core.logging.logError
import dev.hotwire.core.turbo.util.TurboFileProvider
import java.io.File
import java.io.IOException

internal class TurboCameraCaptureDelegate(val context: Context) {
internal class CameraCaptureDelegate(val context: Context) {
private var cameraImagePath: String? = null

fun buildIntent(params: FileChooserParams): Intent? {
if (!params.allowsCameraCapture()) return null

val file = createEmptyImageFile() ?: return null
val uri = TurboFileProvider.contentUriForFile(context, file)
val uri = HotwireFileProvider.contentUriForFile(context, file)

cameraImagePath = file.absolutePath

Expand All @@ -35,7 +35,7 @@ internal class TurboCameraCaptureDelegate(val context: Context) {

private fun buildCameraImageResult(): Array<Uri>? {
val file = cameraImagePath?.let { File(it) } ?: return null
val uri = TurboFileProvider.contentUriForFile(context, file)
val uri = HotwireFileProvider.contentUriForFile(context, file)

return when (file.length()) {
0L -> null
Expand All @@ -45,7 +45,7 @@ internal class TurboCameraCaptureDelegate(val context: Context) {

private fun createEmptyImageFile(): File? {
return try {
val directory: File = TurboFileProvider.directory(context)
val directory: File = HotwireFileProvider.directory(context)
return File.createTempFile("Capture_", ".jpg", directory)
} catch (e: IOException) {
logError("createTempFileError", e)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.hotwire.core.turbo.delegates
package dev.hotwire.core.files.delegates

import android.app.Activity
import android.content.Context
Expand All @@ -10,19 +10,19 @@ import androidx.activity.result.ActivityResult
import dev.hotwire.core.R
import dev.hotwire.core.logging.logError
import dev.hotwire.core.turbo.session.Session
import dev.hotwire.core.turbo.util.TURBO_REQUEST_CODE_FILES
import dev.hotwire.core.turbo.util.TurboFileProvider
import dev.hotwire.core.files.util.HOTWIRE_REQUEST_CODE_FILES
import dev.hotwire.core.files.util.HotwireFileProvider
import dev.hotwire.core.turbo.util.dispatcherProvider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext

class TurboFileChooserDelegate(val session: Session) : CoroutineScope {
class FileChooserDelegate(val session: Session) : CoroutineScope {
private val context: Context = session.context
private var uploadCallback: ValueCallback<Array<Uri>>? = null
private val browseFilesDelegate = TurboBrowseFilesDelegate(context)
private val cameraCaptureDelegate = TurboCameraCaptureDelegate(context)
private val browseFilesDelegate = BrowseFilesDelegate(context)
private val cameraCaptureDelegate = CameraCaptureDelegate(context)

override val coroutineContext: CoroutineContext
get() = dispatcherProvider.io + Job()
Expand All @@ -47,7 +47,7 @@ class TurboFileChooserDelegate(val session: Session) : CoroutineScope {

fun deleteCachedFiles() {
launch {
TurboFileProvider.deleteAllFiles(context)
HotwireFileProvider.deleteAllFiles(context)
}
}

Expand All @@ -69,7 +69,7 @@ class TurboFileChooserDelegate(val session: Session) : CoroutineScope {
val destination = session.currentVisit?.callback?.visitDestination() ?: return false

return try {
destination.activityResultLauncher(TURBO_REQUEST_CODE_FILES)?.launch(intent)
destination.activityResultLauncher(HOTWIRE_REQUEST_CODE_FILES)?.launch(intent)
true
} catch (e: Exception) {
logError("startIntentError", e)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.hotwire.core.files.util

const val HOTWIRE_REQUEST_CODE_FILES = 37
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package dev.hotwire.core.turbo.util
package dev.hotwire.core.files.util

import android.content.Context
import android.net.Uri
import androidx.core.content.FileProvider
import dev.hotwire.core.turbo.util.deleteAllFilesInDirectory
import dev.hotwire.core.turbo.util.dispatcherProvider
import kotlinx.coroutines.withContext
import java.io.File
import java.io.IOException

class TurboFileProvider : FileProvider() {
class HotwireFileProvider : FileProvider() {
companion object {
private const val sharedDir = "shared"

fun authority(context: Context): String {
return "${context.packageName}.turbo.fileprovider"
return "${context.packageName}.hotwire.fileprovider"
}

fun directory(context: Context, dirName: String = sharedDir): File {
Expand All @@ -30,13 +32,13 @@ class TurboFileProvider : FileProvider() {
}

@Suppress("unused")
fun uriAttributes(context: Context, uri: Uri): TurboUriAttributes? {
val uriHelper = TurboUriHelper(context)
fun uriAttributes(context: Context, uri: Uri): UriAttributes? {
val uriHelper = UriHelper(context)
return uriHelper.getAttributes(uri)
}

suspend fun writeUriToFile(context: Context, uri: Uri, dirName: String = sharedDir): File? {
val uriHelper = TurboUriHelper(context)
val uriHelper = UriHelper(context)
return uriHelper.writeFileTo(uri, directory(context, dirName))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.hotwire.core.turbo.util
package dev.hotwire.core.files.util

data class TurboUriAttributes(
data class UriAttributes(
val fileName: String,
val mimeType: String,
val fileSize: Long
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package dev.hotwire.core.turbo.util
package dev.hotwire.core.files.util

import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.provider.OpenableColumns
import android.webkit.MimeTypeMap
import dev.hotwire.core.logging.logError
import dev.hotwire.core.turbo.util.dispatcherProvider
import dev.hotwire.core.turbo.util.extract
import kotlinx.coroutines.withContext
import java.io.File
import java.io.IOException

internal class TurboUriHelper(val context: Context) {
internal class UriHelper(val context: Context) {
@Suppress("BlockingMethodInNonBlockingContext") // https://youtrack.jetbrains.com/issue/KT-39684
suspend fun writeFileTo(uri: Uri, destDirectory: File): File? {
val uriAttributes = getAttributes(uri) ?: return null
Expand Down Expand Up @@ -39,29 +41,29 @@ internal class TurboUriHelper(val context: Context) {
}
}

fun getAttributes(uri: Uri): TurboUriAttributes? {
fun getAttributes(uri: Uri): UriAttributes? {
return when (uri.scheme) {
"file" -> getFileUriAttributes(uri)
"content" -> getContentUriAttributes(context, uri)
else -> null
}
}

private fun getFileUriAttributes(uri: Uri): TurboUriAttributes? {
private fun getFileUriAttributes(uri: Uri): UriAttributes? {
val file = uri.getFile() ?: return null

if (file.originIsAppResource()) {
return null
}

return TurboUriAttributes(
return UriAttributes(
fileName = file.name,
mimeType = uri.mimeType(),
fileSize = file.length()
)
}

private fun getContentUriAttributes(context: Context, uri: Uri): TurboUriAttributes? {
private fun getContentUriAttributes(context: Context, uri: Uri): UriAttributes? {
val projection = arrayOf(OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE)
val mimeType: String? = context.contentResolver.getType(uri)
val cursor = try {
Expand All @@ -80,7 +82,7 @@ internal class TurboUriHelper(val context: Context) {
return cursorAttributes ?: uriAttributesDerivedFromUri(uri, mimeType)
}

private fun uriAttributesFromContentQuery(uri: Uri, mimeType: String?, cursor: Cursor): TurboUriAttributes? {
private fun uriAttributesFromContentQuery(uri: Uri, mimeType: String?, cursor: Cursor): UriAttributes? {
val columnName = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME).takeIf { it >= 0 }
val columnSize = cursor.getColumnIndex(OpenableColumns.SIZE).takeIf { it >= 0 }
val fileName: String? = columnName?.let { cursor.getString(it) }
Expand All @@ -90,21 +92,21 @@ internal class TurboUriHelper(val context: Context) {
return null
}

return TurboUriAttributes(
return UriAttributes(
fileName = fileName ?: "attachment",
mimeType = mimeType ?: uri.mimeType(),
fileSize = fileSize ?: 0L
)
}

private fun uriAttributesDerivedFromUri(uri: Uri, mimeType: String?): TurboUriAttributes? {
private fun uriAttributesDerivedFromUri(uri: Uri, mimeType: String?): UriAttributes? {
val fileName: String? = uri.lastPathSegment

if (fileName == null && mimeType == null) {
return null
}

return TurboUriAttributes(
return UriAttributes(
fileName = fileName ?: "attachment",
mimeType = mimeType ?: uri.mimeType(),
fileSize = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import dev.hotwire.core.logging.logError
import dev.hotwire.core.turbo.http.TurboHttpClient
import dev.hotwire.core.turbo.http.HotwireHttpClient
import dev.hotwire.core.turbo.util.dispatcherProvider
import dev.hotwire.core.turbo.util.toJson
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -37,7 +37,7 @@ internal class PathConfigurationRepository {

private fun issueRequest(request: Request): String? {
return try {
val call = TurboHttpClient.instance.newCall(request)
val call = HotwireHttpClient.instance.newCall(request)

call.execute().use { response ->
if (response.isSuccessful) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import java.util.concurrent.TimeUnit
/**
* Experimental: API may change, not ready for production use.
*/
object TurboHttpClient {
object HotwireHttpClient {
private var cache: Cache? = null
private var httpCacheSize = 100L * 1024L * 1024L // 100 MBs
private val loggingInterceptor = HttpLoggingInterceptor().apply {
Expand Down Expand Up @@ -43,7 +43,7 @@ object TurboHttpClient {
internal fun enableCachingWith(context: Context) {
if (cache == null) {
cache = Cache(
directory = File(context.cacheDir, "turbo_cache"),
directory = File(context.cacheDir, "hotwire_cache"),
maxSize = httpCacheSize
)
reset()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.hotwire.core.turbo.http
package dev.hotwire.core.turbo.offline

/**
* Experimental: API may change, not ready for production use.
*/
enum class TurboOfflineCacheStrategy {
enum class OfflineCacheStrategy {
APP, NONE
}
Loading

0 comments on commit 56ff42f

Please sign in to comment.