blob: 62e265419b51c8713618430e03ee8b85c1643d32 [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 Lision59daaaf2013-12-10 10:41:46 -050032package org.sflphone.views;
33
34import org.sflphone.R;
35import org.sflphone.utils.Compatibility;
36
37import android.content.Context;
38import android.view.View.OnClickListener;
39import android.preference.EditTextPreference;
40import android.text.Editable;
41import android.text.InputType;
42import android.text.TextUtils;
43import android.text.TextWatcher;
44import android.util.AttributeSet;
45import android.util.Log;
46import android.view.View;
47import android.view.ViewGroup;
48import android.view.ViewParent;
49import android.widget.CheckBox;
50import android.widget.EditText;
51
52public class PasswordPreference extends EditTextPreference implements OnClickListener, TextWatcher {
53
54 private static final String THIS_FILE = "PasswordPreference";
55 private CheckBox showPwdCheckbox;
56
57 private boolean canShowPassword = false;
58
59 public PasswordPreference(Context context) {
60 this(context, null);
61 }
62
63 public PasswordPreference(Context context, AttributeSet attrs) {
64 super(context, attrs);
65 }
66
67 @Override
68 protected void onAddEditTextToDialogView(View dialogView, EditText editText) {
69 super.onAddEditTextToDialogView(dialogView, editText);
70 editText.addTextChangedListener(this);
71 }
72
73 @Override
74 protected void onBindDialogView(View view) {
75 super.onBindDialogView(view);
76 try {
77 if (showPwdCheckbox == null) {
78 showPwdCheckbox = new CheckBox(getContext());
79 showPwdCheckbox.setText(R.string.show_password);
80 showPwdCheckbox.setOnClickListener(this);
81 }
82
83 canShowPassword = TextUtils.isEmpty(getText());
84 getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
85 updateCanShowPassword();
86 ViewParent oldParent = showPwdCheckbox.getParent();
87 if (oldParent != view) {
88 if (oldParent != null) {
89 ((ViewGroup) oldParent).removeView(showPwdCheckbox);
90 }
91 }
92
93 ViewGroup container = (ViewGroup) view;
94 if (Compatibility.isCompatible(8)) {
95 container = (ViewGroup) container.getChildAt(0);
96 }
97 if (container != null) {
98 container.addView(showPwdCheckbox, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
99 }
100 } catch (Exception e) {
101 // Just do nothing in case weird ROM in use
102 Log.w(THIS_FILE, "Unsupported device for enhanced password", e);
103 }
104 }
105
106 @Override
107 public void onClick(View view) {
108 if (!canShowPassword) {
109 // Even if not shown, be very very sure we never come here
110 return;
111 }
112 getEditText().setInputType(
113 InputType.TYPE_CLASS_TEXT
114 | (((CheckBox) view).isChecked() ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_TEXT_VARIATION_PASSWORD));
115 }
116
117 @Override
118 public void setText(String text) {
119 super.setText(text);
120 setCanShowPassword(TextUtils.isEmpty(text));
121 }
122
123 private void updateCanShowPassword() {
124 if (showPwdCheckbox != null) {
125 showPwdCheckbox.setVisibility(canShowPassword ? View.VISIBLE : View.GONE);
126 }
127 }
128
129 private void setCanShowPassword(boolean canShow) {
130 canShowPassword = canShow;
131 updateCanShowPassword();
132 }
133
134 @Override
135 public void afterTextChanged(Editable s) {
136 if (s.length() == 0) {
137 setCanShowPassword(true);
138 }
139 }
140
141 @Override
142 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
143 // Nothing to do
144
145 }
146
147 @Override
148 public void onTextChanged(CharSequence s, int start, int before, int count) {
149 // Nothing to do
150 }
151
152}