Skip to content

Commit 810f10c

Browse files
committed
adding Print support
1 parent 84967ac commit 810f10c

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ android {
5757
}
5858

5959
dependencies {
60-
implementation 'com.simplemobiletools:commons:5.31.0'
60+
implementation 'com.simplemobiletools:commons:5.31.13'
6161
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
6262
implementation 'androidx.documentfile:documentfile:1.0.1'
6363

app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.simplemobiletools.notes.pro.activities
22

33
import android.app.Activity
4+
import android.content.Context
45
import android.content.Intent
56
import android.net.Uri
67
import android.os.Bundle
8+
import android.print.PrintAttributes
9+
import android.print.PrintManager
710
import android.text.method.ArrowKeyMovementMethod
811
import android.text.method.LinkMovementMethod
912
import android.util.TypedValue
@@ -12,6 +15,9 @@ import android.view.Gravity
1215
import android.view.Menu
1316
import android.view.MenuItem
1417
import android.view.inputmethod.EditorInfo
18+
import android.webkit.WebResourceRequest
19+
import android.webkit.WebView
20+
import android.webkit.WebViewClient
1521
import android.widget.ImageView
1622
import android.widget.TextView
1723
import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog
@@ -175,6 +181,7 @@ class MainActivity : SimpleActivity() {
175181
R.id.import_folder -> openFolder()
176182
R.id.export_as_file -> tryExportAsFile()
177183
R.id.export_all_notes -> tryExportAllNotes()
184+
R.id.print -> printText()
178185
R.id.delete_note -> displayDeleteNotePrompt()
179186
R.id.settings -> startActivity(Intent(applicationContext, SettingsActivity::class.java))
180187
R.id.about -> launchAbout()
@@ -842,6 +849,32 @@ class MainActivity : SimpleActivity() {
842849
}
843850
}
844851

852+
private fun printText() {
853+
try {
854+
val webView = WebView(this)
855+
webView.webViewClient = object : WebViewClient() {
856+
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest) = false
857+
858+
override fun onPageFinished(view: WebView, url: String) {
859+
createWebPrintJob(view)
860+
}
861+
}
862+
863+
webView.loadData(getPrintableText(), "text/plain", "UTF-8")
864+
} catch (e: Exception) {
865+
showErrorToast(e)
866+
}
867+
}
868+
869+
private fun createWebPrintJob(webView: WebView) {
870+
val jobName = mCurrentNote.title
871+
val printAdapter = webView.createPrintDocumentAdapter(jobName)
872+
873+
(getSystemService(Context.PRINT_SERVICE) as? PrintManager)?.apply {
874+
print(jobName, printAdapter, PrintAttributes.Builder().build())
875+
}
876+
}
877+
845878
private fun getPagerAdapter() = view_pager.adapter as NotesPagerAdapter
846879

847880
private fun getCurrentNoteText() = getPagerAdapter().getCurrentNoteViewText(view_pager.currentItem)
@@ -854,6 +887,18 @@ class MainActivity : SimpleActivity() {
854887
}
855888
}
856889

890+
private fun getPrintableText(): String {
891+
return if (mCurrentNote.type == NoteType.TYPE_TEXT.value) {
892+
getCurrentNoteText() ?: ""
893+
} else {
894+
var printableText = ""
895+
getPagerAdapter().getNoteChecklistRawItems(view_pager.currentItem)?.forEach {
896+
printableText += "${it.title}\n\n"
897+
}
898+
printableText
899+
}
900+
}
901+
857902
private fun addTextToCurrentNote(text: String) = getPagerAdapter().appendText(view_pager.currentItem, text)
858903

859904
private fun saveCurrentNote(force: Boolean) {

app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity
6363

6464
fun saveAllFragmentTexts() = fragments.values.forEach { (it as? TextFragment)?.saveText(false) }
6565

66+
fun getNoteChecklistRawItems(position: Int) = (fragments[position] as? ChecklistFragment)?.items
67+
6668
fun getNoteChecklistItems(position: Int) = (fragments[position] as? ChecklistFragment)?.checklistItems
6769

6870
fun undo(position: Int) = (fragments[position] as? TextFragment)?.undo()

app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ import kotlinx.android.synthetic.main.fragment_checklist.view.*
2626
class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
2727

2828
private var noteId = 0L
29-
private var items = ArrayList<ChecklistItem>()
3029
private var note: Note? = null
3130

3231
lateinit var view: ViewGroup
3332

33+
var items = ArrayList<ChecklistItem>()
3434
val checklistItems get(): String = Gson().toJson(items)
3535

3636
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

app/src/main/res/menu/menu.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
android:id="@+id/export_all_notes"
5858
android:title="@string/export_all_notes"
5959
app:showAsAction="never"/>
60+
<item
61+
android:id="@+id/print"
62+
android:title="@string/print"
63+
app:showAsAction="never"/>
6064
<item
6165
android:id="@+id/delete_note"
6266
android:icon="@drawable/ic_delete_vector"

0 commit comments

Comments
 (0)