diff --git a/app/src/main/java/jp/atled/agileworks/model/SessionRepository.kt b/app/src/main/java/jp/atled/agileworks/model/SessionRepository.kt index c2978b9018b20b5e109a36a27c3c74e3a481dff5..59517d666f674c7f65516ca9ab749e3882fa0d1a 100644 --- a/app/src/main/java/jp/atled/agileworks/model/SessionRepository.kt +++ b/app/src/main/java/jp/atled/agileworks/model/SessionRepository.kt @@ -10,6 +10,6 @@ class SessionRepository(private val activity: Activity?) { } companion object { - fun getInstance(activity: Activity) = SessionRepository(activity) + fun getInstance(activity: Activity?) = SessionRepository(activity) } } \ No newline at end of file diff --git a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt index e5681a5b12fd976f38e0d49a5b58f459629c61cb..185531064d628514941ded1ae2d791f61aa80f84 100644 --- a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt +++ b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt @@ -5,9 +5,6 @@ import android.net.Uri import android.util.Log import android.webkit.* import jp.atled.agileworks.model.CertRepository -import jp.atled.agileworks.model.LoginRepository -import jp.atled.agileworks.model.api.ApiClient -import kotlinx.coroutines.* /** * [DocumentWebFragment] の [WebView] で用いる [WebViewClient] @@ -56,28 +53,7 @@ class DocumentWebViewClient(private val webShare: DocumentWebShare): WebViewClie } super.doUpdateVisitedHistory(view, url, isReload) - - // 非同期にセッション情報取得,cookie設定 - val job = SupervisorJob() - val scope = CoroutineScope(Dispatchers.Default + job) - scope.launch { - CookieManager.getInstance().apply { - try { - val response = ApiClient.instance.getSession().execute() - if (response.isSuccessful) { - val uri = Uri.parse(url) - response.body()!!.session_id?.apply { - val sessionId = response.body()!!.session_id - setCookie("https://${uri.host}", "JSESSIONID=${sessionId}; Path=/${LoginRepository().loadServerContext()}; HttpOnly;Secure;SameSite=None") - flush() - } - } - } catch (e: Exception) { - // - Log.e(TAG, e.toString(), e) - } - } - } + webShare.presenter.updateSession(url, false) // ネットワーク切断時Viewを表示する必要があるか設定する(URL変更時) url?.let { webShare.presenter.updateDisconnectViewDisplayState(it) } diff --git a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebFragment.kt b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebFragment.kt index 16df17d684ee6f09c255c3614c70726a7f6ddbff..63df9e73bca6e7efcc4e47c076b67e50b91d4c29 100644 --- a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebFragment.kt +++ b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebFragment.kt @@ -1,6 +1,7 @@ package jp.atled.agileworks.view.ui.documentweb import android.annotation.SuppressLint +import android.app.Activity import android.app.AlertDialog import android.content.Context import android.content.Intent @@ -29,6 +30,7 @@ import kotlinx.android.synthetic.main.fragment_webview_detail.* import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch import java.lang.Exception +import kotlin.concurrent.withLock import kotlin.math.absoluteValue class DocumentWebFragment : Fragment(), DocumentWebPresenter { @@ -50,6 +52,8 @@ class DocumentWebFragment : Fragment(), DocumentWebPresenter { private var clearHistoryFlg = false // タブ選択時初期URLリロードするかどうか private var reloadTab = false + //tokenチェック重複防止フラグ + private var sessionflg = false // スワイプのジェスチャー。折に触れて状態設定をする必要があるのでメンバーとして持つ。 // 必要とされるタイミング次第で null のままのことがある恐れがあるので lateinit にはしていない。 @@ -408,36 +412,16 @@ class DocumentWebFragment : Fragment(), DocumentWebPresenter { private fun applyCookieManager(url: String?, urlLoading: Boolean) { CookieManager.getInstance().apply { - SessionRepository.getInstance(requireActivity()).getSession() { isSuccess, response -> - setupWebView() - hideLoadingProgressBar() - setClickListeners() - if (isSuccess) { - url?.let { url -> - val uri = Uri.parse(url) - response!!.session_id?.apply { - sessionId = response!!.session_id - setCookie("https://${uri.host}", "JSESSIONID=${sessionId}; Path=/${LoginRepository().loadServerContext()}; HttpOnly;Secure;SameSite=None") - flush() - // セッションID保存 - AwApp.instance.applicationContext.getSharedPreferences("cookie", Context.MODE_PRIVATE) - .edit() - .putString("sessionId", sessionId) - .apply() - } - if (urlLoading) { - // 多言語対応 - response.user.displayLanguage.let { displayLanguage -> - multilingualControl.setNewLanguage(displayLanguage, webview) - } - webview?.let { - it.loadUrl(url!!) - } - } else { - webShare.presenter.updateAutoReloadState(false) - } - } + setupWebView() + hideLoadingProgressBar() + setClickListeners() + updateSession(url, urlLoading) + if (urlLoading) { + webview?.let { + it.loadUrl(url!!) } + } else { + webShare.presenter.updateAutoReloadState(false) } } } @@ -550,6 +534,59 @@ class DocumentWebFragment : Fragment(), DocumentWebPresenter { return clearHistoryFlg } + //tokenチェック + override fun updateSession(url: String?, updateLanguage: Boolean) { + // 非同期にセッション情報取得,cookie設定 + val lock = java.util.concurrent.locks.ReentrantLock() + lock.withLock { + if (!sessionflg) { + sessionflg = true + + CookieManager.getInstance().apply { + var activity: Activity? = null + try { + activity = requireActivity() + } catch (e: Exception) { + activity = null + } + + SessionRepository.getInstance(activity).getSession() { isSuccess, response -> + if (isSuccess) { + url?.let { url -> + val uri = Uri.parse(url) + response!!.session_id?.apply { + sessionId = response!!.session_id + setCookie( + "https://${uri.host}", + "JSESSIONID=${sessionId}; Path=/${LoginRepository().loadServerContext()}; HttpOnly;Secure;SameSite=None" + ) + flush() + + // セッションID保存 + AwApp.instance.applicationContext.getSharedPreferences( + "cookie", + Context.MODE_PRIVATE + ) + .edit() + .putString("sessionId", sessionId) + .apply() + } + if (updateLanguage) { + // 多言語対応 + response.user.displayLanguage.let { displayLanguage -> + multilingualControl.setNewLanguage(displayLanguage, webview) + } + + } + } + } + sessionflg = false + } + } + } + } + } + companion object { private const val TAG = "DocumentWebFragmentHandler" diff --git a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebPresenter.kt b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebPresenter.kt index fa105ba2a9d5c786172a539b8f3cad664735aba5..4a734bda2105f6bb566f451050f92d7c7208923a 100644 --- a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebPresenter.kt +++ b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebPresenter.kt @@ -59,4 +59,9 @@ interface DocumentWebPresenter { * 閲覧履歴の削除フラグ取得 */ fun getClearHistoryFlg(): Boolean + + /** + * tokenチェック + */ + fun updateSession(url: String?, updateLanguage: Boolean) }