Compare commits
15 commits
42ceff08ea
...
db321c31f1
Author | SHA1 | Date | |
---|---|---|---|
dece | db321c31f1 | ||
dece | a2dd6f4876 | ||
dece | f8b929b00d | ||
dece | a6e910c3fa | ||
dece | f8cfe2ad09 | ||
dece | 44629dd3ad | ||
dece | a44c36bdca | ||
dece | 9743fea7d5 | ||
dece | 6a65df0f4e | ||
dece | 51f822eb9d | ||
dece | 5cb3af8d12 | ||
dece | 3cba46ad5d | ||
dece | 57854e56dc | ||
dece | e540cf7628 | ||
dece | f2eae54234 |
|
@ -1,6 +1,7 @@
|
||||||
plugins {
|
plugins {
|
||||||
id 'com.android.application'
|
id 'com.android.application'
|
||||||
id 'kotlin-android'
|
id 'kotlin-android'
|
||||||
|
id 'kotlin-kapt'
|
||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
|
@ -18,10 +19,14 @@ android {
|
||||||
buildFeatures {
|
buildFeatures {
|
||||||
viewBinding true
|
viewBinding true
|
||||||
}
|
}
|
||||||
|
testOptions {
|
||||||
|
unitTests.returnDefaultValues = true
|
||||||
|
}
|
||||||
buildTypes {
|
buildTypes {
|
||||||
release {
|
release {
|
||||||
minifyEnabled false
|
minifyEnabled false
|
||||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||||
|
signingConfig signingConfigs.debug
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
compileOptions {
|
compileOptions {
|
||||||
|
@ -36,9 +41,12 @@ android {
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'androidx.core:core-ktx:1.7.0'
|
implementation 'androidx.core:core-ktx:1.7.0'
|
||||||
implementation 'androidx.appcompat:appcompat:1.4.0'
|
implementation 'androidx.appcompat:appcompat:1.4.0'
|
||||||
implementation 'com.google.android.material:material:1.4.0'
|
|
||||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
|
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
|
||||||
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"
|
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"
|
||||||
|
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
|
||||||
|
implementation "androidx.room:room-runtime:2.3.0"
|
||||||
|
implementation 'com.google.android.material:material:1.4.0'
|
||||||
|
kapt "androidx.room:room-compiler:2.3.0"
|
||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
package dev.lowrespalmtree.comet
|
|
||||||
|
|
||||||
import androidx.test.platform.app.InstrumentationRegistry
|
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
|
||||||
|
|
||||||
import org.junit.Test
|
|
||||||
import org.junit.runner.RunWith
|
|
||||||
|
|
||||||
import org.junit.Assert.*
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Instrumented test, which will execute on an Android device.
|
|
||||||
*
|
|
||||||
* See [testing documentation](http://d.android.com/tools/testing).
|
|
||||||
*/
|
|
||||||
@RunWith(AndroidJUnit4::class)
|
|
||||||
class ExampleInstrumentedTest {
|
|
||||||
@Test
|
|
||||||
fun useAppContext() {
|
|
||||||
// Context of the app under test.
|
|
||||||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
|
||||||
assertEquals("dev.lowrespalmtree.comet", appContext.packageName)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,76 @@
|
||||||
|
package dev.lowrespalmtree.comet
|
||||||
|
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Example local unit test, which will execute on the development machine (host).
|
||||||
|
*
|
||||||
|
* See [testing documentation](http://d.android.com/tools/testing).
|
||||||
|
*/
|
||||||
|
class UriUtilsTest {
|
||||||
|
@Test
|
||||||
|
fun joinUrls() {
|
||||||
|
assertEquals(
|
||||||
|
"gemini://dece.space/some-file.gmi",
|
||||||
|
joinUrls("gemini://dece.space/", "some-file.gmi").toString()
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"gemini://dece.space/some-file.gmi",
|
||||||
|
joinUrls("gemini://dece.space/", "./some-file.gmi").toString()
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"gemini://dece.space/some-file.gmi",
|
||||||
|
joinUrls("gemini://dece.space/dir1", "/some-file.gmi").toString()
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"gemini://dece.space/dir1/other-file.gmi",
|
||||||
|
joinUrls("gemini://dece.space/dir1/file.gmi", "other-file.gmi").toString()
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"gemini://dece.space/top-level.gmi",
|
||||||
|
joinUrls("gemini://dece.space/dir1/file.gmi", "../top-level.gmi").toString()
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"s://hard/test/b/d/a.gmi",
|
||||||
|
joinUrls("s://hard/dir/a", "./../test/b/c/../d/e/f/../.././a.gmi").toString()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun removeDotSegments() {
|
||||||
|
arrayOf(
|
||||||
|
Pair("index.gmi", "index.gmi"),
|
||||||
|
Pair("/index.gmi", "/index.gmi"),
|
||||||
|
Pair("./index.gmi", "index.gmi"),
|
||||||
|
Pair("/./index.gmi", "/index.gmi"),
|
||||||
|
Pair("/../index.gmi", "/index.gmi"),
|
||||||
|
Pair("/a/b/c/./../../g", "/a/g"),
|
||||||
|
Pair("mid/content=5/../6", "mid/6"),
|
||||||
|
Pair("../../../../g", "g")
|
||||||
|
).forEach { (path, expected) ->
|
||||||
|
assertEquals(expected, removeDotSegments(path))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun removeLastSegment() {
|
||||||
|
assertEquals("", removeLastSegment(""))
|
||||||
|
assertEquals("", removeLastSegment("/"))
|
||||||
|
assertEquals("", removeLastSegment("/a"))
|
||||||
|
assertEquals("/a", removeLastSegment("/a/"))
|
||||||
|
assertEquals("/a", removeLastSegment("/a/b"))
|
||||||
|
assertEquals("/a/b/c", removeLastSegment("/a/b/c/d"))
|
||||||
|
assertEquals("//", removeLastSegment("///"))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun popFirstSegment() {
|
||||||
|
assertEquals(Pair("", ""), popFirstSegment(""))
|
||||||
|
assertEquals(Pair("a", ""), popFirstSegment("a"))
|
||||||
|
assertEquals(Pair("/a", ""), popFirstSegment("/a"))
|
||||||
|
assertEquals(Pair("/a", "/"), popFirstSegment("/a/"))
|
||||||
|
assertEquals(Pair("/a", "/b"), popFirstSegment("/a/b"))
|
||||||
|
assertEquals(Pair("a", "/b"), popFirstSegment("a/b"))
|
||||||
|
}
|
||||||
|
}
|
136
app/src/main/java/dev/lowrespalmtree/comet/ContentAdapter.kt
Normal file
136
app/src/main/java/dev/lowrespalmtree/comet/ContentAdapter.kt
Normal file
|
@ -0,0 +1,136 @@
|
||||||
|
package dev.lowrespalmtree.comet
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.text.SpannableString
|
||||||
|
import android.text.style.UnderlineSpan
|
||||||
|
import android.util.Log
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import dev.lowrespalmtree.comet.databinding.*
|
||||||
|
|
||||||
|
class ContentAdapter(private var content: List<Line>, private val listener: ContentAdapterListen) :
|
||||||
|
RecyclerView.Adapter<ContentAdapter.ContentViewHolder>() {
|
||||||
|
|
||||||
|
private var lastLineCount = 0
|
||||||
|
|
||||||
|
interface ContentAdapterListen {
|
||||||
|
fun onLinkClick(url: String)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemViewType(position: Int): Int =
|
||||||
|
when (content[position]) {
|
||||||
|
is EmptyLine -> TYPE_EMPTY
|
||||||
|
is ParagraphLine -> TYPE_PARAGRAPH
|
||||||
|
is LinkLine -> TYPE_LINK
|
||||||
|
is PreFenceLine -> TYPE_PRE_FENCE
|
||||||
|
is PreTextLine -> TYPE_PRE_TEXT
|
||||||
|
is BlockquoteLine -> TYPE_BLOCKQUOTE
|
||||||
|
is ListItemLine -> TYPE_LIST_ITEM
|
||||||
|
is TitleLine -> when ((content[position] as TitleLine).level) {
|
||||||
|
1 -> TYPE_TITLE_1
|
||||||
|
2 -> TYPE_TITLE_2
|
||||||
|
3 -> TYPE_TITLE_3
|
||||||
|
else -> error("invalid title level")
|
||||||
|
}
|
||||||
|
else -> error("unknown line type")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContentViewHolder {
|
||||||
|
return LayoutInflater.from(parent.context).let {
|
||||||
|
when (viewType) {
|
||||||
|
TYPE_EMPTY -> ContentViewHolder.Empty(GemtextEmptyBinding.inflate(it))
|
||||||
|
TYPE_PARAGRAPH -> ContentViewHolder.Paragraph(GemtextParagraphBinding.inflate(it))
|
||||||
|
TYPE_TITLE_1 -> ContentViewHolder.Title1(GemtextTitle1Binding.inflate(it))
|
||||||
|
TYPE_TITLE_2 -> ContentViewHolder.Title2(GemtextTitle2Binding.inflate(it))
|
||||||
|
TYPE_TITLE_3 -> ContentViewHolder.Title3(GemtextTitle3Binding.inflate(it))
|
||||||
|
TYPE_LINK -> ContentViewHolder.Link(GemtextLinkBinding.inflate(it))
|
||||||
|
TYPE_PRE_FENCE -> ContentViewHolder.PreFence(GemtextEmptyBinding.inflate(it))
|
||||||
|
TYPE_PRE_TEXT -> ContentViewHolder.PreText(GemtextPreformattedBinding.inflate(it))
|
||||||
|
TYPE_BLOCKQUOTE -> ContentViewHolder.Blockquote(GemtextBlockquoteBinding.inflate(it))
|
||||||
|
TYPE_LIST_ITEM -> ContentViewHolder.ListItem(GemtextListItemBinding.inflate(it))
|
||||||
|
else -> error("invalid view type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressLint("SetTextI18n")
|
||||||
|
override fun onBindViewHolder(holder: ContentViewHolder, position: Int) {
|
||||||
|
val line = content[position]
|
||||||
|
when (holder) {
|
||||||
|
is ContentViewHolder.Paragraph ->
|
||||||
|
holder.binding.textView.text = (line as ParagraphLine).text
|
||||||
|
is ContentViewHolder.Title1 -> holder.binding.textView.text = (line as TitleLine).text
|
||||||
|
is ContentViewHolder.Title2 -> holder.binding.textView.text = (line as TitleLine).text
|
||||||
|
is ContentViewHolder.Title3 -> holder.binding.textView.text = (line as TitleLine).text
|
||||||
|
is ContentViewHolder.Link -> {
|
||||||
|
val text = if ((line as LinkLine).label.isNotEmpty()) line.label else line.url
|
||||||
|
val underlined = SpannableString(text)
|
||||||
|
underlined.setSpan(UnderlineSpan(), 0, underlined.length, 0)
|
||||||
|
holder.binding.textView.text = underlined
|
||||||
|
holder.binding.root.setOnClickListener { listener.onLinkClick(line.url) }
|
||||||
|
}
|
||||||
|
is ContentViewHolder.PreText ->
|
||||||
|
holder.binding.textView.text = (line as PreTextLine).text
|
||||||
|
is ContentViewHolder.Blockquote ->
|
||||||
|
holder.binding.textView.text = (line as BlockquoteLine).text
|
||||||
|
is ContentViewHolder.ListItem ->
|
||||||
|
holder.binding.textView.text = "\u25CF ${(line as ListItemLine).text}"
|
||||||
|
else -> {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemCount(): Int = content.size
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace the content rendered by the recycler.
|
||||||
|
*
|
||||||
|
* The new content list may or may not be the same object as the previous one, we don't
|
||||||
|
* assume anything. The assumptions this function do however are:
|
||||||
|
* - If the new content is empty, we are about to load new content, so clear the recycler.
|
||||||
|
* - If it's longer than before, we received new streamed content, so *append* data.
|
||||||
|
* - If it's shorter or the same size than before, we do not notify anything and let the caller
|
||||||
|
* manage the changes itself.
|
||||||
|
*/
|
||||||
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
|
fun setContent(newContent: List<Line>) {
|
||||||
|
content = newContent.toList() // Shallow copy to avoid concurrent update issues.
|
||||||
|
if (content.isEmpty()) {
|
||||||
|
Log.d(TAG, "setContent: empty content")
|
||||||
|
notifyDataSetChanged()
|
||||||
|
} else if (content.size > lastLineCount) {
|
||||||
|
val numAdded = content.size - lastLineCount
|
||||||
|
Log.d(TAG, "setContent: added $numAdded items")
|
||||||
|
notifyItemRangeInserted(lastLineCount, numAdded)
|
||||||
|
}
|
||||||
|
lastLineCount = content.size
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class ContentViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
||||||
|
class Empty(val binding: GemtextEmptyBinding) : ContentViewHolder(binding.root)
|
||||||
|
class Paragraph(val binding: GemtextParagraphBinding) : ContentViewHolder(binding.root)
|
||||||
|
class Title1(val binding: GemtextTitle1Binding) : ContentViewHolder(binding.root)
|
||||||
|
class Title2(val binding: GemtextTitle2Binding) : ContentViewHolder(binding.root)
|
||||||
|
class Title3(val binding: GemtextTitle3Binding) : ContentViewHolder(binding.root)
|
||||||
|
class Link(val binding: GemtextLinkBinding) : ContentViewHolder(binding.root)
|
||||||
|
class PreFence(val binding: GemtextEmptyBinding) : ContentViewHolder(binding.root)
|
||||||
|
class PreText(val binding: GemtextPreformattedBinding) : ContentViewHolder(binding.root)
|
||||||
|
class Blockquote(val binding: GemtextBlockquoteBinding) : ContentViewHolder(binding.root)
|
||||||
|
class ListItem(val binding: GemtextListItemBinding) : ContentViewHolder(binding.root)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val TAG = "ContentRecycler"
|
||||||
|
const val TYPE_EMPTY = 0
|
||||||
|
const val TYPE_PARAGRAPH = 1
|
||||||
|
const val TYPE_TITLE_1 = 2
|
||||||
|
const val TYPE_TITLE_2 = 3
|
||||||
|
const val TYPE_TITLE_3 = 4
|
||||||
|
const val TYPE_LINK = 5
|
||||||
|
const val TYPE_PRE_FENCE = 6
|
||||||
|
const val TYPE_PRE_TEXT = 7
|
||||||
|
const val TYPE_BLOCKQUOTE = 8
|
||||||
|
const val TYPE_LIST_ITEM = 9
|
||||||
|
}
|
||||||
|
}
|
21
app/src/main/java/dev/lowrespalmtree/comet/Database.kt
Normal file
21
app/src/main/java/dev/lowrespalmtree/comet/Database.kt
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
package dev.lowrespalmtree.comet
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.room.Database
|
||||||
|
import androidx.room.Room
|
||||||
|
import androidx.room.RoomDatabase
|
||||||
|
|
||||||
|
@Database(entities = [History.HistoryEntry::class], version = 1)
|
||||||
|
abstract class AppDatabase : RoomDatabase() {
|
||||||
|
abstract fun historyEntryDao(): History.HistoryEntryDao
|
||||||
|
}
|
||||||
|
|
||||||
|
object Database {
|
||||||
|
lateinit var INSTANCE: AppDatabase
|
||||||
|
|
||||||
|
fun init(context: Context) {
|
||||||
|
if (::INSTANCE.isInitialized)
|
||||||
|
return
|
||||||
|
INSTANCE = Room.databaseBuilder(context, AppDatabase::class.java, "comet").build()
|
||||||
|
}
|
||||||
|
}
|
69
app/src/main/java/dev/lowrespalmtree/comet/Gemtext.kt
Normal file
69
app/src/main/java/dev/lowrespalmtree/comet/Gemtext.kt
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
package dev.lowrespalmtree.comet
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.channels.Channel
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import java.nio.ByteBuffer
|
||||||
|
import java.nio.CharBuffer
|
||||||
|
import java.nio.charset.Charset
|
||||||
|
|
||||||
|
interface Line
|
||||||
|
|
||||||
|
class EmptyLine : Line
|
||||||
|
class ParagraphLine(val text: String) : Line
|
||||||
|
class TitleLine(val level: Int, val text: String) : Line
|
||||||
|
class LinkLine(val url: String, val label: String) : Line
|
||||||
|
class PreFenceLine(val caption: String) : Line
|
||||||
|
class PreTextLine(val text: String) : Line
|
||||||
|
class BlockquoteLine(val text: String) : Line
|
||||||
|
class ListItemLine(val text: String) : Line
|
||||||
|
|
||||||
|
const val TAG = "Gemtext"
|
||||||
|
|
||||||
|
fun parseData(
|
||||||
|
inChannel: Channel<ByteArray>,
|
||||||
|
charset: Charset,
|
||||||
|
scope: CoroutineScope
|
||||||
|
): Channel<Line> {
|
||||||
|
val channel = Channel<Line>()
|
||||||
|
scope.launch {
|
||||||
|
var isPref = false
|
||||||
|
var buffer = ByteArray(0)
|
||||||
|
Log.d(TAG, "parseData: start getting data from channel")
|
||||||
|
for (data in inChannel) {
|
||||||
|
buffer += data
|
||||||
|
var nextLineFeed: Int = -1
|
||||||
|
while (buffer.isNotEmpty() && buffer.indexOf(0x0A).also { nextLineFeed = it } > -1) {
|
||||||
|
val lineData = buffer.sliceArray(0 until nextLineFeed)
|
||||||
|
buffer = buffer.sliceArray(nextLineFeed + 1 until buffer.size)
|
||||||
|
val lineString = charset.decode(ByteBuffer.wrap(lineData))
|
||||||
|
val line = parseLine(lineString, isPref)
|
||||||
|
when (line) {
|
||||||
|
is PreFenceLine -> isPref = !isPref
|
||||||
|
}
|
||||||
|
channel.send(line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Log.d(TAG, "parseData: channel closed")
|
||||||
|
channel.close()
|
||||||
|
}
|
||||||
|
return channel
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun parseLine(line: CharBuffer, isPreformatted: Boolean): Line =
|
||||||
|
when {
|
||||||
|
line.isEmpty() -> EmptyLine()
|
||||||
|
line.startsWith("###") -> TitleLine(3, getCharsFrom(line, 3))
|
||||||
|
line.startsWith("##") -> TitleLine(2, getCharsFrom(line, 2))
|
||||||
|
line.startsWith("#") -> TitleLine(1, getCharsFrom(line, 1))
|
||||||
|
line.startsWith(">") -> BlockquoteLine(getCharsFrom(line, 1))
|
||||||
|
line.startsWith("```") -> PreFenceLine(getCharsFrom(line, 3))
|
||||||
|
line.startsWith("* ") -> ListItemLine(getCharsFrom(line, 2))
|
||||||
|
line.startsWith("=>") -> getCharsFrom(line, 2)
|
||||||
|
.split(" ", limit = 2)
|
||||||
|
.run { LinkLine(get(0), if (size == 2) get(1).trimStart() else "") }
|
||||||
|
else -> if (isPreformatted) PreTextLine(line.toString()) else ParagraphLine(line.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getCharsFrom(line: CharBuffer, index: Int) = line.substring(index).trim()
|
37
app/src/main/java/dev/lowrespalmtree/comet/History.kt
Normal file
37
app/src/main/java/dev/lowrespalmtree/comet/History.kt
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
package dev.lowrespalmtree.comet
|
||||||
|
|
||||||
|
import androidx.room.*
|
||||||
|
|
||||||
|
object History {
|
||||||
|
@Entity
|
||||||
|
data class HistoryEntry(
|
||||||
|
@PrimaryKey @ColumnInfo(name = "uri") val uri: String,
|
||||||
|
@ColumnInfo(name = "title") var title: String?,
|
||||||
|
@ColumnInfo(name = "lastVisit") var lastVisit: Long,
|
||||||
|
)
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
interface HistoryEntryDao {
|
||||||
|
@Query("SELECT * FROM HistoryEntry WHERE :uri = uri LIMIT 1")
|
||||||
|
fun get(uri: String): HistoryEntry?
|
||||||
|
|
||||||
|
@Query("SELECT * FROM HistoryEntry ORDER BY lastVisit DESC")
|
||||||
|
fun getAll(): List<HistoryEntry>
|
||||||
|
|
||||||
|
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||||
|
fun insert(vararg entries: HistoryEntry)
|
||||||
|
|
||||||
|
@Update
|
||||||
|
fun update(vararg entries: HistoryEntry)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun record(uri: String, title: String?) {
|
||||||
|
val now = System.currentTimeMillis()
|
||||||
|
val dao = Database.INSTANCE.historyEntryDao()
|
||||||
|
val entry = dao.get(uri)
|
||||||
|
if (entry == null)
|
||||||
|
dao.insert(HistoryEntry(uri, title, now))
|
||||||
|
else
|
||||||
|
dao.update(entry.also { it.title = title; it.lastVisit = now })
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,9 @@
|
||||||
package dev.lowrespalmtree.comet
|
package dev.lowrespalmtree.comet
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
|
import android.content.ActivityNotFoundException
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.Intent.ACTION_VIEW
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
@ -9,31 +11,41 @@ import android.view.inputmethod.EditorInfo
|
||||||
import android.view.inputmethod.InputMethodManager
|
import android.view.inputmethod.InputMethodManager
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.lifecycle.MutableLiveData
|
|
||||||
import androidx.lifecycle.ViewModel
|
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import dev.lowrespalmtree.comet.databinding.ActivityMainBinding
|
import dev.lowrespalmtree.comet.databinding.ActivityMainBinding
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import java.nio.ByteBuffer
|
|
||||||
import java.nio.charset.Charset
|
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
@ExperimentalCoroutinesApi
|
||||||
|
class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
|
||||||
private lateinit var binding: ActivityMainBinding
|
private lateinit var binding: ActivityMainBinding
|
||||||
private lateinit var pageViewModel: PageViewModel
|
private lateinit var pageViewModel: PageViewModel
|
||||||
|
private lateinit var adapter: ContentAdapter
|
||||||
|
|
||||||
|
/** Property to access and set the current address bar URL value. */
|
||||||
|
private var currentUrl
|
||||||
|
get() = binding.addressBar.text.toString()
|
||||||
|
set(value) = binding.addressBar.setText(value)
|
||||||
|
|
||||||
|
/** A non-saved list of visited URLs. Not an history, just used for going back. */
|
||||||
|
private val visitedUrls = mutableListOf<String>()
|
||||||
|
|
||||||
@SuppressLint("SetTextI18n")
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
Database.init(applicationContext)
|
||||||
|
|
||||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
|
|
||||||
pageViewModel = ViewModelProvider(this)[PageViewModel::class.java]
|
pageViewModel = ViewModelProvider(this)[PageViewModel::class.java]
|
||||||
|
adapter = ContentAdapter(listOf(), this)
|
||||||
|
binding.contentRecycler.layoutManager = LinearLayoutManager(this)
|
||||||
|
binding.contentRecycler.adapter = adapter
|
||||||
|
|
||||||
binding.addressBar.setOnEditorActionListener { view, actionId, _ ->
|
binding.addressBar.setOnEditorActionListener { view, actionId, _ ->
|
||||||
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
||||||
pageViewModel.sendRequest(view.text.toString())
|
openUrl(view.text.toString())
|
||||||
val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
|
val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||||
imm.hideSoftInputFromWindow(view.windowToken, 0)
|
imm.hideSoftInputFromWindow(view.windowToken, 0)
|
||||||
view.clearFocus()
|
view.clearFocus()
|
||||||
|
@ -43,57 +55,99 @@ class MainActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pageViewModel.sourceLiveData.observe(this, {
|
binding.contentSwipeLayout.setOnRefreshListener { openUrl(currentUrl) }
|
||||||
binding.sourceBlock.text = it
|
|
||||||
})
|
pageViewModel.state.observe(this, { updateState(it) })
|
||||||
pageViewModel.alertLiveData.observe(this, {
|
pageViewModel.lines.observe(this, { updateLines(it) })
|
||||||
AlertDialog.Builder(this)
|
pageViewModel.event.observe(this, { handleEvent(it) })
|
||||||
.setTitle(R.string.alert_title)
|
|
||||||
.setMessage(it)
|
|
||||||
.create()
|
|
||||||
.show()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class PageViewModel : ViewModel() {
|
override fun onBackPressed() {
|
||||||
var source = ""
|
visitedUrls.removeLastOrNull() // Always remove current page first.
|
||||||
val sourceLiveData: MutableLiveData<String> by lazy { MutableLiveData<String>() }
|
val previousUrl = visitedUrls.removeLastOrNull()
|
||||||
val alertLiveData: MutableLiveData<String> by lazy { MutableLiveData<String>() }
|
if (previousUrl != null)
|
||||||
|
openUrl(previousUrl)
|
||||||
|
else
|
||||||
|
super.onBackPressed()
|
||||||
|
}
|
||||||
|
|
||||||
fun sendRequest(url: String) {
|
override fun onLinkClick(url: String) {
|
||||||
Log.d(TAG, "sendRequest: $url")
|
openUrl(url, base = if (currentUrl.isNotEmpty()) currentUrl else null)
|
||||||
source = ""
|
}
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
|
||||||
val uri = Uri.parse(url)
|
|
||||||
if (uri.scheme != "gemini") {
|
|
||||||
alertLiveData.postValue("Can't process scheme \"${uri.scheme}\".")
|
|
||||||
return@launch
|
|
||||||
}
|
|
||||||
|
|
||||||
val request = Request(uri)
|
private fun openUrl(url: String, base: String? = null, redirections: Int = 0) {
|
||||||
val socket = request.connect()
|
if (redirections >= 5) {
|
||||||
val channel = request.proceed(socket, this)
|
alert("Too many redirections.")
|
||||||
val response = Response.from(channel, viewModelScope)
|
return
|
||||||
if (response == null) {
|
|
||||||
alertLiveData.postValue("Can't parse server response.")
|
|
||||||
return@launch
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.i(TAG, "sendRequest: got ${response.code} with meta \"${response.meta}\"")
|
|
||||||
when (response.code) {
|
|
||||||
Response.Code.SUCCESS -> handleRequestSuccess(response)
|
|
||||||
else -> alertLiveData.postValue("Can't handle code ${response.code}.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun handleRequestSuccess(response: Response) {
|
var uri = Uri.parse(url)
|
||||||
val charset = Charset.defaultCharset()
|
if (!uri.isAbsolute) {
|
||||||
for (data in response.data) {
|
uri = if (!base.isNullOrEmpty()) joinUrls(base, url) else toGeminiUri(uri)
|
||||||
val decoded = charset.decode(ByteBuffer.wrap(data)).toString()
|
}
|
||||||
source += decoded
|
|
||||||
sourceLiveData.postValue(source)
|
when (uri.scheme) {
|
||||||
|
"gemini" -> pageViewModel.sendGeminiRequest(uri)
|
||||||
|
else -> openUnknownScheme(uri)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateState(state: PageViewModel.State) {
|
||||||
|
Log.d(TAG, "updateState: $state")
|
||||||
|
when (state) {
|
||||||
|
PageViewModel.State.IDLE -> {
|
||||||
|
binding.contentProgressBar.hide()
|
||||||
|
binding.contentSwipeLayout.isRefreshing = false
|
||||||
}
|
}
|
||||||
|
PageViewModel.State.CONNECTING -> {
|
||||||
|
binding.contentProgressBar.show()
|
||||||
|
}
|
||||||
|
PageViewModel.State.RECEIVING -> {
|
||||||
|
binding.contentSwipeLayout.isRefreshing = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateLines(lines: List<Line>) {
|
||||||
|
Log.d(TAG, "updateLines: ${lines.size} lines")
|
||||||
|
adapter.setContent(lines)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleEvent(event: PageViewModel.Event) {
|
||||||
|
Log.d(TAG, "handleEvent: $event")
|
||||||
|
if (!event.handled) {
|
||||||
|
when (event) {
|
||||||
|
is PageViewModel.SuccessEvent -> {
|
||||||
|
currentUrl = event.uri
|
||||||
|
visitedUrls.add(event.uri)
|
||||||
|
}
|
||||||
|
is PageViewModel.RedirectEvent -> {
|
||||||
|
openUrl(event.uri, base = currentUrl, redirections = event.redirects)
|
||||||
|
}
|
||||||
|
is PageViewModel.FailureEvent -> {
|
||||||
|
var message = event.details
|
||||||
|
if (!event.serverDetails.isNullOrEmpty())
|
||||||
|
message += "\n\n" + "Server details: ${event.serverDetails}"
|
||||||
|
alert(message, title = event.short)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
event.handled = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun alert(message: String, title: String? = null) {
|
||||||
|
AlertDialog.Builder(this)
|
||||||
|
.setTitle(title ?: getString(R.string.error_alert_title))
|
||||||
|
.setMessage(message)
|
||||||
|
.create()
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun openUnknownScheme(uri: Uri) {
|
||||||
|
try {
|
||||||
|
startActivity(Intent(ACTION_VIEW, uri))
|
||||||
|
} catch (e: ActivityNotFoundException) {
|
||||||
|
alert("Can't open this URL.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.io.BufferedInputStream
|
import java.io.BufferedInputStream
|
||||||
|
import java.net.InetSocketAddress
|
||||||
import java.net.SocketTimeoutException
|
import java.net.SocketTimeoutException
|
||||||
import java.security.cert.X509Certificate
|
import java.security.cert.X509Certificate
|
||||||
import javax.net.ssl.SSLContext
|
import javax.net.ssl.SSLContext
|
||||||
|
@ -20,8 +21,9 @@ class Request(private val uri: Uri) {
|
||||||
Log.d(TAG, "connect")
|
Log.d(TAG, "connect")
|
||||||
val context = SSLContext.getInstance("TLSv1.2")
|
val context = SSLContext.getInstance("TLSv1.2")
|
||||||
context.init(null, arrayOf(TrustManager()), null)
|
context.init(null, arrayOf(TrustManager()), null)
|
||||||
val socket = context.socketFactory.createSocket(uri.host, port) as SSLSocket
|
val socket = context.socketFactory.createSocket() as SSLSocket
|
||||||
socket.soTimeout = 10000
|
socket.soTimeout = 10000
|
||||||
|
socket.connect(InetSocketAddress(uri.host, port), 10000)
|
||||||
socket.startHandshake()
|
socket.startHandshake()
|
||||||
return socket
|
return socket
|
||||||
}
|
}
|
||||||
|
@ -39,25 +41,25 @@ class Request(private val uri: Uri) {
|
||||||
try {
|
try {
|
||||||
@Suppress("BlockingMethodInNonBlockingContext") // what u gonna do
|
@Suppress("BlockingMethodInNonBlockingContext") // what u gonna do
|
||||||
while ((bis.read(buffer).also { numRead = it }) >= 0) {
|
while ((bis.read(buffer).also { numRead = it }) >= 0) {
|
||||||
Log.d(TAG, "proceed coroutine: received $numRead bytes")
|
|
||||||
val received = buffer.sliceArray(0 until numRead)
|
val received = buffer.sliceArray(0 until numRead)
|
||||||
channel.send(received)
|
channel.send(received)
|
||||||
}
|
}
|
||||||
} catch (e: SocketTimeoutException) {
|
} catch (e: SocketTimeoutException) {
|
||||||
Log.i(TAG, "Socket timeout.")
|
Log.i(TAG, "proceed coroutine: socket timeout.")
|
||||||
channel.cancel()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log.d(TAG, "proceed coroutine: reading completed")
|
Log.d(TAG, "proceed coroutine: reading completed")
|
||||||
|
channel.close()
|
||||||
}
|
}
|
||||||
return channel
|
return channel
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("CustomX509TrustManager")
|
@SuppressLint("CustomX509TrustManager")
|
||||||
class TrustManager: X509TrustManager {
|
class TrustManager : X509TrustManager {
|
||||||
@SuppressLint("TrustAllX509TrustManager")
|
@SuppressLint("TrustAllX509TrustManager")
|
||||||
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
|
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {
|
||||||
|
}
|
||||||
|
|
||||||
override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {
|
override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {
|
||||||
Log.d(TAG, "cool cert, please continue")
|
Log.d(TAG, "cool cert, please continue")
|
||||||
|
|
|
@ -3,7 +3,7 @@ package dev.lowrespalmtree.comet
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.channels.consumeEach
|
import kotlinx.coroutines.channels.ClosedReceiveChannelException
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
|
@ -31,6 +31,18 @@ class Response(val code: Code, val meta: String, val data: Channel<ByteArray>) {
|
||||||
CERTIFICATE_NOT_AUTHORISED(61),
|
CERTIFICATE_NOT_AUTHORISED(61),
|
||||||
CERTIFICATE_NOT_VALID(62);
|
CERTIFICATE_NOT_VALID(62);
|
||||||
|
|
||||||
|
enum class Category(val value: Int) {
|
||||||
|
UNKNOWN(0),
|
||||||
|
INPUT(1),
|
||||||
|
SUCCESS(2),
|
||||||
|
REDIRECT(3),
|
||||||
|
SERVER_ERROR(4),
|
||||||
|
CLIENT_ERROR(5),
|
||||||
|
CERTIFICATE(6);
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getCategory(): Category? = Category.values().associateBy(Category::value)[value / 10]
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val MAP = values().associateBy(Code::value)
|
private val MAP = values().associateBy(Code::value)
|
||||||
fun fromInt(type: Int) = MAP[type]
|
fun fromInt(type: Int) = MAP[type]
|
||||||
|
@ -39,43 +51,56 @@ class Response(val code: Code, val meta: String, val data: Channel<ByteArray>) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "Response"
|
private const val TAG = "Response"
|
||||||
|
private const val MAX_META_LEN = 1024
|
||||||
|
private const val MAX_HEADER_LEN = 2 + 1 + MAX_META_LEN + 2
|
||||||
|
|
||||||
/** Return a response object from the incoming server data, served through the channel. */
|
/** Return a response object from the incoming server data, served through the channel. */
|
||||||
suspend fun from(channel: Channel<ByteArray>, scope: CoroutineScope): Response? {
|
suspend fun from(channel: Channel<ByteArray>, scope: CoroutineScope): Response? {
|
||||||
var received = 0
|
var received = 0
|
||||||
val headerBuffer = ByteBuffer.allocate(1024)
|
val headerBuffer = ByteBuffer.allocate(1024)
|
||||||
for (data in channel) {
|
var lfIndex: Int
|
||||||
|
// While we don't have a response object (i.e. no header parsed), keep reading.
|
||||||
|
while (true) {
|
||||||
|
val data = try {
|
||||||
|
channel.receive()
|
||||||
|
} catch (e: ClosedReceiveChannelException) {
|
||||||
|
Log.i(TAG, "companion from: channel closed during initial receive")
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
if (received + data.size > MAX_HEADER_LEN) {
|
||||||
|
Log.i(TAG, "companion from: received too much data for a valid header")
|
||||||
|
return null
|
||||||
|
}
|
||||||
// Push some data into our buffer.
|
// Push some data into our buffer.
|
||||||
headerBuffer.put(data)
|
headerBuffer.put(data)
|
||||||
received += data.size
|
received += data.size
|
||||||
// Check if there is enough data to parse a Gemini header from it (e.g. has \r\n).
|
// Check if there is enough data to parse a Gemini header from it (e.g. has \r\n).
|
||||||
val lfIndex = headerBuffer.array().indexOf(0x0D) // \r
|
lfIndex = headerBuffer.array().indexOf(0x0D) // \r
|
||||||
if (lfIndex == -1)
|
if (lfIndex == -1)
|
||||||
continue
|
continue
|
||||||
if (headerBuffer.array()[lfIndex + 1] != (0x0A.toByte())) // \n
|
if (headerBuffer.array()[lfIndex + 1] != (0x0A.toByte())) // \n
|
||||||
continue
|
continue
|
||||||
// We have our header! Parse it to create our Response object.
|
break
|
||||||
val bytes = headerBuffer.array()
|
|
||||||
val headerData = bytes.sliceArray(0 until lfIndex)
|
|
||||||
val (code, meta) = parseHeader(headerData)
|
|
||||||
?: return null.also { Log.e(TAG, "Failed to parse header") }
|
|
||||||
val responseChannel = Channel<ByteArray>()
|
|
||||||
val response = Response(code, meta, responseChannel)
|
|
||||||
scope.launch {
|
|
||||||
// If we got too much data from the channel: push the trailing data first.
|
|
||||||
val trailingIndex = lfIndex + 2
|
|
||||||
if (trailingIndex < received) {
|
|
||||||
val trailingData = bytes.sliceArray(trailingIndex until received)
|
|
||||||
responseChannel.send(trailingData)
|
|
||||||
}
|
|
||||||
// Forward all incoming data to the Response channel.
|
|
||||||
channel.consumeEach { responseChannel.send(it) }
|
|
||||||
}
|
|
||||||
// Return the response here; this stops consuming the channel from this for-loop so
|
|
||||||
// that the coroutine above can take care of it.
|
|
||||||
return response
|
|
||||||
}
|
}
|
||||||
return null
|
// We have our header! Parse it to create our Response object.
|
||||||
|
val bytes = headerBuffer.array()
|
||||||
|
val headerData = bytes.sliceArray(0 until lfIndex)
|
||||||
|
val (code, meta) = parseHeader(headerData)
|
||||||
|
?: return null .also { Log.i(TAG, "companion from: can't parse header") }
|
||||||
|
val response = Response(code, meta, Channel())
|
||||||
|
scope.launch {
|
||||||
|
// If we got too much data from the channel: push the trailing data first.
|
||||||
|
val trailingIndex = lfIndex + 2
|
||||||
|
if (trailingIndex < received) {
|
||||||
|
val trailingData = bytes.sliceArray(trailingIndex until received)
|
||||||
|
response.data.send(trailingData)
|
||||||
|
}
|
||||||
|
// Forward all incoming data to the Response channel.
|
||||||
|
for (data in channel)
|
||||||
|
response.data.send(data)
|
||||||
|
response.data.close()
|
||||||
|
}
|
||||||
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return the code and meta from this header if it could be parsed correctly. */
|
/** Return the code and meta from this header if it could be parsed correctly. */
|
||||||
|
|
88
app/src/main/java/dev/lowrespalmtree/comet/UriUtils.kt
Normal file
88
app/src/main/java/dev/lowrespalmtree/comet/UriUtils.kt
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
package dev.lowrespalmtree.comet
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import android.util.Log
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform a relative URI to an absolute Gemini URI
|
||||||
|
*
|
||||||
|
* This is mostly to translate user-friendly URLs such as "medusae.space" into
|
||||||
|
* "gemini://medusae.space". This assumes that the Uri parsing put what the user probably intended
|
||||||
|
* as the hostname into the path field instead of the authority. Thus, it will NOT merely translate
|
||||||
|
* any absolute URI into a gemini-scheme URI.
|
||||||
|
*/
|
||||||
|
fun toGeminiUri(uri: Uri): Uri =
|
||||||
|
Uri.Builder()
|
||||||
|
.scheme("gemini")
|
||||||
|
.authority(uri.path)
|
||||||
|
.query(uri.query)
|
||||||
|
.fragment(uri.fragment)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
/** Return the URI obtained from considering the relative part of an URI wrt/ a base URI. */
|
||||||
|
fun joinUrls(base: String, relative: String): Uri {
|
||||||
|
val baseUri = Uri.parse(base)
|
||||||
|
val relUri = Uri.parse(relative)
|
||||||
|
val newPath = removeDotSegments(
|
||||||
|
if (relative.startsWith("/")) {
|
||||||
|
relUri.path ?: ""
|
||||||
|
} else {
|
||||||
|
removeLastSegment(
|
||||||
|
baseUri.path ?: ""
|
||||||
|
) + "/" +
|
||||||
|
(relUri.path ?: "")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return Uri.Builder()
|
||||||
|
.scheme(baseUri.scheme)
|
||||||
|
.authority(baseUri.authority)
|
||||||
|
.path(newPath)
|
||||||
|
.query(relUri.query)
|
||||||
|
.fragment(relUri.fragment)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Remove all the sneaky dot segments from the path. */
|
||||||
|
internal fun removeDotSegments(path: String): String {
|
||||||
|
var output = ""
|
||||||
|
var slice = path
|
||||||
|
while (slice.isNotEmpty()) {
|
||||||
|
if (slice.startsWith("../")) {
|
||||||
|
slice = slice.substring(3)
|
||||||
|
} else if (slice.startsWith("./") || slice.startsWith("/./")) {
|
||||||
|
slice = slice.substring(2)
|
||||||
|
} else if (slice == "/.") {
|
||||||
|
slice = "/"
|
||||||
|
} else if (slice.startsWith("/../")) {
|
||||||
|
slice = "/" + slice.substring(4)
|
||||||
|
output = removeLastSegment(output)
|
||||||
|
} else if (slice == "/..") {
|
||||||
|
slice = "/"
|
||||||
|
output = removeLastSegment(output)
|
||||||
|
} else if (slice == "." || slice == "..") {
|
||||||
|
slice = ""
|
||||||
|
} else {
|
||||||
|
val (firstSegment, remaining) = popFirstSegment(slice)
|
||||||
|
output += firstSegment
|
||||||
|
slice = remaining
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Remove the last segment of that path, including preceding "/" if any. */
|
||||||
|
internal fun removeLastSegment(path: String): String {
|
||||||
|
val lastSlashIndex = path.indexOfLast { it == '/' }
|
||||||
|
return if (lastSlashIndex > -1) path.slice(0 until lastSlashIndex) else path
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Return first segment and the rest. */
|
||||||
|
internal fun popFirstSegment(path: String): Pair<String, String> {
|
||||||
|
if (path.isEmpty())
|
||||||
|
return Pair(path, "")
|
||||||
|
var nextSlash = path.substring(1).indexOf("/")
|
||||||
|
if (nextSlash == -1)
|
||||||
|
return Pair(path, "")
|
||||||
|
nextSlash++
|
||||||
|
return Pair(path.substring(0, nextSlash), path.substring(nextSlash))
|
||||||
|
}
|
|
@ -24,40 +24,48 @@
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:layout_margin="4dp"
|
android:layout_margin="4dp"
|
||||||
android:ems="10"
|
|
||||||
android:hint="@string/url"
|
android:hint="@string/url"
|
||||||
android:imeOptions="actionDone|actionGo"
|
android:imeOptions="actionDone|actionGo"
|
||||||
android:importantForAutofill="no"
|
android:importantForAutofill="no"
|
||||||
android:inputType="textUri"
|
android:inputType="textUri"
|
||||||
android:text="" />
|
android:text=""
|
||||||
|
tools:ignore="TextContrastCheck" />
|
||||||
|
|
||||||
</com.google.android.material.appbar.CollapsingToolbarLayout>
|
</com.google.android.material.appbar.CollapsingToolbarLayout>
|
||||||
|
|
||||||
</com.google.android.material.appbar.AppBarLayout>
|
</com.google.android.material.appbar.AppBarLayout>
|
||||||
|
|
||||||
<androidx.core.widget.NestedScrollView
|
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||||
android:id="@+id/scroll_view"
|
android:id="@+id/content_swipe_layout"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="wrap_content"
|
||||||
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
|
app:layout_behavior="@string/appbar_scrolling_view_behavior">
|
||||||
|
|
||||||
<LinearLayout
|
<RelativeLayout
|
||||||
|
android:id="@+id/content_inner_layout"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:orientation="vertical">
|
app:layout_behavior="@string/appbar_scrolling_view_behavior">
|
||||||
|
|
||||||
<TextView
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
android:id="@+id/source_block"
|
android:id="@+id/content_recycler"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginStart="16dp"
|
android:orientation="vertical"
|
||||||
android:layout_marginEnd="16dp"
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
android:text=""
|
|
||||||
android:textAlignment="textStart"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintStart_toStartOf="parent" />
|
|
||||||
|
|
||||||
</LinearLayout>
|
<androidx.core.widget.ContentLoadingProgressBar
|
||||||
</androidx.core.widget.NestedScrollView>
|
android:id="@+id/content_progress_bar"
|
||||||
|
style="?android:attr/progressBarStyleHorizontal"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="16dp"
|
||||||
|
android:layout_alignParentTop="true"
|
||||||
|
android:layout_marginTop="-8dp"
|
||||||
|
android:indeterminate="true"
|
||||||
|
android:visibility="gone" />
|
||||||
|
|
||||||
|
</RelativeLayout>
|
||||||
|
|
||||||
|
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
|
||||||
|
|
||||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
12
app/src/main/res/layout/gemtext_blockquote.xml
Normal file
12
app/src/main/res/layout/gemtext_blockquote.xml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:textColor="@color/text"
|
||||||
|
android:textStyle="italic"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingStart="32dp"
|
||||||
|
android:paddingEnd="16dp"
|
||||||
|
android:paddingTop="2dp"
|
||||||
|
android:paddingBottom="4dp" />
|
4
app/src/main/res/layout/gemtext_empty.xml
Normal file
4
app/src/main/res/layout/gemtext_empty.xml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="4dp" />
|
12
app/src/main/res/layout/gemtext_link.xml
Normal file
12
app/src/main/res/layout/gemtext_link.xml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:textColor="@color/teal_700"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingStart="16dp"
|
||||||
|
android:paddingEnd="16dp"
|
||||||
|
android:paddingTop="2dp"
|
||||||
|
android:paddingBottom="4dp"
|
||||||
|
android:textIsSelectable="false" />
|
11
app/src/main/res/layout/gemtext_list_item.xml
Normal file
11
app/src/main/res/layout/gemtext_list_item.xml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:textColor="@color/text"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingStart="32dp"
|
||||||
|
android:paddingEnd="16dp"
|
||||||
|
android:paddingTop="2dp"
|
||||||
|
android:paddingBottom="4dp" />
|
11
app/src/main/res/layout/gemtext_paragraph.xml
Normal file
11
app/src/main/res/layout/gemtext_paragraph.xml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:textColor="@color/text"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingStart="16dp"
|
||||||
|
android:paddingEnd="16dp"
|
||||||
|
android:paddingTop="2dp"
|
||||||
|
android:paddingBottom="4dp" />
|
13
app/src/main/res/layout/gemtext_preformatted.xml
Normal file
13
app/src/main/res/layout/gemtext_preformatted.xml
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:textSize="14sp"
|
||||||
|
android:textColor="@color/text"
|
||||||
|
android:typeface="monospace"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingStart="16dp"
|
||||||
|
android:paddingEnd="8dp"
|
||||||
|
android:paddingTop="2dp"
|
||||||
|
android:paddingBottom="2dp"
|
||||||
|
android:background="@color/purple_200" />
|
12
app/src/main/res/layout/gemtext_title1.xml
Normal file
12
app/src/main/res/layout/gemtext_title1.xml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:textSize="30sp"
|
||||||
|
android:textColor="@color/text"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingStart="16dp"
|
||||||
|
android:paddingEnd="16dp"
|
||||||
|
android:paddingTop="16dp"
|
||||||
|
android:paddingBottom="4dp" />
|
12
app/src/main/res/layout/gemtext_title2.xml
Normal file
12
app/src/main/res/layout/gemtext_title2.xml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:textSize="24sp"
|
||||||
|
android:textColor="@color/text"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingStart="16dp"
|
||||||
|
android:paddingEnd="16dp"
|
||||||
|
android:paddingTop="8dp"
|
||||||
|
android:paddingBottom="4dp" />
|
12
app/src/main/res/layout/gemtext_title3.xml
Normal file
12
app/src/main/res/layout/gemtext_title3.xml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/text_view"
|
||||||
|
android:textSize="18sp"
|
||||||
|
android:textColor="@color/text"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingStart="16dp"
|
||||||
|
android:paddingEnd="16dp"
|
||||||
|
android:paddingTop="8dp"
|
||||||
|
android:paddingBottom="4dp" />
|
|
@ -1,6 +1,6 @@
|
||||||
<resources xmlns:tools="http://schemas.android.com/tools">
|
<resources xmlns:tools="http://schemas.android.com/tools">
|
||||||
<!-- Base application theme. -->
|
<!-- Base application theme. -->
|
||||||
<style name="Theme.Comet" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
|
<style name="Theme.Comet" parent="Theme.MaterialComponents.DayNight.NoActionBar">
|
||||||
<!-- Primary brand color. -->
|
<!-- Primary brand color. -->
|
||||||
<item name="colorPrimary">@color/purple_200</item>
|
<item name="colorPrimary">@color/purple_200</item>
|
||||||
<item name="colorPrimaryVariant">@color/purple_700</item>
|
<item name="colorPrimaryVariant">@color/purple_700</item>
|
||||||
|
|
|
@ -7,4 +7,5 @@
|
||||||
<color name="teal_700">#FF018786</color>
|
<color name="teal_700">#FF018786</color>
|
||||||
<color name="black">#FF000000</color>
|
<color name="black">#FF000000</color>
|
||||||
<color name="white">#FFFFFFFF</color>
|
<color name="white">#FFFFFFFF</color>
|
||||||
|
<color name="text">#222222</color>
|
||||||
</resources>
|
</resources>
|
|
@ -1,6 +1,6 @@
|
||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">Comet</string>
|
<string name="app_name">Comet</string>
|
||||||
<string name="alert_title">Alert</string>
|
<string name="error_alert_title">Error</string>
|
||||||
<string name="action_settings">Settings</string>
|
<string name="action_settings">Settings</string>
|
||||||
<string name="url">URL</string>
|
<string name="url">URL</string>
|
||||||
</resources>
|
</resources>
|
|
@ -9,6 +9,8 @@
|
||||||
<item name="colorSecondary">@color/teal_200</item>
|
<item name="colorSecondary">@color/teal_200</item>
|
||||||
<item name="colorSecondaryVariant">@color/teal_700</item>
|
<item name="colorSecondaryVariant">@color/teal_700</item>
|
||||||
<item name="colorOnSecondary">@color/black</item>
|
<item name="colorOnSecondary">@color/black</item>
|
||||||
|
<!-- Status bar color. -->
|
||||||
|
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
|
||||||
<!-- Customize your theme here. -->
|
<!-- Customize your theme here. -->
|
||||||
</style>
|
</style>
|
||||||
</resources>
|
</resources>
|
|
@ -1,17 +0,0 @@
|
||||||
package dev.lowrespalmtree.comet
|
|
||||||
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
import org.junit.Assert.*
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Example local unit test, which will execute on the development machine (host).
|
|
||||||
*
|
|
||||||
* See [testing documentation](http://d.android.com/tools/testing).
|
|
||||||
*/
|
|
||||||
class ExampleUnitTest {
|
|
||||||
@Test
|
|
||||||
fun addition_isCorrect() {
|
|
||||||
assertEquals(4, 2 + 2)
|
|
||||||
}
|
|
||||||
}
|
|
Reference in a new issue