blob: f5326f90e174eebc353f68e50725a0a669134647 [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;
Alexandre Savard12dc3ac2012-09-27 11:17:39 -040055import com.savoirfairelinux.sflphone.service.ISipService;
Alexandre Savarddcd37402012-09-06 18:36:48 -040056
Alexandre Savard393ccab2012-09-11 15:01:20 -040057public class PrefManagementFragment extends PreferenceFragment
Alexandre Savarddcd37402012-09-06 18:36:48 -040058{
59 static final String TAG = "PrefManagementFragment";
Alexandre Savard393ccab2012-09-11 15:01:20 -040060 static final String CURRENT_VALUE = "Current value:: ";
Alexandre Savard12dc3ac2012-09-27 11:17:39 -040061 private ISipService service;
62
63 public PrefManagementFragment(ISipService s)
64 {
65 service = s;
66 }
Alexandre Savarddcd37402012-09-06 18:36:48 -040067
68 @Override
Alexandre Savard393ccab2012-09-11 15:01:20 -040069 public void onCreate(Bundle savedInstanceState)
Alexandre Savarddcd37402012-09-06 18:36:48 -040070 {
Alexandre Savard393ccab2012-09-11 15:01:20 -040071 super.onCreate(savedInstanceState);
Alexandre Savarddcd37402012-09-06 18:36:48 -040072
Alexandre Savard393ccab2012-09-11 15:01:20 -040073 setPreferenceScreen(getAudioPreferenceScreen());
Alexandre Savarddcd37402012-09-06 18:36:48 -040074 }
75
Alexandre Savard5e0b9472012-09-11 17:56:30 -040076 Preference.OnPreferenceChangeListener changePreferenceListener = new Preference.OnPreferenceChangeListener() {
Alexandre Savard393ccab2012-09-11 15:01:20 -040077 public boolean onPreferenceChange(Preference preference, Object newValue) {
78 preference.setSummary(CURRENT_VALUE + (CharSequence)newValue);
79 return true;
80 }
81 };
82
83 public PreferenceScreen getAudioPreferenceScreen()
Alexandre Savarddcd37402012-09-06 18:36:48 -040084 {
Alexandre Savard393ccab2012-09-11 15:01:20 -040085 Activity currentContext = getActivity();
86
87 PreferenceScreen root = getPreferenceManager().createPreferenceScreen(currentContext);
88
89 PreferenceCategory audioPrefCat = new PreferenceCategory(currentContext);
90 audioPrefCat.setTitle(R.string.audio_preferences);
91 root.addPreference(audioPrefCat);
92
Alexandre Savard5e0b9472012-09-11 17:56:30 -040093 // Codec List
Alexandre Savard393ccab2012-09-11 15:01:20 -040094 ListPreference codecListPref = new ListPreference(currentContext);
95 codecListPref.setEntries(R.array.audio_codec_list);
96 codecListPref.setEntryValues(R.array.audio_codec_list_value);
97 codecListPref.setDialogTitle(R.string.dialogtitle_audio_codec_list);
98 codecListPref.setPersistent(false);
99 codecListPref.setTitle(R.string.title_audio_codec_list);
100 codecListPref.setSummary(CURRENT_VALUE + "PCMU");
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400101 codecListPref.setOnPreferenceChangeListener(changePreferenceListener);
Alexandre Savard393ccab2012-09-11 15:01:20 -0400102 audioPrefCat.addPreference(codecListPref);
103
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400104 // Ringtone
105 EditTextPreference audioRingtonePref = new EditTextPreference(currentContext);
106 audioRingtonePref.setDialogTitle(R.string.dialogtitle_audio_ringtone_field);
107 audioRingtonePref.setPersistent(false);
108 audioRingtonePref.setTitle(R.string.title_audio_ringtone_field);
109 audioRingtonePref.setSummary(CURRENT_VALUE + "path/to/ringtone");
110 audioRingtonePref.setOnPreferenceChangeListener(changePreferenceListener);
111 audioPrefCat.addPreference(audioRingtonePref);
112
113 // Speaker volume seekbar
114 SeekBarPreference speakerSeekBarPref = new SeekBarPreference(currentContext);
115 speakerSeekBarPref.setPersistent(false);
116 speakerSeekBarPref.setTitle("Speaker Volume");
Alexandre Savard74eccf32012-09-11 19:12:29 -0400117 speakerSeekBarPref.setProgress(50);
118 speakerSeekBarPref.setSummary(CURRENT_VALUE + speakerSeekBarPref.getProgress());
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400119 audioPrefCat.addPreference(speakerSeekBarPref);
120
121 // Capture volume seekbar
122 SeekBarPreference captureSeekBarPref = new SeekBarPreference(currentContext);
123 captureSeekBarPref.setPersistent(false);
124 captureSeekBarPref.setTitle("Capture Volume");
Alexandre Savard74eccf32012-09-11 19:12:29 -0400125 captureSeekBarPref.setProgress(50);
126 captureSeekBarPref.setSummary(CURRENT_VALUE + captureSeekBarPref.getProgress());
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400127 audioPrefCat.addPreference(captureSeekBarPref);
128
129 // Ringtone volume seekbar
130 SeekBarPreference ringtoneSeekBarPref = new SeekBarPreference(currentContext);
131 ringtoneSeekBarPref.setPersistent(false);
132 ringtoneSeekBarPref.setTitle("Ringtone Volume");
Alexandre Savard74eccf32012-09-11 19:12:29 -0400133 ringtoneSeekBarPref.setProgress(50);
134 ringtoneSeekBarPref.setSummary(CURRENT_VALUE + ringtoneSeekBarPref.getProgress());
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400135 audioPrefCat.addPreference(ringtoneSeekBarPref);
136
Alexandre Savard393ccab2012-09-11 15:01:20 -0400137 return root;
Alexandre Savarddcd37402012-09-06 18:36:48 -0400138 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400139
140 public class SeekBarPreference extends Preference implements OnSeekBarChangeListener
141 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400142 private SeekBar seekbar;
143 private int progress;
144 private int max = 100;
145 private TextView summary;
146 private boolean discard;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400147
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400148 public SeekBarPreference (Context context)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400149 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400150 super( context );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400151 }
152
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400153 public SeekBarPreference (Context context, AttributeSet attrs)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400154 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400155 super( context, attrs );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400156 }
157
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400158 public SeekBarPreference (Context context, AttributeSet attrs, int defStyle)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400159 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400160 super( context, attrs, defStyle );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400161 }
162
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400163 protected View onCreateView (ViewGroup p)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400164 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400165 final Context ctx = getContext();
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400166
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400167 LinearLayout layout = new LinearLayout( ctx );
168 layout.setId( android.R.id.widget_frame );
169 layout.setOrientation( LinearLayout.VERTICAL );
Alexandre Savard74eccf32012-09-11 19:12:29 -0400170 layout.setPadding(65, 10, 15, 10);
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400171
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400172 TextView title = new TextView( ctx );
173 int textColor = title.getCurrentTextColor();
174 title.setId( android.R.id.title );
175 title.setSingleLine();
Alexandre Savard74eccf32012-09-11 19:12:29 -0400176 title.setTextAppearance( ctx, android.R.style.TextAppearance_Medium );
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400177 title.setTextColor( textColor );
178 layout.addView( title );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400179
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400180 summary = new TextView( ctx );
181 summary.setId( android.R.id.summary );
182 summary.setSingleLine();
183 summary.setTextAppearance( ctx, android.R.style.TextAppearance_Small );
184 summary.setTextColor( textColor );
185 layout.addView( summary );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400186
Alexandre Savard616c3802012-09-12 09:44:46 -0400187 seekbar = new SeekBar( ctx );
188 seekbar.setId( android.R.id.progress );
189 seekbar.setMax( max );
190 seekbar.setOnSeekBarChangeListener( this );
191 layout.addView( seekbar );
192
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400193 return layout;
194 }
195
196 @Override
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400197 protected void onBindView (View view)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400198 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400199 super.onBindView( view );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400200
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400201 if (seekbar != null)
202 seekbar.setProgress( progress );
203 }
204
205 public void setProgress (int pcnt) {
206 if (progress != pcnt) {
207 persistInt( progress = pcnt );
208
209 notifyDependencyChange( shouldDisableDependents() );
210 notifyChanged();
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400211 }
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400212 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400213
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400214 public int getProgress () {
215 return progress;
216 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400217
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400218 public void setMax (int max) {
219 this.max = max;
220 if (seekbar != null)
221 seekbar.setMax( max );
222 }
223
224 private SeekBar getSeekBar () {
225 return seekbar;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400226 }
227
228 @Override
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400229 protected Object onGetDefaultValue (TypedArray a, int index) {
230 return a.getInt( index, progress );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400231 }
232
233 @Override
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400234 protected void onSetInitialValue (boolean restoreValue, Object defaultValue) {
235 setProgress( restoreValue ? getPersistedInt( progress ) : (Integer)defaultValue );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400236 }
237
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400238 @Override
239 public boolean shouldDisableDependents () {
240 return progress == 0 || super.shouldDisableDependents();
241 }
242
243 public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {
244 discard = !callChangeListener( progress );
Alexandre Savard74eccf32012-09-11 19:12:29 -0400245 summary.setText(CURRENT_VALUE + progress);
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400246 }
247
248 public void onStartTrackingTouch (SeekBar seekBar) {
249 discard = false;
250 }
251
252 public void onStopTrackingTouch (SeekBar seekBar) {
253 if (discard)
254 seekBar.setProgress( progress );
255 else {
256 setProgress( seekBar.getProgress() );
257
258 OnPreferenceChangeListener listener = getOnPreferenceChangeListener();
259 //if (listener instanceof AbstractSeekBarListener)
260 //// setSummary( ((AbstractSeekBarListener)listener).toSummary( seekBar.getProgress() ) );
261 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400262 }
263 }
Alexandre Savarddcd37402012-09-06 18:36:48 -0400264}