Skip to content

Commit

Permalink
comments added
Browse files Browse the repository at this point in the history
  • Loading branch information
devrath committed Jan 4, 2024
1 parent 9fbe701 commit 22fdf6e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,97 @@ import com.istudio.app.service.StopwatchService
import com.istudio.app.ui.theme.StopWatchServiceAppTheme
import dagger.hilt.android.AndroidEntryPoint


/**
* 1) Why we have used bound service ?
* --> We have used bound service because, When application is destroyed & recreated when we start timer and kill activity. Once we reopen app, it has to be updated so bound service helps us achieve it.
*
*/

@OptIn(ExperimentalAnimationApi::class)
@ExperimentalAnimationApi
@AndroidEntryPoint
class MainActivity : ComponentActivity() {

// We Keep "isBound" Flag keep track when the service is connected/disconnected
private var isBound by mutableStateOf(false)
// Reference to the service used to display the stop clock
private lateinit var stopwatchService: StopwatchService
private val connection = object : ServiceConnection {
override fun onServiceConnected(className: ComponentName, service: IBinder) {
val binder = service as StopwatchService.StopwatchBinder
stopwatchService = binder.getService()
isBound = true
}

override fun onServiceDisconnected(arg0: ComponentName) {
isBound = false
}
/** ********************** Life-Cycle-Methods ********************** **/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initOnCreate()
}

override fun onStart() {
super.onStart()
Intent(this, StopwatchService::class.java).also { intent ->
bindService(intent, connection, Context.BIND_AUTO_CREATE)
}
initOnStart()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
override fun onStop() {
super.onStop()
initOnStop()
}
/** ********************** Life-Cycle-Methods ********************** **/


/** ********************** Init-Methods **************************** **/
private fun initOnCreate() {
setContent {
StopWatchServiceAppTheme {
if (isBound) {
// Only if the service is connected, Then show the UI
MainScreen(stopwatchService = stopwatchService)
}
}
}
requestPermissions(Manifest.permission.POST_NOTIFICATIONS)
}

private fun initOnStart() {
// Bind the service
Intent(this, StopwatchService::class.java).also { intent ->
bindService(intent, connection, BIND_AUTO_CREATE)
}
}

private fun initOnStop() {
// Unbind the service
unbindService(connection)
// Update the flag that service is unbinded
isBound = false
}
/** ********************** Init-Methods **************************** **/


/** ********************** Other Functions ************************* **/
/**
* Initialize the service
* StartService:-> To be done in OnStart
* StartService:-> TO be done in OnDestroy
*/
private val connection = object : ServiceConnection {

override fun onServiceConnected(className: ComponentName, service: IBinder) {
// We get the binder instance
val binder = service as StopwatchService.StopwatchBinder
// Using the binder, We get the instance of the service to handle and control it
stopwatchService = binder.getService()
isBound = true
}

override fun onServiceDisconnected(arg0: ComponentName) {
isBound = false
}
}


/**
* Request permission to display the notification
*/
private fun requestPermissions(vararg permissions: String) {

val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { result ->
Expand All @@ -67,10 +119,6 @@ class MainActivity : ComponentActivity() {
}
requestPermissionLauncher.launch(permissions.asList().toTypedArray())
}
/** ********************** Other Functions ************************* **/

override fun onStop() {
super.onStop()
unbindService(connection)
isBound = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.Build
import android.os.IBinder
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.runtime.mutableStateOf
import androidx.core.app.NotificationCompat
import com.istudio.app.R
import com.istudio.app.util.Constants.ACTION_SERVICE_CANCEL
import com.istudio.app.util.Constants.ACTION_SERVICE_START
import com.istudio.app.util.Constants.ACTION_SERVICE_STOP
Expand Down

0 comments on commit 22fdf6e

Please sign in to comment.