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.analytics.Analytics 9 import host.exp.exponent.di.NativeModuleDepsProvider 10 import host.exp.exponent.experience.ExperienceActivity 11 import host.exp.exponent.kernel.Kernel 12 import host.exp.exponent.kernel.KernelConstants 13 import javax.inject.Inject 14 15 private const val ACTION_RELOAD_EXPERIENCE = "host.exp.exponent.action.RELOAD_EXPERIENCE" 16 private const val ACTION_STAY_AWAKE = "host.exp.exponent.action.STAY_AWAKE" 17 private const val STAY_AWAKE_MS = (1000 * 60).toLong() 18 19 class ExponentIntentService : IntentService("ExponentIntentService") { 20 @Inject 21 lateinit var kernel: Kernel 22 23 private val handler = Handler() 24 25 override fun onCreate() { 26 super.onCreate() 27 NativeModuleDepsProvider.getInstance().inject(ExponentIntentService::class.java, this) 28 } 29 30 override fun onHandleIntent(intent: Intent?) { 31 if (intent == null) { 32 return 33 } 34 35 val action = intent.action 36 var isUserAction = false 37 when (action) { 38 ACTION_RELOAD_EXPERIENCE -> { 39 isUserAction = true 40 handleActionReloadExperience(intent.getStringExtra(KernelConstants.MANIFEST_URL_KEY)!!) 41 } 42 ACTION_STAY_AWAKE -> handleActionStayAwake() 43 } 44 if (isUserAction) { 45 val kernelActivityContext = kernel.activityContext 46 if (kernelActivityContext is ExperienceActivity) { 47 kernelActivityContext.onNotificationAction() 48 } 49 } 50 } 51 52 private fun handleActionReloadExperience(manifestUrl: String) { 53 kernel.reloadVisibleExperience(manifestUrl) 54 val intent = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS) 55 sendBroadcast(intent) 56 Analytics.logEventWithManifestUrl(Analytics.RELOAD_EXPERIENCE, manifestUrl) 57 stopSelf() 58 } 59 60 private fun handleActionStayAwake() { 61 handler.postDelayed({ stopSelf() }, STAY_AWAKE_MS) 62 } 63 64 companion object { 65 @JvmStatic fun getActionReloadExperience(context: Context, manifestUrl: String): Intent { 66 return Intent(context, ExponentIntentService::class.java).apply { 67 action = ACTION_RELOAD_EXPERIENCE 68 putExtra(KernelConstants.MANIFEST_URL_KEY, manifestUrl) 69 } 70 } 71 72 @JvmStatic fun getActionStayAwake(context: Context): Intent { 73 return Intent(context, ExponentIntentService::class.java).apply { 74 action = ACTION_STAY_AWAKE 75 } 76 } 77 } 78 } 79