Skip to content

Commit

Permalink
Merge pull request #145 from SecUSo/bugfix/sketch-crash
Browse files Browse the repository at this point in the history
v1.4.2 Bugfix sharing notes
  • Loading branch information
coderPaddyS authored May 9, 2023
2 parents 16e0eef + ab7845e commit b9bb07c
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 67 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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<View>(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)
}
Expand Down Expand Up @@ -115,7 +115,7 @@ class AudioNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_AUDIO) {
}
}

override fun shareNote(name: String): Intent {
override fun shareNote(name: String): ActionResult<Intent, Int> {
val audioFile = File(mFilePath)
val contentUri = FileProvider.getUriForFile(
applicationContext,
Expand All @@ -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<Boolean, Int> {
Expand Down Expand Up @@ -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<Note, Int> {
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<Note, Int> {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Note, Int>
protected abstract fun updateNoteToSave(name: String, category: Int): ActionResult<Note, Int>
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<Intent, Int>
protected abstract fun onNoteLoadedFromDB(note: Note)
protected abstract fun onNewNote()
protected abstract fun determineToSave(title: String, category: Int): Pair<Boolean, Int>
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -362,35 +368,40 @@ 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())
}
if (edit) updateNote()
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?) {
Expand Down Expand Up @@ -539,4 +550,13 @@ abstract class BaseNoteActivity(noteType: Int) : AppCompatActivity(), View.OnCli
showAlertScheduledToast(this, dayOfMonth, monthOfYear, year, hourOfDay, minute)
loadActivity(false)
}

class ActionResult<O, E>(private val status: Boolean, val ok: O?, val err: E? = null) {
fun isOk(): Boolean {
return this.status
}
fun isErr(): Boolean {
return !this.status
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ class ChecklistNoteActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_CHECKLI
)
}

override fun shareNote(name: String): Intent {
override fun shareNote(name: String): ActionResult<Intent, Int> {
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) {
Expand All @@ -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<Note, Int> {
val a: Adapter = lvItemList.adapter
val jsonArray = JSONArray()
try {
Expand All @@ -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<Note, Int> {
val a: Adapter = lvItemList.adapter
val jsonArray = JSONArray()
try {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH) {
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
)
}

override fun onCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.activity_sketch)
Expand All @@ -62,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() {
Expand All @@ -71,21 +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<Intent, Int> {
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(map, drawView.bitmap)
val canvas = Canvas(bm)
canvas.drawColor(Color.WHITE)
canvas.drawBitmap(
overlay(
BitmapDrawable(
resources, mFilePath
).bitmap, drawView.bitmap
map,
drawView.bitmap
), 0f, 0f, null
)
try {
Expand All @@ -103,18 +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<Boolean, Int> {
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),
sketchLoaded || !drawView.bitmap.sameAs(emptyBitmap()) && -5 != intent.getIntExtra(EXTRA_CATEGORY, -5),
R.string.toast_emptyNote
)
}
Expand All @@ -126,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<Note, Int> {
val oldSketch = BitmapDrawable(resources, mFilePath).bitmap
val newSketch = drawView.bitmap
try {
Expand All @@ -139,15 +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? {
val emptyBitmap = Bitmap.createBitmap(
drawView.bitmap.width,
drawView.bitmap.height,
drawView.bitmap.config
)
override fun noteToSave(name: String, category: Int): ActionResult<Note, Int> {
val bitmap = drawView.bitmap
try {
val fo = FileOutputStream(File(mFilePath!!))
Expand All @@ -159,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() {
Expand Down Expand Up @@ -224,11 +221,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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Intent, Int> {
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<Boolean, Int> {
Expand Down Expand Up @@ -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<Note, Int> {
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<Note, Int> {
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))
}
}

Expand Down

0 comments on commit b9bb07c

Please sign in to comment.