1 // Copyright 2015-present 650 Industries. All rights reserved. 2 package host.exp.exponent 3 4 import android.util.Log 5 import com.facebook.proguard.annotations.DoNotStrip 6 import com.facebook.react.common.JavascriptException 7 import host.exp.exponent.kernel.KernelNetworkInterceptor 8 import host.exp.exponent.network.ExponentNetwork 9 import host.exp.expoview.Exponent 10 import okhttp3.CookieJar 11 import okhttp3.OkHttpClient 12 import java.util.concurrent.TimeUnit 13 14 @DoNotStrip 15 object ReactNativeStaticHelpers { 16 private val TAG = ReactNativeStaticHelpers::class.java.simpleName 17 18 @DoNotStrip reloadFromManifestnull19 @JvmStatic fun reloadFromManifest(activityId: Int) { 20 try { 21 Class.forName("host.exp.exponent.kernel.Kernel") 22 .getMethod("reloadVisibleExperience", Int::class.javaPrimitiveType) 23 .invoke(null, activityId) 24 } catch (e: Exception) { 25 Log.e("reloadFromManifest", "Unable to reload visible experience", e) 26 } 27 } 28 29 @DoNotStrip getBundleUrlForActivityIdnull30 @JvmStatic fun getBundleUrlForActivityId( 31 activityId: Int, 32 host: String?, 33 mainModuleId: String?, 34 bundleTypeId: String?, 35 devMode: Boolean, 36 jsMinify: Boolean 37 ): String? { 38 return try { 39 Class.forName("host.exp.exponent.kernel.Kernel") 40 .getMethod( 41 "getBundleUrlForActivityId", 42 Int::class.javaPrimitiveType, 43 String::class.java, 44 String::class.java, 45 String::class.java, 46 Boolean::class.javaPrimitiveType, 47 Boolean::class.javaPrimitiveType 48 ) 49 .invoke(null, activityId, host, mainModuleId, bundleTypeId, devMode, jsMinify) as String 50 } catch (e: Exception) { 51 null 52 } 53 } 54 55 // <= SDK 25 56 @DoNotStrip getBundleUrlForActivityIdnull57 @JvmStatic fun getBundleUrlForActivityId( 58 activityId: Int, 59 host: String?, 60 jsModulePath: String?, 61 devMode: Boolean, 62 jsMinify: Boolean 63 ): String? { 64 return try { 65 Class.forName("host.exp.exponent.kernel.Kernel") 66 .getMethod( 67 "getBundleUrlForActivityId", 68 Int::class.javaPrimitiveType, 69 String::class.java, 70 String::class.java, 71 Boolean::class.javaPrimitiveType, 72 Boolean::class.javaPrimitiveType 73 ) 74 .invoke(null, activityId, host, jsModulePath, devMode, jsMinify) as String 75 } catch (e: Exception) { 76 null 77 } 78 } 79 80 // <= SDK 21 81 @DoNotStrip getBundleUrlForActivityIdnull82 @JvmStatic fun getBundleUrlForActivityId( 83 activityId: Int, 84 host: String?, 85 jsModulePath: String?, 86 devMode: Boolean, 87 hmr: Boolean, 88 jsMinify: Boolean 89 ): String? { 90 return try { 91 Class.forName("host.exp.exponent.kernel.Kernel") 92 .getMethod( 93 "getBundleUrlForActivityId", 94 Int::class.javaPrimitiveType, 95 String::class.java, 96 String::class.java, 97 Boolean::class.javaPrimitiveType, 98 Boolean::class.javaPrimitiveType, 99 Boolean::class.javaPrimitiveType 100 ) 101 .invoke(null, activityId, host, jsModulePath, devMode, hmr, jsMinify) as String 102 } catch (e: Exception) { 103 null 104 } 105 } 106 107 @DoNotStrip handleReactNativeErrornull108 @JvmStatic fun handleReactNativeError( 109 errorMessage: String?, 110 stackUnversioned: Any?, 111 exceptionId: Int?, 112 isFatal: Boolean? 113 ) { 114 try { 115 Class.forName("host.exp.exponent.kernel.Kernel").getMethod( 116 "handleReactNativeError", 117 String::class.java, 118 Any::class.java, 119 Int::class.java, 120 Boolean::class.java 121 ).invoke(null, errorMessage, stackUnversioned, exceptionId, isFatal) 122 } catch (e: Exception) { 123 throw JavascriptException(errorMessage) 124 } 125 } 126 127 @DoNotStrip handleReactNativeErrornull128 @JvmStatic fun handleReactNativeError( 129 throwable: Throwable?, 130 errorMessage: String?, 131 stackUnversioned: Any?, 132 exceptionId: Int?, 133 isFatal: Boolean? 134 ) { 135 try { 136 Class.forName("host.exp.exponent.kernel.Kernel").getMethod( 137 "handleReactNativeError", 138 Throwable::class.java, 139 String::class.java, 140 Any::class.java, 141 Int::class.java, 142 Boolean::class.java 143 ).invoke(null, throwable, errorMessage, stackUnversioned, exceptionId, isFatal) 144 } catch (e: Exception) { 145 throw JavascriptException(errorMessage) 146 } 147 } 148 149 @DoNotStrip getBundleSourceForPathnull150 @JvmStatic fun getBundleSourceForPath(path: String?): String? { 151 return try { 152 Exponent.instance.getBundleSource(path!!) 153 } catch (e: Exception) { 154 null 155 } 156 } 157 158 private var exponentNetwork: ExponentNetwork? = null setExponentNetworknull159 fun setExponentNetwork(exponentNetwork: ExponentNetwork?) { 160 this.exponentNetwork = exponentNetwork 161 } 162 163 @DoNotStrip getOkHttpClientnull164 @JvmStatic fun getOkHttpClient(callingClass: Class<*>): Any { 165 // we build the OkHttp client here so that one cache instance is shared by all concurrent OkHttp instances 166 val version = RNObject.versionForClassname(callingClass.name) 167 val cookieJar = 168 RNObject("com.facebook.react.modules.network.ReactCookieJarContainer").loadVersion(version) 169 .construct().get() 170 val client = OkHttpClient.Builder() 171 .connectTimeout(0, TimeUnit.MILLISECONDS) 172 .readTimeout(0, TimeUnit.MILLISECONDS) 173 .writeTimeout(0, TimeUnit.MILLISECONDS) 174 .cookieJar(cookieJar as CookieJar) 175 .cache(exponentNetwork!!.cache) 176 .addInterceptor(KernelNetworkInterceptor.okhttpAppInterceptorProxy) 177 .addNetworkInterceptor(KernelNetworkInterceptor.okhttpNetworkInterceptorProxy) 178 return client.build() 179 } 180 } 181