blob: 32b7db45fa87c55e897ec6570b6b3a4915517f7e [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 Lision6e8931e2013-09-19 16:49:34 -040038import android.os.Bundle;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040039import 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 Savard5e0b9472012-09-11 17:56:30 -040045import android.util.AttributeSet;
Alexandre Savarddcd37402012-09-06 18:36:48 -040046import android.view.View;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040047import android.view.ViewGroup;
Alexandre Savarde2bb2592012-09-11 18:39:57 -040048import android.widget.LinearLayout;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040049import android.widget.SeekBar;
50import android.widget.SeekBar.OnSeekBarChangeListener;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040051import android.widget.TextView;
Alexandre Savarddcd37402012-09-06 18:36:48 -040052
53import com.savoirfairelinux.sflphone.R;
54
alisiond9e29442013-04-17 16:10:18 -040055public class AudioManagementFragment extends PreferenceFragment
Alexandre Savarddcd37402012-09-06 18:36:48 -040056{
57 static final String TAG = "PrefManagementFragment";
Alexandre Savard393ccab2012-09-11 15:01:20 -040058 static final String CURRENT_VALUE = "Current value:: ";
Alexandre Savard12dc3ac2012-09-27 11:17:39 -040059
alisiond9e29442013-04-17 16:10:18 -040060 public AudioManagementFragment()
Alexandre Savard12dc3ac2012-09-27 11:17:39 -040061 {
Alexandre Savard12dc3ac2012-09-27 11:17:39 -040062 }
Alexandre Savarddcd37402012-09-06 18:36:48 -040063
64 @Override
Alexandre Savard393ccab2012-09-11 15:01:20 -040065 public void onCreate(Bundle savedInstanceState)
Alexandre Savarddcd37402012-09-06 18:36:48 -040066 {
Alexandre Savard393ccab2012-09-11 15:01:20 -040067 super.onCreate(savedInstanceState);
Alexandre Savarddcd37402012-09-06 18:36:48 -040068
Alexandre Savard393ccab2012-09-11 15:01:20 -040069 setPreferenceScreen(getAudioPreferenceScreen());
Alexandre Savarddcd37402012-09-06 18:36:48 -040070 }
71
Alexandre Savard5e0b9472012-09-11 17:56:30 -040072 Preference.OnPreferenceChangeListener changePreferenceListener = new Preference.OnPreferenceChangeListener() {
Alexandre Savard393ccab2012-09-11 15:01:20 -040073 public boolean onPreferenceChange(Preference preference, Object newValue) {
74 preference.setSummary(CURRENT_VALUE + (CharSequence)newValue);
75 return true;
76 }
77 };
78
79 public PreferenceScreen getAudioPreferenceScreen()
Alexandre Savarddcd37402012-09-06 18:36:48 -040080 {
Alexandre Savard393ccab2012-09-11 15:01:20 -040081 Activity currentContext = getActivity();
82
83 PreferenceScreen root = getPreferenceManager().createPreferenceScreen(currentContext);
84
85 PreferenceCategory audioPrefCat = new PreferenceCategory(currentContext);
86 audioPrefCat.setTitle(R.string.audio_preferences);
87 root.addPreference(audioPrefCat);
88
Alexandre Savard5e0b9472012-09-11 17:56:30 -040089 // Codec List
Alexandre Savard393ccab2012-09-11 15:01:20 -040090 ListPreference codecListPref = new ListPreference(currentContext);
91 codecListPref.setEntries(R.array.audio_codec_list);
92 codecListPref.setEntryValues(R.array.audio_codec_list_value);
93 codecListPref.setDialogTitle(R.string.dialogtitle_audio_codec_list);
94 codecListPref.setPersistent(false);
95 codecListPref.setTitle(R.string.title_audio_codec_list);
96 codecListPref.setSummary(CURRENT_VALUE + "PCMU");
Alexandre Savard5e0b9472012-09-11 17:56:30 -040097 codecListPref.setOnPreferenceChangeListener(changePreferenceListener);
Alexandre Savard393ccab2012-09-11 15:01:20 -040098 audioPrefCat.addPreference(codecListPref);
99
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400100 // Ringtone
101 EditTextPreference audioRingtonePref = new EditTextPreference(currentContext);
102 audioRingtonePref.setDialogTitle(R.string.dialogtitle_audio_ringtone_field);
103 audioRingtonePref.setPersistent(false);
104 audioRingtonePref.setTitle(R.string.title_audio_ringtone_field);
105 audioRingtonePref.setSummary(CURRENT_VALUE + "path/to/ringtone");
106 audioRingtonePref.setOnPreferenceChangeListener(changePreferenceListener);
107 audioPrefCat.addPreference(audioRingtonePref);
108
109 // Speaker volume seekbar
110 SeekBarPreference speakerSeekBarPref = new SeekBarPreference(currentContext);
111 speakerSeekBarPref.setPersistent(false);
112 speakerSeekBarPref.setTitle("Speaker Volume");
Alexandre Savard74eccf32012-09-11 19:12:29 -0400113 speakerSeekBarPref.setProgress(50);
114 speakerSeekBarPref.setSummary(CURRENT_VALUE + speakerSeekBarPref.getProgress());
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400115 audioPrefCat.addPreference(speakerSeekBarPref);
116
117 // Capture volume seekbar
118 SeekBarPreference captureSeekBarPref = new SeekBarPreference(currentContext);
119 captureSeekBarPref.setPersistent(false);
120 captureSeekBarPref.setTitle("Capture Volume");
Alexandre Savard74eccf32012-09-11 19:12:29 -0400121 captureSeekBarPref.setProgress(50);
122 captureSeekBarPref.setSummary(CURRENT_VALUE + captureSeekBarPref.getProgress());
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400123 audioPrefCat.addPreference(captureSeekBarPref);
124
125 // Ringtone volume seekbar
126 SeekBarPreference ringtoneSeekBarPref = new SeekBarPreference(currentContext);
127 ringtoneSeekBarPref.setPersistent(false);
128 ringtoneSeekBarPref.setTitle("Ringtone Volume");
Alexandre Savard74eccf32012-09-11 19:12:29 -0400129 ringtoneSeekBarPref.setProgress(50);
130 ringtoneSeekBarPref.setSummary(CURRENT_VALUE + ringtoneSeekBarPref.getProgress());
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400131 audioPrefCat.addPreference(ringtoneSeekBarPref);
132
Alexandre Savard393ccab2012-09-11 15:01:20 -0400133 return root;
Alexandre Savarddcd37402012-09-06 18:36:48 -0400134 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400135
136 public class SeekBarPreference extends Preference implements OnSeekBarChangeListener
137 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400138 private SeekBar seekbar;
139 private int progress;
140 private int max = 100;
141 private TextView summary;
142 private boolean discard;
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400143
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400144 public SeekBarPreference (Context context)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400145 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400146 super( context );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400147 }
148
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400149 public SeekBarPreference (Context context, AttributeSet attrs)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400150 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400151 super( context, attrs );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400152 }
153
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400154 public SeekBarPreference (Context context, AttributeSet attrs, int defStyle)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400155 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400156 super( context, attrs, defStyle );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400157 }
158
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400159 protected View onCreateView (ViewGroup p)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400160 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400161 final Context ctx = getContext();
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400162
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400163 LinearLayout layout = new LinearLayout( ctx );
164 layout.setId( android.R.id.widget_frame );
165 layout.setOrientation( LinearLayout.VERTICAL );
Alexandre Savard74eccf32012-09-11 19:12:29 -0400166 layout.setPadding(65, 10, 15, 10);
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400167
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400168 TextView title = new TextView( ctx );
169 int textColor = title.getCurrentTextColor();
170 title.setId( android.R.id.title );
171 title.setSingleLine();
Alexandre Savard74eccf32012-09-11 19:12:29 -0400172 title.setTextAppearance( ctx, android.R.style.TextAppearance_Medium );
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400173 title.setTextColor( textColor );
174 layout.addView( title );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400175
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400176 summary = new TextView( ctx );
177 summary.setId( android.R.id.summary );
178 summary.setSingleLine();
179 summary.setTextAppearance( ctx, android.R.style.TextAppearance_Small );
180 summary.setTextColor( textColor );
181 layout.addView( summary );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400182
Alexandre Savard616c3802012-09-12 09:44:46 -0400183 seekbar = new SeekBar( ctx );
184 seekbar.setId( android.R.id.progress );
185 seekbar.setMax( max );
186 seekbar.setOnSeekBarChangeListener( this );
187 layout.addView( seekbar );
188
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400189 return layout;
190 }
191
192 @Override
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400193 protected void onBindView (View view)
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400194 {
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400195 super.onBindView( view );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400196
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400197 if (seekbar != null)
198 seekbar.setProgress( progress );
199 }
200
201 public void setProgress (int pcnt) {
202 if (progress != pcnt) {
203 persistInt( progress = pcnt );
204
205 notifyDependencyChange( shouldDisableDependents() );
206 notifyChanged();
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400207 }
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400208 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400209
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400210 public int getProgress () {
211 return progress;
212 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400213
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400214 public void setMax (int max) {
215 this.max = max;
216 if (seekbar != null)
217 seekbar.setMax( max );
218 }
219
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400220 @Override
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400221 protected Object onGetDefaultValue (TypedArray a, int index) {
222 return a.getInt( index, progress );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400223 }
224
225 @Override
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400226 protected void onSetInitialValue (boolean restoreValue, Object defaultValue) {
227 setProgress( restoreValue ? getPersistedInt( progress ) : (Integer)defaultValue );
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400228 }
229
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400230 @Override
231 public boolean shouldDisableDependents () {
232 return progress == 0 || super.shouldDisableDependents();
233 }
234
235 public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {
236 discard = !callChangeListener( progress );
Alexandre Savard74eccf32012-09-11 19:12:29 -0400237 summary.setText(CURRENT_VALUE + progress);
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400238 }
239
240 public void onStartTrackingTouch (SeekBar seekBar) {
241 discard = false;
242 }
243
244 public void onStopTrackingTouch (SeekBar seekBar) {
245 if (discard)
246 seekBar.setProgress( progress );
247 else {
248 setProgress( seekBar.getProgress() );
249
Alexandre Lision6e8931e2013-09-19 16:49:34 -0400250// OnPreferenceChangeListener listener = getOnPreferenceChangeListener();
Alexandre Savarde2bb2592012-09-11 18:39:57 -0400251 //if (listener instanceof AbstractSeekBarListener)
252 //// setSummary( ((AbstractSeekBarListener)listener).toSummary( seekBar.getProgress() ) );
253 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400254 }
255 }
Alexandre Savarddcd37402012-09-06 18:36:48 -0400256}