From 47392e13f48e51e38259a087fd47dc9e4734fae0 Mon Sep 17 00:00:00 2001
From: Agnieszka C <85929121+Aga-C@users.noreply.github.com>
Date: Sun, 17 Oct 2021 20:38:46 +0200
Subject: [PATCH] Rewritten widget to record in a background
---
app/src/main/AndroidManifest.xml | 4 +++
.../activities/BackgroundRecordActivity.kt | 28 +++++++++++++++++++
.../voicerecorder/activities/MainActivity.kt | 14 ----------
.../WidgetRecordDisplayConfigureActivity.kt | 2 +-
.../fragments/RecorderFragment.kt | 22 +++++++++------
.../helpers/MyWidgetRecordDisplayProvider.kt | 8 +++---
.../voicerecorder/services/RecorderService.kt | 6 ++++
7 files changed, 57 insertions(+), 27 deletions(-)
create mode 100644 app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/BackgroundRecordActivity.kt
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 8b3d422..d416055 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -49,6 +49,10 @@
android:resource="@xml/widget_record_display" />
+
+
diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/BackgroundRecordActivity.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/BackgroundRecordActivity.kt
new file mode 100644
index 0000000..9d66fa4
--- /dev/null
+++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/BackgroundRecordActivity.kt
@@ -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()
+ }
+}
diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt
index 21458d1..0d10d93 100644
--- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt
@@ -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() {
diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/WidgetRecordDisplayConfigureActivity.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/WidgetRecordDisplayConfigureActivity.kt
index 9304cc2..65baff4 100644
--- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/WidgetRecordDisplayConfigureActivity.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/WidgetRecordDisplayConfigureActivity.kt
@@ -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 {
diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/RecorderFragment.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/RecorderFragment.kt
index 1cf94f4..65b8b95 100644
--- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/RecorderFragment.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/RecorderFragment.kt
@@ -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
diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/MyWidgetRecordDisplayProvider.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/MyWidgetRecordDisplayProvider.kt
index 365865e..1379b5b 100644
--- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/MyWidgetRecordDisplayProvider.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/MyWidgetRecordDisplayProvider.kt
@@ -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)
}
}
diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt
index b021286..31971f7 100644
--- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt
@@ -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
}