1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.dialog
;
20 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
21 import com
.owncloud
.android
.R
;
22 import com
.owncloud
.android
.authentication
.AuthenticatorActivity
;
24 import android
.app
.AlertDialog
;
25 import android
.app
.Dialog
;
26 import android
.app
.AlertDialog
.Builder
;
27 import android
.content
.DialogInterface
;
28 import android
.os
.Bundle
;
29 import android
.text
.InputType
;
30 import android
.view
.WindowManager
.LayoutParams
;
31 import android
.webkit
.HttpAuthHandler
;
32 import android
.webkit
.WebView
;
33 import android
.widget
.EditText
;
34 import android
.widget
.LinearLayout
;
39 * Dialog to input authentication credentials
42 public class CredentialsDialogFragment
extends SherlockDialogFragment
43 implements DialogInterface
.OnClickListener
{
45 private WebView mWebView
= null
;
46 private HttpAuthHandler mHandler
= null
;
48 private EditText mUsernameET
;
49 private EditText mPasswordET
;
51 private String mUsernameStr
;
52 private String mPasswordStr
;
56 * Public factory method to create new CredentialsDialogFragment instances.
57 * @param webView WebView that is being loaded
58 * @param handler HttpAuthHandler
59 * @return Dialog ready to show
61 public static CredentialsDialogFragment
newInstanceForCredentials(WebView webView
, HttpAuthHandler handler
) {
62 if (handler
== null
) {
63 throw new IllegalArgumentException("Trying to create instance with parameter handler == null");
65 CredentialsDialogFragment frag
= new CredentialsDialogFragment();
66 frag
.mHandler
= handler
;
67 frag
.mWebView
= webView
;
73 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
75 // Create field for username
76 mUsernameET
= new EditText(getSherlockActivity());
77 mUsernameET
.setHint(getSherlockActivity().getText(R
.string
.auth_username
));
79 // Create field for password
80 mPasswordET
= new EditText(getSherlockActivity());
81 mPasswordET
.setHint(getSherlockActivity().getText(R
.string
.auth_password
));
82 mPasswordET
.setInputType(InputType
.TYPE_CLASS_TEXT
| InputType
.TYPE_TEXT_VARIATION_PASSWORD
);
84 // Prepare LinearLayout for dialog
85 LinearLayout ll
= new LinearLayout(getSherlockActivity());
86 ll
.setOrientation(LinearLayout
.VERTICAL
);
87 ll
.addView(mUsernameET
);
88 ll
.addView(mPasswordET
);
92 setRetainInstance(true
);
94 Builder authDialog
= new AlertDialog
95 .Builder(getSherlockActivity())
96 .setTitle(getSherlockActivity().getText(R
.string
.saml_authentication_required_text
))
99 .setPositiveButton(R
.string
.common_ok
, this)
100 .setNegativeButton(R
.string
.common_cancel
, this);
102 Dialog d
= authDialog
.create();
103 d
.getWindow().setSoftInputMode(LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
109 public void onPause() {
111 // Due to the use of setRetainInstance(true) for keep the dialog over the rest of dialogs,
112 // we need to save the inputs text for being injected in onResume()
113 mUsernameStr
= mUsernameET
.getText().toString();
114 mPasswordStr
= mPasswordET
.getText().toString();
119 public void onResume() {
121 mUsernameET
.setText(mUsernameStr
);
122 mPasswordET
.setText(mPasswordStr
);
127 public void onClick(DialogInterface dialog
, int which
) {
128 if (which
== AlertDialog
.BUTTON_POSITIVE
) {
130 String username
= mUsernameET
.getText().toString().trim();
131 String password
= mPasswordET
.getText().toString().trim();
133 // Proceed with the authentication
134 mHandler
.proceed(username
, password
);
137 } else if (which
== AlertDialog
.BUTTON_NEGATIVE
) {
139 mWebView
.stopLoading();
140 ((AuthenticatorActivity
)getActivity()).doNegativeAuthenticatioDialogClick();
146 public void onDestroyView() {
147 if (getDialog() != null
&& getRetainInstance())
148 getDialog().setDismissMessage(null
);
149 super.onDestroyView();