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
.actionbarsherlock
.app
.SherlockDialogFragment
;
23 import com
.owncloud
.android
.R
;
24 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
26 import android
.app
.AlertDialog
;
27 import android
.app
.Dialog
;
28 import android
.app
.AlertDialog
.Builder
;
29 import android
.content
.DialogInterface
;
30 import android
.os
.Bundle
;
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 SherlockDialogFragment
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
, HttpAuthHandler handler
) {
64 if (handler
== null
) {
65 throw new IllegalArgumentException("Trying to create instance with parameter handler == null");
67 CredentialsDialogFragment frag
= new CredentialsDialogFragment();
68 frag
.mHandler
= handler
;
69 frag
.mWebView
= webView
;
75 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
77 // Create field for username
78 mUsernameET
= new EditText(getSherlockActivity());
79 mUsernameET
.setHint(getSherlockActivity().getText(R
.string
.auth_username
));
81 // Create field for password
82 mPasswordET
= new EditText(getSherlockActivity());
83 mPasswordET
.setHint(getSherlockActivity().getText(R
.string
.auth_password
));
84 mPasswordET
.setInputType(InputType
.TYPE_CLASS_TEXT
| InputType
.TYPE_TEXT_VARIATION_PASSWORD
);
86 // Prepare LinearLayout for dialog
87 LinearLayout ll
= new LinearLayout(getSherlockActivity());
88 ll
.setOrientation(LinearLayout
.VERTICAL
);
89 ll
.addView(mUsernameET
);
90 ll
.addView(mPasswordET
);
94 setRetainInstance(true
);
96 Builder authDialog
= new AlertDialog
97 .Builder(getSherlockActivity())
98 .setTitle(getSherlockActivity().getText(R
.string
.saml_authentication_required_text
))
100 .setCancelable(false
)
101 .setPositiveButton(R
.string
.common_ok
, this)
102 .setNegativeButton(R
.string
.common_cancel
, this);
104 Dialog d
= authDialog
.create();
105 d
.getWindow().setSoftInputMode(LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
111 public void onPause() {
113 // Due to the use of setRetainInstance(true) for keep the dialog over the rest of dialogs,
114 // we need to save the inputs text for being injected in onResume()
115 mUsernameStr
= mUsernameET
.getText().toString();
116 mPasswordStr
= mPasswordET
.getText().toString();
121 public void onResume() {
123 mUsernameET
.setText(mUsernameStr
);
124 mPasswordET
.setText(mPasswordStr
);
129 public void onClick(DialogInterface dialog
, int which
) {
130 if (which
== AlertDialog
.BUTTON_POSITIVE
) {
132 String username
= mUsernameET
.getText().toString();
133 String password
= mPasswordET
.getText().toString();
135 // Proceed with the authentication
136 mHandler
.proceed(username
, password
);
138 } else if (which
== AlertDialog
.BUTTON_NEGATIVE
) {
139 mWebView
.stopLoading();
140 ((AuthenticatorActivity
)getActivity()).doNegativeAuthenticatioDialogClick();
148 public void onDestroyView() {
149 if (getDialog() != null
&& getRetainInstance())
150 getDialog().setDismissMessage(null
);
151 super.onDestroyView();