blob: 39cbf951ed535b2b862d6413a75df876153fb84b [file] [log] [blame]
Alexandre Lisiona8b78722013-12-13 10:18:33 -05001/*
2 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3 *
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
59 EditText mUsernameField, mPasswordField, mRealmField;
60
61 public CredentialsPreference(Context context, AttributeSet attrs) {
62 super(context, attrs);
63
64 }
65
66 @Override
67 protected View onCreateDialogView() {
68
69 LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
70 View view = inflater.inflate(R.layout.credentials_pref, null);
71
72 mUsernameField = (EditText) view.findViewById(R.id.credentials_username);
73 mPasswordField = (EditText) view.findViewById(R.id.credentials_password);
74 mRealmField = (EditText) view.findViewById(R.id.credentials_realm);
75
76 if (getExtras().getSerializable(CredentialsManager.CURRENT_CRED) != null) {
77 HashMap<String, String> details = (HashMap<String, String>) getExtras().getSerializable(CredentialsManager.CURRENT_CRED);
78 mUsernameField.setText(details.get(AccountCredentials.CONFIG_ACCOUNT_USERNAME));
79 mPasswordField.setText(details.get(AccountCredentials.CONFIG_ACCOUNT_PASSWORD));
80 mRealmField.setText(details.get(AccountCredentials.CONFIG_ACCOUNT_REALM));
81 }
82
83 mRealmField.setOnEditorActionListener(new OnEditorActionListener() {
84
85 @Override
86 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
87 String to = mRealmField.getText().toString();
88 if (to.contentEquals("")) {
89 mRealmField.setError(getContext().getString(R.string.dial_error_no_number_dialed));
90 }
91 return true;
92 }
93 });
94
95 return view;
96 }
97
98 private boolean isValid() {
99 return mUsernameField.getText().length() > 0 && mPasswordField.getText().length() > 0 && mRealmField.getText().length() > 0;
100 }
101
102 @Override
103 protected void showDialog(Bundle state) {
104 super.showDialog(state);
105
106 final AlertDialog d = (AlertDialog) getDialog();
107
108 // Prevent dismissing the dialog if they are any empty field
109 d.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
110 @Override
111 public void onClick(View v) {
112
113 if (isValid()) {
114 d.dismiss();
115 onDialogClosed(true);
116 } else {
117 Toast t = Toast.makeText(getContext(), "All fields are mandatory!", Toast.LENGTH_LONG);
118 t.setGravity(Gravity.CENTER, 0, 0);
119 t.show();
120 }
121 }
122 });
123
124 d.setButton(DialogInterface.BUTTON_NEUTRAL, "Delete", new OnClickListener() {
125
126 @Override
127 public void onClick(DialogInterface dialog, int which) {
128 Bundle toReturn = getExtras();
129 getOnPreferenceChangeListener().onPreferenceChange(CredentialsPreference.this, toReturn);
130 }
131 });
132
133 }
134
135 @Override
136 public void onPrepareDialogBuilder(Builder builder) {
137
138 if (getExtras().getSerializable(CredentialsManager.CURRENT_CRED) != null) {
139 // If the user is editing an entry, he can delete it, otherwise don't show this button
140 builder.setNeutralButton("Delete", new OnClickListener() {
141
142 @Override
143 public void onClick(DialogInterface dialog, int which) {
144 Bundle toReturn = getExtras();
145 getOnPreferenceChangeListener().onPreferenceChange(CredentialsPreference.this, toReturn);
146 }
147 });
148 }
149 super.onPrepareDialogBuilder(builder);
150 }
151
152 @Override
153 protected void onDialogClosed(boolean positiveResult) {
154 if (positiveResult) {
155 if (getExtras().getSerializable(CredentialsManager.CURRENT_CRED) != null) {
156 Bundle toReturn = getExtras();
157 HashMap<String, String> fields = new HashMap<String, String>();
158 fields.put(AccountCredentials.CONFIG_ACCOUNT_USERNAME, mUsernameField.getText().toString());
159 fields.put(AccountCredentials.CONFIG_ACCOUNT_PASSWORD, mPasswordField.getText().toString());
160 fields.put(AccountCredentials.CONFIG_ACCOUNT_REALM, mRealmField.getText().toString());
161 toReturn.putSerializable(CredentialsManager.NEW_CRED, fields);
162 getOnPreferenceChangeListener().onPreferenceChange(this, toReturn);
163 } else {
164 HashMap<String, String> fields = new HashMap<String, String>();
165 fields.put(AccountCredentials.CONFIG_ACCOUNT_USERNAME, mUsernameField.getText().toString());
166 fields.put(AccountCredentials.CONFIG_ACCOUNT_PASSWORD, mPasswordField.getText().toString());
167 fields.put(AccountCredentials.CONFIG_ACCOUNT_REALM, mRealmField.getText().toString());
168 getOnPreferenceChangeListener().onPreferenceChange(this, new AccountCredentials(fields));
169 }
170
171 }
172 }
173
174}