Chrome 自定义标签在代理后面失败

问题描述

我们有一个 Android 应用程序打开 Chrome 自定义标签来执行用户身份验证。 (下面列出了片段代码的剥离版本。)

当移动网络配置了代理地址:端口时,CCT 浏览器会显示一个弹出窗口以收集凭据。这有 3 个问题:

  1. 浏览器无法进行身份验证并崩溃
  2. 弹出窗口很粗糙,与我们应用的对话框风格不匹配
  3. 应用程序通常已经有凭据,因此不需要对话框——一种方法应该允许在没有用户输入的情况下插入它们

我们的旧实现使用具有 onReceivedHttpAuthRequest() 回调的 WebViewClient。这将控制权交还给应用,以显示它自己的对话框或直接提供凭据。

有没有办法对 CCT 做同样的事情?

open class CustomTabsFragment {
    ...
    private var cctServiceConnection: CustomTabsServiceConnection? = null
    private var cctClient: CustomTabsClient? = null
    private var cctSession: CustomTabsSession? = null
    private var cctBuilder = CustomTabsIntent.Builder()
    private lateinit var chromePackageName: String
    lateinit var url: String

    override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
        ...
        // get Bundle arguments
        url = requiredArguments().getString("url","")
        chromePackageName = requiredArguments().getString("package","")
        ...
    }

    override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
        super.onViewCreated(view,savedInstanceState)

        val uri = Uri.parse(url)
        cctServiceConnection = object : CustomTabsServiceConnection() {
            override fun onCustomTabsServiceConnected(name: ComponentName,mClient: CustomTabsClient) {
                cctClient = mClient
                cctClient?.let {
                    it.warmup(0L)
                    cctSession = it.newSession(chromeCustomTabsCallback)
                }
                cctSession?.let {
                    it.mayLaunchUrl(uri,null,null)
                    cctBuilder.setSession(it)

                    val customTabsIntent = cctBuilder
                        .setShowTitle(true)
                        .setDefaultShareMenuItemEnabled(false)
                        .setUrlBarHidingEnabled(true)
                        .setStartAnimations(requireActivity(),android.R.anim.fade_in,android.R.anim.fade_out)
                        .setExitAnimations(requireActivity(),android.R.anim.fade_out)
                        .build()

                    customTabsIntent.intent.data = uri
                    startActivityForResult(customTabsIntent.intent,1000)
                }
            }

            override fun onServicedisconnected(name: ComponentName?) {
                cctClient = null
                cctSession = null
            }
        }

        // Binds to the service.
        cctServiceConnection?.let {
            CustomTabsClient.bindCustomTabsService(requireContext(),chromePackageName,it)
        }
    }

    override fun onActivityResult(requestCode: Int,resultCode: Int,data: Intent?) {
        val loginActivity = activity as? LoginActivity
        loginActivity?.let {
            if (requestCode == 1000 && !it.isAuthenticated) {
                // Handle close or back pressed when opening web page via chrome custom tabs
                findNavController().navigate(R.id.destinationFragment)
            }
        }
        super.onActivityResult(requestCode,resultCode,data)
    }

    private val chromeCustomTabsCallback = object : CustomTabsCallback() {
        override fun onNavigationEvent(navigationEvent: Int,extras: Bundle?) {
            super.onNavigationEvent(navigationEvent,extras)
            when (navigationEvent) {
                NAVIGATION_STARTED -> Logger("Chrome Custom Tabs Navigation started - the tab has started loading a page.")
                NAVIGATION_FINISHED -> Logger("Chrome Custom Tabs Navigation finished - the tab has finished loading a page.")
                NAVIGATION_Failed -> Logger("Chrome Custom Tabs Navigation Failed - the tab Couldn't finish loading due to a failure.")
                NAVIGATION_ABORTED -> Logger("Chrome Custom Tabs Navigation aborted - loading was aborted by a user action before it finishes like clicking on a link or refreshing the page.")
                TAB_SHOWN -> Logger("Chrome Custom Tabs Navigation tab shown")
                TAB_HIDDEN -> Logger("Chrome Custom Tabs Navigation tab hidden")
                else -> Logger("Chrome Custom Tabs Navigation UnkNown")
            }
        }
    }

    ...
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...