1 package com.android.onboarding.nodes.testing.testapp 2 3 import android.app.Service 4 import android.content.Intent 5 import android.os.IBinder 6 import android.util.Log 7 import com.android.onboarding.process.KeepAliveReason 8 import com.android.onboarding.process.OnboardingProcessImpl 9 10 /** 11 * This service is utilized by the Onboarding Graph TestApp to examine the behavior of the Keep 12 * Alive APIs. It provides a controlled mechanism to enable or disable process keep-alive 13 * functionality, facilitating testing of how the TestApp interacts with these APIs. 14 * 15 * **Responds to Start Intent:** The service expects a boolean extra named "usingKeepAlive" in its 16 * start Intent. 17 * * **True:** Instructs the underlying Keep Alive library to actively keep the process alive. 18 * * **False:** Directs the Keep Alive library to cease any active keep-alive behavior. 19 * 20 * **Testable Scenarios** 21 * 1. **Keep-Alive Enabled:** 22 * * Start the service with the "usingKeepAlive" extra set to `true`. 23 * * Verify that the TestApp process remains alive under conditions that would typically trigger 24 * termination (e.g., background state, low memory). 25 * 2. **Keep-Alive Disabled:** 26 * * Start the service with the "usingKeepAlive" extra set to `false`. 27 * * Verify that the TestApp process behaves normally and is subject to termination under 28 * expected conditions. 29 */ 30 class BackgroundTaskService : Service() { 31 32 private var isRunning = false 33 private var keepAliveToken: AutoCloseable? = null 34 onBindnull35 override fun onBind(intent: Intent?): IBinder? = null 36 37 override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { 38 Log.d("BackgroundTaskService", "BackgroundTaskService Started") 39 sendBroadcast(createStatusIntent(isRunning = true)) 40 41 isRunning = true 42 if (intent?.getBooleanExtra("usingKeepAlive", false) == true) { 43 keepAliveToken = 44 OnboardingProcessImpl(applicationContext).keepAlive(reason = KeepAliveReason.TEST_ONLY) 45 } 46 47 Thread { 48 while (isRunning) { 49 Log.d("BackgroundTaskService", "BackgroundTaskService is running...") 50 Thread.sleep(5000) 51 } 52 } 53 .start() 54 55 return START_STICKY 56 } 57 onDestroynull58 override fun onDestroy() { 59 super.onDestroy() 60 sendBroadcast(createStatusIntent(isRunning = false)) 61 62 isRunning = false 63 keepAliveToken?.close() 64 65 Log.d("BackgroundTaskService", "BackgroundTaskService Stopped") 66 } 67 createStatusIntentnull68 private fun createStatusIntent(isRunning: Boolean): Intent { 69 Log.d("BackgroundTaskService", "Service running status broadcast as $isRunning") 70 71 return Intent(BROADCAST_ACTION).putExtra(SERVICE_STATUS, isRunning) 72 } 73 74 companion object { 75 // Action for broadcasting the current running status of the [BackgroundTaskService]. 76 const val BROADCAST_ACTION = 77 "com.android.onboarding.nodes.testing.testapp.SERVICE_STATUS_UPDATE" 78 // Intent extra for storing the boolean value indicating the current [BackgroundTaskService] 79 // running status. 80 const val SERVICE_STATUS = "service_status" 81 } 82 } 83