|
| 1 | +package com.inappbrowsernitro.browser |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.Intent |
| 5 | +import android.graphics.Bitmap |
| 6 | +import android.graphics.Canvas |
| 7 | +import android.graphics.Color |
| 8 | +import android.graphics.Paint |
| 9 | +import android.graphics.Path |
| 10 | +import android.net.Uri |
| 11 | +import android.os.Build |
| 12 | +import android.os.Bundle |
| 13 | +import androidx.browser.customtabs.CustomTabColorSchemeParams |
| 14 | +import androidx.browser.customtabs.CustomTabsIntent |
| 15 | +import androidx.browser.customtabs.CustomTabsSession |
| 16 | +import com.margelo.nitro.inappbrowsernitro.BrowserAnimations |
| 17 | +import com.margelo.nitro.inappbrowsernitro.BrowserColorScheme |
| 18 | +import com.margelo.nitro.inappbrowsernitro.BrowserShareState |
| 19 | +import com.margelo.nitro.inappbrowsernitro.DynamicColor |
| 20 | +import com.margelo.nitro.inappbrowsernitro.InAppBrowserOptions |
| 21 | + |
| 22 | +internal class CustomTabsIntentFactory( |
| 23 | + private val context: Context, |
| 24 | + private val session: CustomTabsSession? |
| 25 | +) { |
| 26 | + fun create(options: InAppBrowserOptions?): CustomTabsIntent { |
| 27 | + val builder = session?.let { CustomTabsIntent.Builder(it) } ?: CustomTabsIntent.Builder() |
| 28 | + |
| 29 | + applyColors(builder, options) |
| 30 | + applyBehaviours(builder, options) |
| 31 | + applyNavigation(builder, options) |
| 32 | + applyAnimations(builder, options?.animations) |
| 33 | + |
| 34 | + val intent = builder.build() |
| 35 | + |
| 36 | + configureIntent(intent.intent, options) |
| 37 | + |
| 38 | + options?.browserPackage?.takeIf { it.isNotBlank() }?.let(intent.intent::setPackage) |
| 39 | + |
| 40 | + return intent |
| 41 | + } |
| 42 | + |
| 43 | + private fun applyColors(builder: CustomTabsIntent.Builder, options: InAppBrowserOptions?) { |
| 44 | + val toolbarParams = buildColorParams(options?.toolbarColor) |
| 45 | + if (toolbarParams != null) { |
| 46 | + builder.setDefaultColorSchemeParams(toolbarParams.system) |
| 47 | + builder.setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_LIGHT, toolbarParams.light) |
| 48 | + builder.setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, toolbarParams.dark) |
| 49 | + } |
| 50 | + |
| 51 | + buildColorParams(options?.secondaryToolbarColor)?.systemColor?.let { |
| 52 | + builder.setSecondaryToolbarColor(it) |
| 53 | + } |
| 54 | + |
| 55 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { |
| 56 | + buildColorParams(options?.navigationBarColor)?.systemColor?.let { color -> |
| 57 | + builder.setNavigationBarColor(color) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { |
| 62 | + buildColorParams(options?.navigationBarDividerColor)?.systemColor?.let { color -> |
| 63 | + builder.setNavigationBarDividerColor(color) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + options?.colorScheme?.let { scheme -> |
| 68 | + builder.setColorScheme(scheme.toCustomTabsScheme()) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private fun applyBehaviours(builder: CustomTabsIntent.Builder, options: InAppBrowserOptions?) { |
| 73 | + builder.setShowTitle(options?.showTitle ?: true) |
| 74 | + |
| 75 | + options?.enableUrlBarHiding?.let(builder::setUrlBarHidingEnabled) |
| 76 | + |
| 77 | + when (options?.shareState) { |
| 78 | + BrowserShareState.ON -> builder.setShareState(CustomTabsIntent.SHARE_STATE_ON) |
| 79 | + BrowserShareState.OFF -> builder.setShareState(CustomTabsIntent.SHARE_STATE_OFF) |
| 80 | + BrowserShareState.DEFAULT, null -> if (options?.enableDefaultShare == false) { |
| 81 | + builder.setShareState(CustomTabsIntent.SHARE_STATE_OFF) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + options?.instantAppsEnabled?.let(builder::setInstantAppsEnabled) |
| 86 | + |
| 87 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && options?.enablePartialCustomTab == true) { |
| 88 | + val height = (context.resources.displayMetrics.heightPixels * PARTIAL_TAB_RATIO).toInt() |
| 89 | + builder.setInitialActivityHeightPx(height, CustomTabsIntent.ACTIVITY_HEIGHT_ADJUSTABLE) |
| 90 | + } |
| 91 | + |
| 92 | + // Emulators rely on soft navigation buttons; keeping pull-to-refresh disabled avoids accidental reloads. |
| 93 | + if (options?.enablePullToRefresh == true) { |
| 94 | + builder.setUrlBarHidingEnabled(true) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private fun applyNavigation(builder: CustomTabsIntent.Builder, options: InAppBrowserOptions?) { |
| 99 | + if (options?.hasBackButton == true) { |
| 100 | + builder.setCloseButtonIcon(createBackArrow()) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private fun applyAnimations(builder: CustomTabsIntent.Builder, animations: BrowserAnimations?) { |
| 105 | + animations ?: return |
| 106 | + val startEnter = resolveAnimation(animations.startEnter) |
| 107 | + val startExit = resolveAnimation(animations.startExit) |
| 108 | + if (startEnter != null && startExit != null) { |
| 109 | + builder.setStartAnimations(context, startEnter, startExit) |
| 110 | + } |
| 111 | + |
| 112 | + val endEnter = resolveAnimation(animations.endEnter) |
| 113 | + val endExit = resolveAnimation(animations.endExit) |
| 114 | + if (endEnter != null && endExit != null) { |
| 115 | + builder.setExitAnimations(context, endEnter, endExit) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private fun configureIntent(intent: Intent, options: InAppBrowserOptions?) { |
| 120 | + if (options?.showInRecents == false) { |
| 121 | + intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) |
| 122 | + } |
| 123 | + |
| 124 | + if (options?.includeReferrer == true) { |
| 125 | + val referrer = Uri.parse("android-app://${context.packageName}") |
| 126 | + intent.putExtra(Intent.EXTRA_REFERRER, referrer) |
| 127 | + } |
| 128 | + |
| 129 | + options?.headers?.takeIf { it.isNotEmpty() }?.let { headers -> |
| 130 | + val bundle = Bundle() |
| 131 | + headers.forEach { (key, value) -> |
| 132 | + bundle.putString(key, value) |
| 133 | + } |
| 134 | + intent.putExtra(BROWSER_EXTRA_HEADERS, bundle) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private fun buildColorParams(color: DynamicColor?): ColorSchemeParams? { |
| 139 | + val system = DynamicColorResolver.resolveForScheme(color, DynamicColorResolver.DynamicScheme.SYSTEM) |
| 140 | + val light = DynamicColorResolver.resolveForScheme(color, DynamicColorResolver.DynamicScheme.LIGHT) |
| 141 | + val dark = DynamicColorResolver.resolveForScheme(color, DynamicColorResolver.DynamicScheme.DARK) |
| 142 | + |
| 143 | + if (system == null && light == null && dark == null) { |
| 144 | + return null |
| 145 | + } |
| 146 | + |
| 147 | + return ColorSchemeParams( |
| 148 | + system = CustomTabColorSchemeParams.Builder().apply { |
| 149 | + system?.let { setToolbarColor(it) } |
| 150 | + }.build(), |
| 151 | + light = CustomTabColorSchemeParams.Builder().apply { |
| 152 | + light?.let { setToolbarColor(it) } |
| 153 | + }.build(), |
| 154 | + dark = CustomTabColorSchemeParams.Builder().apply { |
| 155 | + dark?.let { setToolbarColor(it) } |
| 156 | + }.build(), |
| 157 | + systemColor = system |
| 158 | + ) |
| 159 | + } |
| 160 | + |
| 161 | + private fun resolveAnimation(name: String?): Int? { |
| 162 | + if (name.isNullOrBlank()) { |
| 163 | + return null |
| 164 | + } |
| 165 | + |
| 166 | + val identifier = context.resources.getIdentifier(name, "anim", context.packageName) |
| 167 | + return identifier.takeIf { it != 0 } |
| 168 | + } |
| 169 | + |
| 170 | + private fun createBackArrow(): Bitmap { |
| 171 | + val size = context.resources.displayMetrics.density * 24 |
| 172 | + val bitmap = Bitmap.createBitmap(size.toInt(), size.toInt(), Bitmap.Config.ARGB_8888) |
| 173 | + val canvas = Canvas(bitmap) |
| 174 | + |
| 175 | + val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { |
| 176 | + color = DEFAULT_CLOSE_BUTTON_COLOR |
| 177 | + style = Paint.Style.STROKE |
| 178 | + strokeWidth = context.resources.displayMetrics.density * 2 |
| 179 | + strokeCap = Paint.Cap.ROUND |
| 180 | + strokeJoin = Paint.Join.ROUND |
| 181 | + } |
| 182 | + |
| 183 | + val path = Path().apply { |
| 184 | + moveTo(size * 0.75f, size * 0.2f) |
| 185 | + lineTo(size * 0.35f, size * 0.5f) |
| 186 | + lineTo(size * 0.75f, size * 0.8f) |
| 187 | + } |
| 188 | + |
| 189 | + canvas.drawPath(path, paint) |
| 190 | + return bitmap |
| 191 | + } |
| 192 | + |
| 193 | + private data class ColorSchemeParams( |
| 194 | + val system: CustomTabColorSchemeParams, |
| 195 | + val light: CustomTabColorSchemeParams, |
| 196 | + val dark: CustomTabColorSchemeParams, |
| 197 | + val systemColor: Int?, |
| 198 | + ) |
| 199 | + |
| 200 | + private fun BrowserColorScheme.toCustomTabsScheme(): Int { |
| 201 | + return when (this) { |
| 202 | + BrowserColorScheme.LIGHT -> CustomTabsIntent.COLOR_SCHEME_LIGHT |
| 203 | + BrowserColorScheme.DARK -> CustomTabsIntent.COLOR_SCHEME_DARK |
| 204 | + BrowserColorScheme.SYSTEM -> CustomTabsIntent.COLOR_SCHEME_SYSTEM |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + private companion object { |
| 209 | + private const val PARTIAL_TAB_RATIO = 0.85f |
| 210 | + private const val BROWSER_EXTRA_HEADERS = "android.support.customtabs.extra.EXTRA_HEADERS" |
| 211 | + private val DEFAULT_CLOSE_BUTTON_COLOR = Color.argb(0xFF, 0x3A, 0x3A, 0x3A) |
| 212 | + } |
| 213 | +} |
0 commit comments