Handle record audio intent

There's a related Intent-extra used to define the maximum file size `MediaStore.Audio.Media.EXTRA_MAX_BYTES` but it's not yet handled by the app (I doubt many apps use it)
This commit is contained in:
Naveen 2023-02-04 02:06:49 +05:30
parent 67a3c25af5
commit 9a4dcd1d7d
4 changed files with 49 additions and 7 deletions

View File

@ -68,8 +68,13 @@
<activity
android:name=".activities.MainActivity"
android:configChanges="orientation|screenSize"
android:exported="false"
android:launchMode="singleTask" />
android:exported="true"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.provider.MediaStore.RECORD_SOUND" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".activities.SettingsActivity"

View File

@ -1,7 +1,9 @@
package com.simplemobiletools.voicerecorder.activities
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.provider.MediaStore
import android.widget.ImageView
import android.widget.TextView
import com.simplemobiletools.commons.extensions.*
@ -12,12 +14,18 @@ import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.adapters.ViewPagerAdapter
import com.simplemobiletools.voicerecorder.extensions.config
import com.simplemobiletools.voicerecorder.helpers.STOP_AMPLITUDE_UPDATE
import com.simplemobiletools.voicerecorder.models.Events
import com.simplemobiletools.voicerecorder.services.RecorderService
import kotlinx.android.synthetic.main.activity_main.*
import me.grantland.widget.AutofitHelper
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
class MainActivity : SimpleActivity() {
private var bus: EventBus? = null
override fun onCreate(savedInstanceState: Bundle?) {
isMaterialActivity = true
super.onCreate(savedInstanceState)
@ -41,6 +49,8 @@ class MainActivity : SimpleActivity() {
}
}
bus = EventBus.getDefault()
bus!!.register(this)
if (config.recordAfterLaunch && !RecorderService.isRunning) {
Intent(this@MainActivity, RecorderService::class.java).apply {
try {
@ -65,6 +75,7 @@ class MainActivity : SimpleActivity() {
override fun onDestroy() {
super.onDestroy()
bus?.unregister(this)
getPagerAdapter()?.onDestroy()
Intent(this@MainActivity, RecorderService::class.java).apply {
@ -79,6 +90,9 @@ class MainActivity : SimpleActivity() {
override fun onBackPressed() {
if (main_menu.isSearchOpen) {
main_menu.closeSearch()
} else if (isThirdPartyIntent()) {
setResult(Activity.RESULT_CANCELED, null)
super.onBackPressed()
} else {
super.onBackPressed()
}
@ -166,8 +180,12 @@ class MainActivity : SimpleActivity() {
(view_pager.adapter as ViewPagerAdapter).finishActMode()
}
view_pager.currentItem = config.lastUsedViewPagerPage
main_tabs_holder.getTabAt(config.lastUsedViewPagerPage)?.select()
if (isThirdPartyIntent()) {
view_pager.currentItem = 0
} else {
view_pager.currentItem = config.lastUsedViewPagerPage
main_tabs_holder.getTabAt(config.lastUsedViewPagerPage)?.select()
}
}
private fun setupTabColors() {
@ -206,4 +224,18 @@ class MainActivity : SimpleActivity() {
startAboutActivity(R.string.app_name, licenses, BuildConfig.VERSION_NAME, faqItems, true)
}
private fun isThirdPartyIntent() = intent?.action == MediaStore.Audio.Media.RECORD_SOUND_ACTION
@Subscribe(threadMode = ThreadMode.MAIN)
fun recordingSaved(event: Events.RecordingSaved) {
if (isThirdPartyIntent()) {
Intent().apply {
data = event.uri!!
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
setResult(Activity.RESULT_OK, this)
}
finish()
}
}
}

View File

@ -1,8 +1,11 @@
package com.simplemobiletools.voicerecorder.models
import android.net.Uri
class Events {
class RecordingDuration internal constructor(val duration: Int)
class RecordingStatus internal constructor(val status: Int)
class RecordingAmplitude internal constructor(val amplitude: Int)
class RecordingCompleted internal constructor()
class RecordingSaved internal constructor(val uri: Uri?)
}

View File

@ -7,6 +7,7 @@ import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Build
import android.os.IBinder
import android.provider.MediaStore
@ -202,7 +203,7 @@ class RecorderService : Service() {
val outputStream = contentResolver.openOutputStream(newUri)
val inputStream = getFileInputStreamSync(currFilePath)
inputStream!!.copyTo(outputStream!!, DEFAULT_BUFFER_SIZE)
recordingSavedSuccessfully()
recordingSavedSuccessfully(newUri)
} catch (e: Exception) {
showErrorToast(e)
}
@ -213,11 +214,12 @@ class RecorderService : Service() {
this,
arrayOf(currFilePath),
arrayOf(currFilePath.getMimeType())
) { _, _ -> recordingSavedSuccessfully() }
) { _, uri -> recordingSavedSuccessfully(uri) }
}
private fun recordingSavedSuccessfully() {
private fun recordingSavedSuccessfully(savedUri: Uri) {
toast(R.string.recording_saved_successfully)
EventBus.getDefault().post(Events.RecordingSaved(savedUri))
}
private fun getDurationUpdateTask() = object : TimerTask() {