diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 5b15fb2..83e8dde 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -1,2 +1,6 @@ -keep class * extends android.webkit.WebChromeClient { *; } -dontwarn im.delight.android.webview.** +-keepclassmembers class telegra.ph.Editor$MyJavaScriptInterface { + public *; +} +-keepattributes JavascriptInterface \ No newline at end of file diff --git a/app/src/main/assets/editor.html b/app/src/main/assets/editor.html new file mode 100644 index 0000000..e19a9b3 --- /dev/null +++ b/app/src/main/assets/editor.html @@ -0,0 +1,41 @@ + + + + + + + + + + +
+ + + + + + \ No newline at end of file diff --git a/app/src/main/java/telegra/ph/Editor.kt b/app/src/main/java/telegra/ph/Editor.kt new file mode 100644 index 0000000..fdf523f --- /dev/null +++ b/app/src/main/java/telegra/ph/Editor.kt @@ -0,0 +1,71 @@ +package telegra.ph + +import android.content.Context +import android.support.annotation.Keep +import android.util.AttributeSet +import android.webkit.JavascriptInterface +import android.webkit.WebSettings +import android.webkit.WebView +import android.webkit.WebViewClient + +class Editor : WebView { + private var text = "" + internal var context: Context + + constructor(context: Context) : super(context) { + this.context = context + init() + } + + constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { + this.context = context + init() + } + + constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) { + this.context = context + init() + } + + private fun init() { + this.settings.javaScriptEnabled = true + this.settings.cacheMode = WebSettings.LOAD_NO_CACHE + this.addJavascriptInterface(MyJavaScriptInterface(), "android") + this.settings.loadWithOverviewMode = true + this.settings.useWideViewPort = true + this.loadUrl("file:///android_asset/editor.html") + } + + private inner class MyJavaScriptInterface { + @JavascriptInterface + fun getText(html: String) { + text = html + } + } + + fun setText(html: String) { + setWebViewClient(object : WebViewClient() { + override fun onPageFinished(view: WebView, url: String) { + setText(html) + } + }) + this.loadUrl("javascript:$('#summernote').summernote('reset');") + this.loadUrl("javascript:$('#summernote').summernote('code', '" + html.replace("'", "\\'") + "');") + } + + fun getText(): String { + text = "P/%TE5XpkAijBc%LjA;_-pZcbiU25E6feX5y/n6qxCTmhprLrqC3H%^hU!%q2,k'm`SHheoW^'mQ~zW93,C?~GtYk!wi/&'3KxW8" + this.loadUrl("javascript:window.android.getText" + "(document.getElementsByClassName('note-editable')[0].innerHTML);") + var i = 0 + try { + while (text == "P/%TE5XpkAijBc%LjA;_-pZcbiU25E6feX5y/n6qxCTmhprLrqC3H%^hU!%q2,k'm`SHheoW^'mQ~zW93,C?~GtYk!wi/&'3KxW8" && i < 100) { + Thread.sleep(50) + i++ + } + } catch (e: Exception) { + text = "" + } + return text + } + +} \ No newline at end of file diff --git a/app/src/main/java/telegra/ph/MainActivity.kt b/app/src/main/java/telegra/ph/MainActivity.kt index 3587198..a293396 100644 --- a/app/src/main/java/telegra/ph/MainActivity.kt +++ b/app/src/main/java/telegra/ph/MainActivity.kt @@ -16,14 +16,30 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener { private val htmlHead = "" private val htmlEnd = "" - private val webView: AdvancedWebView? by lazy { findViewById(R.id.webView) as AdvancedWebView } + private val webView: AdvancedWebView? by lazy { findViewById(R.id.webView) as AdvancedWebView? } + private val editor: Editor? by lazy { findViewById(R.id.editor) as Editor? } private var url = "" + private var editorMode = true override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_main) + if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph")) loadPage(intent.dataString.split("/").last()) + else loadEditor() + } + private fun loadEditor() { + editorMode = true + invalidateOptionsMenu() + // Init + setContentView(R.layout.main_write) + } + + private fun loadPage(path: String) { + editorMode = false + invalidateOptionsMenu() + // Init + setContentView(R.layout.main_read) webView?.apply { setListener(this@MainActivity, this@MainActivity) setMixedContentAllowed(true) @@ -34,17 +50,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener { isVerticalScrollBarEnabled = false overScrollMode = View.OVER_SCROLL_NEVER } - - if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph")) - loadPage(intent.dataString.split("/").last()) - else loadEditor() - } - - private fun loadEditor() { - webView?.loadUrl(TELEGRAPH) - } - - private fun loadPage(path: String) { + // Load Api().getPage(path) { page -> page?.let { var html = htmlHead @@ -107,6 +113,13 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener { return true } + override fun onPrepareOptionsMenu(menu: Menu?): Boolean { + super.onPrepareOptionsMenu(menu) + menu?.findItem(R.id.create)?.isVisible = !editorMode + menu?.findItem(R.id.publish)?.isVisible = editorMode + return true + } + override fun onOptionsItemSelected(item: MenuItem): Boolean { return when (item.itemId) { R.id.create -> { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/main_read.xml similarity index 100% rename from app/src/main/res/layout/activity_main.xml rename to app/src/main/res/layout/main_read.xml diff --git a/app/src/main/res/layout/main_write.xml b/app/src/main/res/layout/main_write.xml new file mode 100644 index 0000000..300520e --- /dev/null +++ b/app/src/main/res/layout/main_write.xml @@ -0,0 +1,6 @@ + + \ No newline at end of file diff --git a/app/src/main/res/menu/activity_main.xml b/app/src/main/res/menu/activity_main.xml index def311a..a2e0a1d 100644 --- a/app/src/main/res/menu/activity_main.xml +++ b/app/src/main/res/menu/activity_main.xml @@ -5,6 +5,10 @@ android:id="@+id/create" android:title="@string/create" app:showAsAction="ifRoom"/> + Awesome Post #1 Delete Do you really want to delete this? + Publish