Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 32 | package com.savoirfairelinux.sflphone.client; |
| 33 | |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 34 | import android.app.Activity; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 35 | import android.content.Context; |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 36 | import android.content.res.TypedArray; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 37 | import android.graphics.Typeface; |
| 38 | import android.preference.EditTextPreference; |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 39 | import android.preference.ListPreference; |
| 40 | import android.preference.Preference; |
| 41 | import android.preference.PreferenceCategory; |
| 42 | import android.preference.PreferenceFragment; |
| 43 | import android.preference.PreferenceScreen; |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 44 | import android.os.Bundle; |
| 45 | import android.util.Log; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 46 | import android.util.AttributeSet; |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 47 | import android.view.View; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 48 | import android.view.ViewGroup; |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 49 | import android.widget.LinearLayout; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 50 | import android.widget.SeekBar; |
| 51 | import android.widget.SeekBar.OnSeekBarChangeListener; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 52 | import android.widget.TextView; |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 53 | |
| 54 | import com.savoirfairelinux.sflphone.R; |
| 55 | |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 56 | public class PrefManagementFragment extends PreferenceFragment |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 57 | { |
| 58 | static final String TAG = "PrefManagementFragment"; |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 59 | static final String CURRENT_VALUE = "Current value:: "; |
Alexandre Savard | 12dc3ac | 2012-09-27 11:17:39 -0400 | [diff] [blame] | 60 | |
Emeric Vigier | 1f1ced3 | 2012-11-02 16:56:32 -0400 | [diff] [blame] | 61 | public PrefManagementFragment() |
Alexandre Savard | 12dc3ac | 2012-09-27 11:17:39 -0400 | [diff] [blame] | 62 | { |
Alexandre Savard | 12dc3ac | 2012-09-27 11:17:39 -0400 | [diff] [blame] | 63 | } |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 64 | |
| 65 | @Override |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 66 | public void onCreate(Bundle savedInstanceState) |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 67 | { |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 68 | super.onCreate(savedInstanceState); |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 69 | |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 70 | setPreferenceScreen(getAudioPreferenceScreen()); |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 73 | Preference.OnPreferenceChangeListener changePreferenceListener = new Preference.OnPreferenceChangeListener() { |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 74 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 75 | preference.setSummary(CURRENT_VALUE + (CharSequence)newValue); |
| 76 | return true; |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | public PreferenceScreen getAudioPreferenceScreen() |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 81 | { |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 82 | Activity currentContext = getActivity(); |
| 83 | |
| 84 | PreferenceScreen root = getPreferenceManager().createPreferenceScreen(currentContext); |
| 85 | |
| 86 | PreferenceCategory audioPrefCat = new PreferenceCategory(currentContext); |
| 87 | audioPrefCat.setTitle(R.string.audio_preferences); |
| 88 | root.addPreference(audioPrefCat); |
| 89 | |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 90 | // Codec List |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 91 | ListPreference codecListPref = new ListPreference(currentContext); |
| 92 | codecListPref.setEntries(R.array.audio_codec_list); |
| 93 | codecListPref.setEntryValues(R.array.audio_codec_list_value); |
| 94 | codecListPref.setDialogTitle(R.string.dialogtitle_audio_codec_list); |
| 95 | codecListPref.setPersistent(false); |
| 96 | codecListPref.setTitle(R.string.title_audio_codec_list); |
| 97 | codecListPref.setSummary(CURRENT_VALUE + "PCMU"); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 98 | codecListPref.setOnPreferenceChangeListener(changePreferenceListener); |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 99 | audioPrefCat.addPreference(codecListPref); |
| 100 | |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 101 | // Ringtone |
| 102 | EditTextPreference audioRingtonePref = new EditTextPreference(currentContext); |
| 103 | audioRingtonePref.setDialogTitle(R.string.dialogtitle_audio_ringtone_field); |
| 104 | audioRingtonePref.setPersistent(false); |
| 105 | audioRingtonePref.setTitle(R.string.title_audio_ringtone_field); |
| 106 | audioRingtonePref.setSummary(CURRENT_VALUE + "path/to/ringtone"); |
| 107 | audioRingtonePref.setOnPreferenceChangeListener(changePreferenceListener); |
| 108 | audioPrefCat.addPreference(audioRingtonePref); |
| 109 | |
| 110 | // Speaker volume seekbar |
| 111 | SeekBarPreference speakerSeekBarPref = new SeekBarPreference(currentContext); |
| 112 | speakerSeekBarPref.setPersistent(false); |
| 113 | speakerSeekBarPref.setTitle("Speaker Volume"); |
Alexandre Savard | 74eccf3 | 2012-09-11 19:12:29 -0400 | [diff] [blame] | 114 | speakerSeekBarPref.setProgress(50); |
| 115 | speakerSeekBarPref.setSummary(CURRENT_VALUE + speakerSeekBarPref.getProgress()); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 116 | audioPrefCat.addPreference(speakerSeekBarPref); |
| 117 | |
| 118 | // Capture volume seekbar |
| 119 | SeekBarPreference captureSeekBarPref = new SeekBarPreference(currentContext); |
| 120 | captureSeekBarPref.setPersistent(false); |
| 121 | captureSeekBarPref.setTitle("Capture Volume"); |
Alexandre Savard | 74eccf3 | 2012-09-11 19:12:29 -0400 | [diff] [blame] | 122 | captureSeekBarPref.setProgress(50); |
| 123 | captureSeekBarPref.setSummary(CURRENT_VALUE + captureSeekBarPref.getProgress()); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 124 | audioPrefCat.addPreference(captureSeekBarPref); |
| 125 | |
| 126 | // Ringtone volume seekbar |
| 127 | SeekBarPreference ringtoneSeekBarPref = new SeekBarPreference(currentContext); |
| 128 | ringtoneSeekBarPref.setPersistent(false); |
| 129 | ringtoneSeekBarPref.setTitle("Ringtone Volume"); |
Alexandre Savard | 74eccf3 | 2012-09-11 19:12:29 -0400 | [diff] [blame] | 130 | ringtoneSeekBarPref.setProgress(50); |
| 131 | ringtoneSeekBarPref.setSummary(CURRENT_VALUE + ringtoneSeekBarPref.getProgress()); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 132 | audioPrefCat.addPreference(ringtoneSeekBarPref); |
| 133 | |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 134 | return root; |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 135 | } |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 136 | |
| 137 | public class SeekBarPreference extends Preference implements OnSeekBarChangeListener |
| 138 | { |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 139 | private SeekBar seekbar; |
| 140 | private int progress; |
| 141 | private int max = 100; |
| 142 | private TextView summary; |
| 143 | private boolean discard; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 144 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 145 | public SeekBarPreference (Context context) |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 146 | { |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 147 | super( context ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 150 | public SeekBarPreference (Context context, AttributeSet attrs) |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 151 | { |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 152 | super( context, attrs ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 155 | public SeekBarPreference (Context context, AttributeSet attrs, int defStyle) |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 156 | { |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 157 | super( context, attrs, defStyle ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 160 | protected View onCreateView (ViewGroup p) |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 161 | { |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 162 | final Context ctx = getContext(); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 163 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 164 | LinearLayout layout = new LinearLayout( ctx ); |
| 165 | layout.setId( android.R.id.widget_frame ); |
| 166 | layout.setOrientation( LinearLayout.VERTICAL ); |
Alexandre Savard | 74eccf3 | 2012-09-11 19:12:29 -0400 | [diff] [blame] | 167 | layout.setPadding(65, 10, 15, 10); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 168 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 169 | TextView title = new TextView( ctx ); |
| 170 | int textColor = title.getCurrentTextColor(); |
| 171 | title.setId( android.R.id.title ); |
| 172 | title.setSingleLine(); |
Alexandre Savard | 74eccf3 | 2012-09-11 19:12:29 -0400 | [diff] [blame] | 173 | title.setTextAppearance( ctx, android.R.style.TextAppearance_Medium ); |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 174 | title.setTextColor( textColor ); |
| 175 | layout.addView( title ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 176 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 177 | 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 Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 183 | |
Alexandre Savard | 616c380 | 2012-09-12 09:44:46 -0400 | [diff] [blame] | 184 | seekbar = new SeekBar( ctx ); |
| 185 | seekbar.setId( android.R.id.progress ); |
| 186 | seekbar.setMax( max ); |
| 187 | seekbar.setOnSeekBarChangeListener( this ); |
| 188 | layout.addView( seekbar ); |
| 189 | |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 190 | return layout; |
| 191 | } |
| 192 | |
| 193 | @Override |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 194 | protected void onBindView (View view) |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 195 | { |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 196 | super.onBindView( view ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 197 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 198 | if (seekbar != null) |
| 199 | seekbar.setProgress( progress ); |
| 200 | } |
| 201 | |
| 202 | public void setProgress (int pcnt) { |
| 203 | if (progress != pcnt) { |
| 204 | persistInt( progress = pcnt ); |
| 205 | |
| 206 | notifyDependencyChange( shouldDisableDependents() ); |
| 207 | notifyChanged(); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 208 | } |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 209 | } |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 210 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 211 | public int getProgress () { |
| 212 | return progress; |
| 213 | } |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 214 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 215 | public void setMax (int max) { |
| 216 | this.max = max; |
| 217 | if (seekbar != null) |
| 218 | seekbar.setMax( max ); |
| 219 | } |
| 220 | |
| 221 | private SeekBar getSeekBar () { |
| 222 | return seekbar; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 226 | protected Object onGetDefaultValue (TypedArray a, int index) { |
| 227 | return a.getInt( index, progress ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 231 | protected void onSetInitialValue (boolean restoreValue, Object defaultValue) { |
| 232 | setProgress( restoreValue ? getPersistedInt( progress ) : (Integer)defaultValue ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 235 | @Override |
| 236 | public boolean shouldDisableDependents () { |
| 237 | return progress == 0 || super.shouldDisableDependents(); |
| 238 | } |
| 239 | |
| 240 | public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) { |
| 241 | discard = !callChangeListener( progress ); |
Alexandre Savard | 74eccf3 | 2012-09-11 19:12:29 -0400 | [diff] [blame] | 242 | summary.setText(CURRENT_VALUE + progress); |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | public void onStartTrackingTouch (SeekBar seekBar) { |
| 246 | discard = false; |
| 247 | } |
| 248 | |
| 249 | public void onStopTrackingTouch (SeekBar seekBar) { |
| 250 | if (discard) |
| 251 | seekBar.setProgress( progress ); |
| 252 | else { |
| 253 | setProgress( seekBar.getProgress() ); |
| 254 | |
| 255 | OnPreferenceChangeListener listener = getOnPreferenceChangeListener(); |
| 256 | //if (listener instanceof AbstractSeekBarListener) |
| 257 | //// setSummary( ((AbstractSeekBarListener)listener).toSummary( seekBar.getProgress() ) ); |
| 258 | } |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 259 | } |
| 260 | } |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 261 | } |