Merge pull request #918 from owncloud/share_password_support
[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.actionbarsherlock.app.SherlockDialogFragment;
23 import com.owncloud.android.R;
24 import com.owncloud.android.authentication.AuthenticatorActivity;
25
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;
37
38
39
40 /**
41 * Dialog to input authentication credentials
42 *
43 */
44 public class CredentialsDialogFragment extends SherlockDialogFragment
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, HttpAuthHandler handler) {
64 if (handler == null) {
65 throw new IllegalArgumentException("Trying to create instance with parameter handler == null");
66 }
67 CredentialsDialogFragment frag = new CredentialsDialogFragment();
68 frag.mHandler = handler;
69 frag.mWebView = webView;
70 return frag;
71 }
72
73
74 @Override
75 public Dialog onCreateDialog(Bundle savedInstanceState) {
76
77 // Create field for username
78 mUsernameET = new EditText(getSherlockActivity());
79 mUsernameET.setHint(getSherlockActivity().getText(R.string.auth_username));
80
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);
85
86 // Prepare LinearLayout for dialog
87 LinearLayout ll = new LinearLayout(getSherlockActivity());
88 ll.setOrientation(LinearLayout.VERTICAL);
89 ll.addView(mUsernameET);
90 ll.addView(mPasswordET);
91
92 ll.requestFocus();
93
94 setRetainInstance(true);
95
96 Builder authDialog = new AlertDialog
97 .Builder(getSherlockActivity())
98 .setTitle(getSherlockActivity().getText(R.string.saml_authentication_required_text))
99 .setView(ll)
100 .setCancelable(false)
101 .setPositiveButton(R.string.common_ok, this)
102 .setNegativeButton(R.string.common_cancel, this);
103
104 Dialog d = authDialog.create();
105 d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
106 return d;
107 }
108
109
110 @Override
111 public void onPause() {
112 super.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();
117 }
118
119
120 @Override
121 public void onResume() {
122 super.onResume();
123 mUsernameET.setText(mUsernameStr);
124 mPasswordET.setText(mPasswordStr);
125 }
126
127
128 @Override
129 public void onClick(DialogInterface dialog, int which) {
130 if (which == AlertDialog.BUTTON_POSITIVE) {
131
132 String username = mUsernameET.getText().toString();
133 String password = mPasswordET.getText().toString();
134
135 // Proceed with the authentication
136 mHandler.proceed(username, password);
137
138 } else if (which == AlertDialog.BUTTON_NEGATIVE) {
139 mWebView.stopLoading();
140 ((AuthenticatorActivity)getActivity()).doNegativeAuthenticatioDialogClick();
141 }
142
143 dialog.dismiss();
144 }
145
146
147 @Override
148 public void onDestroyView() {
149 if (getDialog() != null && getRetainInstance())
150 getDialog().setDismissMessage(null);
151 super.onDestroyView();
152 }
153
154 }