Rewritten widget to record in a background

This commit is contained in:
Agnieszka C 2021-10-17 20:38:46 +02:00
parent a6dbd976fd
commit 47392e13f4
7 changed files with 57 additions and 27 deletions

View File

@ -49,6 +49,10 @@
android:resource="@xml/widget_record_display" />
</receiver>
<activity
android:name=".activities.BackgroundRecordActivity"
android:theme="@android:style/Theme.NoDisplay" />
<activity
android:name=".activities.SplashActivity"
android:theme="@style/SplashTheme" />

View File

@ -0,0 +1,28 @@
package com.simplemobiletools.voicerecorder.activities
import android.content.Intent
import com.simplemobiletools.voicerecorder.services.RecorderService
class BackgroundRecordActivity : SimpleActivity() {
companion object {
const val RECORD_INTENT_ACTION = "RECORD_ACTION";
}
override fun onResume() {
super.onResume()
if (intent.action == RECORD_INTENT_ACTION) {
Intent(this@BackgroundRecordActivity, RecorderService::class.java).apply {
try {
if (RecorderService.isRunning) {
stopService(this)
} else {
startService(this)
}
} catch (ignored: Exception) {
}
}
}
moveTaskToBack(true)
finish()
}
}

View File

@ -18,10 +18,6 @@ import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : SimpleActivity() {
companion object {
const val RECORD_INTENT_ACTION = "RECORD_ACTION";
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
@ -44,16 +40,6 @@ class MainActivity : SimpleActivity() {
super.onResume()
getPagerAdapter()?.onResume()
setupTabColors()
if (intent.action == RECORD_INTENT_ACTION) {
view_pager.currentItem = 0
Intent(this@MainActivity, RecorderService::class.java).apply {
try {
startService(this)
} catch (ignored: Exception) {
}
}
}
}
override fun onDestroy() {

View File

@ -42,7 +42,7 @@ class WidgetRecordDisplayConfigureActivity : SimpleActivity() {
}
private fun initVariables() {
mWidgetColor = config.widgetBgColor
mWidgetColor = resources.getColor(R.color.color_primary)
mWidgetAlpha = if (mWidgetColor == DEFAULT_WIDGET_BG_COLOR) {
1f
} else {

View File

@ -26,6 +26,8 @@ class RecorderFragment(context: Context, attributeSet: AttributeSet) : MyViewPag
override fun onResume() {
setupColors()
if (!RecorderService.isRunning) status = RECORDING_STOPPED
refreshView()
}
override fun onDestroy() {
@ -127,14 +129,7 @@ class RecorderFragment(context: Context, attributeSet: AttributeSet) : MyViewPag
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun gotDurationEvent(event: Events.RecordingDuration) {
updateRecordingDuration(event.duration)
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun gotStatusEvent(event: Events.RecordingStatus) {
status = event.status
private fun refreshView() {
toggle_recording_button.setImageDrawable(getToggleButtonIcon())
toggle_pause_button.beVisibleIf(status != RECORDING_STOPPED && isNougatPlus())
if (status == RECORDING_PAUSED) {
@ -149,6 +144,17 @@ class RecorderFragment(context: Context, attributeSet: AttributeSet) : MyViewPag
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun gotDurationEvent(event: Events.RecordingDuration) {
updateRecordingDuration(event.duration)
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun gotStatusEvent(event: Events.RecordingStatus) {
status = event.status
refreshView()
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun gotAmplitudeEvent(event: Events.RecordingAmplitude) {
val amplitude = event.amplitude

View File

@ -11,8 +11,8 @@ import android.graphics.Color
import android.widget.RemoteViews
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.BackgroundRecordActivity
import com.simplemobiletools.voicerecorder.extensions.config
import com.simplemobiletools.voicerecorder.activities.MainActivity
import com.simplemobiletools.voicerecorder.extensions.drawableToBitmap
class MyWidgetRecordDisplayProvider : AppWidgetProvider() {
@ -37,9 +37,9 @@ class MyWidgetRecordDisplayProvider : AppWidgetProvider() {
private fun getComponentName(context: Context) = ComponentName(context, MyWidgetRecordDisplayProvider::class.java)
private fun setupAppOpenIntent(context: Context, views: RemoteViews) {
Intent(context, MainActivity::class.java).apply {
action = MainActivity.RECORD_INTENT_ACTION;
val pendingIntent = PendingIntent.getActivity(context, OPEN_APP_INTENT_ID, this, PendingIntent.FLAG_CANCEL_CURRENT)
Intent(context, BackgroundRecordActivity::class.java).apply {
action = BackgroundRecordActivity.RECORD_INTENT_ACTION
val pendingIntent = PendingIntent.getActivity(context, OPEN_APP_INTENT_ID, this, PendingIntent.FLAG_UPDATE_CURRENT)
views.setOnClickPendingIntent(R.id.record_display_btn, pendingIntent)
}
}

View File

@ -29,6 +29,10 @@ import java.io.File
import java.util.*
class RecorderService : Service() {
companion object {
var isRunning = false
}
private val AMPLITUDE_UPDATE_MS = 75L
private var currFilePath = ""
@ -56,10 +60,12 @@ class RecorderService : Service() {
override fun onDestroy() {
super.onDestroy()
stopRecording()
isRunning = false
}
// mp4 output format with aac encoding should produce good enough m4a files according to https://stackoverflow.com/a/33054794/1967672
private fun startRecording() {
isRunning = true
if (status == RECORDING_RUNNING) {
return
}