2 * ownCloud Android client application
4 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
20 package com
.owncloud
.android
.ui
.dialog
;
22 import com
.owncloud
.android
.R
;
23 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
25 import android
.support
.v7
.app
.AlertDialog
;
26 import android
.app
.Dialog
;
27 import android
.support
.v7
.app
.AlertDialog
.Builder
;
28 import android
.content
.DialogInterface
;
29 import android
.os
.Bundle
;
30 import android
.support
.v4
.app
.DialogFragment
;
31 import android
.text
.InputType
;
32 import android
.view
.WindowManager
.LayoutParams
;
33 import android
.webkit
.HttpAuthHandler
;
34 import android
.webkit
.WebView
;
35 import android
.widget
.EditText
;
36 import android
.widget
.LinearLayout
;
41 * Dialog to input authentication credentials
44 public class CredentialsDialogFragment
extends DialogFragment
45 implements DialogInterface
.OnClickListener
{
47 private WebView mWebView
= null
;
48 private HttpAuthHandler mHandler
= null
;
50 private EditText mUsernameET
;
51 private EditText mPasswordET
;
53 private String mUsernameStr
;
54 private String mPasswordStr
;
58 * Public factory method to create new CredentialsDialogFragment instances.
59 * @param webView WebView that is being loaded
60 * @param handler HttpAuthHandler
61 * @return Dialog ready to show
63 public static CredentialsDialogFragment
newInstanceForCredentials(WebView webView
,
64 HttpAuthHandler handler
) {
65 if (handler
== null
) {
66 throw new IllegalArgumentException("Trying to create instance with parameter handler" +
69 CredentialsDialogFragment frag
= new CredentialsDialogFragment();
70 frag
.mHandler
= handler
;
71 frag
.mWebView
= webView
;
77 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
79 // Create field for username
80 mUsernameET
= new EditText(getActivity());
81 mUsernameET
.setHint(getActivity().getText(R
.string
.auth_username
));
83 // Create field for password
84 mPasswordET
= new EditText(getActivity());
85 mPasswordET
.setHint(getActivity().getText(R
.string
.auth_password
));
86 mPasswordET
.setInputType(InputType
.TYPE_CLASS_TEXT
| InputType
.TYPE_TEXT_VARIATION_PASSWORD
);
88 // Prepare LinearLayout for dialog
89 LinearLayout ll
= new LinearLayout(getActivity());
90 ll
.setOrientation(LinearLayout
.VERTICAL
);
91 ll
.addView(mUsernameET
);
92 ll
.addView(mPasswordET
);
96 setRetainInstance(true
);
98 Builder authDialog
= new AlertDialog
99 .Builder(getActivity())
100 .setTitle(getActivity().getText(R
.string
.saml_authentication_required_text
))
102 .setCancelable(false
)
103 .setPositiveButton(R
.string
.common_ok
, this)
104 .setNegativeButton(R
.string
.common_cancel
, this);
106 Dialog d
= authDialog
.create();
107 d
.getWindow().setSoftInputMode(LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
113 public void onPause() {
115 // Due to the use of setRetainInstance(true) for keep the dialog over the rest of dialogs,
116 // we need to save the inputs text for being injected in onResume()
117 mUsernameStr
= mUsernameET
.getText().toString();
118 mPasswordStr
= mPasswordET
.getText().toString();
123 public void onResume() {
125 mUsernameET
.setText(mUsernameStr
);
126 mPasswordET
.setText(mPasswordStr
);
131 public void onClick(DialogInterface dialog
, int which
) {
132 if (which
== AlertDialog
.BUTTON_POSITIVE
) {
134 String username
= mUsernameET
.getText().toString();
135 String password
= mPasswordET
.getText().toString();
137 // Proceed with the authentication
138 mHandler
.proceed(username
, password
);
140 } else if (which
== AlertDialog
.BUTTON_NEGATIVE
) {
141 mWebView
.stopLoading();
142 ((AuthenticatorActivity
)getActivity()).doNegativeAuthenticatioDialogClick();
150 public void onDestroyView() {
151 if (getDialog() != null
&& getRetainInstance())
152 getDialog().setDismissMessage(null
);
153 super.onDestroyView();