diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 565bddf..abd90df 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -45,7 +45,18 @@ + android:windowSoftInputMode="adjustResize" + android:launchMode="singleTask"> + + + + + + + + () { activity?.toast(R.string.LoginFailTip) } }) - initWeb() - + + // Check if we're being called back from OAuth + activity?.intent?.data?.let { uri -> + if (uri.scheme == "gsygithubapp" && uri.host == "authed") { + val code = uri.getQueryParameter("code") + if (code != null) { + binding!!.oauthWebviewLoadingBar.visibility = View.VISIBLE + context?.let { ctx -> + loginViewModel.oauth(ctx, code) + } + // Clear the intent data to avoid re-processing + activity?.intent?.data = null + return + } + } + } + + // Launch OAuth in external browser instead of WebView + launchOAuthInBrowser() } override fun getLayoutId(): Int { return R.layout.fragment_login_oauth } - - private fun initWeb() { - val settings = binding!!.oauthWebview.settings - settings.javaScriptEnabled = true - settings.loadWithOverviewMode = true - settings.builtInZoomControls = false - settings.displayZoomControls = false - settings.domStorageEnabled = true - settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS - settings.cacheMode = LOAD_CACHE_ELSE_NETWORK - - val webViewClient: WebViewClient = object : WebViewClient() { - - override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { - } - - override fun onPageFinished(view: WebView?, url: String?) { - binding!!.oauthWebviewLoadingBar.visibility = View.GONE - } - - override fun shouldOverrideUrlLoading( - view: WebView?, request: WebResourceRequest? - ): Boolean { - if (request != null && request.url != null && request.url.toString() - .startsWith("gsygithubapp://authed") - ) { - val code = request.url.getQueryParameter("code") - if (code != null) { - loginViewModel.oauth(context!!, code) - }; - return true - } - return false - } + private fun launchOAuthInBrowser() { + val url = "https://github.com/login/oauth/authorize?" + + "client_id=${BuildConfig.CLIENT_ID}&" + + "state=app&" + + "redirect_uri=gsygithubapp://authed" + + try { + val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) + // Add FLAG_ACTIVITY_NEW_TASK to open in external browser + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + startActivity(intent) + } catch (e: Exception) { + activity?.toast(R.string.LoginFailTip) } - - - binding!!.oauthWebview.webViewClient = webViewClient - - - val url = - "https://github.com/login/oauth/authorize?" + "client_id=${BuildConfig.CLIENT_ID}&" + "state=app&redirect_uri=gsygithubapp://authed"; - - binding!!.oauthWebview.loadUrl(url) } } \ No newline at end of file