1 // Copyright 2015-present 650 Industries. All rights reserved.
2 package host.exp.exponent
3 
4 import android.os.Debug
5 import androidx.multidex.MultiDexApplication
6 import com.facebook.react.bridge.ReactApplicationContext
7 import com.facebook.soloader.SoLoader
8 import host.exp.exponent.analytics.Analytics
9 import host.exp.exponent.analytics.EXL
10 import host.exp.exponent.branch.BranchManager
11 import host.exp.exponent.di.NativeModuleDepsProvider
12 import host.exp.exponent.kernel.*
13 import host.exp.exponent.kernel.ExponentKernelModuleProvider.ExponentKernelModuleFactory
14 import host.exp.exponent.kernel.KernelProvider.KernelFactory
15 import host.exp.exponent.modules.ExponentKernelModule
16 import host.exp.exponent.storage.ExponentSharedPreferences
17 import host.exp.expoview.ExpoViewBuildConfig
18 import host.exp.expoview.Exponent
19 import me.leolin.shortcutbadger.ShortcutBadger
20 import javax.inject.Inject
21 
22 abstract class ExpoApplication : MultiDexApplication() {
23   // Override me!
24   abstract val isDebug: Boolean
25 
26   @Inject
27   lateinit var exponentSharedPreferences: ExponentSharedPreferences
28 
29   override fun onCreate() {
30     super.onCreate()
31 
32     ExpoViewBuildConfig.DEBUG = isDebug
33     ExpoViewBuildConfig.USE_INTERNET_KERNEL = shouldUseInternetKernel()
34 
35     if (ExpoViewBuildConfig.DEBUG && Constants.WAIT_FOR_DEBUGGER) {
36       Debug.waitForDebugger()
37     }
38 
39     if (!Constants.isStandaloneApp()) {
40       KernelConstants.MAIN_ACTIVITY_CLASS = LauncherActivity::class.java
41     }
42 
43     KernelProvider.setFactory(object : KernelFactory {
44       override fun create(): KernelInterface {
45         return Kernel()
46       }
47     })
48 
49     ExponentKernelModuleProvider.setFactory(object : ExponentKernelModuleFactory {
50       override fun create(reactContext: ReactApplicationContext): ExponentKernelModuleInterface {
51         return ExponentKernelModule(
52           reactContext
53         )
54       }
55     })
56 
57     Exponent.initialize(this, this)
58 
59     NativeModuleDepsProvider.instance.add(Kernel::class.java, KernelProvider.instance)
60     NativeModuleDepsProvider.instance.add(DevMenuManager::class.java, DevMenuManager())
61     NativeModuleDepsProvider.instance.inject(ExpoApplication::class.java, this)
62 
63     BranchManager.initialize(this)
64 
65     try {
66       // Remove the badge count on weird launchers
67       // TODO: doesn't work on the Xiaomi phone. bug with the library
68       ShortcutBadger.removeCount(this)
69     } catch (e: Throwable) {
70       EXL.e(TAG, e)
71     }
72 
73     if (Constants.DEBUG_COLD_START_METHOD_TRACING) {
74       Debug.startMethodTracing("coldStart")
75     }
76 
77     Analytics.markEvent(Analytics.TimedEvent.LAUNCHER_ACTIVITY_STARTED)
78     SoLoader.init(applicationContext, false)
79 
80     // Add exception handler. This is used by the entire process, so only need to add it here.
81     Thread.setDefaultUncaughtExceptionHandler(
82       ExponentUncaughtExceptionHandler(
83         applicationContext
84       )
85     )
86   }
87 
88   // we're leaving this stub in here so that if people don't modify their MainApplication to
89   // remove the override of shouldUseInternetKernel() their project will still build without errors
90   fun shouldUseInternetKernel(): Boolean {
91     return !isDebug
92   }
93 
94   companion object {
95     private val TAG = ExpoApplication::class.java.simpleName
96   }
97 }
98