Skip to content

Commit

Permalink
Pause and resume implemented by core_screen_recorder
Browse files Browse the repository at this point in the history
  • Loading branch information
gargVader committed May 11, 2024
1 parent fce7bc3 commit 1d13702
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 162 deletions.
3 changes: 0 additions & 3 deletions app/src/main/java/com/screensnap/app/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.screensnap.app

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand All @@ -9,7 +8,6 @@ import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
import com.screensnap.core.screen_recorder.receiver.NotificationReceiver
import com.screensnap.core.ui.theme.ScreenSnapTheme
import com.screensnap.domain.navigation.HomeScreenDestinations
import com.screensnap.feature.home.nav.homeScreenNavGraph
Expand All @@ -31,7 +29,6 @@ class MainActivity : ComponentActivity() {
navController = navController,
startDestination = HomeScreenDestinations.ROUTE,
) {
val x = MainActivity::class.java
homeScreenNavGraph(navController = navController)
settingsScreenNavGraph(navController)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class ScreenRecorder(
audioState = screenSnapDatastore.getAudioState()
mediaRecorder = createMediaRecorder()
virtualDisplay = createVirtualDisplay()
if (audioState.shouldRecordSystemAudio()) {
// if (audioState.shouldRecordSystemAudio()) {
systemAudioRecorder = createSystemAudioRecorder()
}
// }

coroutineScope {
mediaRecorder.start()
Expand All @@ -61,12 +61,12 @@ class ScreenRecorder(
}
}

fun pauseRecording(){
fun pauseRecording() {
mediaRecorder.pause()
systemAudioRecorder.pauseRecording()
}

fun resumeRecording(){
fun resumeRecording() {
mediaRecorder.resume()
systemAudioRecorder.resumeRecording()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.screensnap.core.screen_recorder.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import com.screensnap.core.screen_recorder.services.ScreenRecorderService
import com.screensnap.core.screen_recorder.services.ScreenSnapNotification

Expand All @@ -19,8 +18,8 @@ class NotificationReceiver : BroadcastReceiver() {
// context.startService()
// }

ScreenSnapNotification.ACTION_RESUME_RECORDING -> {}
ScreenSnapNotification.ACTION_STOP_RECORDING -> {
ScreenSnapNotification.ACTION_RECORDING_RESUME -> {}
ScreenSnapNotification.ACTION_RECORDING_STOP -> {
context.stopService(Intent(context, ScreenRecorderService::class.java))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,40 @@ package com.screensnap.core.screen_recorder.services

import android.app.Notification
import android.app.Notification.DecoratedCustomViewStyle
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_IMMUTABLE
import android.app.Service
import android.content.Intent
import android.media.projection.MediaProjection
import android.media.projection.MediaProjectionManager
import android.os.IBinder
import android.util.Log
import android.widget.RemoteViews
import com.screensnap.core.datastore.ScreenSnapDatastore
import com.screensnap.core.screen_recorder.R
import com.screensnap.core.screen_recorder.RecordingState
import com.screensnap.core.screen_recorder.ScreenRecorder
import com.screensnap.core.screen_recorder.ScreenRecorderRepository
import com.screensnap.core.screen_recorder.services.ScreenSnapNotification.ACTION_RECORDING_PAUSE
import com.screensnap.core.screen_recorder.services.ScreenSnapNotification.ACTION_RECORDING_RESUME
import com.screensnap.core.screen_recorder.services.ScreenSnapNotification.ACTION_RECORDING_START
import com.screensnap.core.screen_recorder.services.ScreenSnapNotification.ACTION_RECORDING_STOP
import com.screensnap.core.screen_recorder.utils.RecorderConfigValues
import dagger.hilt.android.AndroidEntryPoint
import java.io.File
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.newSingleThreadContext
import java.io.File
import javax.inject.Inject

@AndroidEntryPoint
class ScreenRecorderService : Service() {

init {
Log.d("Girish", "ScreenRecorderService: init")
}

@Inject
lateinit var screenSnapDatastore: ScreenSnapDatastore

Expand All @@ -48,6 +60,24 @@ class ScreenRecorderService : Service() {
flags: Int,
startId: Int,
): Int {
when (intent.action) {
ACTION_RECORDING_START -> {}
ACTION_RECORDING_PAUSE -> {
screenRecorder.pauseRecording()
}

ACTION_RECORDING_RESUME -> {
screenRecorder.resumeRecording()
}

ACTION_RECORDING_STOP -> {}
else -> onStartRecording(intent)
}

return START_STICKY
}

private fun onStartRecording(intent: Intent): Int {
// Extract info
val config = ScreenRecorderServiceConfig.createFromScreenRecorderServiceIntent(intent)

Expand All @@ -72,13 +102,29 @@ class ScreenRecorderService : Service() {
// Notification for foreground service
private fun createNotification(): Notification {
val view = RemoteViews("com.screensnap.app", R.layout.notification)

val pauseIntent =
Intent(this, ScreenRecorderService::class.java).apply {
action = ACTION_RECORDING_PAUSE
}
val pausePendingIntent = PendingIntent.getService(this, 0, pauseIntent, FLAG_IMMUTABLE)

view.setOnClickPendingIntent(R.id.pause_layout, pausePendingIntent)

return Notification.Builder(this, SCREEN_RECORDER_NOTIFICATION_CHANNEL_ID)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.baseline_send_24)
.setContentTitle("Screen Snap")
.setContentText("Recording in progress")
.setStyle(DecoratedCustomViewStyle())
.setCustomContentView(view)
// .setStyle(DecoratedCustomViewStyle())
.addAction(
Notification.Action(
R.drawable.baseline_pause_24,
"pause",
pausePendingIntent
)
)
// .setCustomContentView(view)
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.screensnap.core.screen_recorder.BuildConfig
object ScreenSnapNotification {
val ACTION_RECORDING_START = createActionName("recording_start")
val ACTION_RECORDING_PAUSE = createActionName("recording_pause")
val ACTION_RESUME_RECORDING = createActionName("recording_resume")
val ACTION_STOP_RECORDING = createActionName("recording_stop")
val ACTION_RECORDING_RESUME = createActionName("recording_resume")
val ACTION_RECORDING_STOP = createActionName("recording_stop")

private fun createActionName(action: String) = "${BuildConfig.LIBRARY_PACKAGE_NAME}.$action"
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package com.screensnap.core.screen_recorder.services.pendingintent

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import com.screensnap.core.screen_recorder.receiver.NotificationReceiver
import com.screensnap.core.screen_recorder.services.ScreenSnapNotification

//fun createScreenRecorderServicePendingIntent(
// fun createScreenRecorderServicePendingIntent(
// context: Context,
// pendingIntentId: Int,
//): PendingIntent {
// ): PendingIntent {
// val screenRecorderServiceIntent =
// createScreenRecorderServiceIntent(context, pendingIntentId)
// return PendingIntent.getBroadcast(
Expand All @@ -18,13 +12,13 @@ import com.screensnap.core.screen_recorder.services.ScreenSnapNotification
// screenRecorderServiceIntent,
// PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
// )
//}
// }
//
//private fun createScreenRecorderServiceIntent(
// private fun createScreenRecorderServiceIntent(
// context: Context,
// notificationId: Int,
//): Intent {
// ): Intent {
// return Intent(ScreenSnapNotification.ACTION_OPEN_APP).apply {
// setClass(context, NotificationReceiver::class.java)
// }
//}
// }
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ class AudioEncoder(
encoder.start()

try {
while (isActive && !isPaused) {
writeToEncoder(onInputBufferAvailable)
readFromEncoder(onOutputBufferAvailable, onOutputFormatChanged)
while (isActive) {
if (!isPaused){
writeToEncoder(onInputBufferAvailable)
readFromEncoder(onOutputBufferAvailable, onOutputFormatChanged)
}
}
} finally {
// addEndOfStreamFlag()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ abstract class AudioRecorder(
}
}

fun pauseRecording(){
fun pauseRecording() {
audioEncoder.pauseEncode()
audioRecord.stop()
}

fun resumeRecording(){
fun resumeRecording() {
audioEncoder.resumeEncode()
audioRecord.startRecording()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.widget.Toast
import android.widget.Toast.LENGTH_SHORT
Expand All @@ -19,6 +18,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
Expand Down Expand Up @@ -102,10 +102,16 @@ internal fun HomeScreen(
Box(modifier = Modifier.padding(paddingValues)) {
Column(
modifier =
Modifier
.fillMaxSize()
.padding(start = 16.dp, end = 16.dp),
Modifier
.fillMaxSize()
.padding(start = 16.dp, end = 16.dp),
) {
Button(onClick = { viewModel.onEvent(HomeScreenEvents.OnPauseRecord) }) {
Text(text = "Pause")
}
Button(onClick = { viewModel.onEvent(HomeScreenEvents.OnResumeRecord) }) {
Text(text = "Resume")
}
HomeChips(viewModel = viewModel, audioPermissionLauncher = audioPermissionLauncher)
YourRecordingsHeader()
if (state.isListRefreshing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ sealed interface HomeScreenEvents {

object OnStopRecord : HomeScreenEvents

object OnPauseRecord: HomeScreenEvents

object OnResumeRecord: HomeScreenEvents

data class OnUpdateAudioState(val audioState: com.screensnap.core.datastore.AudioState) :
HomeScreenEvents
}
Loading

0 comments on commit 1d13702

Please sign in to comment.