Create dialog fragment for showing authentication dialog
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / CredentialsDialogFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.ui.dialog;
19
20 import com.actionbarsherlock.app.SherlockDialogFragment;
21 import com.owncloud.android.R;
22 import com.owncloud.android.authentication.AuthenticatorActivity;
23
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;
35
36
37
38 /**
39 * Dialog to input authentication credentials
40 *
41 */
42 public class CredentialsDialogFragment extends SherlockDialogFragment
43 implements DialogInterface.OnClickListener {
44
45 private WebView mWebView = null;
46 private HttpAuthHandler mHandler = null;
47
48 private EditText mUsernameET;
49 private EditText mPasswordET;
50
51 private String mUsernameStr;
52 private String mPasswordStr;
53
54
55 /**
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
60 */
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");
64 }
65 CredentialsDialogFragment frag = new CredentialsDialogFragment();
66 frag.mHandler = handler;
67 frag.mWebView = webView;
68 return frag;
69 }
70
71
72 @Override
73 public Dialog onCreateDialog(Bundle savedInstanceState) {
74
75 // Create field for username
76 mUsernameET = new EditText(getSherlockActivity());
77 mUsernameET.setHint(getSherlockActivity().getText(R.string.auth_username));
78
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);
83
84 // Prepare LinearLayout for dialog
85 LinearLayout ll = new LinearLayout(getSherlockActivity());
86 ll.setOrientation(LinearLayout.VERTICAL);
87 ll.addView(mUsernameET);
88 ll.addView(mPasswordET);
89
90 ll.requestFocus();
91
92 setRetainInstance(true);
93
94 Builder authDialog = new AlertDialog
95 .Builder(getSherlockActivity())
96 .setTitle(getSherlockActivity().getText(R.string.saml_authentication_required_text))
97 .setView(ll)
98 .setCancelable(false)
99 .setPositiveButton(R.string.common_ok, this)
100 .setNegativeButton(R.string.common_cancel, this);
101
102 Dialog d = authDialog.create();
103 d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
104 return d;
105 }
106
107
108 @Override
109 public void onPause() {
110 super.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();
115 }
116
117
118 @Override
119 public void onResume() {
120 super.onResume();
121 mUsernameET.setText(mUsernameStr);
122 mPasswordET.setText(mPasswordStr);
123 }
124
125
126 @Override
127 public void onClick(DialogInterface dialog, int which) {
128 if (which == AlertDialog.BUTTON_POSITIVE) {
129
130 String username = mUsernameET.getText().toString().trim();
131 String password = mPasswordET.getText().toString().trim();
132
133 // Proceed with the authentication
134 mHandler.proceed(username, password);
135 dialog.dismiss();
136
137 } else if (which == AlertDialog.BUTTON_NEGATIVE) {
138 dialog.dismiss();
139 mWebView.stopLoading();
140 ((AuthenticatorActivity)getActivity()).doNegativeAuthenticatioDialogClick();
141 }
142 }
143
144
145 @Override
146 public void onDestroyView() {
147 if (getDialog() != null && getRetainInstance())
148 getDialog().setDismissMessage(null);
149 super.onDestroyView();
150 }
151
152 }