blob: 43cd363e1e403adca0e33882f3d58240dbc15c23 [file] [log] [blame]
Alexandre Savarddcd37402012-09-06 18:36:48 -04001/*
2 * Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Savard <alexandre.savard@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
32package com.savoirfairelinux.sflphone.client;
33
Alexandre Savard393ccab2012-09-11 15:01:20 -040034import android.app.Activity;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040035import android.content.Context;
Alexandre Savarde2bb2592012-09-11 18:39:57 -040036import android.content.res.TypedArray;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040037import android.graphics.Typeface;
38import android.preference.EditTextPreference;
Alexandre Savard393ccab2012-09-11 15:01:20 -040039import android.preference.ListPreference;
40import android.preference.Preference;
41import android.preference.PreferenceCategory;
42import android.preference.PreferenceFragment;
43import android.preference.PreferenceScreen;
Alexandre Savarddcd37402012-09-06 18:36:48 -040044import android.os.Bundle;
45import android.util.Log;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040046import android.util.AttributeSet;
Alexandre Savarddcd37402012-09-06 18:36:48 -040047import android.view.View;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040048import android.view.ViewGroup;
Alexandre Savarde2bb2592012-09-11 18:39:57 -040049import android.widget.LinearLayout;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040050import android.widget.SeekBar;
51import android.widget.SeekBar.OnSeekBarChangeListener;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040052import android.widget.TextView;
Alexandre Savarddcd37402012-09-06 18:36:48 -040053
54import com.savoirfairelinux.sflphone.R;
55
Alexandre Savard393ccab2012-09-11 15:01:20 -040056public class PrefManagementFragment extends PreferenceFragment
Alexandre Savarddcd37402012-09-06 18:36:48 -040057{
58 static final String TAG = "PrefManagementFragment";
Alexandre Savard393ccab2012-09-11 15:01:20 -040059 static final String CURRENT_VALUE = "Current value:: ";
Alexandre Savarddcd37402012-09-06 18:36:48 -040060
61 @Override
Alexandre Savard393ccab2012-09-11 15:01:20 -040062 public void onCreate(Bundle savedInstanceState)
Alexandre Savarddcd37402012-09-06 18:36:48 -040063 {
Alexandre Savard393ccab2012-09-11 15:01:20 -040064 super.onCreate(savedInstanceState);
Alexandre Savarddcd37402012-09-06 18:36:48 -040065
Alexandre Savard393ccab2012-09-11 15:01:20 -040066 setPreferenceScreen(getAudioPreferenceScreen());
Alexandre Savarddcd37402012-09-06 18:36:48 -040067 }
68
Alexandre Savard5e0b9472012-09-11 17:56:30 -040069 Preference.OnPreferenceChangeListener changePreferenceListener = new Preference.OnPreferenceChangeListener() {
Alexandre Savard393ccab2012-09-11 15:01:20 -040070 public boolean onPreferenceChange(Preference preference, Object newValue) {
71 preference.setSummary(CURRENT_VALUE + (CharSequence)newValue);
72 return true;
73 }
74 };
75
76 public PreferenceScreen getAudioPreferenceScreen()
Alexandre Savarddcd37402012-09-06 18:36:48 -040077 {
Alexandre Savard393ccab2012-09-11 15:01:20 -040078 Activity currentContext = getActivity();
79
80 PreferenceScreen root = getPreferenceManager().createPreferenceScreen(currentContext);
81
82 PreferenceCategory audioPrefCat = new PreferenceCategory(currentContext);
83 audioPrefCat.setTitle(R.string.audio_preferences);
84 root.addPreference(audioPrefCat);
85
Alexandre Savard5e0b9472012-09-11 17:56:30 -040086 // Codec List
Alexandre Savard393ccab2012-09-11 15:01:20 -040087 ListPreference codecListPref = new ListPreference(currentContext);
88 codecListPref.setEntries(R.array.audio_codec_list);
89 codecListPref.setEntryValues(R.array.audio_codec_list_value);
90 codecListPref.setDialogTitle(R.string.dialogtitle_audio_codec_list);
91 codecListPref.setPersistent(false);
92 codecListPref.setTitle(R.string.title_audio_codec_list);
93 codecListPref.setSummary(CURRENT_VALUE + "PCMU");
Alexandre Savard5e0b9472012-09-11 17:56:30 -040094 codecListPref.setOnPreferenceChangeListener(changePreferenceListener);
Alexandre Savard393ccab2012-09-11 15:01:20 -040095 audioPrefCat.addPreference(codecListPref);
96
Alexandre Savard5e0b9472012-09-11 17:56:30 -040097 // Ringtone
98 EditTextPreference audioRingtonePref = new EditTextPreference(currentContext);
99 audioRingtonePref.setDialogTitle(R.string.dialogtitle_audio_ringtone_field);
100 audioRingtonePref.setPersistent(false);
101 audioRingtonePref.setTitle(R.string.title_audio_ringtone_field);
102 audioRingtonePref.setSummary(CURRENT_VALUE + "path/to/ringtone");
103 audioRingtonePref.setOnPreferenceChangeListener(changePreferenceListener);
104 audioPrefCat.addPreference(audioRingtonePref);
105
106 // Speaker volume seekbar
107 SeekBarPreference speakerSeekBarPref = new SeekBarPreference(currentContext);
108 speakerSeekBarPref.setPersistent(false);
109 speakerSeekBarPref.setTitle("Speaker Volume");
110 speakerSeekBarPref.setSummary("Set the volume for speaker");
111 audioPrefCat.addPreference(speakerSeekBarPref);
112
113 // Capture volume seekbar
114 SeekBarPreference captureSeekBarPref = new SeekBarPreference(currentContext);
115 captureSeekBarPref.setPersistent(false);
116 captureSeekBarPref.setTitle("Capture Volume");
117 captureSeekBarPref.setSummary("Set the volume for microphone");
118 audioPrefCat.addPreference(captureSeekBarPref);
119
120 // Ringtone volume seekbar
121 SeekBarPreference ringtoneSeekBarPref = new SeekBarPreference(currentContext);
122 ringtoneSeekBarPref.setPersistent(false);
123 ringtoneSeekBarPref.setTitle("Ringtone Volume");
124 ringtoneSeekBarPref.setSummary("Set the volume for ringtone");
125 audioPrefCat.addPreference(ringtoneSeekBarPref);
126
Alexandre Savard393ccab2012-09-11 15:01:20 -0400127 return root;
Alexandre Savarddcd37402012-09-06 18:36:48 -0400128 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400129
130 public class SeekBarPreference extends Preference implements OnSeekBarChangeListener
131 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400132 private SeekBar seekbar;
133 private int progress;
134 private int max = 100;
135 private TextView summary;
136 private boolean discard;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400137
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400138 public SeekBarPreference (Context context)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400139 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400140 super( context );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400141 }
142
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400143 public SeekBarPreference (Context context, AttributeSet attrs)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400144 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400145 super( context, attrs );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400146 }
147
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400148 public SeekBarPreference (Context context, AttributeSet attrs, int defStyle)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400149 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400150 super( context, attrs, defStyle );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400151 }
152
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400153 protected View onCreateView (ViewGroup p)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400154 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400155 final Context ctx = getContext();
156 // final Tools T = new Tools( ctx );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400157
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400158 LinearLayout layout = new LinearLayout( ctx );
159 layout.setId( android.R.id.widget_frame );
160 layout.setOrientation( LinearLayout.VERTICAL );
161 // T.setPadding( layout, 15, 10, 15, 10 );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400162
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400163 TextView title = new TextView( ctx );
164 int textColor = title.getCurrentTextColor();
165 title.setId( android.R.id.title );
166 title.setSingleLine();
167 title.setTextAppearance( ctx, android.R.style.TextAppearance_Large );
168 title.setTextColor( textColor );
169 layout.addView( title );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400170
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400171 seekbar = new SeekBar( ctx );
172 seekbar.setId( android.R.id.progress );
173 seekbar.setMax( max );
174 seekbar.setOnSeekBarChangeListener( this );
175 layout.addView( seekbar );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400176
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400177 summary = new TextView( ctx );
178 summary.setId( android.R.id.summary );
179 summary.setSingleLine();
180 summary.setTextAppearance( ctx, android.R.style.TextAppearance_Small );
181 summary.setTextColor( textColor );
182 layout.addView( summary );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400183
184 return layout;
185 }
186
187 @Override
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400188 protected void onBindView (View view)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400189 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400190 super.onBindView( view );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400191
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400192 if (seekbar != null)
193 seekbar.setProgress( progress );
194 }
195
196 public void setProgress (int pcnt) {
197 if (progress != pcnt) {
198 persistInt( progress = pcnt );
199
200 notifyDependencyChange( shouldDisableDependents() );
201 notifyChanged();
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400202 }
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400203 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400204
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400205 public int getProgress () {
206 return progress;
207 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400208
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400209 public void setMax (int max) {
210 this.max = max;
211 if (seekbar != null)
212 seekbar.setMax( max );
213 }
214
215 private SeekBar getSeekBar () {
216 return seekbar;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400217 }
218
219 @Override
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400220 protected Object onGetDefaultValue (TypedArray a, int index) {
221 return a.getInt( index, progress );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400222 }
223
224 @Override
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400225 protected void onSetInitialValue (boolean restoreValue, Object defaultValue) {
226 setProgress( restoreValue ? getPersistedInt( progress ) : (Integer)defaultValue );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400227 }
228
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400229 @Override
230 public boolean shouldDisableDependents () {
231 return progress == 0 || super.shouldDisableDependents();
232 }
233
234 public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {
235 discard = !callChangeListener( progress );
236 }
237
238 public void onStartTrackingTouch (SeekBar seekBar) {
239 discard = false;
240 }
241
242 public void onStopTrackingTouch (SeekBar seekBar) {
243 if (discard)
244 seekBar.setProgress( progress );
245 else {
246 setProgress( seekBar.getProgress() );
247
248 OnPreferenceChangeListener listener = getOnPreferenceChangeListener();
249 //if (listener instanceof AbstractSeekBarListener)
250 //// setSummary( ((AbstractSeekBarListener)listener).toSummary( seekBar.getProgress() ) );
251 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400252 }
253 }
Alexandre Savarddcd37402012-09-06 18:36:48 -0400254}