diff --git a/app/src/main/java/io/lowrespalmtree/harvestdawn/MainActivity.kt b/app/src/main/java/io/lowrespalmtree/harvestdawn/MainActivity.kt index 3c691fe..5ab39fa 100644 --- a/app/src/main/java/io/lowrespalmtree/harvestdawn/MainActivity.kt +++ b/app/src/main/java/io/lowrespalmtree/harvestdawn/MainActivity.kt @@ -53,7 +53,7 @@ class MainActivity : AppCompatActivity() { if (intent.action == Intent.ACTION_SEND) { intent.getParcelableExtra(Intent.EXTRA_STREAM)?.also { - processVideo(it as Uri) + processVideoUri(it as Uri) } } } @@ -87,6 +87,9 @@ class MainActivity : AppCompatActivity() { R.id.action_save -> { save(); true } + R.id.action_clear_cache -> { + clearVideoCache(); true + } else -> super.onOptionsItemSelected(item) } } @@ -96,7 +99,9 @@ class MainActivity : AppCompatActivity() { REQ_TAKE_VID -> { if (resultCode != Activity.RESULT_OK) return - data?.data?.let { processVideo(it) } + data?.data?.let { + processVideoUri(it) + } } else -> super.onActivityResult(requestCode, resultCode, data) } @@ -147,27 +152,33 @@ class MainActivity : AppCompatActivity() { startActivityForResult(intent, REQ_TAKE_VID) } - private fun processVideo(videoUri: Uri) { - currentVideoPath = null - currentVideoUri = null - + private fun processVideoUri(inputVideoUri: Uri) { // Save captured video for FFMPEG. Use MP4 extension, completely arbitrary. val inputFile = File.createTempFile("input", ".mp4", cacheDir) - contentResolver.openInputStream(videoUri).use { inputStream -> + contentResolver.openInputStream(inputVideoUri).use { inputStream -> if (inputStream == null) return Unit .also { Log.e(TAG, "Could not open input file") } FileOutputStream(inputFile).use { fos -> fos.buffered().write(inputStream.buffered().readBytes()) } } + processVideo(inputFile.canonicalPath) + } - val mediaInfo = FFprobe.getMediaInformation(inputFile.canonicalPath) + private fun processVideo(inputVideoPath: String) { + currentVideoPath = null + currentVideoUri = null + + val mediaInfo = FFprobe.getMediaInformation(inputVideoPath) // Detect vertical/horizontal resolution, because at the moment we downscale everything to // fit Telegram's media preview requirements. If FFprobe fails to returns data, we consider // by default the video to be vertical. val isVertical = mediaInfo?.let { it.streams.getOrNull(0)?.let { streamInformation -> - streamInformation.height > streamInformation.width + if (streamInformation.height != null && streamInformation.width != null) + streamInformation.height > streamInformation.width + else + true } } ?: true val duration = mediaInfo?.duration ?: 0 @@ -187,7 +198,7 @@ class MainActivity : AppCompatActivity() { val outputDir = getShareableDir() ?: return val outputFile = File.createTempFile("npc", ".mp4", outputDir) MixTask(WeakReference(this), duration, isVertical) - .execute(inputFile.canonicalPath, soundFile.canonicalPath, outputFile.canonicalPath) + .execute(inputVideoPath, soundFile.canonicalPath, outputFile.canonicalPath) } private fun getShareableDir(): File? { @@ -197,6 +208,17 @@ class MainActivity : AppCompatActivity() { return outputDir } + private fun clearVideoCache() { + val currentFileName = currentVideoPath?.let { File(it).name } + getShareableDir()?.apply { + listFiles()?.forEach { file -> + // Don't delete current file. + if (currentFileName == null || currentFileName != file.name) + file.delete() + } + } + } + private class MixTask( private val activity: WeakReference, private val duration: Long, diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index 7451fcd..7a2590e 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -12,4 +12,9 @@ android:orderInCategory="100" android:title="@string/action_share" app:showAsAction="never" /> + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 980442f..bde94e1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,4 +6,5 @@ Hello second fragment. Arg: %1$s Share Save + Clear cache \ No newline at end of file