blob: 022cf2ea5e6ab0429dac18de3b1bac56b1c6a5bc [file] [log] [blame]
Alexandre Savarddcd37402012-09-06 18:36:48 -04001/*
alision2ec64f92013-06-17 17:28:58 -04002 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
Alexandre Savarddcd37402012-09-06 18:36:48 -04003 *
4 * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
alision2ec64f92013-06-17 17:28:58 -04005 * Alexandre Lision <alexandre.lision@savoirfairelinux.com>
Alexandre Savarddcd37402012-09-06 18:36:48 -04006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * Additional permission under GNU GPL version 3 section 7:
22 *
23 * If you modify this program, or any covered work, by linking or
24 * combining it with the OpenSSL project's OpenSSL library (or a
25 * modified version of that library), containing parts covered by the
26 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
27 * grants you additional permission to convey the resulting work.
28 * Corresponding Source for a non-source form of such a combination
29 * shall include the source code for the parts of OpenSSL used as well
30 * as that of the covered work.
31 */
32
alisionf76de3b2013-04-16 15:35:22 -040033package com.savoirfairelinux.sflphone.fragments;
Alexandre Savarddcd37402012-09-06 18:36:48 -040034
Alexandre Savard393ccab2012-09-11 15:01:20 -040035import android.app.Activity;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040036import android.content.Context;
Alexandre Savarde2bb2592012-09-11 18:39:57 -040037import android.content.res.TypedArray;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040038import android.graphics.Typeface;
39import android.preference.EditTextPreference;
Alexandre Savard393ccab2012-09-11 15:01:20 -040040import android.preference.ListPreference;
41import android.preference.Preference;
42import android.preference.PreferenceCategory;
43import android.preference.PreferenceFragment;
44import android.preference.PreferenceScreen;
Alexandre Savarddcd37402012-09-06 18:36:48 -040045import android.os.Bundle;
46import android.util.Log;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040047import android.util.AttributeSet;
Alexandre Savarddcd37402012-09-06 18:36:48 -040048import android.view.View;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040049import android.view.ViewGroup;
Alexandre Savarde2bb2592012-09-11 18:39:57 -040050import android.widget.LinearLayout;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040051import android.widget.SeekBar;
52import android.widget.SeekBar.OnSeekBarChangeListener;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040053import android.widget.TextView;
Alexandre Savarddcd37402012-09-06 18:36:48 -040054
55import com.savoirfairelinux.sflphone.R;
56
alisiond9e29442013-04-17 16:10:18 -040057public class AudioManagementFragment 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
alisiond9e29442013-04-17 16:10:18 -040062 public AudioManagementFragment()
Alexandre Savard12dc3ac2012-09-27 11:17:39 -040063 {
Alexandre Savard12dc3ac2012-09-27 11:17:39 -040064 }
Alexandre Savarddcd37402012-09-06 18:36:48 -040065
66 @Override
Alexandre Savard393ccab2012-09-11 15:01:20 -040067 public void onCreate(Bundle savedInstanceState)
Alexandre Savarddcd37402012-09-06 18:36:48 -040068 {
Alexandre Savard393ccab2012-09-11 15:01:20 -040069 super.onCreate(savedInstanceState);
Alexandre Savarddcd37402012-09-06 18:36:48 -040070
Alexandre Savard393ccab2012-09-11 15:01:20 -040071 setPreferenceScreen(getAudioPreferenceScreen());
Alexandre Savarddcd37402012-09-06 18:36:48 -040072 }
73
Alexandre Savard5e0b9472012-09-11 17:56:30 -040074 Preference.OnPreferenceChangeListener changePreferenceListener = new Preference.OnPreferenceChangeListener() {
Alexandre Savard393ccab2012-09-11 15:01:20 -040075 public boolean onPreferenceChange(Preference preference, Object newValue) {
76 preference.setSummary(CURRENT_VALUE + (CharSequence)newValue);
77 return true;
78 }
79 };
80
81 public PreferenceScreen getAudioPreferenceScreen()
Alexandre Savarddcd37402012-09-06 18:36:48 -040082 {
Alexandre Savard393ccab2012-09-11 15:01:20 -040083 Activity currentContext = getActivity();
84
85 PreferenceScreen root = getPreferenceManager().createPreferenceScreen(currentContext);
86
87 PreferenceCategory audioPrefCat = new PreferenceCategory(currentContext);
88 audioPrefCat.setTitle(R.string.audio_preferences);
89 root.addPreference(audioPrefCat);
90
Alexandre Savard5e0b9472012-09-11 17:56:30 -040091 // Codec List
Alexandre Savard393ccab2012-09-11 15:01:20 -040092 ListPreference codecListPref = new ListPreference(currentContext);
93 codecListPref.setEntries(R.array.audio_codec_list);
94 codecListPref.setEntryValues(R.array.audio_codec_list_value);
95 codecListPref.setDialogTitle(R.string.dialogtitle_audio_codec_list);
96 codecListPref.setPersistent(false);
97 codecListPref.setTitle(R.string.title_audio_codec_list);
98 codecListPref.setSummary(CURRENT_VALUE + "PCMU");
Alexandre Savard5e0b9472012-09-11 17:56:30 -040099 codecListPref.setOnPreferenceChangeListener(changePreferenceListener);
Alexandre Savard393ccab2012-09-11 15:01:20 -0400100 audioPrefCat.addPreference(codecListPref);
101
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400102 // Ringtone
103 EditTextPreference audioRingtonePref = new EditTextPreference(currentContext);
104 audioRingtonePref.setDialogTitle(R.string.dialogtitle_audio_ringtone_field);
105 audioRingtonePref.setPersistent(false);
106 audioRingtonePref.setTitle(R.string.title_audio_ringtone_field);
107 audioRingtonePref.setSummary(CURRENT_VALUE + "path/to/ringtone");
108 audioRingtonePref.setOnPreferenceChangeListener(changePreferenceListener);
109 audioPrefCat.addPreference(audioRingtonePref);
110
111 // Speaker volume seekbar
112 SeekBarPreference speakerSeekBarPref = new SeekBarPreference(currentContext);
113 speakerSeekBarPref.setPersistent(false);
114 speakerSeekBarPref.setTitle("Speaker Volume");
Alexandre Savard74eccf32012-09-11 19:12:29 -0400115 speakerSeekBarPref.setProgress(50);
116 speakerSeekBarPref.setSummary(CURRENT_VALUE + speakerSeekBarPref.getProgress());
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400117 audioPrefCat.addPreference(speakerSeekBarPref);
118
119 // Capture volume seekbar
120 SeekBarPreference captureSeekBarPref = new SeekBarPreference(currentContext);
121 captureSeekBarPref.setPersistent(false);
122 captureSeekBarPref.setTitle("Capture Volume");
Alexandre Savard74eccf32012-09-11 19:12:29 -0400123 captureSeekBarPref.setProgress(50);
124 captureSeekBarPref.setSummary(CURRENT_VALUE + captureSeekBarPref.getProgress());
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400125 audioPrefCat.addPreference(captureSeekBarPref);
126
127 // Ringtone volume seekbar
128 SeekBarPreference ringtoneSeekBarPref = new SeekBarPreference(currentContext);
129 ringtoneSeekBarPref.setPersistent(false);
130 ringtoneSeekBarPref.setTitle("Ringtone Volume");
Alexandre Savard74eccf32012-09-11 19:12:29 -0400131 ringtoneSeekBarPref.setProgress(50);
132 ringtoneSeekBarPref.setSummary(CURRENT_VALUE + ringtoneSeekBarPref.getProgress());
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400133 audioPrefCat.addPreference(ringtoneSeekBarPref);
134
Alexandre Savard393ccab2012-09-11 15:01:20 -0400135 return root;
Alexandre Savarddcd37402012-09-06 18:36:48 -0400136 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400137
138 public class SeekBarPreference extends Preference implements OnSeekBarChangeListener
139 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400140 private SeekBar seekbar;
141 private int progress;
142 private int max = 100;
143 private TextView summary;
144 private boolean discard;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400145
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400146 public SeekBarPreference (Context context)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400147 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400148 super( context );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400149 }
150
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400151 public SeekBarPreference (Context context, AttributeSet attrs)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400152 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400153 super( context, attrs );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400154 }
155
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400156 public SeekBarPreference (Context context, AttributeSet attrs, int defStyle)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400157 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400158 super( context, attrs, defStyle );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400159 }
160
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400161 protected View onCreateView (ViewGroup p)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400162 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400163 final Context ctx = getContext();
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400164
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400165 LinearLayout layout = new LinearLayout( ctx );
166 layout.setId( android.R.id.widget_frame );
167 layout.setOrientation( LinearLayout.VERTICAL );
Alexandre Savard74eccf32012-09-11 19:12:29 -0400168 layout.setPadding(65, 10, 15, 10);
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400169
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400170 TextView title = new TextView( ctx );
171 int textColor = title.getCurrentTextColor();
172 title.setId( android.R.id.title );
173 title.setSingleLine();
Alexandre Savard74eccf32012-09-11 19:12:29 -0400174 title.setTextAppearance( ctx, android.R.style.TextAppearance_Medium );
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400175 title.setTextColor( textColor );
176 layout.addView( title );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400177
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400178 summary = new TextView( ctx );
179 summary.setId( android.R.id.summary );
180 summary.setSingleLine();
181 summary.setTextAppearance( ctx, android.R.style.TextAppearance_Small );
182 summary.setTextColor( textColor );
183 layout.addView( summary );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400184
Alexandre Savard616c3802012-09-12 09:44:46 -0400185 seekbar = new SeekBar( ctx );
186 seekbar.setId( android.R.id.progress );
187 seekbar.setMax( max );
188 seekbar.setOnSeekBarChangeListener( this );
189 layout.addView( seekbar );
190
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400191 return layout;
192 }
193
194 @Override
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400195 protected void onBindView (View view)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400196 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400197 super.onBindView( view );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400198
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400199 if (seekbar != null)
200 seekbar.setProgress( progress );
201 }
202
203 public void setProgress (int pcnt) {
204 if (progress != pcnt) {
205 persistInt( progress = pcnt );
206
207 notifyDependencyChange( shouldDisableDependents() );
208 notifyChanged();
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400209 }
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400210 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400211
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400212 public int getProgress () {
213 return progress;
214 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400215
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400216 public void setMax (int max) {
217 this.max = max;
218 if (seekbar != null)
219 seekbar.setMax( max );
220 }
221
222 private SeekBar getSeekBar () {
223 return seekbar;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400224 }
225
226 @Override
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400227 protected Object onGetDefaultValue (TypedArray a, int index) {
228 return a.getInt( index, progress );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400229 }
230
231 @Override
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400232 protected void onSetInitialValue (boolean restoreValue, Object defaultValue) {
233 setProgress( restoreValue ? getPersistedInt( progress ) : (Integer)defaultValue );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400234 }
235
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400236 @Override
237 public boolean shouldDisableDependents () {
238 return progress == 0 || super.shouldDisableDependents();
239 }
240
241 public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {
242 discard = !callChangeListener( progress );
Alexandre Savard74eccf32012-09-11 19:12:29 -0400243 summary.setText(CURRENT_VALUE + progress);
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400244 }
245
246 public void onStartTrackingTouch (SeekBar seekBar) {
247 discard = false;
248 }
249
250 public void onStopTrackingTouch (SeekBar seekBar) {
251 if (discard)
252 seekBar.setProgress( progress );
253 else {
254 setProgress( seekBar.getProgress() );
255
256 OnPreferenceChangeListener listener = getOnPreferenceChangeListener();
257 //if (listener instanceof AbstractSeekBarListener)
258 //// setSummary( ((AbstractSeekBarListener)listener).toSummary( seekBar.getProgress() ) );
259 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400260 }
261 }
Alexandre Savarddcd37402012-09-06 18:36:48 -0400262}