SamlWebViewDialog.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package com.owncloud.android.ui.dialog;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.app.Dialog;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.support.v4.app.DialogFragment;
  9. import android.webkit.CookieManager;
  10. import android.webkit.WebSettings;
  11. import android.webkit.WebView;
  12. import com.owncloud.android.Log_OC;
  13. import com.owncloud.android.authentication.SsoWebViewClient;
  14. import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener;
  15. import eu.alefzero.webdav.WebdavClient;
  16. /**
  17. * Dialog to show the WebView for SAML Authentication
  18. *
  19. * @author Maria Asensio
  20. */
  21. public class SamlWebViewDialog extends DialogFragment
  22. {
  23. public final String SAML_DIALOG_TAG = "SamlWebViewDialog";
  24. private final static String TAG = SamlWebViewDialog.class.getSimpleName();
  25. private WebView mSsoWebView;
  26. private SsoWebViewClient mWebViewClient;
  27. private static String mUrl;
  28. private static String mTargetUrl;
  29. private Handler mHandler;
  30. private SsoWebViewClientListener mSsoWebViewClientListener;
  31. /**
  32. * Public factory method to get dialog instances.
  33. *
  34. * @param handler
  35. * @param Url Url to open at WebView
  36. * @param targetURL mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType)
  37. * @return New dialog instance, ready to show.
  38. */
  39. public static SamlWebViewDialog newInstance(String url, String targetUrl) {
  40. SamlWebViewDialog fragment = new SamlWebViewDialog();
  41. mUrl = url;
  42. mTargetUrl = targetUrl;
  43. return fragment;
  44. }
  45. @Override
  46. public void onSaveInstanceState(Bundle outState) {
  47. super.onSaveInstanceState(outState);
  48. // Save the state of the WebView
  49. mSsoWebView.saveState(outState);
  50. }
  51. @SuppressLint("SetJavaScriptEnabled")
  52. @Override
  53. public Dialog onCreateDialog(Bundle savedInstanceState) {
  54. Log_OC.d(TAG, "On Create Dialog");
  55. mHandler = new Handler();
  56. mSsoWebView = new WebView(getActivity()) {
  57. @Override
  58. public boolean onCheckIsTextEditor() {
  59. return true;
  60. }
  61. };
  62. mWebViewClient = new SsoWebViewClient(mHandler, mSsoWebViewClientListener);
  63. mSsoWebView.setWebViewClient(mWebViewClient);
  64. mWebViewClient.setTargetUrl(mTargetUrl);
  65. mSsoWebView.setFocusable(true);
  66. mSsoWebView.setFocusableInTouchMode(true);
  67. mSsoWebView.setClickable(true);
  68. WebSettings webSettings = mSsoWebView.getSettings();
  69. webSettings.setJavaScriptEnabled(true);
  70. webSettings.setBuiltInZoomControls(true);
  71. webSettings.setLoadWithOverviewMode(false);
  72. webSettings.setSavePassword(false);
  73. webSettings.setUserAgentString(WebdavClient.USER_AGENT);
  74. // load the dialog
  75. if (savedInstanceState == null) {
  76. initWebView();
  77. }
  78. else {
  79. restoreWebView(savedInstanceState);
  80. }
  81. // build the dialog
  82. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  83. Dialog dialog = builder.setView(mSsoWebView).create();
  84. return dialog;
  85. }
  86. @SuppressLint("SetJavaScriptEnabled")
  87. private void initWebView() {
  88. CookieManager cookieManager = CookieManager.getInstance();
  89. cookieManager.setAcceptCookie(true);
  90. cookieManager.removeAllCookie();
  91. mSsoWebView.loadUrl(mUrl);
  92. }
  93. @SuppressLint("SetJavaScriptEnabled")
  94. private void restoreWebView(Bundle savedInstanceState) {
  95. mSsoWebView.restoreState(savedInstanceState);
  96. CookieManager cookieManager = CookieManager.getInstance();
  97. Log_OC.e(TAG, "Accept Cookie: " + cookieManager.acceptCookie());
  98. }
  99. @Override
  100. public void onDestroyView() {
  101. Dialog dialog = getDialog();
  102. Log_OC.d(TAG, "On Destroy");
  103. // Work around bug: http://code.google.com/p/android/issues/detail?id=17423
  104. if ((dialog != null) && getRetainInstance())
  105. getDialog().setOnDismissListener(null);
  106. super.onDestroyView();
  107. }
  108. @Override
  109. public void onAttach(Activity activity) {
  110. super.onAttach(activity);
  111. Log_OC.e(TAG, "onAttach");
  112. try {
  113. mSsoWebViewClientListener = (SsoWebViewClientListener) activity;
  114. } catch (ClassCastException e) {
  115. throw new ClassCastException(activity.toString() + " must implement " + SsoWebViewClientListener.class.getSimpleName());
  116. }
  117. }
  118. }