Skip to content

Instantly share code, notes, and snippets.

@mzorz
Created April 2, 2020 20:39
Show Gist options
  • Save mzorz/e5ea8040b271f08080ce5c185b205d95 to your computer and use it in GitHub Desktop.
Save mzorz/e5ea8040b271f08080ce5c185b205d95 to your computer and use it in GitHub Desktop.
Test code to measure saving times
diff --git a/app/src/main/java/com/automattic/portkey/MainActivity.kt b/app/src/main/java/com/automattic/portkey/MainActivity.kt
index db5eb17..1f63d82 100644
--- a/app/src/main/java/com/automattic/portkey/MainActivity.kt
+++ b/app/src/main/java/com/automattic/portkey/MainActivity.kt
@@ -13,6 +13,7 @@ import com.automattic.portkey.compose.frame.FrameSaveService.StorySaveProcessSta
import com.automattic.portkey.compose.frame.FrameSaveService.StorySaveResult
import com.automattic.portkey.compose.story.StoryRepository
import com.automattic.portkey.intro.IntroActivity
+import com.automattic.portkey.util.KEY_STORY_INDEX
import com.automattic.portkey.util.KEY_STORY_SAVE_RESULT
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_main.*
@@ -45,8 +46,18 @@ class MainActivity : AppCompatActivity(), MainFragment.OnFragmentInteractionList
fab.setOnClickListener { view ->
fab.isEnabled = false
- Navigation.findNavController(this, R.id.nav_host_fragment)
- .navigate(R.id.action_mainFragment_to_composeLoopFrameActivity)
+ // TODO REMOVE TEST CODE
+ if (!StoryRepository.firstTime) {
+ val intent = Intent(this, ComposeLoopFrameActivity::class.java)
+ intent.putExtra(KEY_STORY_INDEX, 0)
+ intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ startActivity(intent)
+ // TODO END REMOVE TEST CODE
+ } else {
+ Navigation.findNavController(this, R.id.nav_host_fragment)
+ .navigate(R.id.action_mainFragment_to_composeLoopFrameActivity)
+ }
}
}
diff --git a/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveManager.kt b/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveManager.kt
index f653c02..f94df7e 100644
--- a/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveManager.kt
+++ b/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveManager.kt
@@ -4,6 +4,7 @@ import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Color
import android.net.Uri
+import android.util.Log
import android.view.ViewGroup.LayoutParams
import android.widget.RelativeLayout
import com.automattic.photoeditor.PhotoEditor
@@ -30,6 +31,7 @@ import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import java.io.File
import kotlin.coroutines.CoroutineContext
+import kotlin.system.measureTimeMillis
typealias FrameIndex = Int
@@ -52,21 +54,29 @@ class FrameSaveManager(private val photoEditor: PhotoEditor) : CoroutineScope {
suspend fun saveStory(
context: Context,
frames: List<StoryFrameItem>
- ): List<File> {
- // first, save all images async and wait
- val listImages = saveLoopFrameAsyncAwaitForType(
- context, frames, IMAGE, 10
- )
+ ): List<File>? {
+ var listTotal: List<File>? = null
- yield()
+ Log.d("PORTKEY", "START SAVE: " + frames.size)
- // now, save all videos async and wait - this process is intense so only allow for 3 videos to be processed
- // concurrently
- val listVideos = saveLoopFrameAsyncAwaitForType(
- context, frames, VIDEO, 3
- )
+ val time = measureTimeMillis {
+ // first, save all images async and wait
+ val listImages = saveLoopFrameAsyncAwaitForType(
+ context, frames, IMAGE, 10
+ )
+
+ yield()
+
+ // now, save all videos async and wait - this process is intense so only allow for 3 videos to be processed
+ // concurrently
+ val listVideos = saveLoopFrameAsyncAwaitForType(
+ context, frames, VIDEO, 3
+ )
+ listTotal = listImages + listVideos
+ }
+
+ Log.d("PORTKEY", "TOTAL TIME: " + time)
- val listTotal = listImages + listVideos
return listTotal
}
@@ -106,11 +116,14 @@ class FrameSaveManager(private val photoEditor: PhotoEditor) : CoroutineScope {
// saveVideoWithStaticBackground()
} else {
try {
- saveProgressListener?.onFrameSaveStart(frameIndex)
- // create ghost PhotoEditorView to be used for saving off-screen
- val ghostPhotoEditorView = createGhostPhotoEditor(context, photoEditor.composedCanvas)
- frameFile = saveImageFrame(frame, ghostPhotoEditorView, frameIndex)
- saveProgressListener?.onFrameSaveCompleted(frameIndex)
+ val time = measureTimeMillis {
+ saveProgressListener?.onFrameSaveStart(frameIndex)
+ // create ghost PhotoEditorView to be used for saving off-screen
+ val ghostPhotoEditorView = createGhostPhotoEditor(context, photoEditor.composedCanvas)
+ frameFile = saveImageFrame(frame, ghostPhotoEditorView, frameIndex)
+ saveProgressListener?.onFrameSaveCompleted(frameIndex)
+ }
+ Log.d("PORTKEY", "END SAVE: " + frameIndex + " time: " + time)
} catch (ex: Exception) {
saveProgressListener?.onFrameSaveFailed(frameIndex, ex.message)
}
diff --git a/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveService.kt b/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveService.kt
index a517bd7..edaa45a 100644
--- a/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveService.kt
+++ b/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveService.kt
@@ -115,7 +115,7 @@ class FrameSaveService : Service() {
)
// once all frames have been saved, issue a broadcast so the system knows these frames are ready
- sendNewMediaReadyBroadcast(frameFileList)
+ sendNewMediaReadyBroadcast(frameFileList!!)
// if we got the same amount of output files it means all went good, otherwise there were errors
prepareStorySaveResult(storySaveProcessor, storyIndex, frames.size == frameFileList.size)
diff --git a/app/src/main/java/com/automattic/portkey/compose/story/StoryRepository.kt b/app/src/main/java/com/automattic/portkey/compose/story/StoryRepository.kt
index 490278a..4c2c3c0 100644
--- a/app/src/main/java/com/automattic/portkey/compose/story/StoryRepository.kt
+++ b/app/src/main/java/com/automattic/portkey/compose/story/StoryRepository.kt
@@ -8,8 +8,22 @@ object StoryRepository {
var currentStoryIndex = DEFAULT_NONE_SELECTED
private set
private val stories = ArrayList<Story>()
+ // TODO TEST CODE - REMOVE
+ var firstTime = true
fun loadStory(storyIndex: Int): Story? {
+ // TODO TEST CODE - REMOVE
+ if (firstTime) {
+ firstTime = false
+ }
+// else {
+// currentStoryIndex = 0
+// currentStoryFrames.clear()
+// currentStoryFrames.addAll(stories[0].frames)
+// return stories[0]
+// }
+ // TODO END TEST CODE - REMOVE
+
if (storyIndex == DEFAULT_NONE_SELECTED) {
// if there's no specific Story to select, create and add a new empty Story, and return it
createNewStory()
diff --git a/app/src/main/java/com/automattic/portkey/MainActivity.kt b/app/src/main/java/com/automattic/portkey/MainActivity.kt
index db5eb17..1f63d82 100644
--- a/app/src/main/java/com/automattic/portkey/MainActivity.kt
+++ b/app/src/main/java/com/automattic/portkey/MainActivity.kt
@@ -13,6 +13,7 @@ import com.automattic.portkey.compose.frame.FrameSaveService.StorySaveProcessSta
import com.automattic.portkey.compose.frame.FrameSaveService.StorySaveResult
import com.automattic.portkey.compose.story.StoryRepository
import com.automattic.portkey.intro.IntroActivity
+import com.automattic.portkey.util.KEY_STORY_INDEX
import com.automattic.portkey.util.KEY_STORY_SAVE_RESULT
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_main.*
@@ -45,8 +46,18 @@ class MainActivity : AppCompatActivity(), MainFragment.OnFragmentInteractionList
fab.setOnClickListener { view ->
fab.isEnabled = false
- Navigation.findNavController(this, R.id.nav_host_fragment)
- .navigate(R.id.action_mainFragment_to_composeLoopFrameActivity)
+ // TODO REMOVE TEST CODE
+ if (!StoryRepository.firstTime) {
+ val intent = Intent(this, ComposeLoopFrameActivity::class.java)
+ intent.putExtra(KEY_STORY_INDEX, 0)
+ intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ startActivity(intent)
+ // TODO END REMOVE TEST CODE
+ } else {
+ Navigation.findNavController(this, R.id.nav_host_fragment)
+ .navigate(R.id.action_mainFragment_to_composeLoopFrameActivity)
+ }
}
}
diff --git a/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveManager.kt b/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveManager.kt
index f653c02..f94df7e 100644
--- a/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveManager.kt
+++ b/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveManager.kt
@@ -4,6 +4,7 @@ import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Color
import android.net.Uri
+import android.util.Log
import android.view.ViewGroup.LayoutParams
import android.widget.RelativeLayout
import com.automattic.photoeditor.PhotoEditor
@@ -30,6 +31,7 @@ import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import java.io.File
import kotlin.coroutines.CoroutineContext
+import kotlin.system.measureTimeMillis
typealias FrameIndex = Int
@@ -52,21 +54,29 @@ class FrameSaveManager(private val photoEditor: PhotoEditor) : CoroutineScope {
suspend fun saveStory(
context: Context,
frames: List<StoryFrameItem>
- ): List<File> {
- // first, save all images async and wait
- val listImages = saveLoopFrameAsyncAwaitForType(
- context, frames, IMAGE, 10
- )
+ ): List<File>? {
+ var listTotal: List<File>? = null
- yield()
+ Log.d("PORTKEY", "START SAVE: " + frames.size)
- // now, save all videos async and wait - this process is intense so only allow for 3 videos to be processed
- // concurrently
- val listVideos = saveLoopFrameAsyncAwaitForType(
- context, frames, VIDEO, 3
- )
+ val time = measureTimeMillis {
+ // first, save all images async and wait
+ val listImages = saveLoopFrameAsyncAwaitForType(
+ context, frames, IMAGE, 10
+ )
+
+ yield()
+
+ // now, save all videos async and wait - this process is intense so only allow for 3 videos to be processed
+ // concurrently
+ val listVideos = saveLoopFrameAsyncAwaitForType(
+ context, frames, VIDEO, 3
+ )
+ listTotal = listImages + listVideos
+ }
+
+ Log.d("PORTKEY", "TOTAL TIME: " + time)
- val listTotal = listImages + listVideos
return listTotal
}
@@ -106,11 +116,14 @@ class FrameSaveManager(private val photoEditor: PhotoEditor) : CoroutineScope {
// saveVideoWithStaticBackground()
} else {
try {
- saveProgressListener?.onFrameSaveStart(frameIndex)
- // create ghost PhotoEditorView to be used for saving off-screen
- val ghostPhotoEditorView = createGhostPhotoEditor(context, photoEditor.composedCanvas)
- frameFile = saveImageFrame(frame, ghostPhotoEditorView, frameIndex)
- saveProgressListener?.onFrameSaveCompleted(frameIndex)
+ val time = measureTimeMillis {
+ saveProgressListener?.onFrameSaveStart(frameIndex)
+ // create ghost PhotoEditorView to be used for saving off-screen
+ val ghostPhotoEditorView = createGhostPhotoEditor(context, photoEditor.composedCanvas)
+ frameFile = saveImageFrame(frame, ghostPhotoEditorView, frameIndex)
+ saveProgressListener?.onFrameSaveCompleted(frameIndex)
+ }
+ Log.d("PORTKEY", "END SAVE: " + frameIndex + " time: " + time)
} catch (ex: Exception) {
saveProgressListener?.onFrameSaveFailed(frameIndex, ex.message)
}
diff --git a/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveService.kt b/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveService.kt
index a517bd7..edaa45a 100644
--- a/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveService.kt
+++ b/app/src/main/java/com/automattic/portkey/compose/frame/FrameSaveService.kt
@@ -115,7 +115,7 @@ class FrameSaveService : Service() {
)
// once all frames have been saved, issue a broadcast so the system knows these frames are ready
- sendNewMediaReadyBroadcast(frameFileList)
+ sendNewMediaReadyBroadcast(frameFileList!!)
// if we got the same amount of output files it means all went good, otherwise there were errors
prepareStorySaveResult(storySaveProcessor, storyIndex, frames.size == frameFileList.size)
diff --git a/app/src/main/java/com/automattic/portkey/compose/story/StoryRepository.kt b/app/src/main/java/com/automattic/portkey/compose/story/StoryRepository.kt
index 490278a..4c2c3c0 100644
--- a/app/src/main/java/com/automattic/portkey/compose/story/StoryRepository.kt
+++ b/app/src/main/java/com/automattic/portkey/compose/story/StoryRepository.kt
@@ -8,8 +8,22 @@ object StoryRepository {
var currentStoryIndex = DEFAULT_NONE_SELECTED
private set
private val stories = ArrayList<Story>()
+ // TODO TEST CODE - REMOVE
+ var firstTime = true
fun loadStory(storyIndex: Int): Story? {
+ // TODO TEST CODE - REMOVE
+ if (firstTime) {
+ firstTime = false
+ }
+// else {
+// currentStoryIndex = 0
+// currentStoryFrames.clear()
+// currentStoryFrames.addAll(stories[0].frames)
+// return stories[0]
+// }
+ // TODO END TEST CODE - REMOVE
+
if (storyIndex == DEFAULT_NONE_SELECTED) {
// if there's no specific Story to select, create and add a new empty Story, and return it
createNewStory()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment