1d44a6d96SWill Schurman // Copyright 2015-present 650 Industries. All rights reserved. 2d44a6d96SWill Schurman package host.exp.exponent 3d44a6d96SWill Schurman 4d44a6d96SWill Schurman import android.app.IntentService 5d44a6d96SWill Schurman import android.content.Context 6d44a6d96SWill Schurman import android.content.Intent 7d44a6d96SWill Schurman import android.os.Handler 8d44a6d96SWill Schurman import host.exp.exponent.di.NativeModuleDepsProvider 9d44a6d96SWill Schurman import host.exp.exponent.experience.ExperienceActivity 10d44a6d96SWill Schurman import host.exp.exponent.kernel.Kernel 11d44a6d96SWill Schurman import host.exp.exponent.kernel.KernelConstants 12d44a6d96SWill Schurman import javax.inject.Inject 13d44a6d96SWill Schurman 14d44a6d96SWill Schurman private const val ACTION_RELOAD_EXPERIENCE = "host.exp.exponent.action.RELOAD_EXPERIENCE" 15d44a6d96SWill Schurman private const val ACTION_STAY_AWAKE = "host.exp.exponent.action.STAY_AWAKE" 16d44a6d96SWill Schurman private const val STAY_AWAKE_MS = (1000 * 60).toLong() 17d44a6d96SWill Schurman 18d44a6d96SWill Schurman class ExponentIntentService : IntentService("ExponentIntentService") { 19d44a6d96SWill Schurman @Inject 20d44a6d96SWill Schurman lateinit var kernel: Kernel 21d44a6d96SWill Schurman 22d44a6d96SWill Schurman private val handler = Handler() 23d44a6d96SWill Schurman onCreatenull24d44a6d96SWill Schurman override fun onCreate() { 25d44a6d96SWill Schurman super.onCreate() 26094c4dddSWill Schurman NativeModuleDepsProvider.instance.inject(ExponentIntentService::class.java, this) 27d44a6d96SWill Schurman } 28d44a6d96SWill Schurman onHandleIntentnull29d44a6d96SWill Schurman override fun onHandleIntent(intent: Intent?) { 30d44a6d96SWill Schurman if (intent == null) { 31d44a6d96SWill Schurman return 32d44a6d96SWill Schurman } 33d44a6d96SWill Schurman 34d44a6d96SWill Schurman val action = intent.action 35d44a6d96SWill Schurman var isUserAction = false 36d44a6d96SWill Schurman when (action) { 37d44a6d96SWill Schurman ACTION_RELOAD_EXPERIENCE -> { 38d44a6d96SWill Schurman isUserAction = true 39d44a6d96SWill Schurman handleActionReloadExperience(intent.getStringExtra(KernelConstants.MANIFEST_URL_KEY)!!) 40d44a6d96SWill Schurman } 41d44a6d96SWill Schurman ACTION_STAY_AWAKE -> handleActionStayAwake() 42d44a6d96SWill Schurman } 43d44a6d96SWill Schurman if (isUserAction) { 44d44a6d96SWill Schurman val kernelActivityContext = kernel.activityContext 45d44a6d96SWill Schurman if (kernelActivityContext is ExperienceActivity) { 46d44a6d96SWill Schurman kernelActivityContext.onNotificationAction() 47d44a6d96SWill Schurman } 48d44a6d96SWill Schurman } 49d44a6d96SWill Schurman } 50d44a6d96SWill Schurman handleActionReloadExperiencenull51d44a6d96SWill Schurman private fun handleActionReloadExperience(manifestUrl: String) { 52d44a6d96SWill Schurman kernel.reloadVisibleExperience(manifestUrl) 53*1c99fe23SŁukasz Kosmaty 54*1c99fe23SŁukasz Kosmaty // Application can't close system dialogs on Android 31 or higher. 55*1c99fe23SŁukasz Kosmaty // See https://developer.android.com/about/versions/12/behavior-changes-all#close-system-dialogs 56*1c99fe23SŁukasz Kosmaty if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.S) { 57d44a6d96SWill Schurman val intent = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS) 58d44a6d96SWill Schurman sendBroadcast(intent) 59*1c99fe23SŁukasz Kosmaty } 60*1c99fe23SŁukasz Kosmaty 61d44a6d96SWill Schurman stopSelf() 62d44a6d96SWill Schurman } 63d44a6d96SWill Schurman handleActionStayAwakenull64d44a6d96SWill Schurman private fun handleActionStayAwake() { 65d44a6d96SWill Schurman handler.postDelayed({ stopSelf() }, STAY_AWAKE_MS) 66d44a6d96SWill Schurman } 67d44a6d96SWill Schurman 68d44a6d96SWill Schurman companion object { getActionReloadExperiencenull697113164bSWill Schurman @JvmStatic fun getActionReloadExperience(context: Context, manifestUrl: String): Intent { 70d44a6d96SWill Schurman return Intent(context, ExponentIntentService::class.java).apply { 71d44a6d96SWill Schurman action = ACTION_RELOAD_EXPERIENCE 72d44a6d96SWill Schurman putExtra(KernelConstants.MANIFEST_URL_KEY, manifestUrl) 73d44a6d96SWill Schurman } 74d44a6d96SWill Schurman } 75d44a6d96SWill Schurman getActionStayAwakenull767113164bSWill Schurman @JvmStatic fun getActionStayAwake(context: Context): Intent { 77d44a6d96SWill Schurman return Intent(context, ExponentIntentService::class.java).apply { 78d44a6d96SWill Schurman action = ACTION_STAY_AWAKE 79d44a6d96SWill Schurman } 80d44a6d96SWill Schurman } 81d44a6d96SWill Schurman } 82d44a6d96SWill Schurman } 83