Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@
<activity
android:name=".module.StartNavigationActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize"/>
android:windowSoftInputMode="adjustResize"
android:launchMode="singleTask">

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="gsygithubapp"
android:host="authed" />
</intent-filter>
</activity>

<activity
android:name=".module.list.GeneralListActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ class StartNavigationActivity : AppCompatActivity(), HasSupportFragmentInjector
//如果是调试版本,启动后台服务测试AIDL
startService(Intent(this, LocalService::class.java))
}

handleOAuthCallback(intent)
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
intent?.let { setIntent(it) }
handleOAuthCallback(intent)
}

private fun handleOAuthCallback(intent: Intent?) {
val uri = intent?.data
if (uri != null && uri.scheme == "gsygithubapp" && uri.host == "authed") {
// OAuth callback received, navigate to OAuth fragment if not already there
val fragment = supportFragmentManager.primaryNavigationFragment
if (fragment is NavHostFragment) {
val navController = fragment.navController
// Always navigate to OAuth fragment to handle the callback
if (navController.currentDestination?.id != R.id.loginOAuthFragment) {
navController.navigate(R.id.loginOAuthFragment)
}
}
}
}

//实现 HasSupportFragmentInjector 的接口,表示有Fragment需要注入
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.shuyu.github.kotlin.module.login

import android.graphics.Bitmap
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebSettings.LOAD_CACHE_ELSE_NETWORK
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
Expand Down Expand Up @@ -50,57 +46,44 @@ class LoginOAuthFragment : BaseFragment<FragmentLoginOauthBinding>() {
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)
}
}