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