blob: 7d57da1f6db1667aa7efacfe76da137b487fe094 [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 Lisiona3650992013-11-13 14:19:35 -050032package org.sflphone.views;
33
34/*
35 * Copyright (C) 2011 The CyanogenMod Project
36 *
37 * Licensed under the Apache License, Version 2.0 (the "License");
38 * you may not use this file except in compliance with the License.
39 * You may obtain a copy of the License at
40 *
41 * http://www.apache.org/licenses/LICENSE-2.0
42 *
43 * Unless required by applicable law or agreed to in writing, software
44 * distributed under the License is distributed on an "AS IS" BASIS,
45 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
46 * See the License for the specific language governing permissions and
47 * limitations under the License.
48 */
49
50import java.lang.reflect.Field;
51
52import org.sflphone.R;
53
54import android.content.Context;
55import android.content.res.TypedArray;
56import android.preference.DialogPreference;
57import android.util.AttributeSet;
58import android.util.Log;
59import android.view.LayoutInflater;
60import android.view.View;
61import android.widget.EditText;
62import android.widget.NumberPicker;
63import android.widget.TextView;
64
65/*
66 * @author Danesh
67 * @author nebkat
68 */
69
70public class DoubleNumberPickerPreference extends DialogPreference {
71 private int mMin1, mMax1, mDefault1;
72 private int mMin2, mMax2, mDefault2;
73
74 private String mMaxExternalKey1, mMinExternalKey1;
75 private String mMaxExternalKey2, mMinExternalKey2;
76
77 private String mPickerTitle1;
78 private String mPickerTitle2;
79
80 private NumberPicker mNumberPicker1;
81 private NumberPicker mNumberPicker2;
82
83 public DoubleNumberPickerPreference(Context context, AttributeSet attrs) {
84 super(context, attrs);
85 // TypedArray dialogType = context.obtainStyledAttributes(attrs,
86 // com.android.internal.R.styleable.DialogPreference, 0, 0);
87 TypedArray doubleNumberPickerType = context.obtainStyledAttributes(attrs, R.styleable.DoubleNumberPickerPreference, 0, 0);
88
89 mMaxExternalKey1 = doubleNumberPickerType.getString(R.styleable.DoubleNumberPickerPreference_maxExternal1);
90 mMinExternalKey1 = doubleNumberPickerType.getString(R.styleable.DoubleNumberPickerPreference_minExternal1);
91 mMaxExternalKey2 = doubleNumberPickerType.getString(R.styleable.DoubleNumberPickerPreference_maxExternal2);
92 mMinExternalKey2 = doubleNumberPickerType.getString(R.styleable.DoubleNumberPickerPreference_minExternal2);
93
94 mPickerTitle1 = doubleNumberPickerType.getString(R.styleable.DoubleNumberPickerPreference_pickerTitle1);
95 mPickerTitle2 = doubleNumberPickerType.getString(R.styleable.DoubleNumberPickerPreference_pickerTitle2);
96
97 mMax1 = doubleNumberPickerType.getInt(R.styleable.DoubleNumberPickerPreference_max1, 5);
98 mMin1 = doubleNumberPickerType.getInt(R.styleable.DoubleNumberPickerPreference_min1, 0);
99 mMax2 = doubleNumberPickerType.getInt(R.styleable.DoubleNumberPickerPreference_max2, 5);
100 mMin2 = doubleNumberPickerType.getInt(R.styleable.DoubleNumberPickerPreference_min2, 0);
101
102 mDefault1 = doubleNumberPickerType.getInt(R.styleable.DoubleNumberPickerPreference_defaultValue1, mMin1);
103 mDefault2 = doubleNumberPickerType.getInt(R.styleable.DoubleNumberPickerPreference_defaultValue2, mMin2);
104
105 // dialogType.recycle();
106 doubleNumberPickerType.recycle();
107 }
108
109 @Override
110 protected View onCreateDialogView() {
111 int max1 = mMax1;
112 int min1 = mMin1;
113 int max2 = mMax2;
114 int min2 = mMin2;
115
116 // External values
117 if (mMaxExternalKey1 != null) {
118 max1 = getSharedPreferences().getInt(mMaxExternalKey1, mMax1);
119 }
120 if (mMinExternalKey1 != null) {
121 min1 = getSharedPreferences().getInt(mMinExternalKey1, mMin1);
122 }
123 if (mMaxExternalKey2 != null) {
124 max2 = getSharedPreferences().getInt(mMaxExternalKey2, mMax2);
125 }
126 if (mMinExternalKey2 != null) {
127 min2 = getSharedPreferences().getInt(mMinExternalKey2, mMin2);
128 }
129
130 LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
131 View view = inflater.inflate(R.layout.double_number_picker_dialog, null);
132
133 mNumberPicker1 = (NumberPicker) view.findViewById(R.id.number_picker_1);
134 mNumberPicker2 = (NumberPicker) view.findViewById(R.id.number_picker_2);
135
136 if (mNumberPicker1 == null || mNumberPicker2 == null) {
137 throw new RuntimeException("mNumberPicker1 or mNumberPicker2 is null!");
138 }
139
140 // Initialize state
141 mNumberPicker1.setWrapSelectorWheel(false);
142 mNumberPicker1.setMaxValue(max1);
143 mNumberPicker1.setMinValue(min1);
144 mNumberPicker1.setValue(getPersistedValue(1));
145 mNumberPicker2.setWrapSelectorWheel(false);
146 mNumberPicker2.setMaxValue(max2);
147 mNumberPicker2.setMinValue(min2);
148 mNumberPicker2.setValue(getPersistedValue(2));
149
150 // Titles
151 TextView pickerTitle1 = (TextView) view.findViewById(R.id.picker_title_1);
152 TextView pickerTitle2 = (TextView) view.findViewById(R.id.picker_title_2);
153
154 if (pickerTitle1 != null && pickerTitle2 != null) {
155 pickerTitle1.setText(mPickerTitle1);
156 pickerTitle2.setText(mPickerTitle2);
157 }
158
159 // No keyboard popup
160 disableTextInput(mNumberPicker1);
161 disableTextInput(mNumberPicker2);
162 // EditText textInput1 = (EditText) mNumberPicker1.findViewById(com.android.internal.R.id.numberpicker_input);
163 // EditText textInput2 = (EditText) mNumberPicker2.findViewById(com.android.internal.R.id.numberpicker_input);
164 // if (textInput1 != null && textInput2 != null) {
165 // textInput1.setCursorVisible(false);
166 // textInput1.setFocusable(false);
167 // textInput1.setFocusableInTouchMode(false);
168 // textInput2.setCursorVisible(false);
169 // textInput2.setFocusable(false);
170 // textInput2.setFocusableInTouchMode(false);
171 // }
172
173 return view;
174 }
175
176 private int getPersistedValue(int value) {
177 String[] values = getPersistedString(mDefault1 + "|" + mDefault2).split("\\|");
178 if (value == 1) {
179 try {
180 return Integer.parseInt(values[0]);
181 } catch (NumberFormatException e) {
182 return mDefault1;
183 }
184 } else {
185 try {
186 return Integer.parseInt(values[1]);
187 } catch (NumberFormatException e) {
188 return mDefault2;
189 }
190 }
191 }
192
193 @Override
194 protected void onDialogClosed(boolean positiveResult) {
195 if (positiveResult) {
196 persistString(mNumberPicker1.getValue() + "|" + mNumberPicker2.getValue());
197 getOnPreferenceChangeListener().onPreferenceChange(this, mNumberPicker1.getValue()+""+mNumberPicker2.getValue());
198 }
199 }
200
201 public void setMin1(int min) {
202 mMin1 = min;
203 }
204
205 public void setMax1(int max) {
206 mMax1 = max;
207 }
208
209 public void setMin2(int min) {
210 mMin2 = min;
211 }
212
213 public void setMax2(int max) {
214 mMax2 = max;
215 }
216
217 public void setDefault1(int def) {
218 mDefault1 = def;
219 }
220
221 public void setDefault2(int def) {
222 mDefault2 = def;
223 }
224
225 /*
226 * reflection of NumberPicker.java verified in 4.1, 4.2
227 */
228 private void disableTextInput(NumberPicker np) {
229 if (np == null)
230 return;
231 Class<?> classType = np.getClass();
232 Field inputTextField;
233 try {
234 inputTextField = classType.getDeclaredField("mInputText");
235 inputTextField.setAccessible(true);
236 EditText textInput = (EditText) inputTextField.get(np);
237 if (textInput != null) {
238 textInput.setCursorVisible(false);
239 textInput.setFocusable(false);
240 textInput.setFocusableInTouchMode(false);
241 }
242 } catch (Exception e) {
243 Log.d("trebuchet", "DoubleNumberPickerPreference disableTextInput error", e);
244 }
245 }
246
247}