diff --git a/Paintroid/src/main/java/org/catrobat/paintroid/MainActivity.kt b/Paintroid/src/main/java/org/catrobat/paintroid/MainActivity.kt index 9f9765071b..83df6e26c5 100644 --- a/Paintroid/src/main/java/org/catrobat/paintroid/MainActivity.kt +++ b/Paintroid/src/main/java/org/catrobat/paintroid/MainActivity.kt @@ -269,7 +269,6 @@ class MainActivity : AppCompatActivity(), MainView, CommandListener { onCreateDrawingSurface() presenterMain.onCreateTool() - var isOpenedFromCatroid = false val receivedIntent = intent isTemporaryFileSavingTest = intent.getBooleanExtra("isTemporaryFileSavingTest", false) when { @@ -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 @@ -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)) { diff --git a/Paintroid/src/main/java/org/catrobat/paintroid/command/serialization/CommandSerializationUtilities.kt b/Paintroid/src/main/java/org/catrobat/paintroid/command/serialization/CommandSerializationUtilities.kt index 53bf33299a..3e5626d743 100644 --- a/Paintroid/src/main/java/org/catrobat/paintroid/command/serialization/CommandSerializationUtilities.kt +++ b/Paintroid/src/main/java/org/catrobat/paintroid/command/serialization/CommandSerializationUtilities.kt @@ -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 @@ -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 @@ -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() @@ -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 } diff --git a/Paintroid/src/main/java/org/catrobat/paintroid/command/serialization/SmudgePathCommandSerializer.kt b/Paintroid/src/main/java/org/catrobat/paintroid/command/serialization/SmudgePathCommandSerializer.kt index c92a716a19..d950782806 100644 --- a/Paintroid/src/main/java/org/catrobat/paintroid/command/serialization/SmudgePathCommandSerializer.kt +++ b/Paintroid/src/main/java/org/catrobat/paintroid/command/serialization/SmudgePathCommandSerializer.kt @@ -19,6 +19,7 @@ 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 @@ -26,16 +27,23 @@ import com.esotericsoftware.kryo.io.Output import org.catrobat.paintroid.command.implementation.SmudgePathCommand class SmudgePathCommandSerializer(version: Int) : VersionSerializer(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) } } @@ -44,16 +52,18 @@ class SmudgePathCommandSerializer(version: Int) : VersionSerializer): SmudgePathCommand { return with(kryo) { - val originalBitmap = readObject(input, Bitmap::class.java) - val pointPath = mutableListOf() - 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() + 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) } } }