1aa3c92eaSWill Schurman // Copyright 2015-present 650 Industries. All rights reserved.
2aa3c92eaSWill Schurman package host.exp.exponent.kernel
3aa3c92eaSWill Schurman 
4aa3c92eaSWill Schurman import android.app.Application
5aa3c92eaSWill Schurman import android.content.Context
6aa3c92eaSWill Schurman import android.content.pm.PackageManager
7aa3c92eaSWill Schurman import de.greenrobot.event.EventBus
8aa3c92eaSWill Schurman import host.exp.exponent.ExpoUpdatesAppLoader
9aa3c92eaSWill Schurman import host.exp.exponent.analytics.EXL
10aa3c92eaSWill Schurman import host.exp.exponent.di.NativeModuleDepsProvider
11aa3c92eaSWill Schurman import host.exp.exponent.kernel.KernelConstants.ExperienceOptions
12aa3c92eaSWill Schurman import host.exp.expoview.ExpoViewBuildConfig
13aa3c92eaSWill Schurman import javax.inject.Inject
14aa3c92eaSWill Schurman 
15aa3c92eaSWill Schurman class ExpoViewKernel private constructor() : KernelInterface() {
16aa3c92eaSWill Schurman   class ExpoViewErrorEvent internal constructor(val errorMessage: String)
17aa3c92eaSWill Schurman 
18aa3c92eaSWill Schurman   @Inject
19aa3c92eaSWill Schurman   lateinit var context: Context
20aa3c92eaSWill Schurman 
21aa3c92eaSWill Schurman   @Inject
22aa3c92eaSWill Schurman   lateinit var applicationContext: Application
23aa3c92eaSWill Schurman 
handleErrornull24aa3c92eaSWill Schurman   override fun handleError(errorMessage: String) {
25aa3c92eaSWill Schurman     if (ExpoViewBuildConfig.DEBUG) {
26aa3c92eaSWill Schurman       EventBus.getDefault().post(ExpoViewErrorEvent(errorMessage))
27aa3c92eaSWill Schurman     } else {
28aa3c92eaSWill Schurman       throw RuntimeException(errorMessage)
29aa3c92eaSWill Schurman     }
30aa3c92eaSWill Schurman   }
31aa3c92eaSWill Schurman 
handleErrornull32aa3c92eaSWill Schurman   override fun handleError(exception: Exception) {
33aa3c92eaSWill Schurman     if (ExpoViewBuildConfig.DEBUG) {
34aa3c92eaSWill Schurman       EventBus.getDefault().post(ExpoViewErrorEvent(exception.toString()))
35aa3c92eaSWill Schurman     } else {
36aa3c92eaSWill Schurman       throw RuntimeException(exception)
37aa3c92eaSWill Schurman     }
38aa3c92eaSWill Schurman   }
39aa3c92eaSWill Schurman 
openExperiencenull40aa3c92eaSWill Schurman   override fun openExperience(options: ExperienceOptions) {}
41aa3c92eaSWill Schurman 
getAppLoaderForManifestUrlnull42aa3c92eaSWill Schurman   override fun getAppLoaderForManifestUrl(manifestUrl: String?): ExpoUpdatesAppLoader? {
43aa3c92eaSWill Schurman     return null
44aa3c92eaSWill Schurman   }
45aa3c92eaSWill Schurman 
reloadVisibleExperiencenull46aa3c92eaSWill Schurman   override fun reloadVisibleExperience(manifestUrl: String, forceCache: Boolean): Boolean {
47aa3c92eaSWill Schurman     return false
48aa3c92eaSWill Schurman   }
49aa3c92eaSWill Schurman 
50aa3c92eaSWill Schurman   val versionName: String?
51aa3c92eaSWill Schurman 
52aa3c92eaSWill Schurman   companion object {
53aa3c92eaSWill Schurman     private val TAG = ExpoViewKernel::class.java.simpleName
54aa3c92eaSWill Schurman 
<lambda>null55aa3c92eaSWill Schurman     @JvmStatic val instance: ExpoViewKernel by lazy {
56aa3c92eaSWill Schurman       ExpoViewKernel()
57aa3c92eaSWill Schurman     }
58aa3c92eaSWill Schurman   }
59aa3c92eaSWill Schurman 
60aa3c92eaSWill Schurman   init {
61*094c4dddSWill Schurman     NativeModuleDepsProvider.instance.inject(ExpoViewKernel::class.java, this)
62aa3c92eaSWill Schurman 
63aa3c92eaSWill Schurman     versionName = try {
64aa3c92eaSWill Schurman       applicationContext.packageManager.getPackageInfo(
65aa3c92eaSWill Schurman         context.packageName, 0
66aa3c92eaSWill Schurman       ).versionName
67aa3c92eaSWill Schurman     } catch (e: PackageManager.NameNotFoundException) {
68aa3c92eaSWill Schurman       EXL.e(TAG, e)
69aa3c92eaSWill Schurman       null
70aa3c92eaSWill Schurman     } catch (e: Throwable) {
71aa3c92eaSWill Schurman       EXL.e(TAG, e)
72aa3c92eaSWill Schurman       null
73aa3c92eaSWill Schurman     }
74aa3c92eaSWill Schurman   }
75aa3c92eaSWill Schurman }
76