Remove actionbarsherlock , replace it with Android Support Library (v7)
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / CredentialsDialogFragment.java
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
20 package com.owncloud.android.ui.dialog;
21
22 import com.owncloud.android.R;
23 import com.owncloud.android.authentication.AuthenticatorActivity;
24
25 import android.app.AlertDialog;
26 import android.app.Dialog;
27 import android.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;
37
38
39
40 /**
41 * Dialog to input authentication credentials
42 *
43 */
44 public class CredentialsDialogFragment extends DialogFragment
45 implements DialogInterface.OnClickListener {
46
47 private WebView mWebView = null;
48 private HttpAuthHandler mHandler = null;
49
50 private EditText mUsernameET;
51 private EditText mPasswordET;
52
53 private String mUsernameStr;
54 private String mPasswordStr;
55
56
57 /**
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
62 */
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" +
67 " == null");
68 }
69 CredentialsDialogFragment frag = new CredentialsDialogFragment();
70 frag.mHandler = handler;
71 frag.mWebView = webView;
72 return frag;
73 }
74
75
76 @Override
77 public Dialog onCreateDialog(Bundle savedInstanceState) {
78
79 // Create field for username
80 mUsernameET = new EditText(getActivity());
81 mUsernameET.setHint(getActivity().getText(R.string.auth_username));
82
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);
87
88 // Prepare LinearLayout for dialog
89 LinearLayout ll = new LinearLayout(getActivity());
90 ll.setOrientation(LinearLayout.VERTICAL);
91 ll.addView(mUsernameET);
92 ll.addView(mPasswordET);
93
94 ll.requestFocus();
95
96 setRetainInstance(true);
97
98 Builder authDialog = new AlertDialog
99 .Builder(getActivity())
100 .setTitle(getActivity().getText(R.string.saml_authentication_required_text))
101 .setView(ll)
102 .setCancelable(false)
103 .setPositiveButton(R.string.common_ok, this)
104 .setNegativeButton(R.string.common_cancel, this);
105
106 Dialog d = authDialog.create();
107 d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
108 return d;
109 }
110
111
112 @Override
113 public void onPause() {
114 super.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();
119 }
120
121
122 @Override
123 public void onResume() {
124 super.onResume();
125 mUsernameET.setText(mUsernameStr);
126 mPasswordET.setText(mPasswordStr);
127 }
128
129
130 @Override
131 public void onClick(DialogInterface dialog, int which) {
132 if (which == AlertDialog.BUTTON_POSITIVE) {
133
134 String username = mUsernameET.getText().toString();
135 String password = mPasswordET.getText().toString();
136
137 // Proceed with the authentication
138 mHandler.proceed(username, password);
139
140 } else if (which == AlertDialog.BUTTON_NEGATIVE) {
141 mWebView.stopLoading();
142 ((AuthenticatorActivity)getActivity()).doNegativeAuthenticatioDialogClick();
143 }
144
145 dialog.dismiss();
146 }
147
148
149 @Override
150 public void onDestroyView() {
151 if (getDialog() != null && getRetainInstance())
152 getDialog().setDismissMessage(null);
153 super.onDestroyView();
154 }
155
156 }