CredentialsDialogFragment.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2015 ownCloud Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2,
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package com.owncloud.android.ui.dialog;
  20. import android.app.Dialog;
  21. import android.content.DialogInterface;
  22. import android.os.Bundle;
  23. import android.support.v4.app.DialogFragment;
  24. import android.support.v7.app.AlertDialog;
  25. import android.support.v7.app.AlertDialog.Builder;
  26. import android.text.InputType;
  27. import android.view.WindowManager.LayoutParams;
  28. import android.webkit.HttpAuthHandler;
  29. import android.webkit.WebView;
  30. import android.widget.EditText;
  31. import android.widget.LinearLayout;
  32. import com.owncloud.android.R;
  33. import com.owncloud.android.authentication.AuthenticatorActivity;
  34. /**
  35. * Dialog to input authentication credentials
  36. *
  37. */
  38. public class CredentialsDialogFragment extends DialogFragment
  39. implements DialogInterface.OnClickListener {
  40. private WebView mWebView = null;
  41. private HttpAuthHandler mHandler = null;
  42. private EditText mUsernameET;
  43. private EditText mPasswordET;
  44. private String mUsernameStr;
  45. private String mPasswordStr;
  46. /**
  47. * Public factory method to create new CredentialsDialogFragment instances.
  48. * @param webView WebView that is being loaded
  49. * @param handler HttpAuthHandler
  50. * @return Dialog ready to show
  51. */
  52. public static CredentialsDialogFragment newInstanceForCredentials(WebView webView,
  53. HttpAuthHandler handler) {
  54. if (handler == null) {
  55. throw new IllegalArgumentException("Trying to create instance with parameter handler" +
  56. " == null");
  57. }
  58. CredentialsDialogFragment frag = new CredentialsDialogFragment();
  59. frag.mHandler = handler;
  60. frag.mWebView = webView;
  61. return frag;
  62. }
  63. @Override
  64. public Dialog onCreateDialog(Bundle savedInstanceState) {
  65. // Create field for username
  66. mUsernameET = new EditText(getActivity());
  67. mUsernameET.setHint(getActivity().getText(R.string.auth_username));
  68. // Create field for password
  69. mPasswordET = new EditText(getActivity());
  70. mPasswordET.setHint(getActivity().getText(R.string.auth_password));
  71. mPasswordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
  72. // Prepare LinearLayout for dialog
  73. LinearLayout ll = new LinearLayout(getActivity());
  74. ll.setOrientation(LinearLayout.VERTICAL);
  75. ll.addView(mUsernameET);
  76. ll.addView(mPasswordET);
  77. ll.requestFocus();
  78. setRetainInstance(true);
  79. Builder authDialog = new AlertDialog
  80. .Builder(getActivity())
  81. .setTitle(getActivity().getText(R.string.saml_authentication_required_text))
  82. .setView(ll)
  83. .setCancelable(false)
  84. .setPositiveButton(R.string.common_ok, this)
  85. .setNegativeButton(R.string.common_cancel, this);
  86. Dialog d = authDialog.create();
  87. d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  88. return d;
  89. }
  90. @Override
  91. public void onPause() {
  92. super.onPause();
  93. // Due to the use of setRetainInstance(true) for keep the dialog over the rest of dialogs,
  94. // we need to save the inputs text for being injected in onResume()
  95. mUsernameStr = mUsernameET.getText().toString();
  96. mPasswordStr = mPasswordET.getText().toString();
  97. }
  98. @Override
  99. public void onResume() {
  100. super.onResume();
  101. mUsernameET.setText(mUsernameStr);
  102. mPasswordET.setText(mPasswordStr);
  103. }
  104. @Override
  105. public void onClick(DialogInterface dialog, int which) {
  106. if (which == AlertDialog.BUTTON_POSITIVE) {
  107. String username = mUsernameET.getText().toString();
  108. String password = mPasswordET.getText().toString();
  109. // Proceed with the authentication
  110. mHandler.proceed(username, password);
  111. } else if (which == AlertDialog.BUTTON_NEGATIVE) {
  112. mWebView.stopLoading();
  113. ((AuthenticatorActivity)getActivity()).doNegativeAuthenticatioDialogClick();
  114. }
  115. dialog.dismiss();
  116. }
  117. @Override
  118. public void onDestroyView() {
  119. if (getDialog() != null && getRetainInstance()) {
  120. getDialog().setDismissMessage(null);
  121. }
  122. super.onDestroyView();
  123. }
  124. }