blob: 5b18f1dfe6f06f3f26627f4817bed9636ac4e141 [file] [log] [blame]
Alexandre Lisiona8b78722013-12-13 10:18:33 -05001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Alexandre Lisiona8b78722013-12-13 10:18:33 -05003 *
4 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
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 as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Additional permission under GNU GPL version 3 section 7:
21 *
22 * If you modify this program, or any covered work, by linking or
23 * combining it with the OpenSSL project's OpenSSL library (or a
24 * modified version of that library), containing parts covered by the
25 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
26 * grants you additional permission to convey the resulting work.
27 * Corresponding Source for a non-source form of such a combination
28 * shall include the source code for the parts of OpenSSL used as well
29 * as that of the covered work.
30 */
31
Alexandre Lision3cefec22013-11-14 17:26:35 -050032package org.sflphone.views;
33
34import java.util.HashMap;
35
36import org.sflphone.R;
37import org.sflphone.account.AccountCredentials;
38import org.sflphone.account.CredentialsManager;
39
40import android.app.AlertDialog;
41import android.app.AlertDialog.Builder;
42import android.content.Context;
43import android.content.DialogInterface;
44import android.content.DialogInterface.OnClickListener;
45import android.os.Bundle;
46import android.preference.DialogPreference;
47import android.util.AttributeSet;
48import android.view.Gravity;
49import android.view.KeyEvent;
50import android.view.LayoutInflater;
51import android.view.View;
52import android.widget.EditText;
53import android.widget.TextView;
54import android.widget.TextView.OnEditorActionListener;
55import android.widget.Toast;
56
57public class CredentialsPreference extends DialogPreference {
58
Alexandre Lision569ea2e2014-04-09 00:01:40 -040059 EditText mUsernameField;
60 PasswordEditText mPasswordField;
61 EditText mRealmField;
Alexandre Lision3cefec22013-11-14 17:26:35 -050062
63 public CredentialsPreference(Context context, AttributeSet attrs) {
64 super(context, attrs);
65
66 }
67
68 @Override
69 protected View onCreateDialogView() {
70
71 LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
72 View view = inflater.inflate(R.layout.credentials_pref, null);
73
74 mUsernameField = (EditText) view.findViewById(R.id.credentials_username);
Alexandre Lision569ea2e2014-04-09 00:01:40 -040075 mPasswordField = (PasswordEditText) view.findViewById(R.id.credentials_password);
Alexandre Lision3cefec22013-11-14 17:26:35 -050076 mRealmField = (EditText) view.findViewById(R.id.credentials_realm);
77
78 if (getExtras().getSerializable(CredentialsManager.CURRENT_CRED) != null) {
79 HashMap<String, String> details = (HashMap<String, String>) getExtras().getSerializable(CredentialsManager.CURRENT_CRED);
80 mUsernameField.setText(details.get(AccountCredentials.CONFIG_ACCOUNT_USERNAME));
Alexandre Lision569ea2e2014-04-09 00:01:40 -040081 mPasswordField.getEdit_text().setText(details.get(AccountCredentials.CONFIG_ACCOUNT_PASSWORD));
Alexandre Lision3cefec22013-11-14 17:26:35 -050082 mRealmField.setText(details.get(AccountCredentials.CONFIG_ACCOUNT_REALM));
83 }
84
85 mRealmField.setOnEditorActionListener(new OnEditorActionListener() {
86
87 @Override
88 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
89 String to = mRealmField.getText().toString();
90 if (to.contentEquals("")) {
91 mRealmField.setError(getContext().getString(R.string.dial_error_no_number_dialed));
92 }
93 return true;
94 }
95 });
96
97 return view;
98 }
99
100 private boolean isValid() {
101 return mUsernameField.getText().length() > 0 && mPasswordField.getText().length() > 0 && mRealmField.getText().length() > 0;
102 }
103
104 @Override
105 protected void showDialog(Bundle state) {
106 super.showDialog(state);
107
108 final AlertDialog d = (AlertDialog) getDialog();
109
110 // Prevent dismissing the dialog if they are any empty field
111 d.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
112 @Override
113 public void onClick(View v) {
114
115 if (isValid()) {
116 d.dismiss();
117 onDialogClosed(true);
118 } else {
119 Toast t = Toast.makeText(getContext(), "All fields are mandatory!", Toast.LENGTH_LONG);
120 t.setGravity(Gravity.CENTER, 0, 0);
121 t.show();
122 }
123 }
124 });
125
126 d.setButton(DialogInterface.BUTTON_NEUTRAL, "Delete", new OnClickListener() {
127
128 @Override
129 public void onClick(DialogInterface dialog, int which) {
130 Bundle toReturn = getExtras();
131 getOnPreferenceChangeListener().onPreferenceChange(CredentialsPreference.this, toReturn);
132 }
133 });
134
135 }
136
137 @Override
138 public void onPrepareDialogBuilder(Builder builder) {
139
140 if (getExtras().getSerializable(CredentialsManager.CURRENT_CRED) != null) {
141 // If the user is editing an entry, he can delete it, otherwise don't show this button
142 builder.setNeutralButton("Delete", new OnClickListener() {
143
144 @Override
145 public void onClick(DialogInterface dialog, int which) {
146 Bundle toReturn = getExtras();
147 getOnPreferenceChangeListener().onPreferenceChange(CredentialsPreference.this, toReturn);
148 }
149 });
150 }
151 super.onPrepareDialogBuilder(builder);
152 }
153
154 @Override
155 protected void onDialogClosed(boolean positiveResult) {
156 if (positiveResult) {
157 if (getExtras().getSerializable(CredentialsManager.CURRENT_CRED) != null) {
158 Bundle toReturn = getExtras();
159 HashMap<String, String> fields = new HashMap<String, String>();
160 fields.put(AccountCredentials.CONFIG_ACCOUNT_USERNAME, mUsernameField.getText().toString());
161 fields.put(AccountCredentials.CONFIG_ACCOUNT_PASSWORD, mPasswordField.getText().toString());
162 fields.put(AccountCredentials.CONFIG_ACCOUNT_REALM, mRealmField.getText().toString());
163 toReturn.putSerializable(CredentialsManager.NEW_CRED, fields);
164 getOnPreferenceChangeListener().onPreferenceChange(this, toReturn);
165 } else {
166 HashMap<String, String> fields = new HashMap<String, String>();
167 fields.put(AccountCredentials.CONFIG_ACCOUNT_USERNAME, mUsernameField.getText().toString());
168 fields.put(AccountCredentials.CONFIG_ACCOUNT_PASSWORD, mPasswordField.getText().toString());
169 fields.put(AccountCredentials.CONFIG_ACCOUNT_REALM, mRealmField.getText().toString());
170 getOnPreferenceChangeListener().onPreferenceChange(this, new AccountCredentials(fields));
171 }
172
173 }
174 }
175
176}