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