blob: 77918956836192ee7b713df23f8b518346d2faef [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 Lision76592472013-11-12 17:38:52 -050032package org.sflphone.views;
33
34import java.lang.reflect.Field;
35
36import org.sflphone.R;
37
38import android.content.Context;
39import android.content.res.TypedArray;
40import android.preference.DialogPreference;
41import android.util.AttributeSet;
42import android.util.Log;
43import android.view.LayoutInflater;
44import android.view.View;
45import android.widget.EditText;
46import android.widget.NumberPicker;
47
48public class NumberPickerPreference extends DialogPreference {
49 private int mMin, mMax, mDefault;
50
51 private String mMaxExternalKey, mMinExternalKey;
52
53 private NumberPicker mNumberPicker;
54
55 public NumberPickerPreference(Context context, AttributeSet attrs) {
56 super(context, attrs);
57 // TypedArray dialogType = context.obtainStyledAttributes(attrs,
58 // com.android.internal.R.styleable.DialogPreference, 0, 0);
59 TypedArray numberPickerType = context.obtainStyledAttributes(attrs, R.styleable.NumberPickerPreference, 0, 0);
60
61 mMaxExternalKey = numberPickerType.getString(R.styleable.NumberPickerPreference_maxExternal);
62 mMinExternalKey = numberPickerType.getString(R.styleable.NumberPickerPreference_minExternal);
63
64 mMax = numberPickerType.getInt(R.styleable.NumberPickerPreference_max, 5);
65 mMin = numberPickerType.getInt(R.styleable.NumberPickerPreference_min, 0);
66
67 // mDefault = dialogType.getInt(com.android.internal.R.styleable.Preference_defaultValue, mMin);
68 mDefault = mMin;
69 // dialogType.recycle();
70 numberPickerType.recycle();
71 }
72
73 @Override
74 protected View onCreateDialogView() {
75 int max = mMax;
76 int min = mMin;
77
78 // External values
79 if (mMaxExternalKey != null) {
80 max = getSharedPreferences().getInt(mMaxExternalKey, mMax);
81 }
82 if (mMinExternalKey != null) {
83 min = getSharedPreferences().getInt(mMinExternalKey, mMin);
84 }
85
86 LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
87 View view = inflater.inflate(R.layout.number_picker_dialog, null);
88
89 mNumberPicker = (NumberPicker) view.findViewById(R.id.number_picker);
90
91 if (mNumberPicker == null) {
92 throw new RuntimeException("mNumberPicker is null!");
93 }
94
95 // Initialize state
96 mNumberPicker.setWrapSelectorWheel(false);
97 mNumberPicker.setMaxValue(max);
98 mNumberPicker.setMinValue(min);
99 mNumberPicker.setValue(getPersistedInt(mDefault));
100
101 // No keyboard popup
102 disableTextInput(mNumberPicker);
103 // EditText textInput = (EditText) mNumberPicker.findViewById(com.android.internal.R.id.numberpicker_input);
104 // if (textInput != null) {
105 // textInput.setCursorVisible(false);
106 // textInput.setFocusable(false);
107 // textInput.setFocusableInTouchMode(false);
108 // }
109
110 return view;
111 }
112
113 @Override
114 protected void onDialogClosed(boolean positiveResult) {
115 if (positiveResult) {
116 persistInt(mNumberPicker.getValue());
Alexandre Lisiona3650992013-11-13 14:19:35 -0500117 getOnPreferenceChangeListener().onPreferenceChange(this, String.valueOf(mNumberPicker.getValue()));
Alexandre Lision76592472013-11-12 17:38:52 -0500118 }
119 }
120
121 /*
122 * reflection of NumberPicker.java verified in 4.1, 4.2
123 */
124 private void disableTextInput(NumberPicker np) {
125 if (np == null)
126 return;
127 Class<?> classType = np.getClass();
128 Field inputTextField;
129 try {
130 inputTextField = classType.getDeclaredField("mInputText");
131 inputTextField.setAccessible(true);
132 EditText textInput = (EditText) inputTextField.get(np);
133 if (textInput != null) {
134 textInput.setCursorVisible(false);
135 textInput.setFocusable(false);
136 textInput.setFocusableInTouchMode(false);
137 }
138 } catch (Exception e) {
139 Log.d("trebuchet", "NumberPickerPreference disableTextInput error", e);
140 }
141 }
142}