xref: /aosp_15_r20/external/android_onboarding/java/com/android/onboarding/process/OnboardingProcessImpl.kt (revision c625018464ae97c56936c82b1b617e11aa899faa)
1 package com.android.onboarding.process
2 
3 import android.annotation.TargetApi
4 import android.content.Context
5 import android.content.Intent
6 import android.os.Build
7 import android.util.Log
8 import javax.inject.Singleton
9 
10 /** Default production implementation of [OnboardingProcess]. */
11 @Singleton
12 @TargetApi(Build.VERSION_CODES.O)
13 class OnboardingProcessImpl(private val context: Context) : OnboardingProcess {
14 
keepAlivenull15   override fun keepAlive(reason: KeepAliveReason): KeepAliveToken {
16     context.startForegroundService(Intent(context, NotificationKeepAliveService::class.java))
17 
18     val keepAliveToken = KeepAliveToken()
19     currentTokens.add(keepAliveToken)
20 
21     return keepAliveToken
22   }
23 
24   /** This token is used by the calling process to terminate the KeepAlive Service API. */
25   inner class KeepAliveToken : AutoCloseable {
26 
closenull27     override fun close() {
28       Log.d("OnboardingProcessImpl", "KeepAliveToken#close invoked - $this")
29 
30       currentTokens.remove(this)
31 
32       if (currentTokens.isEmpty()) {
33         context.stopService(Intent(context, NotificationKeepAliveService::class.java))
34       }
35     }
36   }
37 
38   private companion object {
39 
40     val currentTokens = mutableSetOf<KeepAliveToken>()
41   }
42 }
43