blob: 4cd35780cd8aab81f0e2b8c99c289eee3227634a [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;
36import android.graphics.Typeface;
37import android.preference.EditTextPreference;
Alexandre Savard393ccab2012-09-11 15:01:20 -040038import android.preference.ListPreference;
39import android.preference.Preference;
40import android.preference.PreferenceCategory;
41import android.preference.PreferenceFragment;
42import android.preference.PreferenceScreen;
Alexandre Savarddcd37402012-09-06 18:36:48 -040043import android.os.Bundle;
44import android.util.Log;
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 Savard9a93f062012-09-11 10:42:43 -040048import android.widget.ArrayAdapter;
49import android.widget.ListView;
Alexandre Savard5e0b9472012-09-11 17:56:30 -040050import android.widget.SeekBar;
51import android.widget.SeekBar.OnSeekBarChangeListener;
52import android.widget.RelativeLayout;
53import android.widget.TextView;
Alexandre Savarddcd37402012-09-06 18:36:48 -040054
55import com.savoirfairelinux.sflphone.R;
56
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 Savarddcd37402012-09-06 18:36:48 -040061
62 @Override
Alexandre Savard393ccab2012-09-11 15:01:20 -040063 public void onCreate(Bundle savedInstanceState)
Alexandre Savarddcd37402012-09-06 18:36:48 -040064 {
Alexandre Savard393ccab2012-09-11 15:01:20 -040065 super.onCreate(savedInstanceState);
Alexandre Savarddcd37402012-09-06 18:36:48 -040066
Alexandre Savard393ccab2012-09-11 15:01:20 -040067 setPreferenceScreen(getAudioPreferenceScreen());
Alexandre Savarddcd37402012-09-06 18:36:48 -040068 }
69
Alexandre Savard5e0b9472012-09-11 17:56:30 -040070 Preference.OnPreferenceChangeListener changePreferenceListener = new Preference.OnPreferenceChangeListener() {
Alexandre Savard393ccab2012-09-11 15:01:20 -040071 public boolean onPreferenceChange(Preference preference, Object newValue) {
72 preference.setSummary(CURRENT_VALUE + (CharSequence)newValue);
73 return true;
74 }
75 };
76
77 public PreferenceScreen getAudioPreferenceScreen()
Alexandre Savarddcd37402012-09-06 18:36:48 -040078 {
Alexandre Savard393ccab2012-09-11 15:01:20 -040079 Activity currentContext = getActivity();
80
81 PreferenceScreen root = getPreferenceManager().createPreferenceScreen(currentContext);
82
83 PreferenceCategory audioPrefCat = new PreferenceCategory(currentContext);
84 audioPrefCat.setTitle(R.string.audio_preferences);
85 root.addPreference(audioPrefCat);
86
Alexandre Savard5e0b9472012-09-11 17:56:30 -040087 // Codec List
Alexandre Savard393ccab2012-09-11 15:01:20 -040088 ListPreference codecListPref = new ListPreference(currentContext);
89 codecListPref.setEntries(R.array.audio_codec_list);
90 codecListPref.setEntryValues(R.array.audio_codec_list_value);
91 codecListPref.setDialogTitle(R.string.dialogtitle_audio_codec_list);
92 codecListPref.setPersistent(false);
93 codecListPref.setTitle(R.string.title_audio_codec_list);
94 codecListPref.setSummary(CURRENT_VALUE + "PCMU");
Alexandre Savard5e0b9472012-09-11 17:56:30 -040095 codecListPref.setOnPreferenceChangeListener(changePreferenceListener);
Alexandre Savard393ccab2012-09-11 15:01:20 -040096 audioPrefCat.addPreference(codecListPref);
97
Alexandre Savard5e0b9472012-09-11 17:56:30 -040098 // Ringtone
99 EditTextPreference audioRingtonePref = new EditTextPreference(currentContext);
100 audioRingtonePref.setDialogTitle(R.string.dialogtitle_audio_ringtone_field);
101 audioRingtonePref.setPersistent(false);
102 audioRingtonePref.setTitle(R.string.title_audio_ringtone_field);
103 audioRingtonePref.setSummary(CURRENT_VALUE + "path/to/ringtone");
104 audioRingtonePref.setOnPreferenceChangeListener(changePreferenceListener);
105 audioPrefCat.addPreference(audioRingtonePref);
106
107 // Speaker volume seekbar
108 SeekBarPreference speakerSeekBarPref = new SeekBarPreference(currentContext);
109 speakerSeekBarPref.setPersistent(false);
110 speakerSeekBarPref.setTitle("Speaker Volume");
111 speakerSeekBarPref.setSummary("Set the volume for speaker");
112 audioPrefCat.addPreference(speakerSeekBarPref);
113
114 // Capture volume seekbar
115 SeekBarPreference captureSeekBarPref = new SeekBarPreference(currentContext);
116 captureSeekBarPref.setPersistent(false);
117 captureSeekBarPref.setTitle("Capture Volume");
118 captureSeekBarPref.setSummary("Set the volume for microphone");
119 audioPrefCat.addPreference(captureSeekBarPref);
120
121 // Ringtone volume seekbar
122 SeekBarPreference ringtoneSeekBarPref = new SeekBarPreference(currentContext);
123 ringtoneSeekBarPref.setPersistent(false);
124 ringtoneSeekBarPref.setTitle("Ringtone Volume");
125 ringtoneSeekBarPref.setSummary("Set the volume for ringtone");
126 audioPrefCat.addPreference(ringtoneSeekBarPref);
127
Alexandre Savard393ccab2012-09-11 15:01:20 -0400128 return root;
Alexandre Savarddcd37402012-09-06 18:36:48 -0400129 }
Alexandre Savard5e0b9472012-09-11 17:56:30 -0400130
131 public class SeekBarPreference extends Preference implements OnSeekBarChangeListener
132 {
133 public int MAXIMUM = 100;
134 public int INTERVAL = 5;
135 private float oldValue = 25;
136 private TextView Indicator;
137
138 public SeekBarPreference(Context context)
139 {
140 super(context);
141 }
142
143 public SeekBarPreference(Context context, AttributeSet attrs)
144 {
145 super(context, attrs);
146 }
147
148 public SeekBarPreference(Context context, AttributeSet attrs, int defStyle)
149 {
150 super(context, attrs, defStyle);
151 }
152
153 @Override
154 protected View onCreateView(ViewGroup parent)
155 {
156 float scale = getContext().getResources().getDisplayMetrics().density;
157
158 RelativeLayout layout = new RelativeLayout(getContext());
159
160 RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(
161 RelativeLayout.LayoutParams.WRAP_CONTENT,
162 RelativeLayout.LayoutParams.WRAP_CONTENT);
163
164 RelativeLayout.LayoutParams sbarParams = new RelativeLayout.LayoutParams(
165 Math.round(scale * 160),
166 RelativeLayout.LayoutParams.WRAP_CONTENT);
167
168 RelativeLayout.LayoutParams indParams = new RelativeLayout.LayoutParams(
169 RelativeLayout.LayoutParams.WRAP_CONTENT,
170 RelativeLayout.LayoutParams.WRAP_CONTENT);
171
172 TextView preferenceText = new TextView(getContext());
173 preferenceText.setId(0);
174 preferenceText.setText(getTitle());
175 preferenceText.setTextSize(18);
176 preferenceText.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
177
178 SeekBar sbar = new SeekBar(getContext());
179 sbar.setId(1);
180 sbar.setMax(MAXIMUM);
181 sbar.setProgress((int)this.oldValue);
182 sbar.setOnSeekBarChangeListener(this);
183
184 this.Indicator = new TextView(getContext());
185 this.Indicator.setTextSize(12);
186 this.Indicator.setTypeface(Typeface.MONOSPACE, Typeface.ITALIC);
187 this.Indicator.setText("" + sbar.getProgress());
188
189 textParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
190 sbarParams.addRule(RelativeLayout.RIGHT_OF, preferenceText.getId());
191 indParams.setMargins(Math.round(20*scale), 0, 0, 0);
192 indParams.addRule(RelativeLayout.RIGHT_OF, sbar.getId());
193
194 preferenceText.setLayoutParams(textParams);
195 sbar.setLayoutParams(sbarParams);
196 this.Indicator.setLayoutParams(indParams);
197 layout.addView(preferenceText);
198 layout.addView(this.Indicator);
199 layout.addView(sbar);
200 layout.setId(android.R.id.widget_frame);
201
202 return layout;
203 }
204
205 @Override
206 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
207 {
208 progress = Math.round(((float) progress)/INTERVAL) * INTERVAL;
209
210 if(!callChangeListener(progress)) {
211 seekBar.setProgress((int) this.oldValue);
212 return;
213 }
214
215 seekBar.setProgress(progress);
216 this.oldValue = progress;
217 this.Indicator.setText("" + progress);
218 updatePreference(progress);
219
220 notifyChanged();
221 }
222
223 @Override
224 public void onStartTrackingTouch(SeekBar seekBar)
225 {
226 }
227
228 @Override
229 public void onStopTrackingTouch(SeekBar seekBar)
230 {
231 }
232
233 private void updatePreference(int newValue) {
234 // SharedPreference.Editor editor = getEditor();
235 // editor.putInt(getKey(), newValue);
236 // editor.commit();
237 }
238 }
Alexandre Savarddcd37402012-09-06 18:36:48 -0400239}