CredentialsDialogFragment.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.Window;
  28. import android.view.WindowManager.LayoutParams;
  29. import android.webkit.HttpAuthHandler;
  30. import android.webkit.WebView;
  31. import android.widget.EditText;
  32. import android.widget.LinearLayout;
  33. import com.owncloud.android.R;
  34. import com.owncloud.android.authentication.AuthenticatorActivity;
  35. /**
  36. * Dialog to input authentication credentials
  37. *
  38. */
  39. public class CredentialsDialogFragment extends DialogFragment
  40. implements DialogInterface.OnClickListener {
  41. private WebView mWebView;
  42. private HttpAuthHandler mHandler;
  43. private EditText mUsernameET;
  44. private EditText mPasswordET;
  45. private String mUsernameStr;
  46. private String mPasswordStr;
  47. /**
  48. * Public factory method to create new CredentialsDialogFragment instances.
  49. * @param webView WebView that is being loaded
  50. * @param handler HttpAuthHandler
  51. * @return Dialog ready to show
  52. */
  53. public static CredentialsDialogFragment newInstanceForCredentials(WebView webView,
  54. HttpAuthHandler handler) {
  55. if (handler == null) {
  56. throw new IllegalArgumentException("Trying to create instance with parameter handler" +
  57. " == null");
  58. }
  59. CredentialsDialogFragment frag = new CredentialsDialogFragment();
  60. frag.mHandler = handler;
  61. frag.mWebView = webView;
  62. return frag;
  63. }
  64. @Override
  65. public Dialog onCreateDialog(Bundle savedInstanceState) {
  66. // Create field for username
  67. mUsernameET = new EditText(getActivity());
  68. mUsernameET.setHint(getActivity().getText(R.string.auth_username));
  69. // Create field for password
  70. mPasswordET = new EditText(getActivity());
  71. mPasswordET.setHint(getActivity().getText(R.string.auth_password));
  72. mPasswordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
  73. // Prepare LinearLayout for dialog
  74. LinearLayout ll = new LinearLayout(getActivity());
  75. ll.setOrientation(LinearLayout.VERTICAL);
  76. ll.addView(mUsernameET);
  77. ll.addView(mPasswordET);
  78. ll.requestFocus();
  79. setRetainInstance(true);
  80. Builder authDialog = new AlertDialog
  81. .Builder(getActivity())
  82. .setTitle(getActivity().getText(R.string.saml_authentication_required_text))
  83. .setView(ll)
  84. .setCancelable(false)
  85. .setPositiveButton(R.string.common_ok, this)
  86. .setNegativeButton(R.string.common_cancel, this);
  87. Dialog d = authDialog.create();
  88. Window window = d.getWindow();
  89. if (window != null) {
  90. window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  91. }
  92. return d;
  93. }
  94. @Override
  95. public void onPause() {
  96. super.onPause();
  97. // Due to the use of setRetainInstance(true) for keep the dialog over the rest of dialogs,
  98. // we need to save the inputs text for being injected in onResume()
  99. mUsernameStr = mUsernameET.getText().toString();
  100. mPasswordStr = mPasswordET.getText().toString();
  101. }
  102. @Override
  103. public void onResume() {
  104. super.onResume();
  105. mUsernameET.setText(mUsernameStr);
  106. mPasswordET.setText(mPasswordStr);
  107. }
  108. @Override
  109. public void onClick(DialogInterface dialog, int which) {
  110. if (which == AlertDialog.BUTTON_POSITIVE) {
  111. String username = mUsernameET.getText().toString();
  112. String password = mPasswordET.getText().toString();
  113. // Proceed with the authentication
  114. mHandler.proceed(username, password);
  115. } else if (which == AlertDialog.BUTTON_NEGATIVE) {
  116. mWebView.stopLoading();
  117. ((AuthenticatorActivity)getActivity()).doNegativeAuthenticationDialogClick();
  118. }
  119. dialog.dismiss();
  120. }
  121. @Override
  122. public void onDestroyView() {
  123. if (getDialog() != null && getRetainInstance()) {
  124. getDialog().setDismissMessage(null);
  125. }
  126. super.onDestroyView();
  127. }
  128. }