|
| 1 | +package io.swan.rnbrowser |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import android.net.Uri |
| 5 | + |
| 6 | +import androidx.annotation.ColorInt |
| 7 | +import androidx.browser.customtabs.CustomTabColorSchemeParams |
| 8 | +import androidx.browser.customtabs.CustomTabsIntent |
| 9 | +import androidx.core.content.ContextCompat |
| 10 | +import androidx.core.graphics.ColorUtils |
| 11 | + |
| 12 | +import com.facebook.react.bridge.Promise |
| 13 | +import com.facebook.react.bridge.ReactApplicationContext |
| 14 | +import com.facebook.react.bridge.ReadableMap |
| 15 | +import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter |
| 16 | + |
| 17 | +import io.swan.rnbrowser.helpers.CustomTabActivityHelper |
| 18 | + |
| 19 | +object RNSwanBrowserModuleImpl { |
| 20 | + const val NAME = "RNSwanBrowser" |
| 21 | + private var browserVisible = false |
| 22 | + |
| 23 | + internal fun onHostResume(reactContext: ReactApplicationContext) { |
| 24 | + if (browserVisible && reactContext.hasActiveReactInstance()) { |
| 25 | + browserVisible = false |
| 26 | + |
| 27 | + reactContext |
| 28 | + .getJSModule(RCTDeviceEventEmitter::class.java) |
| 29 | + .emit("swanBrowserDidClose", null) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + internal fun open( |
| 34 | + reactContext: ReactApplicationContext, |
| 35 | + url: String, |
| 36 | + options: ReadableMap, |
| 37 | + promise: Promise |
| 38 | + ) { |
| 39 | + if (browserVisible) { |
| 40 | + return promise.reject( |
| 41 | + "swan_browser_visible", |
| 42 | + "An instance of the swan browser is already visible" |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + val activity = reactContext.currentActivity |
| 47 | + ?: return promise.reject( |
| 48 | + "no_current_activity", |
| 49 | + "Couldn't call open() when the app is in background" |
| 50 | + ) |
| 51 | + |
| 52 | + browserVisible = true |
| 53 | + |
| 54 | + val intentBuilder = CustomTabsIntent.Builder().apply { |
| 55 | + setBookmarksButtonEnabled(false) |
| 56 | + setDownloadButtonEnabled(false) |
| 57 | + setInstantAppsEnabled(false) |
| 58 | + setSendToExternalDefaultHandlerEnabled(false) |
| 59 | + setShowTitle(false) |
| 60 | + |
| 61 | + if (options.getString("animationType") == "fade") { |
| 62 | + setStartAnimations(activity, com.facebook.react.R.anim.catalyst_fade_in, R.anim.inert) |
| 63 | + setExitAnimations(activity, R.anim.inert, com.facebook.react.R.anim.catalyst_fade_out) |
| 64 | + } else { |
| 65 | + setStartAnimations(activity, com.facebook.react.R.anim.catalyst_slide_up, R.anim.inert) |
| 66 | + setExitAnimations(activity, R.anim.inert, com.facebook.react.R.anim.catalyst_slide_down) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @ColorInt val blackColor = ContextCompat.getColor(activity ,android.R.color.black) |
| 71 | + |
| 72 | + val paramsBuilder = CustomTabColorSchemeParams.Builder().apply { |
| 73 | + setNavigationBarColor(blackColor) |
| 74 | + |
| 75 | + if (options.hasKey("barTintColor")) { |
| 76 | + @ColorInt val barTintColor = options.getInt("barTintColor") |
| 77 | + |
| 78 | + setToolbarColor(barTintColor) |
| 79 | + setSecondaryToolbarColor(barTintColor) |
| 80 | + |
| 81 | + intentBuilder.setColorScheme( |
| 82 | + when (ColorUtils.calculateLuminance(barTintColor) > 0.5) { |
| 83 | + true -> CustomTabsIntent.COLOR_SCHEME_LIGHT |
| 84 | + false -> CustomTabsIntent.COLOR_SCHEME_DARK |
| 85 | + } |
| 86 | + ) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + intentBuilder.setDefaultColorSchemeParams(paramsBuilder.build()) |
| 91 | + |
| 92 | + val customTabsIntent = intentBuilder.build().apply { |
| 93 | + intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) |
| 94 | + intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) |
| 95 | + } |
| 96 | + |
| 97 | + CustomTabActivityHelper.openCustomTab( |
| 98 | + activity, customTabsIntent, Uri.parse(url) |
| 99 | + ) { currentActivity, uri -> |
| 100 | + currentActivity.startActivity(Intent(Intent.ACTION_VIEW, uri)) |
| 101 | + } |
| 102 | + |
| 103 | + promise.resolve(null) |
| 104 | + } |
| 105 | +} |
0 commit comments