Compare commits

...

3 Commits

Author SHA1 Message Date
yova 482c3e9756 toggle themes
3 months ago
yova 96c2fefd0d better read
3 months ago
yova 2b816a923d run openInputstream in IO context
3 months ago

@ -7,6 +7,7 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.net.Uri.parse
import android.os.Build
import android.os.Bundle
import android.os.ParcelFileDescriptor
import android.provider.DocumentsContract
@ -16,6 +17,8 @@ import android.util.Log
import android.view.Menu
import android.view.Menu.NONE
import android.view.MenuItem
import android.view.WindowInsets
import android.view.WindowManager
import android.webkit.ConsoleMessage
import android.webkit.JavascriptInterface
import android.webkit.ValueCallback
@ -169,17 +172,43 @@ class MainActivity : AppCompatActivity() {
@JavascriptInterface
fun getCursor(): String {
Log.i(javaClass.simpleName,"delivering cursor: ${metaData.cursor}")
val cursor = JSONObject(JSONObject(metaData.cursor), arrayOf("ch", "line"))
Log.i(javaClass.simpleName,"delivering cursor: $cursor")
return cursor.toString()
}
@JavascriptInterface
fun isFullscreen() : Boolean{
return supportActionBar!!.isShowing
}
@JavascriptInterface
fun toggleBar() {
this@MainActivity.runOnUiThread({
if (supportActionBar!!.isShowing) supportActionBar!!.hide()
else supportActionBar!!.show()
if (supportActionBar!!.isShowing) {
@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.hide(WindowInsets.Type.statusBars())
} else {
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
0
)
}
supportActionBar!!.hide()
}
else {
@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.show(WindowInsets.Type.statusBars())
} else {
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
supportActionBar!!.show()
}
})
}
}
@ -228,28 +257,18 @@ class MainActivity : AppCompatActivity() {
super.onResume()
loadMetaFromSharedPrefs()
CoroutineScope(Dispatchers.Main).launch {
withContext(Dispatchers.IO) {
//if (contentResolver.getType(parse(intent.dataString))!!.split("/")[0] == "text" && intentScheme == "content") {
if (intent.data != null) {
if (contentResolver.getType(parse(intent.dataString))!!.split("/")[0] == "text") {
readFile(intent.data!!)
intent.data = null
}
} else { // No start with intent data. Load previously used file
if (thisFileUri !== null) readFile(thisFileUri!!)
}
}
webView.evaluateJavascript("onRead();") {}
if (thisFileUri !== null) {
readFile(thisFileUri!!)
Log.d(javaClass.simpleName,"Loading on resume from thisFileUri: ${thisFileUri}")
}
if (metaData.metaData.get("url") !== null) {
val url = parse(metaData.metaData.get("url"))
deleteVisible = true
invalidateOptionsMenu()
val apiHost = url.scheme + "://" + url.host
Log.i(javaClass.simpleName, "Starting api controller for: $apiHost")
api = ghostAPI(applicationContext, apiHost)
}
if (metaData.metaData.get("url") !== null) {
val url = parse(metaData.metaData.get("url"))
deleteVisible = true
invalidateOptionsMenu()
val apiHost = url.scheme + "://" + url.host
Log.i(javaClass.simpleName, "Starting api controller for: $apiHost")
api = ghostAPI(applicationContext, apiHost)
}
}
override fun onNewIntent(intent:Intent) {
@ -281,7 +300,7 @@ class MainActivity : AppCompatActivity() {
}
}
}
Log.i(javaClass.simpleName,"Uri: $uri")
Log.i(javaClass.simpleName,"Got a new intent with URI: $uri")
if (uri != null && uri.toString()!="null") {
intentScheme = uri.scheme!!
if (intentScheme == "content") {
@ -296,7 +315,6 @@ class MainActivity : AppCompatActivity() {
if (intentScheme == "link" && uri.toString() !="null") {
mdToAppend += "[](${uri})\n"
webView.evaluateJavascript("onRead();"){}
intent.putExtra(Intent.EXTRA_TEXT, null as CharSequence?)
}
if (intentScheme == "content") {
@ -304,12 +322,7 @@ class MainActivity : AppCompatActivity() {
pushImage(uri)
intent.putExtra(Intent.EXTRA_STREAM, null as Uri?)
} else if (mimeType == "text" && intent.data != null ) {
CoroutineScope(Dispatchers.Main).launch {
withContext(Dispatchers.IO) {
readFile(intent.data!!)
}
webView.evaluateJavascript("onRead();") {}
}
readFile(intent.data!!)
}
}
}
@ -831,8 +844,8 @@ class MainActivity : AppCompatActivity() {
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION and Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
readFile(uri)
thisFileUri = uri
saveMetaToSharedPrefs()
if (metaData.metaData.get("url") == null) {
deleteVisible = false
invalidateOptionsMenu()
@ -990,48 +1003,59 @@ class MainActivity : AppCompatActivity() {
).show()
})
}
try {
contentResolver.openInputStream(uri)?.use { inputStream ->
BufferedReader(InputStreamReader(inputStream)).use { reader ->
var i = ""
if (reader.ready()) {
i = reader.readLine()
var line: String? = ""
while (line != null) {
i += line + '\n'
line = reader.readLine()
CoroutineScope(Dispatchers.Main).launch {
withContext(Dispatchers.IO) {
try {
contentResolver.openInputStream(uri)?.use { inputStream ->
BufferedReader(InputStreamReader(inputStream)).use { reader ->
var i = ""
if (reader.ready()) {
i = reader.readLine()
var line: String? = ""
while (line != null) {
i += line + '\n'
line = reader.readLine()
}
}
mdeValue = metaData.extractMetadataFromMarkdown(i)
}
mdeValue = metaData.extractMetadataFromMarkdown(i)
}
}
catch (e: java.io.FileNotFoundException) {
Log.d(javaClass.simpleName, "File not found during reading:\n${e.stackTraceToString()}")
this@MainActivity.runOnUiThread({
selectFileForSaveAs()
Toast.makeText(
this@MainActivity,
"File not found during reading.\n$e",
Toast.LENGTH_LONG
).show()
})
return@withContext
}
catch (e: Exception) {
Log.d(javaClass.simpleName, e.stackTraceToString())
this@MainActivity.runOnUiThread({
Toast.makeText(
this@MainActivity,
"Error during reading.\n$e",
Toast.LENGTH_LONG
).show()
})
return@withContext
}
this@MainActivity.runOnUiThread({
val script = "easyMDE.codemirror.doc.setValue(`${mdeValue}`);" +
"easyMDE.codemirror.doc.setCursor(JSON.parse(`${JSONObject(JSONObject(metaData.cursor), arrayOf("ch", "line"))}`));" +
"pasteText();"
Log.d(javaClass.simpleName, "executing in webview:\n${script}")
webView.evaluateJavascript(script, {})
})
thisFileUri = uri
saveMetaToSharedPrefs()
Log.d(javaClass.simpleName,"File read: ${thisFileUri}")
}
} catch (e: java.io.FileNotFoundException) {
Log.d(javaClass.simpleName, "File not found during reading:\n${e.stackTraceToString()}")
this.runOnUiThread({
selectFileForSaveAs()
Toast.makeText(
this,
"File not found during reading.\n$e",
Toast.LENGTH_LONG
).show()
})
}
catch (e: Exception) {
Log.d(javaClass.simpleName, e.stackTraceToString())
this.runOnUiThread({
Toast.makeText(
this,
"Error during reading.\n$e",
Toast.LENGTH_LONG
).show()
})
return false
}
thisFileUri = uri
saveMetaToSharedPrefs()
this.runOnUiThread({
webView.evaluateJavascript("onRead();", {})
})
return true
}

@ -2,7 +2,6 @@ function onRead() {
easyMDE.codemirror.doc.setValue(Android.getValue())
easyMDE.codemirror.doc.markClean()
easyMDE.codemirror.focus()
console.log(Android.getCursor())
easyMDE.codemirror.doc.setCursor(JSON.parse(Android.getCursor()))
pasteText()
}
@ -68,15 +67,29 @@ function pasteText() {
document.getElementsByClassName("CodeMirror-scroll")[0].dispatchEvent(event);
saveFile()
}
windowHeight = window.innerHeight
function toggleBar() {
if (Android.isFullscreen()) {
easyMDE.codemirror.getScrollerElement().style.height=String(windowHeight-40)+"px"
} else {
easyMDE.codemirror.getScrollerElement().style.height=String(windowHeight-120)+"px"
}
Android.toggleBar()
easyMDE.codemirror.focus()
}
themes = [ "solarized", "3024-night"]
i=1
function toggleTheme() {
easyMDE.codemirror.setOption("theme",themes[i])
i++
if (i>themes.length-1) i=0
}
const easyMDE = new EasyMDE({
spellChecker: false,
nativeSpellcheck: false,
maxHeight: String(window.innerHeight-120)+"px",
maxHeight: String(windowHeight-120)+"px",
inputStyle: "textarea",
autoDownloadFontAwesome: false,
theme: "solarized",
@ -103,24 +116,17 @@ const easyMDE = new EasyMDE({
],
toolbar: [
{
name: "day",
action: () => easyMDE.codemirror.setOption("theme","solarized"),
className: "fa fa-sun",
title: "Day Theme"
},
{
name: "night",
action: () => easyMDE.codemirror.setOption("theme","3024-night"),
name: "toggleTheme",
action: toggleTheme,
className: "fa fa-moon",
title: "Night Theme"
title: "Toggle Theme"
},
"guide",
{
name: "share",
action: shareText,
className: "fa fa-share-nodes",
title: "Share"
}, "undo",
},"strikethrough", "horizontal-rule","undo",
{
name: "preview",
action: myPreview,

Loading…
Cancel
Save