|
|
|
@ -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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|