Skip to content

Commit

Permalink
Merge pull request #228 from droibit/feature/refactoring
Browse files Browse the repository at this point in the history
Refactor native codes in `flutter_custom_tabs_android`
  • Loading branch information
droibit authored Jan 11, 2025
2 parents c951e57 + 85bd810 commit c22f854
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.github.droibit.flutter.plugins.customtabs.core.ExternalBrowserLaunche
import com.github.droibit.flutter.plugins.customtabs.core.NativeAppLauncher
import com.github.droibit.flutter.plugins.customtabs.core.PartialCustomTabsLauncher
import com.github.droibit.flutter.plugins.customtabs.core.session.CustomTabsSessionManager
import com.github.droibit.flutter.plugins.customtabs.core.utils.CODE_LAUNCH_ERROR

@RestrictTo(RestrictTo.Scope.LIBRARY)
internal class CustomTabsLauncher @VisibleForTesting constructor(
Expand Down Expand Up @@ -153,4 +152,8 @@ internal class CustomTabsLauncher @VisibleForTesting constructor(
pm.resolveService(intent, flags)
}
}

private companion object {
const val CODE_LAUNCH_ERROR = "LAUNCH_ERROR"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.github.droibit.flutter.plugins.customtabs.core.options.CustomTabsColo
import com.github.droibit.flutter.plugins.customtabs.core.options.CustomTabsIntentOptions
import com.github.droibit.flutter.plugins.customtabs.core.options.PartialCustomTabsConfiguration
import com.github.droibit.flutter.plugins.customtabs.core.session.CustomTabsSessionProvider
import com.github.droibit.flutter.plugins.customtabs.core.utils.extractBundle
import com.github.droibit.flutter.plugins.customtabs.core.utils.bundleOf

class CustomTabsIntentFactory @VisibleForTesting internal constructor(
private val resources: ResourceFactory
Expand Down Expand Up @@ -130,7 +130,7 @@ class CustomTabsIntentFactory @VisibleForTesting internal constructor(
options: BrowserConfiguration
) {
val rawIntent = customTabsIntent.intent
options.headers?.let { rawIntent.putExtra(EXTRA_HEADERS, extractBundle(it)) }
options.headers?.let { rawIntent.putExtra(EXTRA_HEADERS, bundleOf(it)) }

// Avoid overriding the package if using CustomTabsSession.
if (rawIntent.getPackage() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.net.Uri
import android.provider.Browser.EXTRA_HEADERS
import androidx.annotation.VisibleForTesting
import com.github.droibit.flutter.plugins.customtabs.core.options.CustomTabsIntentOptions
import com.github.droibit.flutter.plugins.customtabs.core.utils.extractBundle
import com.github.droibit.flutter.plugins.customtabs.core.utils.bundleOf

class ExternalBrowserLauncher {
fun launch(context: Context, uri: Uri, options: CustomTabsIntentOptions?): Boolean {
Expand All @@ -26,7 +26,7 @@ class ExternalBrowserLauncher {
val browserOptions = options.browser ?: return null
val prefersExternalBrowser = browserOptions.prefersExternalBrowser
if (prefersExternalBrowser == true) {
browserOptions.headers?.let { intent.putExtra(EXTRA_HEADERS, extractBundle(it)) }
browserOptions.headers?.let { intent.putExtra(EXTRA_HEADERS, bundleOf(it)) }
return intent
}
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.net.Uri
import androidx.browser.customtabs.CustomTabsIntent
import androidx.browser.customtabs.CustomTabsIntent.EXTRA_ACTIVITY_HEIGHT_RESIZE_BEHAVIOR
import androidx.browser.customtabs.CustomTabsIntent.EXTRA_INITIAL_ACTIVITY_HEIGHT_PX
import com.github.droibit.flutter.plugins.customtabs.core.utils.REQUEST_CODE_PARTIAL_CUSTOM_TABS

class PartialCustomTabsLauncher {
fun launch(activity: Activity, uri: Uri, customTabsIntent: CustomTabsIntent): Boolean {
Expand All @@ -20,4 +19,8 @@ class PartialCustomTabsLauncher {
}
return false
}

private companion object {
const val REQUEST_CODE_PARTIAL_CUSTOM_TABS = 1001
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import android.os.Bundle

internal const val TAG = "CustomTabsAndroid"

internal const val CODE_LAUNCH_ERROR: String = "LAUNCH_ERROR"
internal const val REQUEST_CODE_PARTIAL_CUSTOM_TABS = 1001

internal fun extractBundle(headers: Map<String, String>): Bundle {
internal fun bundleOf(headers: Map<String, String>): Bundle {
return Bundle(headers.size).apply {
for ((key, value) in headers) {
putString(key, value)
Expand Down

0 comments on commit c22f854

Please sign in to comment.