From b2a458dd91cfeeaa8248420ffa2d5ee3b4404c56 Mon Sep 17 00:00:00 2001 From: Patrick Schneider Date: Wed, 3 May 2023 22:01:41 +0200 Subject: [PATCH 1/2] Fixes bug where bitmap is null --- .../ui/notes/SketchActivity.kt | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/SketchActivity.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/SketchActivity.kt index a2884372..535cfb3f 100644 --- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/SketchActivity.kt +++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/SketchActivity.kt @@ -42,6 +42,13 @@ import java.io.IOException */ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH) { private val drawView: InkView by lazy { findViewById(R.id.draw_view) } + private val emptyBitmap by lazy { + Bitmap.createBitmap( + drawView.bitmap.width, + drawView.bitmap.height, + drawView.bitmap.config + ) + } private val btnColorSelector: Button by lazy { findViewById(R.id.btn_color_selector) } private var mFileName = "finde_die_datei.mp4" private var mFilePath: String? = null @@ -74,18 +81,15 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH) { override fun shareNote(name: String): Intent { val tempPath = mFilePath!!.substring(0, mFilePath!!.length - 3) + "jpg" val sketchFile = File(tempPath) - val bm = overlay( - BitmapDrawable( - resources, mFilePath - ).bitmap, drawView.bitmap - ) + + val map = BitmapDrawable(resources, mFilePath).bitmap ?: emptyBitmap + val bm = overlay(drawView.bitmap, map) val canvas = Canvas(bm) canvas.drawColor(Color.WHITE) canvas.drawBitmap( overlay( - BitmapDrawable( - resources, mFilePath - ).bitmap, drawView.bitmap + drawView.bitmap, + map ), 0f, 0f, null ) try { @@ -107,11 +111,6 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH) { } override fun determineToSave(title: String, category: Int): Pair { - val emptyBitmap = Bitmap.createBitmap( - drawView.bitmap.width, - drawView.bitmap.height, - drawView.bitmap.config - ) val intent = intent return Pair( !drawView.bitmap.sameAs(emptyBitmap) && -5 != intent.getIntExtra(EXTRA_CATEGORY, -5), @@ -143,11 +142,6 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH) { } override fun noteToSave(name: String, category: Int): Note? { - val emptyBitmap = Bitmap.createBitmap( - drawView.bitmap.width, - drawView.bitmap.height, - drawView.bitmap.config - ) val bitmap = drawView.bitmap try { val fo = FileOutputStream(File(mFilePath!!)) @@ -224,11 +218,11 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH) { companion object { //taken from http://stackoverflow.com/a/10616868 - fun overlay(bmp1: Bitmap, bmp2: Bitmap?): Bitmap { + fun overlay(bmp1: Bitmap, bmp2: Bitmap): Bitmap { val bmOverlay = Bitmap.createBitmap(bmp1.width, bmp1.height, bmp1.config) val canvas = Canvas(bmOverlay) canvas.drawBitmap(bmp1, Matrix(), null) - canvas.drawBitmap(bmp2!!, 0f, 0f, null) + canvas.drawBitmap(bmp2, 0f, 0f, null) return bmOverlay } } From ab7845e7c85ffeb8add31566493fafe5e0cc1b29 Mon Sep 17 00:00:00 2001 From: Patrick Schneider Date: Mon, 8 May 2023 23:57:50 +0200 Subject: [PATCH 2/2] Refactors the result of note actions. --- app/build.gradle | 4 +- .../ui/notes/AudioNoteActivity.kt | 20 ++++----- .../ui/notes/BaseNoteActivity.kt | 44 ++++++++++++++----- .../ui/notes/ChecklistNoteActivity.kt | 14 +++--- .../ui/notes/SketchActivity.kt | 39 ++++++++-------- .../ui/notes/TextNoteActivity.kt | 14 +++--- 6 files changed, 79 insertions(+), 56 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 1b820428..1704728b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -24,8 +24,8 @@ android { applicationId "org.secuso.privacyfriendlynotes" minSdkVersion 21 targetSdkVersion 32 - versionCode 14 - versionName "1.4.1" + versionCode 15 + versionName "1.4.2" javaCompileOptions { annotationProcessorOptions { diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/AudioNoteActivity.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/AudioNoteActivity.kt index 1d5d852d..b0b73e0a 100644 --- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/AudioNoteActivity.kt +++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/AudioNoteActivity.kt @@ -52,7 +52,7 @@ class AudioNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_AUDIO) { private val btnRecord: ImageButton by lazy { findViewById(R.id.btn_record) } private val tvRecordingTime: TextView by lazy { findViewById(R.id.recording_time) } private val seekBar: SeekBar by lazy { findViewById(R.id.seekbar) } - + private var mRecorder: MediaRecorder? = null private var mPlayer: MediaPlayer? = null private val mHandler = Handler() @@ -62,13 +62,13 @@ class AudioNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_AUDIO) { private var playing = false private var isEmpty = true private var startTime = System.currentTimeMillis() - + override fun onCreate(savedInstanceState: Bundle?) { setContentView(R.layout.activity_audio_note) findViewById(R.id.btn_record).setOnClickListener(this) btnPlayPause.setOnClickListener(this) - + if (ContextCompat.checkSelfPermission(this@AudioNoteActivity, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this@AudioNoteActivity, arrayOf(Manifest.permission.RECORD_AUDIO), REQUEST_CODE_AUDIO) } @@ -115,7 +115,7 @@ class AudioNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_AUDIO) { } } - override fun shareNote(name: String): Intent { + override fun shareNote(name: String): ActionResult { val audioFile = File(mFilePath) val contentUri = FileProvider.getUriForFile( applicationContext, @@ -127,7 +127,7 @@ class AudioNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_AUDIO) { sendIntent.type = "audio/*" sendIntent.putExtra(Intent.EXTRA_STREAM, contentUri) sendIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION - return sendIntent + return ActionResult(true, sendIntent) } override fun determineToSave(title: String, category: Int): Pair { @@ -260,15 +260,15 @@ class AudioNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_AUDIO) { } } - override fun updateNoteToSave(name: String, category: Int): Note { - return Note(name, mFileName, DbContract.NoteEntry.TYPE_AUDIO, category) + override fun updateNoteToSave(name: String, category: Int): ActionResult { + return ActionResult(true, Note(name, mFileName, DbContract.NoteEntry.TYPE_AUDIO, category)) } - override fun noteToSave(name: String, category: Int): Note? { + override fun noteToSave(name: String, category: Int): ActionResult { if (isEmpty) { - return null + return ActionResult(false, null, null) } - return Note(name, mFileName, DbContract.NoteEntry.TYPE_AUDIO, category) + return ActionResult(true, Note(name, mFileName, DbContract.NoteEntry.TYPE_AUDIO, category)) } override fun onSaveExternalStorage(basePath: File, name: String) { diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/BaseNoteActivity.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/BaseNoteActivity.kt index 5244f897..00a654e1 100644 --- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/BaseNoteActivity.kt +++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/BaseNoteActivity.kt @@ -87,11 +87,11 @@ abstract class BaseNoteActivity(noteType: Int) : AppCompatActivity(), View.OnCli private val noteType by lazy { noteType } - protected abstract fun noteToSave(name: String, category: Int): Note? - protected abstract fun updateNoteToSave(name: String, category: Int): Note? + protected abstract fun noteToSave(name: String, category: Int): ActionResult + protected abstract fun updateNoteToSave(name: String, category: Int): ActionResult protected abstract fun onLoadActivity() protected abstract fun onSaveExternalStorage(basePath: File, name: String) - protected abstract fun shareNote(name: String): Intent + protected abstract fun shareNote(name: String): ActionResult protected abstract fun onNoteLoadedFromDB(note: Note) protected abstract fun onNewNote() protected abstract fun determineToSave(title: String, category: Int): Pair @@ -273,8 +273,14 @@ abstract class BaseNoteActivity(noteType: Int) : AppCompatActivity(), View.OnCli return true } R.id.action_share -> { - saveOrUpdateNote() - startActivity(Intent.createChooser(shareNote(etName.text.toString()), null)) + val result = shareNote(etName.text.toString()) + if (saveOrUpdateNote()) { + if (result.isOk()) { + startActivity(Intent.createChooser(result.ok, null)) + } else { + Toast.makeText(applicationContext, result.err!!, Toast.LENGTH_SHORT).show() + } + } } R.id.action_delete -> { displayTrashDialog() @@ -362,9 +368,9 @@ abstract class BaseNoteActivity(noteType: Int) : AppCompatActivity(), View.OnCli } } - private fun saveOrUpdateNote() { + private fun saveOrUpdateNote(): Boolean { val (toSave, mes) = determineToSave(etName.text.toString(), if (currentCat >= 0) currentCat else savedCat) - if (toSave) { + return if (toSave) { if (etName.text.isEmpty()) { etName.setText(generateStandardName()) } @@ -372,25 +378,30 @@ abstract class BaseNoteActivity(noteType: Int) : AppCompatActivity(), View.OnCli else saveNote() } else { Toast.makeText(applicationContext, mes, Toast.LENGTH_SHORT).show() + false } } - private fun saveNote() { + private fun saveNote(): Boolean { val note = noteToSave( etName.text.toString(), if (currentCat >= 0) currentCat else savedCat ) - if (note != null) { - insertNoteIntoDB(note) + if (note.isOk()) { + insertNoteIntoDB(note.ok) } + return note.isOk() } - private fun updateNote() { + private fun updateNote(): Boolean { val note = updateNoteToSave( etName.text.toString(), if (currentCat >= 0) currentCat else savedCat ) - insertNoteIntoDB(note) + if (note.isOk()) { + insertNoteIntoDB(note.ok) + } + return note.isOk() } private fun insertNoteIntoDB(note: Note?) { @@ -539,4 +550,13 @@ abstract class BaseNoteActivity(noteType: Int) : AppCompatActivity(), View.OnCli showAlertScheduledToast(this, dayOfMonth, monthOfYear, year, hourOfDay, minute) loadActivity(false) } + + class ActionResult(private val status: Boolean, val ok: O?, val err: E? = null) { + fun isOk(): Boolean { + return this.status + } + fun isErr(): Boolean { + return !this.status + } + } } \ No newline at end of file diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/ChecklistNoteActivity.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/ChecklistNoteActivity.kt index aaafef7a..16aed7d5 100644 --- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/ChecklistNoteActivity.kt +++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/ChecklistNoteActivity.kt @@ -155,12 +155,12 @@ class ChecklistNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_CHECKLI ) } - override fun shareNote(name: String): Intent { + override fun shareNote(name: String): ActionResult { val sendIntent = Intent() sendIntent.action = Intent.ACTION_SEND sendIntent.type = "text/plain" sendIntent.putExtra(Intent.EXTRA_TEXT, "$name\n\n${getContentString()}") - return sendIntent + return ActionResult(true, sendIntent) } override fun onClick(v: View) { @@ -171,7 +171,7 @@ class ChecklistNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_CHECKLI } } - override fun updateNoteToSave(name: String, category: Int): Note { + override fun updateNoteToSave(name: String, category: Int): ActionResult { val a: Adapter = lvItemList.adapter val jsonArray = JSONArray() try { @@ -185,10 +185,10 @@ class ChecklistNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_CHECKLI } catch (e: JSONException) { e.printStackTrace() } - return Note(name, jsonArray.toString(), DbContract.NoteEntry.TYPE_CHECKLIST, category) + return ActionResult(true, Note(name, jsonArray.toString(), DbContract.NoteEntry.TYPE_CHECKLIST, category)) } - override fun noteToSave(name: String, category: Int): Note? { + override fun noteToSave(name: String, category: Int): ActionResult { val a: Adapter = lvItemList.adapter val jsonArray = JSONArray() try { @@ -203,9 +203,9 @@ class ChecklistNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_CHECKLI e.printStackTrace() } if (name.isEmpty() && jsonArray.length() == 0) { - return null + return ActionResult(false, null) } - return Note(name, jsonArray.toString(), DbContract.NoteEntry.TYPE_CHECKLIST, category) + return ActionResult(true, Note(name, jsonArray.toString(), DbContract.NoteEntry.TYPE_CHECKLIST, category)) } override fun onSaveExternalStorage(basePath: File, name: String) { diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/SketchActivity.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/SketchActivity.kt index 535cfb3f..3fd94fed 100644 --- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/SketchActivity.kt +++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/SketchActivity.kt @@ -42,16 +42,18 @@ import java.io.IOException */ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH) { private val drawView: InkView by lazy { findViewById(R.id.draw_view) } - private val emptyBitmap by lazy { - Bitmap.createBitmap( + private val btnColorSelector: Button by lazy { findViewById(R.id.btn_color_selector) } + private var mFileName = "finde_die_datei.mp4" + private var mFilePath: String? = null + private var sketchLoaded = false + + private fun emptyBitmap(): Bitmap { + return Bitmap.createBitmap( drawView.bitmap.width, drawView.bitmap.height, drawView.bitmap.config ) } - private val btnColorSelector: Button by lazy { findViewById(R.id.btn_color_selector) } - private var mFileName = "finde_die_datei.mp4" - private var mFilePath: String? = null override fun onCreate(savedInstanceState: Bundle?) { setContentView(R.layout.activity_sketch) @@ -69,6 +71,7 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH) { mFileName = note.content mFilePath = filesDir.path + "/sketches" + mFileName drawView.background = BitmapDrawable(resources, mFilePath) + sketchLoaded = true } override fun onNewNote() { @@ -78,18 +81,18 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH) { mFilePath = filesDir.path + "/sketches" + mFileName } - override fun shareNote(name: String): Intent { + override fun shareNote(name: String): ActionResult { val tempPath = mFilePath!!.substring(0, mFilePath!!.length - 3) + "jpg" val sketchFile = File(tempPath) - val map = BitmapDrawable(resources, mFilePath).bitmap ?: emptyBitmap - val bm = overlay(drawView.bitmap, map) + val map = BitmapDrawable(resources, mFilePath).bitmap ?: emptyBitmap() + val bm = overlay(map, drawView.bitmap) val canvas = Canvas(bm) canvas.drawColor(Color.WHITE) canvas.drawBitmap( overlay( - drawView.bitmap, - map + map, + drawView.bitmap ), 0f, 0f, null ) try { @@ -107,13 +110,13 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH) { sendIntent.type = "image/*" sendIntent.putExtra(Intent.EXTRA_STREAM, contentUri) sendIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION - return sendIntent + return ActionResult(true, sendIntent) } override fun determineToSave(title: String, category: Int): Pair { val intent = intent return Pair( - !drawView.bitmap.sameAs(emptyBitmap) && -5 != intent.getIntExtra(EXTRA_CATEGORY, -5), + sketchLoaded || !drawView.bitmap.sameAs(emptyBitmap()) && -5 != intent.getIntExtra(EXTRA_CATEGORY, -5), R.string.toast_emptyNote ) } @@ -125,7 +128,7 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH) { } } - override fun updateNoteToSave(name: String, category: Int): Note { + override fun updateNoteToSave(name: String, category: Int): ActionResult { val oldSketch = BitmapDrawable(resources, mFilePath).bitmap val newSketch = drawView.bitmap try { @@ -138,10 +141,10 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH) { } catch (e: IOException) { e.printStackTrace() } - return Note(name, mFileName, DbContract.NoteEntry.TYPE_SKETCH, category) + return ActionResult(true, Note(name, mFileName, DbContract.NoteEntry.TYPE_SKETCH, category)) } - override fun noteToSave(name: String, category: Int): Note? { + override fun noteToSave(name: String, category: Int): ActionResult { val bitmap = drawView.bitmap try { val fo = FileOutputStream(File(mFilePath!!)) @@ -153,10 +156,10 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH) { } catch (e: IOException) { e.printStackTrace() } - if (name.isEmpty() && bitmap.sameAs(emptyBitmap)) { - return null + if (name.isEmpty() && bitmap.sameAs(emptyBitmap())) { + return ActionResult(false, null) } - return Note(name, mFileName, DbContract.NoteEntry.TYPE_SKETCH, category) + return ActionResult(true, Note(name, mFileName, DbContract.NoteEntry.TYPE_SKETCH, category)) } private fun displayColorDialog() { diff --git a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/TextNoteActivity.kt b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/TextNoteActivity.kt index 46fd5714..d8e1420b 100644 --- a/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/TextNoteActivity.kt +++ b/app/src/main/java/org/secuso/privacyfriendlynotes/ui/notes/TextNoteActivity.kt @@ -93,12 +93,12 @@ class TextNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_TEXT) { adaptFontSize(etContent) } - public override fun shareNote(name: String): Intent { + public override fun shareNote(name: String): ActionResult { val sendIntent = Intent() sendIntent.action = Intent.ACTION_SEND sendIntent.type = "text/plain" sendIntent.putExtra(Intent.EXTRA_TEXT, "$name \n\n ${etContent.text}") - return sendIntent + return ActionResult(true, sendIntent) } override fun determineToSave(title: String, category: Int): Pair { @@ -350,15 +350,15 @@ class TextNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_TEXT) { etContent.setSelection(startSelection) } - override fun updateNoteToSave(name: String, category: Int): Note { - return Note(name, Html.toHtml(etContent.text), DbContract.NoteEntry.TYPE_TEXT, category) + override fun updateNoteToSave(name: String, category: Int): ActionResult { + return ActionResult(true, Note(name, Html.toHtml(etContent.text), DbContract.NoteEntry.TYPE_TEXT, category)) } - override fun noteToSave(name: String, category: Int): Note? { + override fun noteToSave(name: String, category: Int): ActionResult { return if (name.isEmpty() && etContent.text.toString().isEmpty()) { - null + ActionResult(false, null) } else { - Note(name, Html.toHtml(etContent.text), DbContract.NoteEntry.TYPE_TEXT, category) + ActionResult(true, Note(name, Html.toHtml(etContent.text), DbContract.NoteEntry.TYPE_TEXT, category)) } }