mirror of
https://github.com/SimpleMobileTools/Simple-Voice-Recorder.git
synced 2025-06-05 21:59:31 +02:00
moving the actual functionality in the Recorder fragment
This commit is contained in:
@ -1,28 +1,21 @@
|
|||||||
package com.simplemobiletools.voicerecorder.activities
|
package com.simplemobiletools.voicerecorder.activities
|
||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.graphics.drawable.Drawable
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import com.simplemobiletools.commons.extensions.*
|
import com.simplemobiletools.commons.extensions.appLaunched
|
||||||
|
import com.simplemobiletools.commons.extensions.checkAppSideloading
|
||||||
import com.simplemobiletools.commons.helpers.*
|
import com.simplemobiletools.commons.helpers.*
|
||||||
import com.simplemobiletools.commons.models.FAQItem
|
import com.simplemobiletools.commons.models.FAQItem
|
||||||
import com.simplemobiletools.voicerecorder.BuildConfig
|
import com.simplemobiletools.voicerecorder.BuildConfig
|
||||||
import com.simplemobiletools.voicerecorder.R
|
import com.simplemobiletools.voicerecorder.R
|
||||||
import com.simplemobiletools.voicerecorder.extensions.config
|
import com.simplemobiletools.voicerecorder.adapters.ViewPagerAdapter
|
||||||
import com.simplemobiletools.voicerecorder.helpers.GET_RECORDER_INFO
|
|
||||||
import com.simplemobiletools.voicerecorder.helpers.STOP_AMPLITUDE_UPDATE
|
import com.simplemobiletools.voicerecorder.helpers.STOP_AMPLITUDE_UPDATE
|
||||||
import com.simplemobiletools.voicerecorder.models.Events
|
|
||||||
import com.simplemobiletools.voicerecorder.services.RecorderService
|
import com.simplemobiletools.voicerecorder.services.RecorderService
|
||||||
import kotlinx.android.synthetic.main.fragment_recorder.*
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
import org.greenrobot.eventbus.EventBus
|
|
||||||
import org.greenrobot.eventbus.Subscribe
|
|
||||||
import org.greenrobot.eventbus.ThreadMode
|
|
||||||
|
|
||||||
class MainActivity : SimpleActivity() {
|
class MainActivity : SimpleActivity() {
|
||||||
private var isRecording = false
|
|
||||||
private var bus: EventBus? = null
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@ -44,19 +37,12 @@ class MainActivity : SimpleActivity() {
|
|||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
val adjustedPrimaryColor = getAdjustedPrimaryColor()
|
getPagerAdapter()?.onResume()
|
||||||
toggle_recording_button.apply {
|
|
||||||
setImageDrawable(getToggleButtonIcon())
|
|
||||||
background.applyColorFilter(adjustedPrimaryColor)
|
|
||||||
}
|
|
||||||
|
|
||||||
recorder_visualizer.chunkColor = adjustedPrimaryColor
|
|
||||||
recording_duration.setTextColor(config.textColor)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
bus?.unregister(this)
|
getPagerAdapter()?.onDestroy()
|
||||||
|
|
||||||
Intent(this@MainActivity, RecorderService::class.java).apply {
|
Intent(this@MainActivity, RecorderService::class.java).apply {
|
||||||
action = STOP_AMPLITUDE_UPDATE
|
action = STOP_AMPLITUDE_UPDATE
|
||||||
@ -80,11 +66,11 @@ class MainActivity : SimpleActivity() {
|
|||||||
|
|
||||||
private fun tryInitVoiceRecorder() {
|
private fun tryInitVoiceRecorder() {
|
||||||
if (isQPlus()) {
|
if (isQPlus()) {
|
||||||
initVoiceRecorder()
|
setupViewPager()
|
||||||
} else {
|
} else {
|
||||||
handlePermission(PERMISSION_WRITE_STORAGE) {
|
handlePermission(PERMISSION_WRITE_STORAGE) {
|
||||||
if (it) {
|
if (it) {
|
||||||
initVoiceRecorder()
|
setupViewPager()
|
||||||
} else {
|
} else {
|
||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
@ -92,73 +78,11 @@ class MainActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initVoiceRecorder() {
|
private fun setupViewPager() {
|
||||||
recorder_visualizer.recreate()
|
viewpager.adapter = ViewPagerAdapter(this)
|
||||||
bus = EventBus.getDefault()
|
|
||||||
bus!!.register(this)
|
|
||||||
|
|
||||||
updateRecordingDuration(0)
|
|
||||||
toggle_recording_button.setOnClickListener {
|
|
||||||
toggleRecording()
|
|
||||||
}
|
|
||||||
|
|
||||||
Intent(this@MainActivity, RecorderService::class.java).apply {
|
|
||||||
action = GET_RECORDER_INFO
|
|
||||||
startService(this)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun toggleRecording() {
|
private fun getPagerAdapter() = (viewpager.adapter as? ViewPagerAdapter)
|
||||||
isRecording = !isRecording
|
|
||||||
toggle_recording_button.setImageDrawable(getToggleButtonIcon())
|
|
||||||
|
|
||||||
if (isRecording) {
|
|
||||||
startRecording()
|
|
||||||
} else {
|
|
||||||
stopRecording()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun updateRecordingDuration(duration: Int) {
|
|
||||||
recording_duration.text = duration.getFormattedDuration()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun startRecording() {
|
|
||||||
Intent(this@MainActivity, RecorderService::class.java).apply {
|
|
||||||
startService(this)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun stopRecording() {
|
|
||||||
Intent(this@MainActivity, RecorderService::class.java).apply {
|
|
||||||
stopService(this)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
||||||
fun gotDurationEvent(event: Events.RecordingDuration) {
|
|
||||||
updateRecordingDuration(event.duration)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
||||||
fun gotStatusEvent(event: Events.RecordingStatus) {
|
|
||||||
isRecording = event.isRecording
|
|
||||||
toggle_recording_button.setImageDrawable(getToggleButtonIcon())
|
|
||||||
if (isRecording) {
|
|
||||||
recorder_visualizer.recreate()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
||||||
fun gotAmplitudeEvent(event: Events.RecordingAmplitude) {
|
|
||||||
val amplitude = event.amplitude
|
|
||||||
recorder_visualizer.update(amplitude)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getToggleButtonIcon(): Drawable {
|
|
||||||
val drawable = if (isRecording) R.drawable.ic_stop_vector else R.drawable.ic_microphone_vector
|
|
||||||
return resources.getColoredDrawableWithColor(drawable, getFABIconColor())
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun launchSettings() {
|
private fun launchSettings() {
|
||||||
startActivity(Intent(applicationContext, SettingsActivity::class.java))
|
startActivity(Intent(applicationContext, SettingsActivity::class.java))
|
||||||
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.simplemobiletools.voicerecorder.adapters
|
||||||
|
|
||||||
|
import android.util.SparseArray
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.viewpager.widget.PagerAdapter
|
||||||
|
import com.simplemobiletools.voicerecorder.R
|
||||||
|
import com.simplemobiletools.voicerecorder.activities.SimpleActivity
|
||||||
|
import com.simplemobiletools.voicerecorder.fragments.MyViewPagerFragment
|
||||||
|
|
||||||
|
class ViewPagerAdapter(private val activity: SimpleActivity) : PagerAdapter() {
|
||||||
|
private val mFragments = SparseArray<MyViewPagerFragment>()
|
||||||
|
|
||||||
|
override fun instantiateItem(container: ViewGroup, position: Int): Any {
|
||||||
|
val layout = R.layout.fragment_recorder
|
||||||
|
val view = activity.layoutInflater.inflate(layout, container, false)
|
||||||
|
container.addView(view)
|
||||||
|
|
||||||
|
mFragments.put(position, view as MyViewPagerFragment)
|
||||||
|
|
||||||
|
return view
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun destroyItem(container: ViewGroup, position: Int, item: Any) {
|
||||||
|
container.removeView(item as View)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getCount() = 1
|
||||||
|
|
||||||
|
override fun isViewFromObject(view: View, item: Any) = view == item
|
||||||
|
|
||||||
|
fun onResume() {
|
||||||
|
mFragments.get(0)?.onResume()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onDestroy() {
|
||||||
|
mFragments.get(0)?.onDestroy()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.simplemobiletools.voicerecorder.fragments
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.util.AttributeSet
|
||||||
|
import android.widget.RelativeLayout
|
||||||
|
|
||||||
|
abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) {
|
||||||
|
abstract fun onResume()
|
||||||
|
|
||||||
|
abstract fun onDestroy()
|
||||||
|
}
|
@ -1,7 +1,110 @@
|
|||||||
package com.simplemobiletools.voicerecorder.fragments
|
package com.simplemobiletools.voicerecorder.fragments
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.graphics.drawable.Drawable
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.widget.RelativeLayout
|
import com.simplemobiletools.commons.extensions.*
|
||||||
|
import com.simplemobiletools.voicerecorder.R
|
||||||
|
import com.simplemobiletools.voicerecorder.extensions.config
|
||||||
|
import com.simplemobiletools.voicerecorder.helpers.GET_RECORDER_INFO
|
||||||
|
import com.simplemobiletools.voicerecorder.models.Events
|
||||||
|
import com.simplemobiletools.voicerecorder.services.RecorderService
|
||||||
|
import kotlinx.android.synthetic.main.fragment_recorder.view.*
|
||||||
|
import org.greenrobot.eventbus.EventBus
|
||||||
|
import org.greenrobot.eventbus.Subscribe
|
||||||
|
import org.greenrobot.eventbus.ThreadMode
|
||||||
|
|
||||||
class RecorderFragment(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet)
|
class RecorderFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) {
|
||||||
|
private var isRecording = false
|
||||||
|
private var bus: EventBus? = null
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
setupColors()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
bus?.unregister(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onAttachedToWindow() {
|
||||||
|
super.onAttachedToWindow()
|
||||||
|
setupColors()
|
||||||
|
recorder_visualizer.recreate()
|
||||||
|
bus = EventBus.getDefault()
|
||||||
|
bus!!.register(this)
|
||||||
|
|
||||||
|
updateRecordingDuration(0)
|
||||||
|
toggle_recording_button.setOnClickListener {
|
||||||
|
toggleRecording()
|
||||||
|
}
|
||||||
|
|
||||||
|
Intent(context, RecorderService::class.java).apply {
|
||||||
|
action = GET_RECORDER_INFO
|
||||||
|
context.startService(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun setupColors() {
|
||||||
|
val adjustedPrimaryColor = context.getAdjustedPrimaryColor()
|
||||||
|
toggle_recording_button.apply {
|
||||||
|
setImageDrawable(getToggleButtonIcon())
|
||||||
|
background.applyColorFilter(adjustedPrimaryColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
recorder_visualizer.chunkColor = adjustedPrimaryColor
|
||||||
|
recording_duration.setTextColor(context.config.textColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateRecordingDuration(duration: Int) {
|
||||||
|
recording_duration.text = duration.getFormattedDuration()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getToggleButtonIcon(): Drawable {
|
||||||
|
val drawable = if (isRecording) R.drawable.ic_stop_vector else R.drawable.ic_microphone_vector
|
||||||
|
return resources.getColoredDrawableWithColor(drawable, context.getFABIconColor())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun toggleRecording() {
|
||||||
|
isRecording = !isRecording
|
||||||
|
toggle_recording_button.setImageDrawable(getToggleButtonIcon())
|
||||||
|
|
||||||
|
if (isRecording) {
|
||||||
|
startRecording()
|
||||||
|
} else {
|
||||||
|
stopRecording()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startRecording() {
|
||||||
|
Intent(context, RecorderService::class.java).apply {
|
||||||
|
context.startService(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun stopRecording() {
|
||||||
|
Intent(context, RecorderService::class.java).apply {
|
||||||
|
context.stopService(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||||
|
fun gotDurationEvent(event: Events.RecordingDuration) {
|
||||||
|
updateRecordingDuration(event.duration)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||||
|
fun gotStatusEvent(event: Events.RecordingStatus) {
|
||||||
|
isRecording = event.isRecording
|
||||||
|
toggle_recording_button.setImageDrawable(getToggleButtonIcon())
|
||||||
|
if (isRecording) {
|
||||||
|
recorder_visualizer.recreate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||||
|
fun gotAmplitudeEvent(event: Events.RecordingAmplitude) {
|
||||||
|
val amplitude = event.amplitude
|
||||||
|
recorder_visualizer.update(amplitude)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,6 +4,9 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
<include layout="@layout/fragment_recorder" />
|
<com.simplemobiletools.commons.views.MyViewPager
|
||||||
|
android:id="@+id/viewpager"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent" />
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
Reference in New Issue
Block a user