MainActivity: add option to clear cache

This commit is contained in:
dece 2021-03-17 20:28:32 +01:00
parent a7e7cd0794
commit eecb704a7f
3 changed files with 38 additions and 10 deletions

View file

@ -53,7 +53,7 @@ class MainActivity : AppCompatActivity() {
if (intent.action == Intent.ACTION_SEND) {
intent.getParcelableExtra<Parcelable>(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<MainActivity>,
private val duration: Long,

View file

@ -12,4 +12,9 @@
android:orderInCategory="100"
android:title="@string/action_share"
app:showAsAction="never" />
<item
android:id="@+id/action_clear_cache"
android:orderInCategory="100"
android:title="@string/action_clear_cache"
app:showAsAction="never" />
</menu>

View file

@ -6,4 +6,5 @@
<string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string>
<string name="send">Share</string>
<string name="action_save">Save</string>
<string name="action_clear_cache">Clear cache</string>
</resources>