Skip to content

Commit

Permalink
PAINTROID-427: Very high frequency crash hotfix (Catrobat#1111)
Browse files Browse the repository at this point in the history
https://jira.catrob.at/browse/PAINTROID-427

* PAINTROID-427 Very high frequency crash hotfix
  • Loading branch information
SebastianGrief authored Jun 2, 2022
1 parent 9a0d8f7 commit 5b86b2f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 33 deletions.
10 changes: 5 additions & 5 deletions Paintroid/src/main/java/org/catrobat/paintroid/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ class MainActivity : AppCompatActivity(), MainView, CommandListener {
onCreateDrawingSurface()
presenterMain.onCreateTool()

var isOpenedFromCatroid = false
val receivedIntent = intent
isTemporaryFileSavingTest = intent.getBooleanExtra("isTemporaryFileSavingTest", false)
when {
Expand All @@ -287,14 +286,15 @@ class MainActivity : AppCompatActivity(), MainView, CommandListener {
val picturePath = intent.getStringExtra(PAINTROID_PICTURE_PATH)
val pictureName = intent.getStringExtra(PAINTROID_PICTURE_NAME)
presenterMain.initializeFromCleanState(picturePath, pictureName)
if (presenterMain.checkForTemporaryFile() && (!isRunningEspressoTests || isTemporaryFileSavingTest)) {

if (!model.isOpenedFromCatroid && presenterMain.checkForTemporaryFile() && (!isRunningEspressoTests || isTemporaryFileSavingTest)) {
commandManager.loadCommandsCatrobatImage(presenterMain.openTemporaryFile(workspace))
}
}
else -> {
val isFullscreen = savedInstanceState.getBoolean(IS_FULLSCREEN_KEY, false)
val isSaved = savedInstanceState.getBoolean(IS_SAVED_KEY, false)
isOpenedFromCatroid =
val isOpenedFromCatroid =
savedInstanceState.getBoolean(IS_OPENED_FROM_CATROID_KEY, false)
val isOpenedFromFormulaEditorInCatroid = savedInstanceState.getBoolean(
IS_OPENED_FROM_FORMULA_EDITOR_IN_CATROID_KEY, false
Expand All @@ -310,12 +310,12 @@ class MainActivity : AppCompatActivity(), MainView, CommandListener {

commandManager.addCommandListener(this)
lastInteractionTime = System.currentTimeMillis()
if ((!isRunningEspressoTests || isTemporaryFileSavingTest) && !isOpenedFromCatroid) {
if ((!isRunningEspressoTests || isTemporaryFileSavingTest) && !model.isOpenedFromCatroid) {
startAutoSaveCoroutine()
}
presenterMain.finishInitialize()

if (!org.catrobat.paintroid.BuildConfig.DEBUG) {
if (!BuildConfig.DEBUG) {
val prefs = getSharedPreferences(SHARED_PREFS_NAME, 0)

if (prefs.getBoolean(FIRST_LAUNCH_AFTER_INSTALL, true)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import android.util.Log
import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.KryoException
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import org.catrobat.paintroid.command.Command
Expand Down Expand Up @@ -64,6 +66,7 @@ import org.catrobat.paintroid.tools.drawable.OvalDrawable
import org.catrobat.paintroid.tools.drawable.RectangleDrawable
import org.catrobat.paintroid.tools.drawable.ShapeDrawable
import org.catrobat.paintroid.tools.drawable.StarDrawable
import org.catrobat.paintroid.ui.DrawingSurfaceThread
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
Expand All @@ -76,6 +79,7 @@ class CommandSerializationUtilities(private val activityContext: Context, privat
companion object {
const val CURRENT_IMAGE_VERSION = 1
const val MAGIC_VALUE = "CATROBAT"
private val TAG = DrawingSurfaceThread::class.java.simpleName
}

val kryo = Kryo()
Expand Down Expand Up @@ -181,22 +185,27 @@ class CommandSerializationUtilities(private val activityContext: Context, privat
}
}

fun readFromInternalMemory(stream: FileInputStream): CommandManagerModel {
var commandModel: CommandManagerModel
fun readFromInternalMemory(stream: FileInputStream): CommandManagerModel? {
var commandModel: CommandManagerModel? = null

Input(stream).use { input ->
if (!input.readString().equals(MAGIC_VALUE)) {
throw NotCatrobatImageException("Magic Value doesn't exist.")
}
val imageVersion = input.readInt()
if (CURRENT_IMAGE_VERSION != imageVersion) {
setRegisterMapVersion(imageVersion)
registerClasses()
try {
Input(stream).use { input ->
if (!input.readString().equals(MAGIC_VALUE)) {
throw NotCatrobatImageException("Magic Value doesn't exist.")
}
val imageVersion = input.readInt()
if (CURRENT_IMAGE_VERSION != imageVersion) {
setRegisterMapVersion(imageVersion)
registerClasses()
}
commandModel = kryo.readObject(input, CommandManagerModel::class.java)
}
commandModel = kryo.readObject(input, CommandManagerModel::class.java)
} catch (ex: KryoException) {
Log.d(TAG, "KryoException while reading autosave: " + ex.message)
}

commandModel.commands.reverse()
commandModel?.commands?.reverse()

return commandModel
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,31 @@
package org.catrobat.paintroid.command.serialization

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.PointF
import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import org.catrobat.paintroid.command.implementation.SmudgePathCommand

class SmudgePathCommandSerializer(version: Int) : VersionSerializer<SmudgePathCommand>(version) {

companion object {
private const val COMPRESSION_QUALITY = 100
}

override fun write(kryo: Kryo, output: Output, command: SmudgePathCommand) {
with(kryo) {
writeObject(output, command.originalBitmap)
writeObject(output, command.pointPath.size)
command.pointPath.forEach {
writeObject(output, it)
with(output) {
command.originalBitmap.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY, output)
writeInt(command.pointPath.size)
command.pointPath.forEach {
writeObject(output, it)
}
writeFloat(command.maxPressure)
writeFloat(command.maxSize)
writeFloat(command.minSize)
}
writeObject(output, command.maxPressure)
writeObject(output, command.maxSize)
writeObject(output, command.minSize)
}
}

Expand All @@ -44,16 +52,18 @@ class SmudgePathCommandSerializer(version: Int) : VersionSerializer<SmudgePathCo

override fun readCurrentVersion(kryo: Kryo, input: Input, type: Class<out SmudgePathCommand>): SmudgePathCommand {
return with(kryo) {
val originalBitmap = readObject(input, Bitmap::class.java)
val pointPath = mutableListOf<PointF>()
val size = readObject(input, Int::class.java)
repeat(size) {
pointPath.add(readObject(input, PointF::class.java))
with(input) {
val originalBitmap = BitmapFactory.decodeStream(input)
val pointPath = mutableListOf<PointF>()
val size = readInt()
repeat(size) {
pointPath.add(readObject(input, PointF::class.java))
}
val maxPressure = readFloat()
val maxSize = readFloat()
val minSize = readFloat()
SmudgePathCommand(originalBitmap, pointPath, maxPressure, maxSize, minSize)
}
val maxPressure = readObject(input, Float::class.java)
val maxSize = readObject(input, Float::class.java)
val minSize = readObject(input, Float::class.java)
SmudgePathCommand(originalBitmap, pointPath, maxPressure, maxSize, minSize)
}
}
}

0 comments on commit 5b86b2f

Please sign in to comment.