adding a Print function at the editor
This commit is contained in:
parent
e6b4b477b8
commit
3a520b427d
|
@ -4,8 +4,13 @@ import android.app.SearchManager
|
|||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.print.PrintAttributes
|
||||
import android.print.PrintManager
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.webkit.WebResourceRequest
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
import androidx.appcompat.widget.SearchView
|
||||
import androidx.core.view.MenuItemCompat
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
|
@ -50,6 +55,7 @@ class ReadTextActivity : SimpleActivity() {
|
|||
when (item.itemId) {
|
||||
R.id.menu_save -> saveText()
|
||||
R.id.menu_open_with -> openPath(intent.dataString, true)
|
||||
R.id.menu_print -> printText()
|
||||
else -> return super.onOptionsItemSelected(item)
|
||||
}
|
||||
return true
|
||||
|
@ -57,7 +63,7 @@ class ReadTextActivity : SimpleActivity() {
|
|||
|
||||
private fun setupSearch(menu: Menu) {
|
||||
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
|
||||
searchMenuItem = menu.findItem(R.id.search)
|
||||
searchMenuItem = menu.findItem(R.id.menu_search)
|
||||
(searchMenuItem!!.actionView as SearchView).apply {
|
||||
setSearchableInfo(searchManager.getSearchableInfo(componentName))
|
||||
isSubmitButtonEnabled = false
|
||||
|
@ -110,6 +116,32 @@ class ReadTextActivity : SimpleActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
private fun printText() {
|
||||
try {
|
||||
val webView = WebView(this)
|
||||
webView.webViewClient = object : WebViewClient() {
|
||||
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest) = false
|
||||
|
||||
override fun onPageFinished(view: WebView, url: String) {
|
||||
createWebPrintJob(view)
|
||||
}
|
||||
}
|
||||
|
||||
webView.loadData(read_text_view.text.toString(), "text/plain", "UTF-8")
|
||||
} catch (e: Exception) {
|
||||
showErrorToast(e)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createWebPrintJob(webView: WebView) {
|
||||
val jobName = if (filePath.isNotEmpty()) filePath.getFilenameFromPath() else getString(R.string.app_name)
|
||||
val printAdapter = webView.createPrintDocumentAdapter(jobName)
|
||||
|
||||
(getSystemService(Context.PRINT_SERVICE) as? PrintManager)?.apply {
|
||||
print(jobName, printAdapter, PrintAttributes.Builder().build())
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkIntent() {
|
||||
read_text_view.setTextColor(config.textColor)
|
||||
val uri = if (intent.extras?.containsKey(REAL_FILE_PATH) == true) {
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<item
|
||||
android:id="@+id/search"
|
||||
android:id="@+id/menu_search"
|
||||
android:icon="@drawable/ic_search"
|
||||
android:title="@string/search"
|
||||
app:actionViewClass="androidx.appcompat.widget.SearchView"
|
||||
|
@ -16,4 +16,8 @@
|
|||
android:id="@+id/menu_open_with"
|
||||
android:title="@string/open_with"
|
||||
app:showAsAction="never"/>
|
||||
<item
|
||||
android:id="@+id/menu_print"
|
||||
android:title="@string/print"
|
||||
app:showAsAction="never"/>
|
||||
</menu>
|
||||
|
|
Loading…
Reference in New Issue