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